inputs: tweak containers-storage implementation

This commit tweaks the excellent work from Gianluca in PR#1550 a
little bit. Now that the container inputs are their own input
type some of the code that used to be part of the original
`inputs/org.osbuild.containers` can be simplified.
This commit is contained in:
Michael Vogt 2024-02-21 17:07:15 +01:00 committed by Ondřej Budai
parent 882c9efbc1
commit b5355c46ac
2 changed files with 9 additions and 21 deletions

View file

@ -84,20 +84,14 @@ SCHEMA = r"""
""" """
class ContainersInput(inputs.InputService): class ContainersStorageInput(inputs.InputService):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.mg = MountGuard() self.mg = MountGuard()
self.storage_conf = None self.storage_conf = containers.get_host_storage()
def map_storage_ref(self, _source, ref, target):
# we only want to read the host once and only for inputs that
# require it. If we do this for all inputs, reading the host
# may cause unexpected errors.
if self.storage_conf is None:
self.storage_conf = containers.get_host_storage()
def bind_mount_local_storage(self, target):
source = self.storage_conf["storage"]["graphroot"] source = self.storage_conf["storage"]["graphroot"]
dest = os.path.join(target, "storage") dest = os.path.join(target, "storage")
@ -105,23 +99,17 @@ class ContainersInput(inputs.InputService):
os.makedirs(dest, exist_ok=True) os.makedirs(dest, exist_ok=True)
self.mg.mount(source, dest) self.mg.mount(source, dest)
return ref, "containers-storage"
def map(self, store, origin, refs, target, _options): def map(self, store, origin, refs, target, _options):
self.bind_mount_local_storage(target)
images = {} images = {}
for ref, data in refs.items(): for ref, data in refs.items():
source = "org.osbuild.containers-storage"
ref, container_format = self.map_storage_ref(source, ref, target)
images[ref] = { images[ref] = {
"format": container_format, "format": "containers-storage",
"name": data["name"] "name": data["name"]
} }
images[ref]["name"] = data["name"] images[ref]["name"] = data["name"]
images[ref]["storage"] = self.storage_conf["storage"]
if container_format == "containers-storage" and self.storage_conf is not None:
images[ref]["storage"] = self.storage_conf["storage"]
reply = { reply = {
"path": target, "path": target,
@ -137,7 +125,7 @@ class ContainersInput(inputs.InputService):
def main(): def main():
service = ContainersInput.from_args(sys.argv[1:]) service = ContainersStorageInput.from_args(sys.argv[1:])
service.main() service.main()

View file

@ -55,7 +55,7 @@ def test_containers_local_inputs_integration(tmp_path, inputs_module):
} }
} }
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
cnt_inputs = inputs_module.ContainersInput.from_args(["--service-fd", str(sock.fileno())]) cnt_inputs = inputs_module.ContainersStorageInput.from_args(["--service-fd", str(sock.fileno())])
store = FakeStoreClient(tmp_path / "fake-sources") store = FakeStoreClient(tmp_path / "fake-sources")
# not using "tmp_path" here as it will "rm -rf" on cleanup and # not using "tmp_path" here as it will "rm -rf" on cleanup and
# that is dangerous as during the tests we bind mount the # that is dangerous as during the tests we bind mount the