From 7eb58ea3489a735e58751eda7c3e96785bd0cbbf Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Thu, 23 Jun 2022 14:34:20 +0200 Subject: [PATCH] inputs: introduce new input manager class Introduce a new class to manage inputs, `InputManger` and move the code to map inputs from the `Input` here. The main insight of why the logic should be place here is that certain information is needed to map inputs, independently of specific type: the path to the input directory, `root`, the store API, `storeapi` and the service manager instance to start the actual service. Instead of passing all this information again and again to the `Input` class, we now have a specialized (service) manager class for inputs that has all the needed information all the time. --- osbuild/inputs.py | 31 +++++++++++++++++++------------ osbuild/pipeline.py | 5 +++-- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/osbuild/inputs.py b/osbuild/inputs.py index 4a029a9c..f42ed281 100644 --- a/osbuild/inputs.py +++ b/osbuild/inputs.py @@ -58,39 +58,46 @@ class Input: m.update(json.dumps(self.options, sort_keys=True).encode()) return m.hexdigest() - def map(self, - mgr: host.ServiceManager, - storeapi: StoreServer, - root: PathLike) -> Tuple[str, Dict]: - target = os.path.join(root, self.name) +class InputManager: + def __init__(self, mgr: host.ServiceManager, storeapi: StoreServer, root: PathLike) -> Dict: + self.service_manager = mgr + self.storeapi = storeapi + self.root = root + self.inputs = {} + + def map(self, ip: Input) -> Tuple[str, Dict]: + + target = os.path.join(self.root, ip.name) os.makedirs(target) args = { # mandatory bits - "origin": self.origin, - "refs": self.refs, + "origin": ip.origin, + "refs": ip.refs, "target": target, # global options - "options": self.options, + "options": ip.options, # API endpoints "api": { - "store": storeapi.socket_address + "store": self.storeapi.socket_address } } - client = mgr.start(f"input/{self.name}", self.info.path) + client = self.service_manager.start(f"input/{ip.name}", ip.info.path) reply = client.call("map", args) path = reply["path"] - if not path.startswith(root): + if not path.startswith(self.root): raise RuntimeError(f"returned {path} has wrong prefix") - reply["path"] = os.path.relpath(path, root) + reply["path"] = os.path.relpath(path, self.root) + + self.inputs[ip.name] = reply return reply diff --git a/osbuild/pipeline.py b/osbuild/pipeline.py index 95721f54..c666d883 100644 --- a/osbuild/pipeline.py +++ b/osbuild/pipeline.py @@ -11,7 +11,7 @@ from . import host from . import objectstore from . import remoteloop from .devices import Device, DeviceManager -from .inputs import Input +from .inputs import Input, InputManager from .mounts import Mount, MountManager from .sources import Source from .util import osrelease @@ -203,8 +203,9 @@ class Stage: mgr = host.ServiceManager(monitor=monitor) cm.enter_context(mgr) + ipmgr = InputManager(mgr, storeapi, inputs_tmpdir) for key, ip in self.inputs.items(): - data = ip.map(mgr, storeapi, inputs_tmpdir) + data = ipmgr.map(ip) inputs[key] = data devmgr = DeviceManager(mgr, build_root.dev, tree)