Skip to content

Commit fd9b7da

Browse files
committed
cmd-prune: prune blob refs from tmp repo
We now keep blob refs in the tmp repo to make importing OCI images more efficient by avoiding having to re-import identical layers. But then we also need to prune them. Do this. The semantic here is simple: we prune any blob ref not tied to one of the kept builds.
1 parent 023f93d commit fd9b7da

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/cmd-prune

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,5 +183,25 @@ for build in builds_to_delete:
183183
error_during_pruning = True
184184
print(f"{e}")
185185

186+
# and delete any "unowned" blob refs from the tmp repo
187+
if os.path.exists('tmp/repo'):
188+
prefix = 'ostree/container/blob/'
189+
referenced_blobs = set()
190+
for build in builds.get_builds():
191+
meta = builds.get_build_meta(build['id'])
192+
build_dir = builds.get_build_dir(build['id'])
193+
with open(os.path.join(build_dir, meta['images']['oci-manifest']['path'])) as f:
194+
oci_manifest = json.load(f)
195+
referenced_blobs.update([prefix + layer['digest'].replace(':', '_3A_')
196+
for layer in oci_manifest['layers']])
197+
blobs = set(subprocess.check_output(['ostree', 'refs', '--repo=tmp/repo',
198+
'--list', 'ostree/container/blob'],
199+
encoding='utf-8').splitlines())
200+
blobs_to_delete = blobs.difference(referenced_blobs)
201+
if len(blobs_to_delete) > 0:
202+
print(f"Deleting {len(blobs_to_delete)} blob refs")
203+
subprocess.check_output(['ostree', 'refs', '--repo=tmp/repo',
204+
'--delete'] + list(blobs_to_delete))
205+
186206
if error_during_pruning:
187207
sys.exit(1)

0 commit comments

Comments
 (0)