Skip to content

Commit

Permalink
Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewarmtemp committed Nov 21, 2023
1 parent 732dfb3 commit a89ea3b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 57 deletions.
22 changes: 16 additions & 6 deletions osbuild/util/ostree.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import typing
# pylint doesn't understand the string-annotation below
from typing import Any, List # pylint: disable=unused-import
import glob

from osbuild.util.rhsm import Subscriptions

Expand Down Expand Up @@ -213,15 +214,24 @@ def parse_input_commits(commits):
def deployment_path(root: PathLike, osname: str, ref: str, serial: int):
"""Return the path to a deployment given the parameters"""

base = os.path.join(root, "ostree")
if osname == "" and ref == "" and serial == None:
filenames = glob.glob(root + f'/ostree/deploy/*/deploy/*.0', recursive=True)
if len(filenames) < 1:
raise ValueError("Cound not find deployment")
elif len(filenames) > 1:
raise ValueError("More than one deployment found")
return filenames[0]

else:
base = os.path.join(root, "ostree")

repo = os.path.join(base, "repo")
stateroot = os.path.join(base, "deploy", osname)
repo = os.path.join(base, "repo")
stateroot = os.path.join(base, "deploy", osname)

commit = rev_parse(repo, ref)
sysroot = f"{stateroot}/deploy/{commit}.{serial}"
commit = rev_parse(repo, ref)
sysroot = f"{stateroot}/deploy/{commit}.{serial}"

return sysroot
return sysroot


class PasswdLike:
Expand Down
93 changes: 52 additions & 41 deletions stages/org.osbuild.ostree.coreos.aleph
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import osbuild.api
from osbuild.util import ostree

import json
from glob import glob

CAPABILITIES = ["CAP_MAC_ADMIN"]

