From e273dd0084656a07c12ca2bae6ba3e45780a42b3 Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Mon, 24 Aug 2020 21:45:14 +0200 Subject: [PATCH] api: add 'get-arguments' call and client method Add a new `get-arguments` API call to fetch the input/arguments. To avoid running into any limitings on maximum package size on the socket, the actual data is written to a temp file and a fd to that passed to the client - very much as in `setup_stdio`. Additionally, new `arguments` method is provided as a client counterpart for the new API call. --- osbuild/api.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/osbuild/api.py b/osbuild/api.py index d4ece715..1c2e4d2e 100644 --- a/osbuild/api.py +++ b/osbuild/api.py @@ -191,11 +191,19 @@ class API(BaseAPI): def _set_metadata(self, message): self.metadata.update(message["metadata"]) + def _get_arguments(self, sock): + with self._prepare_input() as data: + fds = [] + fds.append(data.fileno()) + sock.send({"type": "fd", "fd": 0}, fds=fds) + def _message(self, msg, fds, sock): if msg["method"] == 'setup-stdio': self._setup_stdio(sock) elif msg["method"] == 'add-metadata': self._set_metadata(msg) + elif msg["method"] == 'get-arguments': + self._get_arguments(sock) def _cleanup(self): if self._output_pipe: @@ -203,6 +211,19 @@ class API(BaseAPI): self._output_pipe = None +def arguments(path="/run/osbuild/api/osbuild"): + """Retrieve the input arguments that were supplied to API""" + with jsoncomm.Socket.new_client(path) as client: + req = {"method": "get-arguments"} + client.send(req) + msg, fds, _ = client.recv() + assert msg["type"] == "fd" + fd = msg["fd"] + with os.fdopen(fds.steal(fd), encoding="utf-8") as f: + data = json.load(f) + return data + + def setup_stdio(path="/run/osbuild/api/osbuild"): """Replace standard i/o with the ones provided by the API""" with jsoncomm.Socket.new_client(path) as client: