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
|
|
|
|
|
2024-02-02 14:33:08 +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
|
2024-02-02 14:33:08 +00:00
|
|
|
from docker_images_helper import docker_login, get_images_oredered_list
|
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
|
2024-02-02 14:35:13 +00:00
|
|
|
from report import FAILURE, SUCCESS, StatusType, 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
|
|
|
|
|
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
|
|
|
|
2024-02-26 18:25:02 +00:00
|
|
|
if os.path.isfile(args.image_tags):
|
|
|
|
with open(args.image_tags, "r", encoding="utf-8") as jfd:
|
|
|
|
image_tags = json.load(jfd)
|
|
|
|
else:
|
|
|
|
image_tags = json.loads(args.image_tags)
|
|
|
|
|
|
|
|
if args.missing_images == "all":
|
|
|
|
missing_images = image_tags
|
|
|
|
elif os.path.isfile(args.missing_images):
|
|
|
|
with open(args.missing_images, "r", encoding="utf-8") as jfd:
|
|
|
|
missing_images = json.load(jfd)
|
|
|
|
else:
|
|
|
|
missing_images = json.loads(args.missing_images)
|
|
|
|
|
2023-12-18 08:07:22 +00:00
|
|
|
test_results = []
|
2024-02-02 14:35:13 +00:00
|
|
|
status = SUCCESS # type: StatusType
|
2023-12-15 14:48:01 +00:00
|
|
|
|
2023-12-18 08:07:22 +00:00
|
|
|
ok_cnt, fail_cnt = 0, 0
|
|
|
|
images = get_images_oredered_list()
|
|
|
|
for image_obj in images:
|
|
|
|
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]
|
2024-02-05 17:09:43 +00:00
|
|
|
|
|
|
|
# 1. update multiarch latest manifest for every image
|
2023-12-18 08:07:22 +00:00
|
|
|
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
|
|
|
|
2024-02-05 17:09:43 +00:00
|
|
|
# 2. skip manifest create if not missing
|
|
|
|
if image_obj.repo not in missing_images:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# 3. created image:digest multiarch manifest for changed images only
|
|
|
|
manifest, test_result = create_manifest(image_obj.repo, tag, tags, args.push)
|
|
|
|
test_results.append(TestResult(manifest, test_result))
|
|
|
|
|
2023-12-18 08:07:22 +00:00
|
|
|
if test_result != "OK":
|
2024-02-02 14:35:13 +00:00
|
|
|
status = FAILURE
|
2023-12-18 08:07:22 +00:00
|
|
|
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
|
|
|
|
2024-11-12 11:08:14 +00:00
|
|
|
url = upload_results(
|
|
|
|
s3_helper, pr_info.number, pr_info.sha, pr_info.head_ref, test_results, [], NAME
|
|
|
|
)
|
2022-01-04 13:50:01 +00:00
|
|
|
|
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)
|
2024-02-06 12:39:34 +00:00
|
|
|
if status == FAILURE:
|
2023-12-18 08:07:22 +00:00
|
|
|
sys.exit(1)
|
2022-01-04 13:50:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|