Skip to content

Commit 57bdcf2

Browse files
committed
Return preinstallimage.info and allow podman to use preinstallimage
Download and return preinstallimage.info if preinstallimage exists to be able to check the list of packages in preinstallimage without unpacking it in build environment. Download preinstallimage and preinstallimage.info in a unique named temp file first. Allow podman to use preinstallimage to speed up the build process
1 parent 141157e commit 57bdcf2

File tree

1 file changed

+67
-26
lines changed

1 file changed

+67
-26
lines changed

osc/build.py

Lines changed: 67 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,9 @@ def get_preinstall_image(apiurl, arch, cache_dir, img_info, offline=False):
325325
.. note::
326326
preinstall image can be used only for new build roots!
327327
"""
328-
imagefile = ''
329-
imagesource = ''
328+
imagefile = ""
329+
imagesource = ""
330+
info_file = "preinstallimage.info"
330331
img_bins = []
331332
for bin in img_info.findall('binary'):
332333
img_bins.append(bin.text)
@@ -339,16 +340,17 @@ def get_preinstall_image(apiurl, arch, cache_dir, img_info, offline=False):
339340
img_hdrmd5 = img_info.get('hdrmd5')
340341
if not img_hdrmd5:
341342
img_hdrmd5 = img_file
342-
cache_path = '%s/%s/%s/%s' % (cache_dir, img_project, img_repository, img_arch)
343-
ifile_path = '%s/%s' % (cache_path, img_file)
344-
ifile_path_part = '%s.part' % ifile_path
343+
cache_path = "%s/%s/%s/%s" % (cache_dir, img_project, img_repository, img_arch)
344+
ifile_path = "%s/%s" % (cache_path, img_file)
345+
info_file_path = "%s/%s" % (cache_path, info_file)
345346

346347
imagefile = ifile_path
347348
imagesource = "%s/%s/%s [%s]" % (img_project, img_repository, img_pkg, img_hdrmd5)
349+
imageinfo = info_file_path
348350

349351
if not os.path.exists(ifile_path):
350352
if offline:
351-
return '', '', []
353+
return "", "", "", []
352354
url = "%s/build/%s/%s/%s/%s/%s" % (apiurl, img_project, img_repository, img_arch, img_pkg, img_file)
353355
print("downloading preinstall image %s" % imagesource)
354356
if not os.path.exists(cache_path):
@@ -362,14 +364,34 @@ def get_preinstall_image(apiurl, arch, cache_dir, img_info, offline=False):
362364
if sys.stdout.isatty():
363365
progress_obj = create_text_meter(use_pb_fallback=False)
364366
gr = OscFileGrabber(progress_obj=progress_obj)
365-
try:
366-
gr.urlgrab(url, filename=ifile_path_part, text='fetching image')
367-
except HTTPError as e:
368-
print("Failed to download! ecode:%i reason:%s" % (e.code, e.reason))
369-
return ('', '', [])
370-
# download ok, rename partial file to final file name
371-
os.rename(ifile_path_part, ifile_path)
372-
return (imagefile, imagesource, img_bins)
367+
with NamedTemporaryFile(dir=cache_path, delete=False) as temp_file:
368+
try:
369+
gr.urlgrab(url, filename=temp_file.name, text="fetching image")
370+
# download ok, rename temp file to final file name
371+
os.rename(temp_file.name, ifile_path)
372+
except HTTPError as e:
373+
print("Failed to download! ecode:%i reason:%s" % (e.code, e.reason))
374+
# Clean up temp file if it still exists
375+
if os.path.exists(temp_file.name):
376+
os.unlink(temp_file.name)
377+
return ("", "", "", [])
378+
379+
# Also download the corresponding .info file
380+
if not os.path.exists(info_file_path):
381+
info_url = "%s/build/%s/%s/%s/%s/%s" % (apiurl, img_project, img_repository, img_arch, img_pkg, info_file)
382+
print("downloading preinstall image info file")
383+
with NamedTemporaryFile(dir=cache_path, delete=False) as temp_file:
384+
try:
385+
gr.urlgrab(info_url, filename=temp_file.name, text="fetching image info")
386+
# download ok, rename temp file to final file name
387+
os.rename(temp_file.name, info_file_path)
388+
except HTTPError as e:
389+
print("Failed to download info file! ecode:%i reason:%s" % (e.code, e.reason))
390+
# Clean up temp file if it still exists
391+
if os.path.exists(temp_file.name):
392+
os.unlink(temp_file.name)
393+
394+
return (imagefile, imagesource, imageinfo, img_bins)
373395

374396

375397
def get_built_files(pacdir, buildtype):
@@ -1188,23 +1210,40 @@ def main(apiurl, store, opts, argv):
11881210
# implicitly trust the project we are building for
11891211
check_trusted_projects(apiurl, [i for i in bi.projects.keys() if not i == prj])
11901212

1191-
imagefile = ''
1192-
imagesource = ''
1213+
imagefile = ""
1214+
imagesource = ""
1215+
imageinfo = ""
11931216
imagebins = []
1194-
if build_as_user(vm_type):
1217+
# Check if using container preinstallimage for podman
1218+
using_container_preinstall = (
1219+
vm_type == "podman"
1220+
and opts.build_opt
1221+
and any("--vm-use-container-preinstallimage" in opt for opt in opts.build_opt)
1222+
)
1223+
1224+
if build_as_user(vm_type) and not using_container_preinstall:
11951225
# preinstallimage extraction will fail because unprivileged user cannot chroot or extract devices from the tarball
11961226
bi.preinstallimage = None
11971227
if build_type == 'preinstallimage':
11981228
# preinstallimage would repackage just the previously built preinstallimage
11991229
bi.preinstallimage = None
12001230

1201-
if (not config['no_preinstallimage'] and not opts.nopreinstallimage and
1202-
bi.preinstallimage and
1203-
not opts.noinit and
1204-
(opts.clean or (not os.path.exists(build_root + "/installed-pkg") and
1205-
not os.path.exists(build_root + "/.build/init_buildsystem.data")))):
1206-
(imagefile, imagesource, imagebins) = get_preinstall_image(apiurl, arch, cache_dir, bi.preinstallimage,
1207-
opts.offline)
1231+
if (
1232+
not config["no_preinstallimage"]
1233+
and not opts.nopreinstallimage
1234+
and bi.preinstallimage
1235+
and not opts.noinit
1236+
and (
1237+
opts.clean
1238+
or (
1239+
not os.path.exists(build_root + "/installed-pkg")
1240+
and not os.path.exists(build_root + "/.build/init_buildsystem.data")
1241+
)
1242+
)
1243+
):
1244+
(imagefile, imagesource, imageinfo, imagebins) = get_preinstall_image(
1245+
apiurl, arch, cache_dir, bi.preinstallimage, opts.offline
1246+
)
12081247
if imagefile:
12091248
# remove binaries from build deps which are included in preinstall image
12101249
for i in bi.deps:
@@ -1495,9 +1534,11 @@ def __str__(self):
14951534
rpmlist += ["%s %s\n" % (i[0], i[1]) for i in rpmlist_prefers]
14961535

14971536
if imagefile:
1498-
rpmlist.append('preinstallimage: %s\n' % imagefile)
1537+
rpmlist.append(f"preinstallimage: {imagefile}\n")
14991538
if imagesource:
1500-
rpmlist.append('preinstallimagesource: %s\n' % imagesource)
1539+
rpmlist.append(f"preinstallimagesource: {imagesource}\n")
1540+
if imageinfo:
1541+
rpmlist.append(f"preinstallimageinfo: {imageinfo}\n")
15011542

15021543
rpmlist.append('preinstall: ' + ' '.join(bi.preinstall_list) + '\n')
15031544
rpmlist.append('vminstall: ' + ' '.join(bi.vminstall_list) + '\n')

0 commit comments

Comments
 (0)