From 7d3ef0150c9ce060c6826a364ae481e91ae0e1e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20M=C3=BCnch?= Date: Thu, 14 Nov 2024 16:33:41 +0100 Subject: [PATCH] Adds function to push from dir MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Malte Münch --- pyproject.toml | 2 +- src/python_gardenlinux_lib/oras/registry.py | 37 +++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 08156003..6568a22b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "python_gardenlinux_lib" -version = "0.4.2" +version = "0.4.3" description = "Contains tools to work with the features directory of gardenlinux, for example deducting dependencies from feature sets or validating cnames" authors = ["Garden Linux Maintainers "] license = "Apache-2.0" diff --git a/src/python_gardenlinux_lib/oras/registry.py b/src/python_gardenlinux_lib/oras/registry.py index ecf355f4..5c0bde9e 100644 --- a/src/python_gardenlinux_lib/oras/registry.py +++ b/src/python_gardenlinux_lib/oras/registry.py @@ -662,6 +662,43 @@ def push_from_tar(self, architecture: str, version: str, cname: str, tar: str): print("removed tmp files.") return digest + def push_from_dir(self, architecture: str, version: str, cname: str, dir: str): + # Step 1 scan and extract nested artifacts: + for file in os.listdir(dir): + try: + if file.endswith(".pxe.tar.gz"): + logger.info(f"Found nested artifact {file}") + nested_tar_obj = tarfile.open(f"{dir}/{file}") + nested_tar_obj.extractall(filter="data", path=dir) + nested_tar_obj.close() + except (OSError, tarfile.FilterError, tarfile.TarError) as e: + print(f"Failed to extract nested artifact {file}", e) + exit(1) + + try: + oci_metadata = get_oci_metadata_from_fileset(os.listdir(dir), architecture) + + features = "" + for artifact in oci_metadata: + if artifact["media_type"] == "application/io.gardenlinux.release": + file = open(f"{dir}/{artifact["file_name"]}", "r") + lines = file.readlines() + for line in lines: + if line.strip().startswith("GARDENLINUX_FEATURES="): + features = line.strip().removeprefix( + "GARDENLINUX_FEATURES=" + ) + break + file.close() + + digest = self.push_image_manifest( + architecture, cname, version, dir, oci_metadata, features + ) + except Exception as e: + print("Error: ", e) + exit(1) + return digest + def extract_tar(tar: str, tmpdir: str): """