api: ensure parent of socket dir exists

When creating the socket directory, i.e. in the case that it was
not specified directly, ensure the parent directories exist.
Make it possible to override that parent directory.
This commit is contained in:
Christian Kellner 2020-12-04 10:47:55 +01:00
parent ea3dad17fc
commit 35149c6aec

View file

@ -64,9 +64,10 @@ class BaseAPI(abc.ABC):
"""Called after the event loop is shut down"""
@classmethod
def _make_socket_dir(cls):
def _make_socket_dir(cls, rundir: PathLike = "/run/osbuild"):
"""Called to create the temporary socket dir"""
return tempfile.TemporaryDirectory(prefix="api-", dir="/run/osbuild")
os.makedirs(rundir, exist_ok=True)
return tempfile.TemporaryDirectory(prefix="api-", dir=rundir)
def _dispatch(self, sock: jsoncomm.Socket):
"""Called when data is available on the socket"""