Expand All @@ -20,7 +19,7 @@ SCHEMA_2 = """
"options": {
"additionalProperties": false,
"properties": {
"coreos": {
"coreos_compat": {
"description": "boolean to allow for CoreOS aleph version backwards compatibility",
"type": "boolean"
},
Expand All @@ -47,21 +46,8 @@ SCHEMA_2 = """
}
"""

def write_to_file(tree, filename, data, coreos):
with open(tree + "/" + filename, "w") as f:
json.dump(data, f, indent=4, sort_keys=True)
f.write("\n")
f.close()

# create a symlink for backwards compatibility with CoreOS
if coreos:
cwd = os.getcwd()
os.chdir(tree)
os.symlink(filename, ".coreos-aleph-version.json")
os.chdir(cwd)


def aleph_commit(tree, imgref, coreos):
def aleph_commit(tree, imgref):
extra_args = []
extra_args.append("--print-metadata-key=version")

Expand All @@ -77,10 +63,10 @@ def aleph_commit(tree, imgref, coreos):
"ostree-commit": aleph_ostree_commit
}

write_to_file(tree, ".aleph-version.json", aleph_version_data, coreos)
return aleph_version_data


def aleph_container(tree, imgref, coreos):
def aleph_container(tree, imgref):
# extract the image name from the imgref
imgref_list = imgref.split(':')
if imgref_list[0] in ["ostree-remote-registry", "ostree-remote-image"]:
Expand All @@ -94,17 +80,17 @@ def aleph_container(tree, imgref, coreos):
extra_args.append(f"--repo={tree}/ostree/repo")
extra_args.append(f"registry:{img_name}")

container_data = ostree.cli("container", "image", "metadata", *extra_args).stdout.rstrip()
container_data_json = json.loads(container_data)
container_data_json = ostree.cli("container", "image", "metadata", *extra_args).stdout.rstrip()
container_data = json.loads(container_data_json)

extra_args.append("--config")
container_data_config = ostree.cli("container", "image", "metadata", *extra_args).stdout.rstrip()
container_data_config_json = json.loads(container_data_config)
container_data_config_json = ostree.cli("container", "image", "metadata", *extra_args).stdout.rstrip()
container_data_config = json.loads(container_data_config_json)

aleph_digest = container_data_json['config']['digest']
aleph_digest = container_data['config']['digest']
aleph_ref = f"docker://{imgref}"
aleph_version = container_data_config_json['config']['Labels']['org.opencontainers.image.version']
aleph_container_image = container_data_config_json['config']['Labels']
aleph_version = container_data_config['config']['Labels']['org.opencontainers.image.version']
aleph_container_image = container_data_config['config']['Labels']

aleph_version_data = {
"osbuild-version": osbuild.__version__,
Expand All @@ -120,16 +106,19 @@ def aleph_container(tree, imgref, coreos):

# the 'ostree.commit' label will be optional in the future so
# prevent hard failing if key is not found
aleph_ostree_commit = container_data_config_json['config']['Labels'].get('ostree.commit')
aleph_ostree_commit = container_data_config['config']['Labels'].get('ostree.commit')
if aleph_ostree_commit is not None:
aleph_version_data["ostree-commit"] = aleph_ostree_commit

write_to_file(tree, ".aleph-version.json", aleph_version_data, coreos)
return aleph_version_data


def main(tree, options, inputs,):
coreos = options.get("coreos", False)
def main(tree, options):
coreos_compat = options.get("coreos_compat", False)
dep = options.get("deployment", None)
osname = ""
ref = ""
serial = None
origin = ""

# if deployment is specified, use it to find the origin file.
Expand All @@ -138,23 +127,45 @@ def main(tree, options, inputs,):
osname = dep.get("osname", "")
ref = dep.get("ref", "")
serial = dep.get("serial", 0)
origin = ostree.deployment_path(tree, osname, ref, serial) + ".origin"
else:
for filename in glob(tree + '/**/*.0.origin', recursive=True):
origin = filename

with open(origin) as f: s = f.read()
imgref = s.split("=")[1].rstrip()

if 'docker://' in imgref:
aleph_container(tree, imgref, coreos)
origin = ostree.deployment_path(tree, osname, ref, serial) + ".origin"
# use the origin file to find the target_imgref and dictate whether
# the system was deployed from an container image or ostree commit.
# example container case: container-image-reference=ostree-remote-image:fedora:docker://quay.io/fedora/fedora-coreos:stable
# example ostree commit case: refspec=fedora:fedora/x86_64/coreos/stable
deploy_type = ""
imgref = ""
data = {}
with open(origin) as f:
for line in f:
separated_line = line.split("=")
if separated_line[0] == "container-image-reference":
deploy_type = "container"
imgref = separated_line[1].rstrip()
break
elif separated_line[0] == "refspec":
deploy_type = "ostree_commit"
imgref = separated_line[1].rstrip()
break
if deploy_type == "container":
data = aleph_container(tree, imgref)
elif deploy_type == "ostree_commit":
data = aleph_commit(tree, imgref)
else:
aleph_commit(tree, imgref, coreos)
raise ValueError("Could not find 'container-image-reference' or 'refspec' in origin file")

# write the data out to the file
filename = ".aleph-version.json"
with open(os.path.join(tree, filename), "w") as f:
json.dump(data, f, indent=4, sort_keys=True)
f.write("\n")

# create a symlink for backwards compatibility with CoreOS
if coreos_compat:
os.symlink(filename, os.path.join(tree, ".coreos-aleph-version.json"))

if __name__ == '__main__':
stage_args = osbuild.api.arguments()
r = main(stage_args["tree"],
stage_args["options"],
stage_args["inputs"])
stage_args["options"])
sys.exit(r)
4 changes: 3 additions & 1 deletion stages/org.osbuild.ostree.deploy.container
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ SCHEMA_2 = """
}
"""


def make_fs_identifier(desc):
for key in ["uuid", "label"]:
val = desc.get(key)
Expand All @@ -92,7 +93,7 @@ def ostree_container_deploy(tree, inputs, osname, target_imgref, kopts):
images = containers.parse_containers_input(inputs)
for image in images.values():
print(image)
with containers.container_source(image) as (__, image_source):
with containers.container_source(image) as (_, image_source):
extra_args = []
imgref = f"ostree-unverified-image:{image_source}"

Expand All @@ -105,6 +106,7 @@ def ostree_container_deploy(tree, inputs, osname, target_imgref, kopts):
extra_args.append(f'--target-imgref={target_imgref}')

kargs = [f'--karg={v}' for v in kopts]

ostree.cli("container", "image", "deploy",
*extra_args, sysroot=tree, *kargs)

Expand Down
4 changes: 2 additions & 2 deletions test/data/manifests/fedora-coreos-container.mpp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pipelines:
tag: stable
- type: org.osbuild.ostree.coreos.aleph
options:
coreos: true
coreos_compat: true
# - type: org.osbuild.ostree.deploy
# options:
# osname: fedora-coreos
Expand All @@ -106,7 +106,7 @@ pipelines:
# ref: fedora/x86_64/coreos/stable
# - type: org.osbuild.ostree.coreos.aleph
# options:
# coreos: true
# coreos_compat: true
- type: org.osbuild.ostree.selinux
options:
deployment:
Expand Down
10 changes: 3 additions & 7 deletions tools/osbuild-mpp
Original file line number Diff line number Diff line change
Expand Up @@ -1547,12 +1547,8 @@ class ManifestFileV2(ManifestFile):

def _process_container(self, stage):
if stage.get("type", "") not in \
["org.osbuild.skopeo", "org.osbuild.ostree.deploy.container", "org.osbuild.ostree.coreos.aleph"]:
["org.osbuild.skopeo", "org.osbuild.ostree.deploy.container"]:
return

if stage.get("type", "") == "org.osbuild.ostree.coreos.aleph":
if "images" not in stage.get("inputs", {}):
return

inputs = element_enter(stage, "inputs", {})

Expand Down Expand Up @@ -1626,7 +1622,7 @@ class ManifestFileV2(ManifestFile):

def _process_ostree_commits(self, stage):
if stage.get("type", "") not in \
["org.osbuild.ostree.pull", "org.osbuild.ostree.deploy", "org.osbuild.ostree.coreos.aleph"]:
["org.osbuild.ostree.pull", "org.osbuild.ostree.deploy"]:
return

# The ostree.deploy stage accepts both containers or
Expand All @@ -1635,7 +1631,7 @@ class ManifestFileV2(ManifestFile):
# return early. This prevents an empty "commits" object
# from being created when a container image is used as
# an input to ostree.deploy and not an ostree commit.
if stage.get("type", "") in ["org.osbuild.ostree.deploy", "org.osbuild.ostree.coreos.aleph"]:
if stage.get("type", "") == "org.osbuild.ostree.deploy":
if "commits" not in stage.get("inputs", {}):
return

Expand Down

0 comments on commit a89ea3b

Please sign in to comment.