From 26aac90eb46cca2564b9c73d1b890ad54efcab19 Mon Sep 17 00:00:00 2001 From: Gianluca Zuccarelli Date: Fri, 9 Feb 2024 10:33:52 +0000 Subject: [PATCH] util/containers: read host config Add utility function to read the host's container storage config. --- .mypy.ini | 3 +++ osbuild.spec | 6 ++++++ osbuild/util/containers.py | 17 +++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/.mypy.ini b/.mypy.ini index 0d07e002..5b680f40 100644 --- a/.mypy.ini +++ b/.mypy.ini @@ -5,3 +5,6 @@ ignore_missing_imports = True [mypy-mako.*] ignore_missing_imports = True + +[mypy-tomli.*] +ignore_missing_imports = True diff --git a/osbuild.spec b/osbuild.spec index 39753fb9..f8e3fab3 100644 --- a/osbuild.spec +++ b/osbuild.spec @@ -39,6 +39,12 @@ Requires: util-linux Requires: python3-%{pypi_name} = %{version}-%{release} Requires: (%{name}-selinux if selinux-policy-%{selinuxtype}) +# This is required for `osbuild`, for RHEL-10 and above +# the stdlib toml package can be used instead +%if 0%{?rhel} < 10 +Requires: python3-tomli +%endif + # Turn off dependency generators for runners. The reason is that runners are # tailored to the platform, e.g. on RHEL they are using platform-python. We # don't want to pick up those dependencies on other platform. diff --git a/osbuild/util/containers.py b/osbuild/util/containers.py index 2f4b702f..c4241839 100644 --- a/osbuild/util/containers.py +++ b/osbuild/util/containers.py @@ -128,3 +128,20 @@ def container_source(image): image_source = f"{container_format}:{tmp_source}" yield image_name, image_source + + +def get_host_storage(storage_conf=None): + """ + Read the host storage configuration. + """ + # pylint: disable=import-outside-toplevel + # importing this at the top level will break the buildroot + try: + import tomllib as toml + except ImportError: + import tomli as toml + + if not storage_conf: + with open("/etc/containers/storage.conf", "rb") as conf_file: + storage_conf = toml.load(conf_file) + return storage_conf