Skip to content

Commit

Permalink
Refactor buildextend compression and add zip
Browse files Browse the repository at this point in the history
buildextend --compress supports gzip as an alternate compression over
xz using a key called "gzip" where the value is a bool.  This is now
refactored to a key called "compression" with a value of "gzip", "skip",
or "zip" (see below).  The "skip-compression" key is no longer
applicable.

HyperV images which are used on Windows need to be compressed with zip
because xz and gzip are not supported natively.  Added zip support to
buildextend and set the hyperv platform compression to "zip".

Supports:
  * coreos/fedora-coreos-tracker#1411
  * coreos/fedora-coreos-tracker#1424

Signed-off-by: Brent Baude <bbaude@redhat.com>
  • Loading branch information
baude committed Apr 14, 2023
1 parent 9725f79 commit b081f31
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/cosalib/ibmcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"image_format": "raw",
"image_suffix": "ova.gz",
"platform": "powervs",
"gzip": True,
"compression": "gzip",
"tar_members": [
"disk.raw"
]
Expand Down
34 changes: 20 additions & 14 deletions src/cosalib/qemuvariants.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@
"image_format": "qcow2",
"image_suffix": "qcow2.gz",
"platform": "digitalocean",
"gzip": True
"compression": "gzip"
},
"gcp": {
# See https://cloud.google.com/compute/docs/import/import-existing-image#requirements_for_the_image_file
"image_format": "raw",
"platform": "gcp",
"image_suffix": "tar.gz",
"gzip": True,
"compression": "gzip",
"convert_options": {
'-o': 'preallocation=off'
},
Expand All @@ -93,8 +93,9 @@
},
"hyperv": {
"image_format": "vhdx",
"image_suffix": "vhdx",
"image_suffix": "vhdx.zip",
"platform": "hyperv",
"compression": "zip"
},
"kubevirt": {
"image_format": "qcow2",
Expand All @@ -108,7 +109,7 @@
"nutanix": {
"image_format": "qcow2",
"platform": "nutanix",
"skip_compression": True,
"compression": "skip",
"convert_options": {
'-c': None
}
Expand Down Expand Up @@ -182,10 +183,9 @@ def __init__(self, **kwargs):
self.compress = kwargs.get("compress", False)
self.tar_members = kwargs.pop("tar_members", None)
self.tar_flags = kwargs.pop("tar_flags", [DEFAULT_TAR_FLAGS])
self.gzip = kwargs.pop("gzip", False)
self.compression = kwargs.pop("compression", None)
self.virtual_size = kwargs.pop("virtual_size", None)
self.mutate_callback_creates_final_image = False
self.skip_compression = kwargs.pop("skip_compression", False)

# this is used in case the image has a different disk
# name than the platform
Expand Down Expand Up @@ -304,24 +304,30 @@ def mutate_image(self):
log.info(f"Moving {work_img} to {final_img}")
shutil.move(work_img, final_img)

if self.gzip:
if self.compression == "skip":
meta_patch.update({
'skip-compression': True
})
elif self.compression is not None:
sha256 = sha256sum_file(final_img)
size = os.stat(final_img).st_size
temp_path = f"{final_img}.tmp"

match self.compression:
case "gzip":
rc = ['gzip', '-9c', final_img]
case "zip":
rc = ['zip', '-9', "-", final_img]
case _:
raise ImageError(f"unsupported compression type: {self.compression}")
with open(temp_path, "wb") as fh:
runcmd(['gzip', '-9c', final_img], stdout=fh)
runcmd(rc, stdout=fh)
shutil.move(temp_path, final_img)
meta_patch.update({
'skip-compression': True,
'uncompressed-sha256': sha256,
'uncompressed-size': size,
})

if self.skip_compression:
meta_patch.update({
'skip-compression': True
})

return meta_patch

def _build_artifacts(self, *args, **kwargs):
Expand Down

0 comments on commit b081f31

Please sign in to comment.