From fb3a0c59823737d9750ce6738eac634e887ae4f3 Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Tue, 11 Aug 2020 11:35:13 +0200 Subject: [PATCH] api: add support for metadata Add support for setting metadata via `osbuild.API`. It is meant to be used by modules (stages, assemblers) to pass additional data that belong to the result back to osbuild. For this, a new api method `set-metadata` can be used to set and update a metadata dictionary on the `osbuild.API` class. A client side method `metadata` is provided to do so. --- osbuild/api.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/osbuild/api.py b/osbuild/api.py index 7368a59a..e2fee3ed 100644 --- a/osbuild/api.py +++ b/osbuild/api.py @@ -137,6 +137,7 @@ class API(BaseAPI): self._output_data = io.StringIO() self._output_pipe = None self.monitor = monitor + self.metadata = {} @property def output(self): @@ -176,9 +177,14 @@ class API(BaseAPI): server.send(msg, fds=fds) + def _set_metadata(self, message): + self.metadata.update(message["metadata"]) + def _message(self, msg, fds, sock): if msg["method"] == 'setup-stdio': self._setup_stdio(sock) + elif msg["method"] == 'add-metadata': + self._set_metadata(msg) def _cleanup(self): if self._output_pipe: @@ -197,3 +203,13 @@ def setup_stdio(path="/run/osbuild/api/osbuild"): source = fds[msg[sio]] os.dup2(source, target.fileno()) fds.close() + + +def metadata(data: Dict, path="/run/osbuild/api/osbuild"): + """Update metadata for the current module""" + with jsoncomm.Socket.new_client(path) as client: + msg = { + "method": "add-metadata", + "metadata": data + } + client.send(msg)