From c45569a1b05dbd2627abc933a4286ed5cd0b1d8d Mon Sep 17 00:00:00 2001 From: Luke Yang Date: Wed, 25 Oct 2023 13:47:56 -0400 Subject: [PATCH] stages/ostree.deploy.container: add aleph file Similar to the aleph file created for builds of FCOS based on ostree commit inputs, this adds an aleph file builds based on container image inputs. --- stages/org.osbuild.ostree.deploy.container | 32 +++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/stages/org.osbuild.ostree.deploy.container b/stages/org.osbuild.ostree.deploy.container index 804e27234..029fed2fd 100755 --- a/stages/org.osbuild.ostree.deploy.container +++ b/stages/org.osbuild.ostree.deploy.container @@ -6,12 +6,15 @@ Create an OSTree deployment[1] for a given container image input """ import os +import subprocess import sys import osbuild.api from osbuild.util import containers, ostree from osbuild.util.mnt import MountGuard +import json + CAPABILITIES = ["CAP_MAC_ADMIN"] SCHEMA_2 = """ @@ -80,6 +83,14 @@ SCHEMA_2 = """ } """ +def skopeo_cli(*args): + """Thin wrapper for running the skopeo CLI""" + args = list(args) + print("skopeo " + " ".join(args), file=sys.stderr) + return subprocess.run(["skopeo"] + args, + check=True, + capture_output=True, + text=True).stdout.rstrip().strip('\"') def make_fs_identifier(desc): for key in ["uuid", "label"]: @@ -92,7 +103,7 @@ def make_fs_identifier(desc): def ostree_container_deploy(tree, inputs, osname, target_imgref, kopts): images = containers.parse_containers_input(inputs) for image in images.values(): - with containers.container_source(image) as (_, image_source): + with containers.container_source(image) as (image_name, image_source): extra_args = [] imgref = f"ostree-unverified-image:{image_source}" @@ -109,6 +120,25 @@ def ostree_container_deploy(tree, inputs, osname, target_imgref, kopts): ostree.cli("container", "image", "deploy", *extra_args, sysroot=tree, *kargs) + aleph_image_digest = skopeo_cli("inspect", f"{image_source}", "--format", '"{{ .Digest }}"') + aleph_image_revision = skopeo_cli("inspect", f"{image_source}", "--format", '"{{ index .Labels \"org.opencontainers.image.revision\" }}"') + aleph_image_source = skopeo_cli("inspect", f"{image_source}", "--format", '"{{ index .Labels \"org.opencontainers.image.source\" }}"') + aleph_image_version = skopeo_cli("inspect", f"{image_source}", "--format", '"{{ index .Labels \"org.opencontainers.image.version\" }}"') + + aleph_version_data = { + "osbuild-version": osbuild.__version__, + "image-name": image_name, + "image-digest": aleph_image_digest, + "image-revision": aleph_image_revision, + "image-source": aleph_image_source, + "image-version": aleph_image_version + } + + aleph_json = json.dumps(aleph_version_data, indent=4) + with open(tree + "/.coreos-aleph-version.json", "w") as outfile: + outfile.write(aleph_json + "\n") + outfile.close() + def main(tree, inputs, options): osname = options["osname"]