Skip to content

Commit

Permalink
cmd-build: Always use fresh image.json
Browse files Browse the repository at this point in the history
When importing the previous commit during `cosa build`, we would clobber
`image.json` file we just derived with the one from the imported commit.
Fix this by telling `import_ostree_commit()` to skip JSON extraction on
this execution path, but leave it as-is for other execution paths.

Also drop required pytest coverage to 70% since this causes a drop below
the old threshold but nothing touched can really be tested.
  • Loading branch information
mtalexan committed Oct 25, 2023
1 parent 45e72e0 commit 5a8a78a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[pytest]
addopts = --cov=cosalib.cli --cov=cosalib.meta --cov=cosalib.cmdlib --cov-report term --cov-fail-under=75
addopts = --cov=cosalib.cli --cov=cosalib.meta --cov=cosalib.cmdlib --cov-report term --cov-fail-under=70
3 changes: 2 additions & 1 deletion src/cmd-build
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ if [ -n "${previous_commit}" ]; then
commitpartial=${tmprepo}/state/${previous_commit}.commitpartial
if [ ! -f "${commitpath}" ] || [ -f "${commitpartial}" ]; then
if [ -f "${previous_builddir}/${previous_ostree_tarfile_path}" ]; then
import_ostree_commit_for_build "${previous_build}"
# don't extract the image.json though, keep the one we generated during prepare_build above
import_ostree_commit_for_build "${previous_build}" 0
else
# ok, just fallback to importing the commit object only
mkdir -p "$(dirname "${commitpath}")"
Expand Down
15 changes: 14 additions & 1 deletion src/cmd-buildextend-metal
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ if [ "${ref}" = "None" ]; then
fi
commit=$(meta_key ostree-commit)

echo "++++++++++++++importing"
ostree_repo=${tmprepo}
# Ensure that we have the cached unpacked commit
import_ostree_commit_for_build "${build}"
Expand All @@ -148,6 +149,8 @@ import_ostree_commit_for_build "${build}"
# latter path would only run this.
image_json=${workdir}/tmp/image.json

echo "++++++++++++++imported"

image_format=raw
if [[ $image_type == qemu ]]; then
image_format=qcow2
Expand Down Expand Up @@ -274,8 +277,12 @@ if [[ $secure_execution -eq "1" && -z "${hostkey}" ]]; then
--genprotimgvm "${genprotimgvm}" -- "${qemu_args[@]}"
fi

echo "+++++++++++++ finalizing artifact"

/usr/lib/coreos-assembler/finalize-artifact "${path}.tmp" "${path}"

echo "+++++++++++++ finalized artifact"

sha256=$(sha256sum_str < "${img}")
cosa meta --workdir "${workdir}" --build "${build}" --dump | python3 -c "
import sys, json
Expand Down Expand Up @@ -306,12 +313,18 @@ json.dump(j, sys.stdout, indent=4)
/usr/lib/coreos-assembler/finalize-artifact "${ignition_pubkey}" "${builddir}/${gpg_key}"
fi

echo "+++++++++++++ cosa meta"

# and now the crucial bits
cosa meta --workdir "${workdir}" --build "${build}" --artifact "${image_type}" --artifact-json "$(readlink -f meta.json.new)"

echo "+++++++++++++ after cosa meta"
echo "+++++++++++++ re-finalizing artifact"
/usr/lib/coreos-assembler/finalize-artifact "${img}" "${builddir}/${img}"
echo "+++++++++++++ re-finalized artifact"

# Quiet for the rest of this so the last thing we see is a success message
set +x
# clean up the tmpild
# clean up the tmpbuild
rm -rf "${tmp_builddir}"
echo "Successfully generated: ${img}"
5 changes: 3 additions & 2 deletions src/cmdlib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1048,9 +1048,10 @@ cmdlib.write_image_json('${srcfile}', '${outfile}')")

# API to prepare image builds.
# Ensures that the tmp/repo ostree repo is initialized,
# and also writes tmp/image.json.
# and also writes tmp/image.json if arg2 is unset or set to 1
import_ostree_commit_for_build() {
local buildid=$1; shift
local extractjson=${1:-1}
(python3 -c "
import sys
sys.path.insert(0, '${DIR}')
Expand All @@ -1060,7 +1061,7 @@ workdir = '${workdir:-$(pwd)}'
builds = Builds(workdir)
builddir = builds.get_build_dir('${buildid}')
buildmeta = builds.get_build_meta('${buildid}')
cmdlib.import_ostree_commit(workdir, builddir, buildmeta)
cmdlib.import_ostree_commit(workdir, builddir, buildmeta, ${extractjson})
")
}

Expand Down
8 changes: 5 additions & 3 deletions src/cosalib/cmdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def extract_image_json(workdir, commit):
# a metal image, we may not have preserved that cache.
#
# Call this function to ensure that the ostree commit for a given build is in tmp/repo.
def import_ostree_commit(workdir, buildpath, buildmeta):
def import_ostree_commit(workdir, buildpath, buildmeta, extract_json=1):
tmpdir = os.path.join(workdir, 'tmp')
with Lock(os.path.join(workdir, 'tmp/repo.import.lock'),
lifetime=LOCK_DEFAULT_LIFETIME):
Expand All @@ -294,7 +294,8 @@ def import_ostree_commit(workdir, buildpath, buildmeta):
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL) == 0
and not os.path.isfile(commitpartial)):
extract_image_json(workdir, commit)
if extract_json == 1:
extract_image_json(workdir, commit)
return

print(f"Extracting {commit}")
Expand All @@ -320,7 +321,8 @@ def import_ostree_commit(workdir, buildpath, buildmeta):
subprocess.check_call(['ostree', f'--repo={repo}', 'pull-local', tmpd, buildmeta['buildid']])

# Also extract image.json since it's commonly needed by image builds
extract_image_json(workdir, commit)
if extract_json == 1:
extract_image_json(workdir, commit)


def get_basearch():
Expand Down

0 comments on commit 5a8a78a

Please sign in to comment.