Skip to content

Commit

Permalink
fixups from review
Browse files Browse the repository at this point in the history
  • Loading branch information
cgwalters committed May 31, 2022
1 parent 84388cc commit fa721a0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 31 deletions.
6 changes: 3 additions & 3 deletions src/cmd-buildextend-live
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import time

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from cosalib.builds import Builds
from cosalib.cmdlib import run_verbose, sha256sum_file, extract_image_json
from cosalib.cmdlib import run_verbose, sha256sum_file
from cosalib.cmdlib import import_ostree_commit, get_basearch, ensure_glob
from cosalib.meta import GenericBuildMeta

Expand Down Expand Up @@ -61,8 +61,8 @@ repo = os.path.join(workdir, 'tmp/repo')
buildmeta_commit = buildmeta['ostree-commit']

import_ostree_commit(repo, builddir, buildmeta)

image_json = extract_image_json(repo, buildmeta_commit)
with open(os.path.join(workdir, 'tmp/image.json')) as f:
image_json = json.load(f)
squashfs_compression = 'lz4' if args.fast else image_json['squashfs-compression']

base_name = buildmeta['name']
Expand Down
8 changes: 5 additions & 3 deletions src/cmd-buildextend-metal
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,11 @@ commit=$(meta_key ostree-commit)
ostree_repo=${tmprepo}
# Ensure that we have the cached unpacked commit
import_ostree_commit_for_build "${build}"

image_json=image.json
extract_image_json "${tmprepo}" "${commit}" > "${image_json}"
# Note this overwrote the bits generated in prepare_build
# for image_json. In the future we expect to split prepare_build
# into prepare_ostree_build and prepare_diskimage_build; the
# latter path would only run this.
image_json=${workdir}/tmp/image.json

image_format=raw
if [[ $image_type == qemu ]]; then
Expand Down
25 changes: 9 additions & 16 deletions src/cmdlib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,9 @@ EOF
done
fi

# Store the fully rendered disk image config (image.json) inside
# the ostree commit, so it can later be extracted by disk image
# builds.
local imagejsondir="${tmp_overridesdir}/imagejson"
export ostree_image_json="/usr/share/coreos-assembler/image.json"
mkdir -p "${imagejsondir}/usr/share/coreos-assembler/"
Expand Down Expand Up @@ -887,30 +890,20 @@ from cosalib import cmdlib
cmdlib.write_image_json('${srcfile}', '${outfile}')")
}

# Fetch the image.json from the ostree commit to stdout.
# Should be used by disk image builds.
extract_image_json() {
local repo=$1; shift
local commit=$1; shift
(python3 -c "
import sys, json
sys.path.insert(0, '${DIR}')
from cosalib import cmdlib
json.dump(cmdlib.extract_image_json('${repo}', '${commit}'), sys.stdout, sort_keys=True)
")
}

# Shell wrapper for the Python import_ostree_commit
# API to prepare image builds.
# Ensures that the tmp/repo ostree repo is initialized,
# and also writes tmp/image.json.
import_ostree_commit_for_build() {
local buildid=$1; shift
(python3 -c "
import sys
sys.path.insert(0, '${DIR}')
from cosalib import cmdlib
from cosalib.builds import Builds
builds = Builds('${workdir:-$(pwd)}')
workdir = '${workdir:-$(pwd)}'
builds = Builds(workdir)
builddir = builds.get_build_dir('${buildid}')
buildmeta = builds.get_build_meta('${buildid}')
cmdlib.import_ostree_commit('${workdir:-$(pwd)}/tmp/repo', builddir, buildmeta)
cmdlib.import_ostree_commit_extract_imagejson(workdir, builddir, buildmeta)
")
}
17 changes: 10 additions & 7 deletions src/cosalib/cmdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,16 @@ def import_ostree_commit(repo, buildpath, buildmeta, force=False):
subprocess.check_call(['ostree', f'--repo={repo}', 'pull-local', tmpd, buildmeta['buildid']])


# Ensure the ostree repo is up to date, and also extracts image.json
# to tmp/image.json.
def import_ostree_commit_extract_imagejson(workdir, buildpath, buildmeta, force=False):
repo = os.path.join(workdir, "tmp/repo")
commit = buildmeta['ostree-commit']
import_ostree_commit(repo, buildpath, buildmeta)
with open(os.path.join(workdir, 'tmp/image.json'), 'w') as f:
subprocess.check_call(['ostree', f'--repo={repo}', 'cat', commit, '/usr/share/coreos-assembler/image.json'], stdout=f)


def get_basearch():
try:
return get_basearch.saved
Expand Down Expand Up @@ -350,13 +360,6 @@ def cmdlib_sh(script):
'''])


# Should be used by disk image builds to extract the image.json from the
# ostree commit.
def extract_image_json(repo, commit):
out = subprocess.check_output(['ostree', f'--repo={repo}', 'cat', commit, '/usr/share/coreos-assembler/image.json'])
return json.loads(out)


def generate_image_json(srcfile):
r = yaml.safe_load(open("/usr/lib/coreos-assembler/image-default.yaml"))
for k, v in flatten_image_yaml(srcfile).items():
Expand Down
6 changes: 4 additions & 2 deletions src/cosalib/ova.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import logging as log
import os.path
import sys
import json

cosa_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, f"{cosa_dir}/cosalib")
sys.path.insert(0, cosa_dir)

from cosalib.cmdlib import extract_image_json, image_info
from cosalib.cmdlib import image_info
from cosalib.qemuvariants import QemuVariantImage


Expand Down Expand Up @@ -86,7 +87,8 @@ def generate_ovf_parameters(self, vmdk, cpu=2, memory=4096):
Returns a dictionary with the parameters needed to create an OVF file
based on the qemu, vmdk, image.yaml, and info from the build metadata
"""
image_json = extract_image_json(os.path.join(self._workdir, 'tmp/repo'), self.meta['ostree-commit'])
with open(os.path.join(self._workdir, 'tmp/image.json')) as f:
image_json = json.load(f)

system_type = 'vmx-{}'.format(image_json['vmware-hw-version'])
os_type = image_json['vmware-os-type']
Expand Down

0 comments on commit fa721a0

Please sign in to comment.