Skip to content

Commit 2ef1682

Browse files
committed
Added basic support of GZ images
Added ability to build images in the GZ(zlib) format. The compression level is 5 by default. Closes (#65) Signed-off-by: Anton Kremenetsky <anton.kremenetsky@gmail.com>
1 parent c46b948 commit 2ef1682

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

genesis_devtools/builder/builder.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import tempfile
2020
import shutil
2121
import os
22+
import gzip
2223

2324
from genesis_devtools.builder import base
2425
from genesis_devtools.logger import AbstractLogger, DummyLogger
@@ -80,10 +81,23 @@ def _build_image(
8081
if not os.path.exists(self._images_output_dir):
8182
os.makedirs(self._images_output_dir)
8283

83-
shutil.move(
84-
os.path.join(output_dir, f"{img.name}.{img.format}"),
85-
self._images_output_dir,
86-
)
84+
# Determine source path to move. If gzip was requested,
85+
# compress RAW -> GZ first.
86+
if img.format == "gz":
87+
self._logger.info(f"Compressing {img.name} to {img.name}.raw.gz")
88+
# Source RAW image produced by Packer
89+
raw_src = os.path.join(output_dir, f"{img.name}.raw")
90+
gz_tgt = os.path.join(
91+
self._images_output_dir, f"{img.name}.raw.gz"
92+
)
93+
# Compress using standard library (gzip uses zlib) with level 5
94+
with open(raw_src, "rb") as f_in, gzip.open(
95+
gz_tgt, "wb", compresslevel=5
96+
) as f_out:
97+
shutil.copyfileobj(f_in, f_out)
98+
else:
99+
src_path = os.path.join(output_dir, f"{img.name}.{img.format}")
100+
shutil.move(src_path, self._images_output_dir)
87101

88102
def fetch_dependency(self, deps_dir: str) -> None:
89103
"""Fetch common dependencies for elements."""

genesis_devtools/builder/packer.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,11 @@ def pre_build(
216216
override = image.override or {}
217217

218218
# Enrich with the image format
219-
override["img_format"] = image.format
219+
# If compressed gzip is requested, we build a RAW image first
220+
# and compress it afterwards in the SimpleBuilder.
221+
override["img_format"] = (
222+
"raw" if image.format == "gz" else image.format
223+
)
220224

221225
# Write the packer variables
222226
variables = PackerVariable.variable_file_content(override)

genesis_devtools/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
# Types
3131
ImageProfileType = tp.Literal["ubuntu_24", "genesis_base"]
32-
ImageFormatType = tp.Literal["raw", "qcow2"]
32+
ImageFormatType = tp.Literal["raw", "qcow2", "gz"]
3333
NetType = tp.Literal["network", "bridge"]
3434
VersionSuffixType = tp.Literal["latest", "none", "element"]
3535
DomainState = tp.Literal["all", "inactive", "state-paused"]

0 commit comments

Comments
 (0)