2022-03-30 22:15:01 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# here
|
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import subprocess
|
2022-05-19 22:06:44 +00:00
|
|
|
import sys
|
2022-07-06 11:58:46 +00:00
|
|
|
import time
|
2023-01-05 13:16:31 +00:00
|
|
|
from pathlib import Path
|
2022-03-30 22:15:01 +00:00
|
|
|
from os import path as p, makedirs
|
2023-01-03 14:23:19 +00:00
|
|
|
from typing import List
|
2022-03-30 22:15:01 +00:00
|
|
|
|
|
|
|
from github import Github
|
|
|
|
|
2022-04-01 15:53:43 +00:00
|
|
|
from build_check import get_release_or_pr
|
2022-03-30 22:15:01 +00:00
|
|
|
from clickhouse_helper import ClickHouseHelper, prepare_tests_results_for_clickhouse
|
2023-02-23 14:21:19 +00:00
|
|
|
from commit_status_helper import format_description, post_commit_status
|
2022-03-30 22:15:01 +00:00
|
|
|
from docker_images_check import DockerImage
|
2022-08-11 13:01:32 +00:00
|
|
|
from env_helper import CI, GITHUB_RUN_URL, RUNNER_TEMP, S3_BUILDS_BUCKET, S3_DOWNLOAD
|
2022-03-30 22:15:01 +00:00
|
|
|
from get_robot_token import get_best_robot_token, get_parameter_from_ssm
|
2022-04-22 14:49:49 +00:00
|
|
|
from git_helper import Git
|
2022-03-30 22:15:01 +00:00
|
|
|
from pr_info import PRInfo
|
2023-01-03 14:23:19 +00:00
|
|
|
from report import TestResults, TestResult
|
2022-03-30 22:15:01 +00:00
|
|
|
from s3_helper import S3Helper
|
|
|
|
from stopwatch import Stopwatch
|
2023-01-05 13:16:31 +00:00
|
|
|
from tee_popen import TeePopen
|
2022-03-30 22:15:01 +00:00
|
|
|
from upload_result_helper import upload_results
|
|
|
|
from version_helper import (
|
2022-04-01 15:53:43 +00:00
|
|
|
ClickHouseVersion,
|
2022-03-30 22:15:01 +00:00
|
|
|
get_tagged_versions,
|
|
|
|
get_version_from_repo,
|
2022-04-07 11:48:52 +00:00
|
|
|
version_arg,
|
2022-03-30 22:15:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
TEMP_PATH = p.join(RUNNER_TEMP, "docker_images_check")
|
|
|
|
BUCKETS = {"amd64": "package_release", "arm64": "package_aarch64"}
|
2022-04-22 14:49:49 +00:00
|
|
|
git = Git(ignore_no_tags=True)
|
2022-03-30 22:15:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DelOS(argparse.Action):
|
|
|
|
def __call__(self, _, namespace, __, option_string=None):
|
|
|
|
no_build = self.dest[3:] if self.dest.startswith("no_") else self.dest
|
|
|
|
if no_build in namespace.os:
|
|
|
|
namespace.os.remove(no_build)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_args() -> argparse.Namespace:
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
|
|
|
description="A program to build clickhouse-server image, both alpine and "
|
|
|
|
"ubuntu versions",
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"--version",
|
|
|
|
type=version_arg,
|
2022-04-22 14:49:49 +00:00
|
|
|
default=get_version_from_repo(git=git).string,
|
2022-04-06 16:41:18 +00:00
|
|
|
help="a version to build, automaticaly got from version_helper, accepts either "
|
|
|
|
"tag ('refs/tags/' is removed automatically) or a normal 22.2.2.2 format",
|
2022-03-30 22:15:01 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--release-type",
|
|
|
|
type=str,
|
|
|
|
choices=("auto", "latest", "major", "minor", "patch", "head"),
|
|
|
|
default="head",
|
|
|
|
help="version part that will be updated when '--version' is set; "
|
|
|
|
"'auto' is a special case, it will get versions from github and detect the "
|
|
|
|
"release type (latest, major, minor or patch) automatically",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--image-path",
|
|
|
|
type=str,
|
|
|
|
default="docker/server",
|
|
|
|
help="a path to docker context directory",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--image-repo",
|
|
|
|
type=str,
|
|
|
|
default="clickhouse/clickhouse-server",
|
|
|
|
help="image name on docker hub",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--bucket-prefix",
|
|
|
|
help="if set, then is used as source for deb and tgz files",
|
|
|
|
)
|
|
|
|
parser.add_argument("--reports", default=True, help=argparse.SUPPRESS)
|
|
|
|
parser.add_argument(
|
|
|
|
"--no-reports",
|
|
|
|
action="store_false",
|
|
|
|
dest="reports",
|
|
|
|
default=argparse.SUPPRESS,
|
|
|
|
help="don't push reports to S3 and github",
|
|
|
|
)
|
|
|
|
parser.add_argument("--push", default=True, help=argparse.SUPPRESS)
|
|
|
|
parser.add_argument(
|
|
|
|
"--no-push-images",
|
|
|
|
action="store_false",
|
|
|
|
dest="push",
|
|
|
|
default=argparse.SUPPRESS,
|
|
|
|
help="don't push images to docker hub",
|
|
|
|
)
|
|
|
|
parser.add_argument("--os", default=["ubuntu", "alpine"], help=argparse.SUPPRESS)
|
|
|
|
parser.add_argument(
|
|
|
|
"--no-ubuntu",
|
|
|
|
action=DelOS,
|
|
|
|
nargs=0,
|
|
|
|
default=argparse.SUPPRESS,
|
|
|
|
help="don't build ubuntu image",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--no-alpine",
|
|
|
|
action=DelOS,
|
|
|
|
nargs=0,
|
|
|
|
default=argparse.SUPPRESS,
|
|
|
|
help="don't build alpine image",
|
|
|
|
)
|
|
|
|
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
2023-01-05 13:16:31 +00:00
|
|
|
def retry_popen(cmd: str, log_file: Path) -> int:
|
2022-07-06 11:58:46 +00:00
|
|
|
max_retries = 5
|
|
|
|
for retry in range(max_retries):
|
|
|
|
# From time to time docker build may failed. Curl issues, or even push
|
|
|
|
# It will sleep progressively 5, 15, 30 and 50 seconds between retries
|
|
|
|
progressive_sleep = 5 * sum(i + 1 for i in range(retry))
|
|
|
|
if progressive_sleep:
|
|
|
|
logging.warning(
|
|
|
|
"The following command failed, sleep %s before retry: %s",
|
|
|
|
progressive_sleep,
|
|
|
|
cmd,
|
|
|
|
)
|
|
|
|
time.sleep(progressive_sleep)
|
2023-01-05 13:16:31 +00:00
|
|
|
with TeePopen(
|
2022-07-06 11:58:46 +00:00
|
|
|
cmd,
|
2023-01-05 13:16:31 +00:00
|
|
|
log_file=log_file,
|
2022-07-06 11:58:46 +00:00
|
|
|
) as process:
|
|
|
|
retcode = process.wait()
|
|
|
|
if retcode == 0:
|
|
|
|
return 0
|
2023-01-05 13:16:31 +00:00
|
|
|
|
2022-07-06 11:58:46 +00:00
|
|
|
return retcode
|
|
|
|
|
|
|
|
|
2022-04-01 15:53:43 +00:00
|
|
|
def auto_release_type(version: ClickHouseVersion, release_type: str) -> str:
|
2022-03-30 22:15:01 +00:00
|
|
|
if release_type != "auto":
|
|
|
|
return release_type
|
|
|
|
|
|
|
|
git_versions = get_tagged_versions()
|
|
|
|
reference_version = git_versions[0]
|
|
|
|
for i in reversed(range(len(git_versions))):
|
2022-04-06 16:41:18 +00:00
|
|
|
if git_versions[i] <= version:
|
2022-03-30 22:15:01 +00:00
|
|
|
if i == len(git_versions) - 1:
|
|
|
|
return "latest"
|
|
|
|
reference_version = git_versions[i + 1]
|
|
|
|
break
|
|
|
|
|
2022-04-01 15:53:43 +00:00
|
|
|
if version.major < reference_version.major:
|
2022-03-30 22:15:01 +00:00
|
|
|
return "major"
|
2022-04-01 15:53:43 +00:00
|
|
|
if version.minor < reference_version.minor:
|
2022-03-30 22:15:01 +00:00
|
|
|
return "minor"
|
2022-04-01 15:53:43 +00:00
|
|
|
if version.patch < reference_version.patch:
|
2022-03-30 22:15:01 +00:00
|
|
|
return "patch"
|
|
|
|
|
|
|
|
raise ValueError(
|
|
|
|
"Release type 'tweak' is not supported for "
|
2022-04-01 15:53:43 +00:00
|
|
|
f"{version.string} < {reference_version.string}"
|
2022-03-30 22:15:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-04-01 15:53:43 +00:00
|
|
|
def gen_tags(version: ClickHouseVersion, release_type: str) -> List[str]:
|
2022-03-30 22:15:01 +00:00
|
|
|
"""
|
|
|
|
22.2.2.2 + latest:
|
|
|
|
- latest
|
|
|
|
- 22
|
|
|
|
- 22.2
|
|
|
|
- 22.2.2
|
|
|
|
- 22.2.2.2
|
|
|
|
22.2.2.2 + major:
|
|
|
|
- 22
|
|
|
|
- 22.2
|
|
|
|
- 22.2.2
|
|
|
|
- 22.2.2.2
|
|
|
|
22.2.2.2 + minor:
|
|
|
|
- 22.2
|
|
|
|
- 22.2.2
|
|
|
|
- 22.2.2.2
|
|
|
|
22.2.2.2 + patch:
|
|
|
|
- 22.2.2
|
|
|
|
- 22.2.2.2
|
|
|
|
22.2.2.2 + head:
|
|
|
|
- head
|
|
|
|
"""
|
2022-04-01 15:53:43 +00:00
|
|
|
parts = version.string.split(".")
|
2022-03-30 22:15:01 +00:00
|
|
|
tags = []
|
|
|
|
if release_type == "latest":
|
|
|
|
tags.append(release_type)
|
|
|
|
for i in range(len(parts)):
|
|
|
|
tags.append(".".join(parts[: i + 1]))
|
|
|
|
elif release_type == "major":
|
|
|
|
for i in range(len(parts)):
|
|
|
|
tags.append(".".join(parts[: i + 1]))
|
|
|
|
elif release_type == "minor":
|
|
|
|
for i in range(1, len(parts)):
|
|
|
|
tags.append(".".join(parts[: i + 1]))
|
|
|
|
elif release_type == "patch":
|
|
|
|
for i in range(2, len(parts)):
|
|
|
|
tags.append(".".join(parts[: i + 1]))
|
|
|
|
elif release_type == "head":
|
|
|
|
tags.append(release_type)
|
|
|
|
else:
|
|
|
|
raise ValueError(f"{release_type} is not valid release part")
|
|
|
|
return tags
|
|
|
|
|
|
|
|
|
2022-06-13 15:56:35 +00:00
|
|
|
def buildx_args(bucket_prefix: str, arch: str) -> List[str]:
|
2022-06-13 11:39:28 +00:00
|
|
|
args = [
|
|
|
|
f"--platform=linux/{arch}",
|
|
|
|
f"--label=build-url={GITHUB_RUN_URL}",
|
2022-06-13 15:56:35 +00:00
|
|
|
f"--label=com.clickhouse.build.githash={git.sha}",
|
2022-06-13 11:39:28 +00:00
|
|
|
]
|
2022-03-30 22:15:01 +00:00
|
|
|
if bucket_prefix:
|
|
|
|
url = p.join(bucket_prefix, BUCKETS[arch]) # to prevent a double //
|
|
|
|
args.append(f"--build-arg=REPOSITORY='{url}'")
|
|
|
|
args.append(f"--build-arg=deb_location_url='{url}'")
|
|
|
|
return args
|
|
|
|
|
|
|
|
|
|
|
|
def build_and_push_image(
|
2022-04-01 15:53:43 +00:00
|
|
|
image: DockerImage,
|
|
|
|
push: bool,
|
|
|
|
bucket_prefix: str,
|
|
|
|
os: str,
|
|
|
|
tag: str,
|
|
|
|
version: ClickHouseVersion,
|
2023-01-03 14:23:19 +00:00
|
|
|
) -> TestResults:
|
|
|
|
result = [] # type: TestResults
|
2022-03-30 22:15:01 +00:00
|
|
|
if os != "ubuntu":
|
|
|
|
tag += f"-{os}"
|
2022-04-06 16:41:18 +00:00
|
|
|
init_args = ["docker", "buildx", "build", "--build-arg BUILDKIT_INLINE_CACHE=1"]
|
2022-03-30 22:15:01 +00:00
|
|
|
if push:
|
|
|
|
init_args.append("--push")
|
|
|
|
init_args.append("--output=type=image,push-by-digest=true")
|
|
|
|
init_args.append(f"--tag={image.repo}")
|
|
|
|
else:
|
|
|
|
init_args.append("--output=type=docker")
|
|
|
|
|
|
|
|
# `docker buildx build --load` does not support multiple images currently
|
|
|
|
# images must be built separately and merged together with `docker manifest`
|
|
|
|
digests = []
|
2023-01-05 12:54:31 +00:00
|
|
|
multiplatform_sw = Stopwatch()
|
2022-03-30 22:15:01 +00:00
|
|
|
for arch in BUCKETS:
|
2023-01-05 12:54:31 +00:00
|
|
|
single_sw = Stopwatch()
|
2022-03-30 22:15:01 +00:00
|
|
|
arch_tag = f"{tag}-{arch}"
|
|
|
|
metadata_path = p.join(TEMP_PATH, arch_tag)
|
|
|
|
dockerfile = p.join(image.full_path, f"Dockerfile.{os}")
|
|
|
|
cmd_args = list(init_args)
|
2022-06-13 15:56:35 +00:00
|
|
|
cmd_args.extend(buildx_args(bucket_prefix, arch))
|
2022-03-30 22:15:01 +00:00
|
|
|
if not push:
|
|
|
|
cmd_args.append(f"--tag={image.repo}:{arch_tag}")
|
|
|
|
cmd_args.extend(
|
|
|
|
[
|
|
|
|
f"--metadata-file={metadata_path}",
|
2022-04-01 15:53:43 +00:00
|
|
|
f"--build-arg=VERSION='{version.string}'",
|
2022-03-30 22:15:01 +00:00
|
|
|
"--progress=plain",
|
|
|
|
f"--file={dockerfile}",
|
|
|
|
image.full_path,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
cmd = " ".join(cmd_args)
|
|
|
|
logging.info("Building image %s:%s for arch %s: %s", image.repo, tag, arch, cmd)
|
2023-01-05 13:16:31 +00:00
|
|
|
log_file = Path(TEMP_PATH) / f"{image.repo.replace('/', '__')}:{tag}-{arch}.log"
|
|
|
|
if retry_popen(cmd, log_file) != 0:
|
2023-01-05 12:54:31 +00:00
|
|
|
result.append(
|
|
|
|
TestResult(
|
2023-01-05 13:16:31 +00:00
|
|
|
f"{image.repo}:{tag}-{arch}",
|
|
|
|
"FAIL",
|
|
|
|
single_sw.duration_seconds,
|
|
|
|
[log_file],
|
2023-01-05 12:54:31 +00:00
|
|
|
)
|
|
|
|
)
|
2022-07-06 11:58:46 +00:00
|
|
|
return result
|
2023-01-05 12:54:31 +00:00
|
|
|
result.append(
|
2023-01-05 13:16:31 +00:00
|
|
|
TestResult(
|
|
|
|
f"{image.repo}:{tag}-{arch}",
|
|
|
|
"OK",
|
|
|
|
single_sw.duration_seconds,
|
|
|
|
[log_file],
|
|
|
|
)
|
2023-01-05 12:54:31 +00:00
|
|
|
)
|
2022-07-06 11:58:46 +00:00
|
|
|
with open(metadata_path, "rb") as m:
|
|
|
|
metadata = json.load(m)
|
|
|
|
digests.append(metadata["containerimage.digest"])
|
2022-03-30 22:15:01 +00:00
|
|
|
if push:
|
|
|
|
cmd = (
|
|
|
|
"docker buildx imagetools create "
|
|
|
|
f"--tag {image.repo}:{tag} {' '.join(digests)}"
|
|
|
|
)
|
|
|
|
logging.info("Pushing merged %s:%s image: %s", image.repo, tag, cmd)
|
2023-01-05 13:16:31 +00:00
|
|
|
if retry_popen(cmd, Path("/dev/null")) != 0:
|
2023-01-05 12:54:31 +00:00
|
|
|
result.append(
|
|
|
|
TestResult(
|
|
|
|
f"{image.repo}:{tag}", "FAIL", multiplatform_sw.duration_seconds
|
|
|
|
)
|
|
|
|
)
|
2022-07-06 11:58:46 +00:00
|
|
|
return result
|
2023-01-05 12:54:31 +00:00
|
|
|
result.append(
|
|
|
|
TestResult(f"{image.repo}:{tag}", "OK", multiplatform_sw.duration_seconds)
|
|
|
|
)
|
2022-03-30 22:15:01 +00:00
|
|
|
else:
|
|
|
|
logging.info(
|
|
|
|
"Merging is available only on push, separate %s images are created",
|
|
|
|
f"{image.repo}:{tag}-$arch",
|
|
|
|
)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
stopwatch = Stopwatch()
|
|
|
|
makedirs(TEMP_PATH, exist_ok=True)
|
|
|
|
|
|
|
|
args = parse_args()
|
|
|
|
image = DockerImage(args.image_path, args.image_repo, False)
|
|
|
|
args.release_type = auto_release_type(args.version, args.release_type)
|
|
|
|
tags = gen_tags(args.version, args.release_type)
|
2022-07-21 11:10:22 +00:00
|
|
|
NAME = f"Docker image {image.repo} building check"
|
2022-03-30 22:15:01 +00:00
|
|
|
pr_info = None
|
|
|
|
if CI:
|
|
|
|
pr_info = PRInfo()
|
2022-05-19 22:06:44 +00:00
|
|
|
release_or_pr, _ = get_release_or_pr(pr_info, args.version)
|
2022-03-30 22:15:01 +00:00
|
|
|
args.bucket_prefix = (
|
2022-08-11 13:01:32 +00:00
|
|
|
f"{S3_DOWNLOAD}/{S3_BUILDS_BUCKET}/{release_or_pr}/{pr_info.sha}"
|
2022-03-30 22:15:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if args.push:
|
|
|
|
subprocess.check_output( # pylint: disable=unexpected-keyword-arg
|
|
|
|
"docker login --username 'robotclickhouse' --password-stdin",
|
|
|
|
input=get_parameter_from_ssm("dockerhub_robot_password"),
|
|
|
|
encoding="utf-8",
|
|
|
|
shell=True,
|
|
|
|
)
|
2022-07-21 11:10:22 +00:00
|
|
|
NAME = f"Docker image {image.repo} build and push"
|
2022-03-30 22:15:01 +00:00
|
|
|
|
|
|
|
logging.info("Following tags will be created: %s", ", ".join(tags))
|
|
|
|
status = "success"
|
2023-01-03 14:23:19 +00:00
|
|
|
test_results = [] # type: TestResults
|
2022-03-30 22:15:01 +00:00
|
|
|
for os in args.os:
|
|
|
|
for tag in tags:
|
|
|
|
test_results.extend(
|
|
|
|
build_and_push_image(
|
|
|
|
image, args.push, args.bucket_prefix, os, tag, args.version
|
|
|
|
)
|
|
|
|
)
|
2023-01-03 14:23:19 +00:00
|
|
|
if test_results[-1].status != "OK":
|
2022-03-30 22:15:01 +00:00
|
|
|
status = "failure"
|
|
|
|
|
|
|
|
pr_info = pr_info or PRInfo()
|
2022-08-11 13:01:32 +00:00
|
|
|
s3_helper = S3Helper()
|
2022-03-30 22:15:01 +00:00
|
|
|
|
|
|
|
url = upload_results(s3_helper, pr_info.number, pr_info.sha, test_results, [], NAME)
|
|
|
|
|
|
|
|
print(f"::notice ::Report url: {url}")
|
|
|
|
|
|
|
|
if not args.reports:
|
|
|
|
return
|
|
|
|
|
|
|
|
description = f"Processed tags: {', '.join(tags)}"
|
|
|
|
|
2023-02-27 12:11:17 +00:00
|
|
|
description = format_description(description)
|
2022-03-30 22:15:01 +00:00
|
|
|
|
2022-07-30 05:07:22 +00:00
|
|
|
gh = Github(get_best_robot_token(), per_page=100)
|
2022-03-30 22:15:01 +00:00
|
|
|
post_commit_status(gh, pr_info.sha, NAME, description, status, url)
|
|
|
|
|
|
|
|
prepared_events = prepare_tests_results_for_clickhouse(
|
|
|
|
pr_info,
|
|
|
|
test_results,
|
|
|
|
status,
|
|
|
|
stopwatch.duration_seconds,
|
|
|
|
stopwatch.start_time_str,
|
|
|
|
url,
|
|
|
|
NAME,
|
|
|
|
)
|
|
|
|
ch_helper = ClickHouseHelper()
|
|
|
|
ch_helper.insert_events_into(db="default", table="checks", events=prepared_events)
|
2022-05-19 22:06:44 +00:00
|
|
|
if status != "success":
|
|
|
|
sys.exit(1)
|
2022-03-30 22:15:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|