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.
This commit is contained in:
Achilleas Koutsou 2024-08-15 14:32:30 +02:00 committed by Michael Vogt
parent bce908e4a2
commit 07a597481b
4 changed files with 24 additions and 22 deletions

View file

@ -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"]

View file

@ -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}")

20
osbuild/util/host.py Normal file
View file

@ -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}")

View file

@ -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"]