ClickHouse/tests/ci/build_check.py

235 lines
6.6 KiB
Python
Raw Normal View History

2021-10-21 11:09:15 +00:00
#!/usr/bin/env python3
import argparse
2021-10-21 11:09:15 +00:00
import logging
import subprocess
2021-10-21 11:09:15 +00:00
import sys
import time
from pathlib import Path
from typing import Tuple
import docker_images_helper
2024-06-10 09:18:03 +00:00
from ci_config import CI
from env_helper import REPO_COPY, S3_BUILDS_BUCKET, TEMP_PATH
from git_helper import Git
from lambda_shared_package.lambda_shared.pr import Labels
2021-11-26 14:00:09 +00:00
from pr_info import PRInfo
from report import FAILURE, SUCCESS, JobReport, StatusType
from stopwatch import Stopwatch
from tee_popen import TeePopen
2022-01-12 12:17:21 +00:00
from version_helper import (
ClickHouseVersion,
get_version_from_repo,
update_version_local,
)
2021-10-21 11:09:15 +00:00
2022-02-08 18:12:04 +00:00
IMAGE_NAME = "clickhouse/binary-builder"
BUILD_LOG_NAME = "build_log.log"
2022-02-08 18:12:04 +00:00
2021-10-21 11:09:15 +00:00
2024-06-10 09:18:03 +00:00
def _can_export_binaries(build_config: CI.BuildConfig) -> bool:
if build_config.package_type != "deb":
2021-10-21 11:09:15 +00:00
return False
if build_config.sanitizer != "":
2021-10-21 11:09:15 +00:00
return True
if build_config.debug_build:
2021-10-21 11:09:15 +00:00
return True
return False
def get_packager_cmd(
2024-06-10 09:18:03 +00:00
build_config: CI.BuildConfig,
2023-09-27 14:27:37 +00:00
packager_path: Path,
output_path: Path,
2022-01-12 12:17:21 +00:00
build_version: str,
image_version: str,
2022-03-30 14:29:13 +00:00
official: bool,
2022-01-12 12:17:21 +00:00
) -> str:
package_type = build_config.package_type
comp = build_config.compiler
cmake_flags = "-DENABLE_CLICKHOUSE_SELF_EXTRACTING=1"
cmd = (
f"cd {packager_path} && CMAKE_FLAGS='{cmake_flags}' ./packager "
f"--output-dir={output_path} --package-type={package_type} --compiler={comp}"
)
if build_config.debug_build:
cmd += " --debug-build"
if build_config.sanitizer:
cmd += f" --sanitizer={build_config.sanitizer}"
2024-01-14 02:45:42 +00:00
if build_config.coverage:
cmd += " --coverage"
if build_config.tidy:
cmd += " --clang-tidy"
2023-03-24 16:24:21 +00:00
cmd += " --cache=sccache"
cmd += " --s3-rw-access"
cmd += f" --s3-bucket={S3_BUILDS_BUCKET}"
if build_config.additional_pkgs:
2022-03-14 21:07:58 +00:00
cmd += " --additional-pkgs"
cmd += f" --docker-image-version={image_version}"
cmd += " --with-profiler"
cmd += f" --version={build_version}"
2021-10-21 11:09:15 +00:00
if _can_export_binaries(build_config):
cmd += " --with-binaries=tests"
2021-10-21 11:09:15 +00:00
2022-03-30 14:29:13 +00:00
if official:
cmd += " --official"
2021-10-21 11:09:15 +00:00
return cmd
2022-01-12 12:17:21 +00:00
def build_clickhouse(
2023-09-05 10:59:31 +00:00
packager_cmd: str, logs_path: Path, build_output_path: Path
) -> Tuple[Path, StatusType]:
build_log_path = logs_path / BUILD_LOG_NAME
2022-01-12 12:17:21 +00:00
success = False
2021-12-03 08:33:16 +00:00
with TeePopen(packager_cmd, build_log_path) as process:
retcode = process.wait()
if build_output_path.exists():
2023-09-27 14:27:37 +00:00
results_exists = any(build_output_path.iterdir())
2021-12-14 10:40:03 +00:00
else:
2023-09-27 14:27:37 +00:00
results_exists = False
2021-12-14 10:40:03 +00:00
2021-10-21 11:09:15 +00:00
if retcode == 0:
2023-09-27 14:27:37 +00:00
if results_exists:
2022-01-12 12:17:21 +00:00
success = True
2021-12-14 10:40:03 +00:00
logging.info("Built successfully")
else:
logging.info(
"Success exit code, but no build artifacts => build failed"
)
2021-10-21 11:09:15 +00:00
else:
logging.info("Build failed")
return build_log_path, SUCCESS if success else FAILURE
2021-10-21 11:09:15 +00:00
2021-12-01 14:23:51 +00:00
def is_release_pr(pr_info: PRInfo) -> bool:
return Labels.RELEASE in pr_info.labels or Labels.RELEASE_LTS in pr_info.labels
def get_release_or_pr(pr_info: PRInfo, version: ClickHouseVersion) -> Tuple[str, str]:
"Return prefixes for S3 artifacts paths"
# FIXME performance
# performance builds are havily relies on a fixed path for artifacts, that's why
# we need to preserve 0 for anything but PR number
# It should be fixed in performance-comparison image eventually
# For performance tests we always set PRs prefix
performance_pr = "PRs/0"
if is_release_pr(pr_info):
2022-01-12 12:17:21 +00:00
# for release pull requests we use branch names prefixes, not pr numbers
return pr_info.head_ref, performance_pr
if pr_info.number == 0:
# for pushes to master - major version
return f"{version.major}.{version.minor}", performance_pr
2022-01-12 12:17:21 +00:00
# PR number for anything else
pr_number = f"PRs/{pr_info.number}"
return pr_number, pr_number
2022-01-12 12:17:21 +00:00
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser("Clickhouse builder script")
parser.add_argument(
"build_name",
help="build name",
)
return parser.parse_args()
2022-01-12 12:17:21 +00:00
def main():
2021-10-21 11:09:15 +00:00
logging.basicConfig(level=logging.INFO)
args = parse_args()
stopwatch = Stopwatch()
build_name = args.build_name
2021-10-21 11:09:15 +00:00
2024-06-10 09:18:03 +00:00
build_config = CI.JOB_CONFIGS[build_name].build_config
assert build_config
2021-10-21 11:09:15 +00:00
temp_path = Path(TEMP_PATH)
2023-09-27 14:27:37 +00:00
temp_path.mkdir(parents=True, exist_ok=True)
repo_path = Path(REPO_COPY)
2021-10-21 11:09:15 +00:00
2021-11-26 14:00:09 +00:00
pr_info = PRInfo()
2021-10-21 11:09:15 +00:00
2023-09-27 14:27:37 +00:00
logging.info("Repo copy path %s", repo_path)
2021-10-21 11:09:15 +00:00
version = get_version_from_repo(git=Git(True))
2022-01-28 13:39:23 +00:00
logging.info("Got version from repo %s", version.string)
2022-03-30 14:29:13 +00:00
official_flag = pr_info.number == 0
2022-05-13 17:22:23 +00:00
version_type = "testing"
if is_release_pr(pr_info):
version_type = "stable"
2022-03-30 14:29:13 +00:00
official_flag = True
update_version_local(version, version_type)
logging.info("Updated local files with version")
2021-10-21 11:09:15 +00:00
logging.info("Build short name %s", build_name)
2021-10-21 11:09:15 +00:00
build_output_path = temp_path / build_name
2023-09-27 14:27:37 +00:00
build_output_path.mkdir(parents=True, exist_ok=True)
2021-10-21 11:09:15 +00:00
docker_image = docker_images_helper.pull_image(
docker_images_helper.get_docker_image(IMAGE_NAME)
)
packager_cmd = get_packager_cmd(
build_config,
2023-09-27 14:27:37 +00:00
repo_path / "docker" / "packager",
build_output_path,
2022-01-28 13:39:23 +00:00
version.string,
docker_image.version,
2022-04-06 08:15:36 +00:00
official_flag,
)
2022-03-30 14:29:13 +00:00
2021-10-21 11:09:15 +00:00
logging.info("Going to run packager with %s", packager_cmd)
logs_path = temp_path / "build_log"
2023-09-27 14:27:37 +00:00
logs_path.mkdir(parents=True, exist_ok=True)
2021-10-21 11:09:15 +00:00
2021-10-21 14:41:07 +00:00
start = time.time()
log_path, build_status = build_clickhouse(
packager_cmd, logs_path, build_output_path
)
2021-10-21 14:41:07 +00:00
elapsed = int(time.time() - start)
subprocess.check_call(
f"sudo chown -R ubuntu:ubuntu {build_output_path}", shell=True
)
logging.info("Build finished as %s, log path %s", build_status, log_path)
2024-04-04 17:22:46 +00:00
if build_status != SUCCESS:
# We check if docker works, because if it's down, it's infrastructure
try:
subprocess.check_call("docker info", shell=True)
except subprocess.CalledProcessError:
logging.error(
"The dockerd looks down, won't upload anything and generate report"
)
sys.exit(1)
2021-10-21 11:09:15 +00:00
JobReport(
description=version.describe,
test_results=[],
status=build_status,
start_time=stopwatch.start_time_str,
duration=elapsed,
additional_files=[log_path],
build_dir_for_upload=build_output_path,
version=version.describe,
).dump()
# Fail the build job if it didn't succeed
if build_status != SUCCESS:
2021-11-25 10:01:29 +00:00
sys.exit(1)
2022-01-12 12:17:21 +00:00
if __name__ == "__main__":
main()