Skip to content

Commit ee19d83

Browse files
dustymabejlebon
authored andcommitted
move diff calculations into cosa import
This moves the RPM/Advisory diffing into `cosa import`, which means we'll get that info even when importing from another source. It also changes the code such that we'll only attempt to add that info if there was something to diff against. i.e. for a first build in a stream (like when doing a local dev build for the first time) now we won't error out. Fixes #4336
1 parent 19a0dbf commit ee19d83

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

src/cmd-build-with-buildah

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ DIRECT=
3535
AUTOLOCK_VERSION=
3636
SKIP_PRUNE=
3737
STRICT=
38+
PARENT_BUILD=
3839
rc=0
3940
options=$(getopt --options h,d --longoptions help,version:,versionary,direct,autolock:,skip-prune,parent-build:,force,strict -- "$@") || rc=$?
4041
[ $rc -eq 0 ] || {
@@ -231,22 +232,10 @@ build_with_buildah() {
231232
fi
232233

233234
if [ -z "${skip_import:-}" ]; then
234-
/usr/lib/coreos-assembler/cmd-import "${final_ref}" ${SKIP_PRUNE:+--skip-prune}
235+
/usr/lib/coreos-assembler/cmd-import "${final_ref}" \
236+
${PARENT_BUILD:+--parent-build=${PARENT_BUILD}} ${SKIP_PRUNE:+--skip-prune}
235237
fi
236238

237-
# For the logs, print the RPM diff
238-
/usr/lib/coreos-assembler/cmd-diff \
239-
--rpms ${PARENT_BUILD:+--from=$PARENT_BUILD}
240-
241-
# For meta.json let's record the RPM diff and advisory diff information
242-
/usr/lib/coreos-assembler/cmd-diff \
243-
--rpms-json ${PARENT_BUILD:+--from=$PARENT_BUILD} |
244-
jq '{"pkgdiff": .pkgdiff, "advisories-diff": .advisories}' > "${tempdir}/diff.json"
245-
/usr/lib/coreos-assembler/cmd-meta --build="${VERSION}" \
246-
--skip-validation --artifact-json "${tempdir}/diff.json"
247-
# Run a 'dump' now to perform schema validation since we skipped it above.
248-
/usr/lib/coreos-assembler/cmd-meta --dump --build="${VERSION}" > /dev/null
249-
250239
rm -rf "${tempdir}"
251240
}
252241

src/cmd-import

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ from cosalib.cmdlib import (
2424
get_basearch,
2525
sha256sum_file,
2626
import_oci_archive)
27+
from cosalib.meta import GenericBuildMeta
2728

2829

2930
def main():
@@ -63,6 +64,14 @@ def main():
6364
# move into official location
6465
finalize_build(builds, build_meta, tmp_oci_archive, tmp_oci_manifest, tmp_lockfile, tmp_commitmeta)
6566

67+
# Now that the build has been created (i.e. files put into the
68+
# right places) let's use `cosa diff` to insert package and
69+
# advisory diffs into the meta.json and log the RPM diff, but
70+
# only if we have something to diff against.
71+
if args.parent_build or len(builds.get_builds()) > 1:
72+
starting_buildid = args.parent_build if args.parent_build else builds.get_previous()
73+
calculate_and_log_diffs(builds, buildid, arch, starting_buildid)
74+
6675
if not args.skip_prune:
6776
subprocess.check_call(['/usr/lib/coreos-assembler/cmd-prune'])
6877

@@ -73,6 +82,8 @@ def parse_args():
7382
help="image to import (containers-transports(5) format)")
7483
parser.add_argument("--skip-prune", action='store_true',
7584
help="Skip prunning previous builds")
85+
parser.add_argument("--parent-build",
86+
help="The parent build to diff against")
7687
return parser.parse_args()
7788

7889

@@ -228,6 +239,28 @@ def finalize_build(builds, build_meta, tmp_oci_archive, tmp_oci_manifest, tmp_lo
228239
print(f'Imported OCI image as build {buildid}')
229240

230241

242+
def calculate_and_log_diffs(builds, buildid, arch, starting_buildid):
243+
# Get the RPM/Advisory diff info
244+
diff_cmd_json = ['/usr/lib/coreos-assembler/cmd-diff', '--rpms-json',
245+
'--from', starting_buildid, '--to', buildid]
246+
diff_json_str = subprocess.check_output(diff_cmd_json)
247+
diff_data = json.loads(diff_json_str)
248+
249+
# Update the meta.json file with the RPM/Advisory diff info. Yes we
250+
# just created this file, but let's use the library now for access.
251+
meta = GenericBuildMeta(workdir=os.getcwd(), build=buildid, basearch=arch)
252+
meta.update({
253+
'pkgdiff': diff_data.get('pkgdiff'),
254+
'advisories-diff': diff_data.get('advisories')
255+
})
256+
meta.write()
257+
258+
# For the logs let's output the RPM diff
259+
diff_cmd_log = ['/usr/lib/coreos-assembler/cmd-diff', '--rpms',
260+
'--from', starting_buildid, '--to', buildid]
261+
subprocess.check_call(diff_cmd_log)
262+
263+
231264
def skopeo_inspect(image):
232265
return json.loads(subprocess.check_output(['skopeo', 'inspect', '-n', image]))
233266

0 commit comments

Comments
 (0)