|
| 1 | +#!/usr/bin/python3 -u |
| 2 | +# Upload an oscontainer. This is a wrapper for |
| 3 | +# `cosa oscontainer` that just for historical reasons |
| 4 | +# used to live downstream in the redhat-coreos pipeline. |
| 5 | +# In the future we should just have one `cosa oscontainer` |
| 6 | +# command. |
| 7 | + |
| 8 | +import argparse |
| 9 | +import json |
| 10 | +import yaml |
| 11 | +import os |
| 12 | +import shutil |
| 13 | +import subprocess |
| 14 | +import sys |
| 15 | +from cosalib.cmdlib import sha256sum_file |
| 16 | + |
| 17 | +cosa_dir = os.path.dirname(os.path.abspath(__file__)) |
| 18 | +sys.path.insert(0, cosa_dir) |
| 19 | + |
| 20 | +from cosalib import cmdlib |
| 21 | + |
| 22 | + |
| 23 | +with open('builds/builds.json') as f: |
| 24 | + builds = json.load(f)['builds'] |
| 25 | +if len(builds) == 0: |
| 26 | + cmdlib.fatal("No builds found") |
| 27 | +latest_build = builds[0]['id'] |
| 28 | +arch = cmdlib.get_basearch() |
| 29 | +latest_build_path = f"builds/{latest_build}/{arch}" |
| 30 | + |
| 31 | +metapath = f"{latest_build_path}/meta.json" |
| 32 | +with open(metapath) as f: |
| 33 | + meta = json.load(f) |
| 34 | + |
| 35 | +name = meta['name'] + '-' + meta['buildid'] + '-oscontainer.' + arch + '.ociarchive' |
| 36 | +parser = argparse.ArgumentParser() |
| 37 | +parser.add_argument("--arch-tag", help="append arch name to push tag", |
| 38 | + action='store_true') |
| 39 | +parser.add_argument("--name", help="oscontainer name", |
| 40 | + action='store', default=f'{name}') |
| 41 | +parser.add_argument("--from", help="Base image", default='scratch', |
| 42 | + dest='from_image') |
| 43 | +parser.add_argument("--format", help="Format to use for push") |
| 44 | +parser.add_argument("--add-directory", help="Copy in all content from referenced directory DIR", |
| 45 | + metavar='DIR', action='append', default=[]) |
| 46 | + |
| 47 | +args = parser.parse_args() |
| 48 | + |
| 49 | +# for backcompat, we auto-build extensions if they're missing |
| 50 | +if os.path.exists('src/config/extensions.yaml'): |
| 51 | + if 'extensions' not in meta: |
| 52 | + cmdlib.runcmd(['coreos-assembler', 'buildextend-extensions']) |
| 53 | + with open(metapath) as f: |
| 54 | + meta = json.load(f) |
| 55 | + assert 'extensions' in meta |
| 56 | + |
| 57 | +configdir = os.path.abspath('src/config') |
| 58 | +oscconfigpath = f'{configdir}/oscontainer.yaml' |
| 59 | +# XXX: fold oscontainer.yaml handling into oscontainer.py |
| 60 | +configyaml = {} |
| 61 | +if os.path.exists(oscconfigpath): |
| 62 | + with open(oscconfigpath) as f: |
| 63 | + configyaml = yaml.safe_load(f) |
| 64 | + |
| 65 | +if 'base' in configyaml: |
| 66 | + args.from_image = configyaml['base'] |
| 67 | + |
| 68 | +print("Preparing to upload oscontainer for build: {}".format(latest_build)) |
| 69 | +ostree_commit = meta['ostree-commit'] |
| 70 | + |
| 71 | +tmprepo = "{}/tmp/repo".format(os.getcwd()) |
| 72 | +# if tmprepo is not a directory, but is unexpectedly a file, |
| 73 | +# just nuke it |
| 74 | +if not os.path.isdir(tmprepo) and os.path.exists(tmprepo): |
| 75 | + os.remove(tmprepo) |
| 76 | + |
| 77 | +# if tmprepo is not a directory and not a file, recreate from |
| 78 | +# the tarfile |
| 79 | +if not os.path.exists(tmprepo): |
| 80 | + os.makedirs(tmprepo, exist_ok=True) |
| 81 | + ostree_commit_tar = meta['images']['ostree']['path'] |
| 82 | + subprocess.check_call(['tar', '-xf', |
| 83 | + f'{latest_build_path}/{ostree_commit_tar}', |
| 84 | + '-C', tmprepo]) |
| 85 | + |
| 86 | +tmp_osreleasedir = 'tmp/usrlib-osrelease' |
| 87 | +subprocess.check_call(['rm', '-rf', tmp_osreleasedir]) |
| 88 | +cmdlib.runcmd(['/usr/bin/ostree', 'checkout', '--repo', tmprepo, |
| 89 | + '--user-mode', '--subpath=/usr/lib/os-release', ostree_commit, |
| 90 | + tmp_osreleasedir]) |
| 91 | +display_name = None |
| 92 | +with open(os.path.join(tmp_osreleasedir, "os-release")) as f: |
| 93 | + display_name = subprocess.check_output(['/bin/sh', '-c', 'set -euo pipefail; . /proc/self/fd/0 && echo $NAME'], stdin=f, encoding='UTF-8').strip() |
| 94 | +if display_name == "": |
| 95 | + raise SystemExit(f"Failed to find NAME= in /usr/lib/os-release in commit {ostree_commit}") |
| 96 | +shutil.rmtree(tmp_osreleasedir) |
| 97 | + |
| 98 | +osc_name_and_tag = f"{args.name}:{latest_build}" |
| 99 | +if args.arch_tag: |
| 100 | + arch = meta.get("coreos-assembler.basearch", cmdlib.get_basearch) |
| 101 | + osc_name_and_tag = f"{args.name}:{latest_build}-{arch}" |
| 102 | + |
| 103 | +# TODO: Use labels for the build hash and avoid pulling the oscontainer |
| 104 | +# every time we want to poll. |
| 105 | +# TODO: Remove --from |
| 106 | +digestfile = "tmp/oscontainer-digest" |
| 107 | +print("Entering vm to build oscontainer for build: {}".format(latest_build)) |
| 108 | + |
| 109 | +cosa_argv = (['/usr/lib/coreos-assembler/build-legacy-oscontainer.sh', '--workdir=./tmp', 'build', f'--from={args.from_image}']) |
| 110 | +for d in args.add_directory: |
| 111 | + cosa_argv.append(f'--add-directory="{d}"') |
| 112 | +cosa_argv.append(f'--display-name="{display_name}"') |
| 113 | +if 'labeled-packages' in configyaml: |
| 114 | + pkgs = ' '.join(configyaml['labeled-packages']) |
| 115 | + cosa_argv.append(f'--labeled-packages="{pkgs}"') |
| 116 | +if args.format is not None: |
| 117 | + cosa_argv.append(f'--format={args.format}') |
| 118 | +subprocess.check_call(cosa_argv + |
| 119 | + [f'--digestfile={digestfile}', |
| 120 | + '--push', tmprepo, |
| 121 | + meta['ostree-commit'], |
| 122 | + osc_name_and_tag]) |
| 123 | + |
| 124 | +# Inject the oscontainer with SHA256 into the build metadata |
| 125 | +oci_archive = f"{latest_build_path}/{args.name}" |
| 126 | +meta['images']['legacy-oscontainer'] = {'path': args.name, |
| 127 | + 'sha256': sha256sum_file(oci_archive), |
| 128 | + 'size': os.path.getsize(oci_archive), |
| 129 | + "skip-compression": True} |
| 130 | +metapath_new = f"{metapath}.new" |
| 131 | +with open(metapath_new, 'w') as f: |
| 132 | + json.dump(meta, f, sort_keys=True) |
| 133 | +shutil.move(metapath_new, metapath) |
0 commit comments