Skip to content

Commit 284601e

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 9c5c28d commit 284601e

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

src/cmd-buildextend-metal

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,32 @@ 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+
'skip-compression': ${skip_compress}
52+
}
53+
json.dump(j, sys.stdout, indent=4)
54+
" > meta.json.new
55+
cosa meta --workdir "${workdir}" --build "${build}" --artifact-json meta.json.new
56+
/usr/lib/coreos-assembler/finalize-artifact "${local_filepath}" "${builddir}/${target_filename}"
57+
echo "Successfully generated: ${target_filename}"
58+
}
59+
3460
# Here we generate the input JSON we pass to runvm_osbuild for all of our image builds
3561
generate_runvm_osbuild_config() {
3662
runvm_osbuild_config_json="${workdir}/tmp/runvm-osbuild-config-${build}.json"
@@ -282,44 +308,18 @@ main() {
282308
rm -f "${genprotimg_img}"
283309
exec 9>&-
284310

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}"
311+
# Now store the generated ${ignition_pubkey} in the builddir and meta.json
312+
gpg_key_filename="${name}-${build}-ignition-secex-key.gpg.pub"
313+
postprocess_artifact "ignition-gpg-key" "${ignition_pubkey}" "${gpg_key_filename}" 'True'
300314
fi
301315

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}"
316+
# Now store the generated artifact in the builddir and meta.json
317+
postprocess_artifact "${platform}" "${imgpath}" "${imgname}" 'False'
316318

317319
# Quiet for the rest of this so the last thing we see is a success message
318320
set +x
319321
# clean up the tmpbuild
320322
rm -rf "${tmp_builddir}"
321-
echo "Successfully generated: ${imgname}"
322-
323323
}
324324

325325
main "$@"

0 commit comments

Comments
 (0)