diff --git a/src/cmd-build b/src/cmd-build index 868b7d20d9..aed0311e97 100755 --- a/src/cmd-build +++ b/src/cmd-build @@ -156,7 +156,6 @@ fi image_config_checksum=$(< "${image_input}" sha256sum_str) if [ -n "${previous_build}" ]; then previous_image_input_checksum=$(jq -r '.["coreos-assembler.image-input-checksum"]' < "${previous_builddir}/meta.json") - previous_image_genver=$(jq -r '.["coreos-assembler.image-genver"]' < "${previous_builddir}/meta.json") fi echo "Image Config checksum: ${image_config_checksum}" @@ -277,14 +276,8 @@ fi image_input_checksum=$( (echo "${commit}" && echo "${image_config_checksum}") | sha256sum_str) echo "New image input checksum: ${image_input_checksum}" -version=$(ostree --repo="${tmprepo}" show --print-metadata-key=version "${commit}" | sed -e "s,',,g") -if [ "${previous_commit}" = "${commit}" ] && [ -n "${previous_image_genver:-}" ]; then - image_genver=$((previous_image_genver + 1)) - buildid="${version}"-"${image_genver}" -else - image_genver=0 - buildid="${version}" -fi +init_build_meta_json "${commit}" tmp/ +buildid=$(jq -r '.["buildid"]' < tmp/meta.json) echo "New build ID: ${buildid}" "${dn}"/write-commit-object "${tmprepo}" "${commit}" "$(pwd)" @@ -333,14 +326,12 @@ fi # summary could have double quotes: https://github.com/coreos/coreos-assembler/issues/327 # # shellcheck disable=SC2046 disable=SC2086 -cat > tmp/meta.json < tmp/buildmeta.json < meta.json +cat "${composejson}" "${overridesjson}" tmp/meta.json tmp/buildmeta.json tmp/diff.json tmp/images.json tmp/cosa-image.json "${commitmeta_input_json}" | jq -s add > meta.json # Filter out `ref` if it's temporary if [ -n "${ref_is_temp}" ]; then diff --git a/src/cmdlib.sh b/src/cmdlib.sh index f571c176b2..29c263957b 100755 --- a/src/cmdlib.sh +++ b/src/cmdlib.sh @@ -679,6 +679,16 @@ from cosalib.builds import Builds print(Builds('${workdir:-$(pwd)}').get_build_dir('${buildid}'))") } +init_build_meta_json() { + local ostree_commit=$1; shift + local dir=$1; shift + (python3 -c " +import sys +sys.path.insert(0, '${DIR}') +from cosalib.builds import Builds +print(Builds('${workdir:-$(pwd)}').init_build_meta_json('${ostree_commit}', '${dir}'))") +} + get_latest_qemu() { local latest builddir latest=$(get_latest_build) diff --git a/src/cosalib/builds.py b/src/cosalib/builds.py index 66dbd1bb2a..ca98b296ff 100644 --- a/src/cosalib/builds.py +++ b/src/cosalib/builds.py @@ -2,8 +2,13 @@ Builds interacts with builds.json """ +import json import os import semver +import gi + +gi.require_version('OSTree', '1.0') +from gi.repository import Gio, OSTree from cosalib.cmdlib import ( get_basearch, @@ -84,6 +89,38 @@ def insert_build(self, build_id, basearch=None): ] }) + def init_build_meta_json(self, ostree_commit, destdir): + """ + Given a new ostree version, initialize a new coreos-assembler + build by writing a `meta.json` in destdir. + """ + repopath = os.path.join(self._workdir, 'tmp/repo') + r = OSTree.Repo.new(Gio.File.new_for_path(repopath)) + r.open(None) + [_, rev] = r.resolve_rev(ostree_commit, True) + [_, commit, _] = r.load_commit(rev) + commitmeta = commit.get_child_value(0) + version = commitmeta.unpack()['version'] + image_genver = 0 + buildid = version + genver_key = 'coreos-assembler.image-genver' + if not self.is_empty(): + previous_buildid = self.get_latest() + metapath = self.get_build_dir(previous_buildid) + '/meta.json' + with open(metapath) as f: + previous_buildmeta = json.load(f) + previous_commit = previous_buildmeta['ostree-commit'] + previous_image_genver = int(previous_buildmeta[genver_key]) + if previous_commit == ostree_commit: + image_genver = previous_image_genver + 1 + buildid = f"{version}-{image_genver}" + meta = { + 'buildid': buildid, + genver_key: image_genver + } + with open(destdir + '/meta.json', 'w') as f: + json.dump(meta, f) + def bump_timestamp(self): self._data['timestamp'] = rfc3339_time() self.flush()