Skip to content

Commit 783f4bf

Browse files
committed
cmd-buildextend-metal: create postprocess_artifact() function
Reduces duplicate code by making a function to handle the meta.json update and finalization of the artifact.
1 parent 016cae2 commit 783f4bf

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

src/cmd-buildextend-metal

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,33 @@ getconfig_def() {
3131
jq -re .\""$k"\"//\""${default}"\" < "${config}"
3232
}
3333

34+
# Store information about the artifact into meta.json and also
35+
# "finalize" it, copying it into the builddir.
36+
postprocess_artifact() {
37+
local artifact_name=$1
38+
local local_filepath=$2
39+
local target_filename=$3
40+
local skip_compress=$4
41+
if [[ ! "${skip_compress}" =~ ^(True|False)$ ]]; then
42+
fatal "Must specify 'True' or 'False' for skip_compress. Provided: '${skip_compress}'"
43+
fi
44+
cosa meta --workdir "${workdir}" --build "${build}" --dump | python3 -c "
45+
import sys, json
46+
j = json.load(sys.stdin)
47+
j['images']['${artifact_name}'] = {
48+
'path': '${target_filename}',
49+
'sha256': '$(sha256sum_str < "${local_filepath}")',
50+
'size': $(stat -c '%s' "${local_filepath}")
51+
}
52+
if ${skip_compress}:
53+
j['images']['${artifact_name}']['skip-compression'] = True
54+
json.dump(j, sys.stdout, indent=4)
55+
" > meta.json.new
56+
cosa meta --workdir "${workdir}" --build "${build}" --artifact-json meta.json.new
57+
/usr/lib/coreos-assembler/finalize-artifact "${local_filepath}" "${builddir}/${target_filename}"
58+
echo "Successfully generated: ${target_filename}"
59+
}
60+
3461
# Here we generate the input JSON we pass to runvm_osbuild for all of our image builds
3562
generate_runvm_osbuild_config() {
3663
runvm_osbuild_config_json="${workdir}/tmp/runvm-osbuild-config-${build}.json"
@@ -282,44 +309,18 @@ main() {
282309
rm -f "${genprotimg_img}"
283310
exec 9>&-
284311

285-
# Now store the ${ignition_pubkey} in the builddir and meta.json
286-
gpg_key=${name}-${build}-ignition-secex-key.gpg.pub
287-
python3 -c "
288-
import sys, json
289-
j = json.load(sys.stdin)
290-
j['images']['ignition-gpg-key'] = {
291-
'path': '${gpg_key}',
292-
'sha256': '$(sha256sum_str < "${ignition_pubkey}")',
293-
'size': $(stat -c '%s' "${ignition_pubkey}"),
294-
'skip-compression': True
295-
}
296-
json.dump(j, sys.stdout, indent=4)
297-
" > meta.json.new
298-
cosa meta --workdir "${workdir}" --build "${build}" --artifact-json "$(readlink -f meta.json.new)"
299-
/usr/lib/coreos-assembler/finalize-artifact "${ignition_pubkey}" "${builddir}/${gpg_key}"
312+
# Now store the generated ${ignition_pubkey} in the builddir and meta.json
313+
gpg_key_filename="${name}-${build}-ignition-secex-key.gpg.pub"
314+
postprocess_artifact "ignition-gpg-key" "${ignition_pubkey}" "${gpg_key_filename}" 'True'
300315
fi
301316

302-
sha256=$(sha256sum_str < "${imgpath}")
303-
cosa meta --workdir "${workdir}" --build "${build}" --dump | python3 -c "
304-
import sys, json
305-
j = json.load(sys.stdin)
306-
j['images']['${platform}'] = {
307-
'path': '${imgname}',
308-
'sha256': '${sha256}',
309-
'size': $(stat -c '%s' "${imgpath}")
310-
}
311-
json.dump(j, sys.stdout, indent=4)
312-
" > meta.json.new
313-
# Now store the built artifact in the builddir and meta.json
314-
cosa meta --workdir "${workdir}" --build "${build}" --artifact-json "$(readlink -f meta.json.new)"
315-
/usr/lib/coreos-assembler/finalize-artifact "${imgpath}" "${builddir}/${imgname}"
317+
# Now store the generated artifact in the builddir and meta.json
318+
postprocess_artifact "${platform}" "${imgpath}" "${imgname}" 'False'
316319

317320
# Quiet for the rest of this so the last thing we see is a success message
318321
set +x
319322
# clean up the tmpbuild
320323
rm -rf "${tmp_builddir}"
321-
echo "Successfully generated: ${imgname}"
322-
323324
}
324325

325326
main "$@"

0 commit comments

Comments
 (0)