2022-01-04 13:50:01 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
2023-12-18 08:07:22 +00:00
|
|
|
import sys
|
|
|
|
from typing import List, Tuple
|
2023-09-27 11:39:09 +00:00
|
|
|
|
2022-01-04 13:50:01 +00:00
|
|
|
from github import Github
|
|
|
|
|
2023-09-08 06:42:56 +00:00
|
|
|
from clickhouse_helper import (
|
|
|
|
ClickHouseHelper,
|
|
|
|
prepare_tests_results_for_clickhouse,
|
|
|
|
)
|
2023-04-18 23:03:48 +00:00
|
|
|
from commit_status_helper import format_description, get_commit, post_commit_status
|
2023-12-18 08:07:22 +00:00
|
|
|
from get_robot_token import get_best_robot_token
|
2022-01-04 13:50:01 +00:00
|
|
|
from pr_info import PRInfo
|
2023-12-18 08:07:22 +00:00
|
|
|
from report import TestResult
|
2022-01-04 13:50:01 +00:00
|
|
|
from s3_helper import S3Helper
|
|
|
|
from stopwatch import Stopwatch
|
|
|
|
from upload_result_helper import upload_results
|
2023-12-18 08:07:22 +00:00
|
|
|
from docker_images_helper import docker_login, get_images_oredered_list
|
2022-01-04 13:50:01 +00:00
|
|
|
|
2022-07-21 11:10:22 +00:00
|
|
|
NAME = "Push multi-arch images to Dockerhub"
|
2022-01-04 13:50:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def parse_args() -> argparse.Namespace:
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
2023-01-08 06:08:20 +00:00
|
|
|
description="The program gets images from changed_images_*.json, merges images "
|
2022-01-04 13:50:01 +00:00
|
|
|
"with different architectures into one manifest and pushes back to docker hub",
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"--suffix",
|
|
|
|
dest="suffixes",
|
|
|
|
type=str,
|
|
|
|
required=True,
|
|
|
|
action="append",
|
|
|
|
help="suffixes for existing images' tags. More than two should be given",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2023-12-18 08:07:22 +00:00
|
|
|
"--missing-images",
|
|
|
|
type=str,
|
|
|
|
required=True,
|
|
|
|
help="json (array) string or json file with images to create manifest for",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--image-tags",
|
|
|
|
type=str,
|
|
|
|
required=True,
|
|
|
|
help="json string or json file with all images and their tags {IMAGE: TAG}",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--set-latest",
|
|
|
|
action="store_true",
|
|
|
|
help="add latest tag",
|
2022-01-04 13:50:01 +00:00
|
|
|
)
|
2022-02-21 11:44:37 +00:00
|
|
|
parser.add_argument("--reports", default=True, help=argparse.SUPPRESS)
|
2022-01-04 13:50:01 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--no-reports",
|
2022-02-21 11:44:37 +00:00
|
|
|
action="store_false",
|
|
|
|
dest="reports",
|
|
|
|
default=argparse.SUPPRESS,
|
2022-01-04 13:50:01 +00:00
|
|
|
help="don't push reports to S3 and github",
|
|
|
|
)
|
2022-02-21 11:44:37 +00:00
|
|
|
parser.add_argument("--push", default=True, help=argparse.SUPPRESS)
|
2022-01-04 13:50:01 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--no-push-images",
|
2022-02-21 11:44:37 +00:00
|
|
|
action="store_false",
|
|
|
|
dest="push",
|
|
|
|
default=argparse.SUPPRESS,
|
2022-01-04 13:50:01 +00:00
|
|
|
help="don't push images to docker hub",
|
|
|
|
)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
if len(args.suffixes) < 2:
|
2022-02-10 15:22:32 +00:00
|
|
|
parser.error("more than two --suffix should be given")
|
2022-01-04 13:50:01 +00:00
|
|
|
|
|
|
|
return args
|
|
|
|
|
|
|
|
|
2023-12-18 08:07:22 +00:00
|
|
|
def create_manifest(
|
|
|
|
image: str, result_tag: str, tags: List[str], push: bool
|
|
|
|
) -> Tuple[str, str]:
|
|
|
|
manifest = f"{image}:{result_tag}"
|
|
|
|
cmd = "docker manifest create --amend " + " ".join(
|
|
|
|
(f"{image}:{t}" for t in [result_tag] + tags)
|
|
|
|
)
|
2022-01-04 13:50:01 +00:00
|
|
|
logging.info("running: %s", cmd)
|
2022-02-21 12:16:54 +00:00
|
|
|
with subprocess.Popen(
|
2022-01-04 13:50:01 +00:00
|
|
|
cmd,
|
|
|
|
shell=True,
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
universal_newlines=True,
|
2022-02-21 12:16:54 +00:00
|
|
|
) as popen:
|
|
|
|
retcode = popen.wait()
|
|
|
|
if retcode != 0:
|
|
|
|
output = popen.stdout.read() # type: ignore
|
|
|
|
logging.error("failed to create manifest for %s:\n %s\n", manifest, output)
|
|
|
|
return manifest, "FAIL"
|
|
|
|
if not push:
|
|
|
|
return manifest, "OK"
|
2022-01-04 13:50:01 +00:00
|
|
|
|
|
|
|
cmd = f"docker manifest push {manifest}"
|
|
|
|
logging.info("running: %s", cmd)
|
2022-02-21 12:16:54 +00:00
|
|
|
with subprocess.Popen(
|
2022-01-04 13:50:01 +00:00
|
|
|
cmd,
|
|
|
|
shell=True,
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
universal_newlines=True,
|
2022-02-21 12:16:54 +00:00
|
|
|
) as popen:
|
|
|
|
retcode = popen.wait()
|
|
|
|
if retcode != 0:
|
|
|
|
output = popen.stdout.read() # type: ignore
|
|
|
|
logging.error("failed to push %s:\n %s\n", manifest, output)
|
|
|
|
return manifest, "FAIL"
|
2022-01-04 13:50:01 +00:00
|
|
|
|
|
|
|
return manifest, "OK"
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
stopwatch = Stopwatch()
|
|
|
|
|
|
|
|
args = parse_args()
|
|
|
|
|
2023-12-18 08:07:22 +00:00
|
|
|
if args.push:
|
|
|
|
docker_login()
|
2022-01-04 13:50:01 +00:00
|
|
|
|
2023-12-18 08:07:22 +00:00
|
|
|
archs = args.suffixes
|
|
|
|
assert len(archs) > 1, "arch suffix input param is invalid"
|
2022-01-04 13:50:01 +00:00
|
|
|
|
2023-12-18 08:07:22 +00:00
|
|
|
image_tags = (
|
|
|
|
json.loads(args.image_tags)
|
|
|
|
if not os.path.isfile(args.image_tags)
|
|
|
|
else json.load(open(args.image_tags))
|
|
|
|
)
|
|
|
|
missing_images = (
|
|
|
|
list(image_tags)
|
|
|
|
if args.missing_images == "all"
|
|
|
|
else json.loads(args.missing_images)
|
|
|
|
if not os.path.isfile(args.missing_images)
|
|
|
|
else json.load(open(args.missing_images))
|
|
|
|
)
|
|
|
|
test_results = []
|
2023-12-15 14:48:01 +00:00
|
|
|
status = "success"
|
|
|
|
|
2023-12-18 08:07:22 +00:00
|
|
|
ok_cnt, fail_cnt = 0, 0
|
|
|
|
images = get_images_oredered_list()
|
|
|
|
for image_obj in images:
|
|
|
|
if image_obj.repo not in missing_images:
|
|
|
|
continue
|
|
|
|
tag = image_tags[image_obj.repo]
|
|
|
|
if image_obj.only_amd64:
|
|
|
|
# FIXME: WA until full arm support
|
|
|
|
tags = [f"{tag}-{arch}" for arch in archs if arch != "aarch64"]
|
|
|
|
else:
|
|
|
|
tags = [f"{tag}-{arch}" for arch in archs]
|
|
|
|
manifest, test_result = create_manifest(image_obj.repo, tag, tags, args.push)
|
|
|
|
test_results.append(TestResult(manifest, test_result))
|
|
|
|
if args.set_latest:
|
|
|
|
manifest, test_result = create_manifest(
|
|
|
|
image_obj.repo, "latest", tags, args.push
|
|
|
|
)
|
|
|
|
test_results.append(TestResult(manifest, test_result))
|
2023-09-08 06:42:56 +00:00
|
|
|
|
2023-12-18 08:07:22 +00:00
|
|
|
if test_result != "OK":
|
|
|
|
status = "failure"
|
|
|
|
fail_cnt += 1
|
|
|
|
else:
|
|
|
|
ok_cnt += 1
|
2022-01-04 13:50:01 +00:00
|
|
|
|
|
|
|
pr_info = PRInfo()
|
2022-08-11 13:01:32 +00:00
|
|
|
s3_helper = S3Helper()
|
2022-01-04 13:50:01 +00:00
|
|
|
|
|
|
|
url = upload_results(s3_helper, pr_info.number, pr_info.sha, test_results, [], NAME)
|
|
|
|
|
2022-02-21 12:16:54 +00:00
|
|
|
print(f"::notice ::Report url: {url}")
|
2022-01-04 13:50:01 +00:00
|
|
|
|
2022-02-21 11:44:37 +00:00
|
|
|
if not args.reports:
|
2022-01-04 13:50:01 +00:00
|
|
|
return
|
|
|
|
|
2023-12-18 08:07:22 +00:00
|
|
|
description = format_description(
|
|
|
|
f"Multiarch images created [ok: {ok_cnt}, failed: {fail_cnt}]"
|
|
|
|
)
|
2022-01-04 13:50:01 +00:00
|
|
|
|
2022-07-30 05:07:22 +00:00
|
|
|
gh = Github(get_best_robot_token(), per_page=100)
|
2023-04-18 23:03:48 +00:00
|
|
|
commit = get_commit(gh, pr_info.sha)
|
2023-12-18 08:07:22 +00:00
|
|
|
post_commit_status(
|
|
|
|
commit, status, url, description, NAME, pr_info, dump_to_file=True
|
|
|
|
)
|
2022-01-04 13:50:01 +00:00
|
|
|
|
|
|
|
prepared_events = prepare_tests_results_for_clickhouse(
|
|
|
|
pr_info,
|
|
|
|
test_results,
|
|
|
|
status,
|
|
|
|
stopwatch.duration_seconds,
|
|
|
|
stopwatch.start_time_str,
|
|
|
|
url,
|
|
|
|
NAME,
|
|
|
|
)
|
|
|
|
ch_helper = ClickHouseHelper()
|
2022-03-29 19:06:50 +00:00
|
|
|
ch_helper.insert_events_into(db="default", table="checks", events=prepared_events)
|
2023-12-18 08:07:22 +00:00
|
|
|
if status == "failure":
|
|
|
|
sys.exit(1)
|
2022-01-04 13:50:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|