|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import datetime |
| 5 | +import json |
| 6 | +import os |
| 7 | +import subprocess |
| 8 | +import tempfile |
| 9 | +import shutil |
| 10 | +from stat import ( |
| 11 | + S_IREAD, |
| 12 | + S_IRGRP, |
| 13 | + S_IROTH) |
| 14 | +from cosalib.builds import Builds |
| 15 | +from cosalib.cmdlib import ( |
| 16 | + rfc3339_time, |
| 17 | + get_basearch, |
| 18 | + sha256sum_file) |
| 19 | + |
| 20 | +VALID_NAMES = ["fedora-coreos", "rhcos", "scos"] |
| 21 | + |
| 22 | + |
| 23 | +def main(): |
| 24 | + args = parse_args() |
| 25 | + with tempfile.TemporaryDirectory(prefix='cosa-import-', dir='tmp') as tmpd: |
| 26 | + tmp_ociarchive = os.path.join(tmpd, "out.ociarchive") |
| 27 | + |
| 28 | + metadata = prepare_build(args, tmp_ociarchive) |
| 29 | + |
| 30 | + name = metadata['Labels']['com.coreos.osname'] |
| 31 | + buildid = metadata['Labels']['org.opencontainers.image.version'] |
| 32 | + arch = get_basearch() |
| 33 | + |
| 34 | + manifest = generate_manifest_json(tmpd, tmp_ociarchive, name, buildid, arch) |
| 35 | + meta_json = generate_meta_json(tmp_ociarchive, metadata, manifest['metadata'], name) |
| 36 | + |
| 37 | + finalize_build(tmp_ociarchive, meta_json, manifest, buildid, arch) |
| 38 | + |
| 39 | + |
| 40 | +def parse_args(): |
| 41 | + parser = argparse.ArgumentParser(prog='cosa import') |
| 42 | + parser.add_argument("srcimg", metavar='IMAGE', |
| 43 | + help="image to import (containers-transports(5) format)") |
| 44 | + return parser.parse_args() |
| 45 | + |
| 46 | + |
| 47 | +def prepare_build(args, target_ociarchive): |
| 48 | + import_oci_archive(args, target_ociarchive) |
| 49 | + return inspect_oci_archive(target_ociarchive) |
| 50 | + |
| 51 | + |
| 52 | +def finalize_build(source_ociarchive, meta_json, manifest, buildid, arch): |
| 53 | + os.makedirs(f'builds/{buildid}/{arch}/', exist_ok=True) |
| 54 | + |
| 55 | + archive_name = meta_json['images']['ostree']['path'] |
| 56 | + archive_path = f'builds/{buildid}/{arch}/{archive_name}' |
| 57 | + # Move ociarchive to build dir |
| 58 | + shutil.move(source_ociarchive, archive_path) |
| 59 | + |
| 60 | + # move manifest file |
| 61 | + manifest_fname = manifest['metadata']['path'] |
| 62 | + shutil.move(manifest['src_path'], f'builds/{buildid}/{arch}/{manifest_fname}') |
| 63 | + |
| 64 | + with open(f'builds/{buildid}/{arch}/meta.json', 'w') as meta_file: |
| 65 | + json.dump(meta_json, meta_file, indent=4) |
| 66 | + |
| 67 | + # Symlink build to latest |
| 68 | + if os.path.exists('builds/latest'): |
| 69 | + os.remove('builds/latest') |
| 70 | + os.symlink(f'{buildid}', 'builds/latest', target_is_directory=True) |
| 71 | + |
| 72 | + builds = Builds() |
| 73 | + update_builds_json(builds, buildid, arch) |
| 74 | + |
| 75 | + print(f'Successfully import oci image to {archive_path}') |
| 76 | + |
| 77 | + |
| 78 | +def update_builds_json(builds, buildid, arch): |
| 79 | + builds.insert_build(buildid, arch) |
| 80 | + builds.bump_timestamp() |
| 81 | + builds.flush() |
| 82 | + |
| 83 | + |
| 84 | +def import_oci_archive(args, target): |
| 85 | + subprocess.check_call(['skopeo', 'copy', args.srcimg, |
| 86 | + f"oci-archive:{target}"]) |
| 87 | + |
| 88 | + |
| 89 | +def inspect_oci_archive(image): |
| 90 | + out = subprocess.check_output(['skopeo', 'inspect', |
| 91 | + f'oci-archive:{image}']) |
| 92 | + return json.loads(out) |
| 93 | + |
| 94 | + |
| 95 | +def generate_manifest_json(tmpd, ociarchive, name, buildid, arch): |
| 96 | + manifest = subprocess.check_output(["skopeo", "inspect", "--raw", f"oci-archive:{ociarchive}"]) |
| 97 | + |
| 98 | + ostree_oci_manifest_path = f"{name}-{buildid}-ostree.{arch}-manifest.json" |
| 99 | + manifest_json_dest = f'{tmpd}/manifest.json' |
| 100 | + |
| 101 | + manifest_json_sha256 = None |
| 102 | + manifest_json_size = None |
| 103 | + with open(manifest_json_dest, 'wb') as manifest_json: |
| 104 | + manifest_json.write(manifest) |
| 105 | + os.fchmod(manifest_json.fileno(), S_IREAD | S_IRGRP | S_IROTH) |
| 106 | + |
| 107 | + manifest_json_sha256 = sha256sum_file(manifest_json_dest) |
| 108 | + manifest_json_size = os.path.getsize(manifest_json_dest) |
| 109 | + |
| 110 | + manifest_metadata = { |
| 111 | + 'path': ostree_oci_manifest_path, |
| 112 | + 'sha256': manifest_json_sha256, |
| 113 | + 'size': manifest_json_size, |
| 114 | + "skip-compression": True, |
| 115 | + } |
| 116 | + |
| 117 | + return { |
| 118 | + 'metadata': manifest_metadata, |
| 119 | + 'src_path': manifest_json_dest |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | +def parse_timestamp(timestamp): |
| 124 | + # datetime's doesn't support nanoseconds. |
| 125 | + # So trim it. |
| 126 | + if len(timestamp) > 26 and timestamp[19] == '.': |
| 127 | + timestamp = timestamp[:26] + "Z" |
| 128 | + |
| 129 | + timestamp = datetime.datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ') |
| 130 | + return rfc3339_time(timestamp.replace(tzinfo=datetime.timezone.utc)) |
| 131 | + |
| 132 | + |
| 133 | +def generate_meta_json(ociarchive, metadata, oci_manifest, name): |
| 134 | + archive_sha256sum = sha256sum_file(ociarchive) |
| 135 | + |
| 136 | + # let raise if missing |
| 137 | + assert metadata['Labels']['containers.bootc'] == '1' |
| 138 | + |
| 139 | + buildid = metadata['Labels']['org.opencontainers.image.version'] |
| 140 | + arch = get_basearch() |
| 141 | + created_timestamp = parse_timestamp(metadata['Created']) |
| 142 | + |
| 143 | + meta_json = { |
| 144 | + 'ostree-version': buildid, # proxy version label |
| 145 | + 'buildid': buildid, # also version label |
| 146 | + 'coreos-assembler.build-timestamp': created_timestamp, # proxy OCI build timestamp |
| 147 | + 'coreos-assembler.oci-imported': True, |
| 148 | + 'name': name, |
| 149 | + 'ostree-timestamp': created_timestamp, |
| 150 | + 'images': { |
| 151 | + 'ostree': { |
| 152 | + "path": f"{name}-{buildid}-ostree.{arch}.ociarchive", |
| 153 | + "sha256": archive_sha256sum, |
| 154 | + "skip-compression": True |
| 155 | + }, |
| 156 | + 'oci-manifest': oci_manifest, |
| 157 | + }, |
| 158 | + 'coreos-assembler.basearch': arch, |
| 159 | + } |
| 160 | + |
| 161 | + return meta_json |
| 162 | + |
| 163 | + |
| 164 | +if __name__ == '__main__': |
| 165 | + main() |
0 commit comments