@@ -295,12 +295,14 @@ def import_ostree_commit(workdir, buildpath, buildmeta, extract_json=True, parti
295
295
lifetime = LOCK_DEFAULT_LIFETIME ):
296
296
repo = os .path .join (tmpdir , 'repo' )
297
297
commit = buildmeta ['ostree-commit' ]
298
+ is_oci_imported = buildmeta .get ('coreos-assembler.oci-imported' , False )
298
299
tarfile = os .path .join (buildpath , buildmeta ['images' ]['ostree' ]['path' ])
299
300
# create repo in case e.g. tmp/ was cleared out; idempotent
300
301
subprocess .check_call (['ostree' , 'init' , '--repo' , repo , '--mode=archive' ])
301
302
302
- # in the common case where we're operating on a recent build, the OSTree
303
- # commit should already be in the tmprepo
303
+ # in the common case where we're operating on a recent build (or
304
+ # recently imported OCI image), the OSTree commit should already be in
305
+ # the tmprepo
304
306
commitpartial = os .path .join (repo , f'state/{ commit } .commitpartial' )
305
307
if (subprocess .call (['ostree' , 'show' , '--repo' , repo , commit ],
306
308
stdout = subprocess .DEVNULL ,
@@ -332,8 +334,12 @@ def import_ostree_commit(workdir, buildpath, buildmeta, extract_json=True, parti
332
334
# We do this in two stages, because right now ex-container only writes to
333
335
# non-archive repos. Also, in the privileged case we need sudo to write
334
336
# to `repo-build`, though it might be good to change this by default.
335
- if os .environ .get ('COSA_PRIVILEGED' , '' ) == '1' :
337
+ if is_oci_imported :
338
+ import_oci_archive (tmpdir , tarfile , buildmeta ['buildid' ])
339
+ elif os .environ .get ('COSA_PRIVILEGED' , '' ) == '1' :
336
340
build_repo = os .path .join (repo , '../../cache/repo-build' )
341
+ # note: this actually is the same as `container unencapsulate` and
342
+ # so only works with "pure OSTree OCI" encapsulated commits (legacy path)
337
343
subprocess .check_call (['sudo' , 'ostree' , 'container' , 'import' , '--repo' , build_repo ,
338
344
'--write-ref' , buildmeta ['buildid' ],
339
345
'ostree-unverified-image:oci-archive:' + tarfile ])
@@ -354,6 +360,42 @@ def import_ostree_commit(workdir, buildpath, buildmeta, extract_json=True, parti
354
360
extract_image_json (workdir , commit )
355
361
356
362
363
+ def import_oci_archive (parent_tmpd , ociarchive , ref ):
364
+ with tempfile .TemporaryDirectory (dir = parent_tmpd ) as tmpd :
365
+ subprocess .check_call (['ostree' , 'init' , '--repo' , tmpd , '--mode=bare-user' ])
366
+
367
+ # Init tmp/repo in case it doesn't exist.
368
+ # If it exists, no problem. It's idempotent
369
+ subprocess .check_call (['ostree' , 'init' , '--repo' , 'tmp/repo' , '--mode=archive' ])
370
+
371
+ # import all the blob refs for more efficient import into bare-user repo
372
+ blob_refs = subprocess .check_output (['ostree' , 'refs' , '--repo' , 'tmp/repo' ,
373
+ '--list' , 'ostree/container/blob' ],
374
+ encoding = 'utf-8' ).splitlines ()
375
+ if len (blob_refs ) > 0 :
376
+ subprocess .check_call (['ostree' , 'pull-local' , '--repo' , tmpd , 'tmp/repo' ] + blob_refs )
377
+
378
+ subprocess .check_call (['ostree' , 'container' , 'image' , 'pull' , tmpd ,
379
+ f'ostree-unverified-image:oci-archive:{ ociarchive } ' ])
380
+
381
+ # awkwardly work around the fact that there is no --write-ref equivalent
382
+ refs = subprocess .check_output (['ostree' , 'refs' , '--repo' , tmpd ,
383
+ '--list' , 'ostree/container/image' ],
384
+ encoding = 'utf-8' ).splitlines ()
385
+ assert len (refs ) == 1
386
+ subprocess .check_call (['ostree' , 'refs' , '--repo' , tmpd , refs [0 ], '--create' , ref ])
387
+ subprocess .check_call (['ostree' , 'refs' , '--repo' , 'tmp/repo' , ref , '--delete' ])
388
+ subprocess .check_call (['ostree' , 'pull-local' , '--repo' , 'tmp/repo' , tmpd , ref ])
389
+
390
+ # export back all the blob refs for more efficient imports of next builds
391
+ blob_refs = subprocess .check_output (['ostree' , 'refs' , '--repo' , tmpd ,
392
+ '--list' , 'ostree/container/blob' ],
393
+ encoding = 'utf-8' ).splitlines ()
394
+ subprocess .check_call (['ostree' , 'pull-local' , '--repo' , 'tmp/repo' , tmpd ] + blob_refs )
395
+
396
+ return subprocess .check_output (['ostree' , 'rev-parse' , '--repo' , 'tmp/repo' , ref ], encoding = 'utf-8' ).strip ()
397
+
398
+
357
399
def get_basearch ():
358
400
try :
359
401
return get_basearch .saved
0 commit comments