From 07a597481bea5d22758d2dcb06ddc6d38b3d2ea7 Mon Sep 17 00:00:00 2001 From: Achilleas Koutsou Date: Thu, 15 Aug 2024 14:32:30 +0200 Subject: [PATCH] util: move get_host_storage() to a separate module Add a new util module called host which is used for functions that are meant for interactions with the host. These functions should not be used in stages. The containers.get_host_storage() function is renamed to host.get_container_storage() for clarity, since it is no longer namespaced under containers. --- inputs/org.osbuild.containers-storage | 4 ++-- osbuild/util/containers.py | 18 ------------------ osbuild/util/host.py | 20 ++++++++++++++++++++ sources/org.osbuild.containers-storage | 4 ++-- 4 files changed, 24 insertions(+), 22 deletions(-) create mode 100644 osbuild/util/host.py diff --git a/inputs/org.osbuild.containers-storage b/inputs/org.osbuild.containers-storage index 49b60328..0ba6f0bb 100755 --- a/inputs/org.osbuild.containers-storage +++ b/inputs/org.osbuild.containers-storage @@ -15,7 +15,7 @@ import os import sys from osbuild import inputs -from osbuild.util import containers +from osbuild.util import host from osbuild.util.mnt import MountGuard SCHEMA = r""" @@ -89,7 +89,7 @@ class ContainersStorageInput(inputs.InputService): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.mg = MountGuard() - self.storage_conf = containers.get_host_storage() + self.storage_conf = host.get_container_storage() def bind_mount_local_storage(self, target): source = self.storage_conf["storage"]["graphroot"] diff --git a/osbuild/util/containers.py b/osbuild/util/containers.py index dcf061df..b3b99f77 100644 --- a/osbuild/util/containers.py +++ b/osbuild/util/containers.py @@ -180,21 +180,3 @@ def container_source(image): with container_source_fn(image, image_filepath, container_format) as image_source: yield image_name, image_source - - -def get_host_storage(): - """ - Read the host storage configuration. - """ - # pylint: disable=import-outside-toplevel - # importing this at the top level will break the buildroot - from osbuild.util import toml - - config_paths = ("/etc/containers/storage.conf", "/usr/share/containers/storage.conf") - for conf_path in config_paths: - try: - return toml.load_from_file(conf_path) - except FileNotFoundError: - pass - - raise FileNotFoundError(f"could not find container storage configuration in any of {config_paths}") diff --git a/osbuild/util/host.py b/osbuild/util/host.py new file mode 100644 index 00000000..8b956622 --- /dev/null +++ b/osbuild/util/host.py @@ -0,0 +1,20 @@ +""" +Utility functions that only run on the host (osbuild internals or host modules like sources). + +These should not be used by stages or code that runs in the build root. +""" +from osbuild.util import toml + + +def get_container_storage(): + """ + Read the host storage configuration. + """ + config_paths = ("/etc/containers/storage.conf", "/usr/share/containers/storage.conf") + for conf_path in config_paths: + try: + return toml.load_from_file(conf_path) + except FileNotFoundError: + pass + + raise FileNotFoundError(f"could not find container storage configuration in any of {config_paths}") diff --git a/sources/org.osbuild.containers-storage b/sources/org.osbuild.containers-storage index 4be3c652..01898d9a 100755 --- a/sources/org.osbuild.containers-storage +++ b/sources/org.osbuild.containers-storage @@ -18,7 +18,7 @@ import subprocess as sp import sys from osbuild import sources -from osbuild.util import containers +from osbuild.util import host SCHEMA = """ "additionalProperties": false, @@ -49,7 +49,7 @@ class ContainersStorageSource(sources.SourceService): Construct the full image name that references an image with a given checksum in the local storage. """ if self.storage_conf is None: - conf = containers.get_host_storage() + conf = host.get_container_storage() driver = conf["storage"]["driver"] graphroot = conf["storage"]["graphroot"] runroot = conf["storage"]["runroot"]