dnf-json: Can be started without systemd

Instead of starting the socket in the entrypoint, make dnf-json able to
bind on the unixsocket by itself.
This commit is contained in:
sanne 2021-12-01 16:04:28 +01:00 committed by Thomas Lavocat
parent 0877ae3ac0
commit 83e16afda4
3 changed files with 58 additions and 7 deletions

View file

@ -10,6 +10,7 @@ import socketserver
import logging
import sys
from http.server import BaseHTTPRequestHandler
import pathlib
import dnf
import hawkey
@ -254,15 +255,21 @@ class DnfJsonRequestHandler(BaseHTTPRequestHandler):
log.info("Starting the dnf-json server")
LISTEN_FDS = int(os.environ.get("LISTEN_FDS", 0))
SOCK_PATH = "/run/osbuild-dnf-json/"
SOCK_NAME = "api.sock"
class SystemDActivationSocketServer(socketserver.ForkingMixIn, socketserver.UnixStreamServer):
def server_bind(self):
log.debug("service bind")
assert LISTEN_FDS == 1
log.debug("rebind socket")
log.debug("address_family: %d ", self.address_family)
log.debug("socket_type: %d ", self.socket_type)
self.socket = socket.fromfd(3, self.address_family, self.socket_type)
if LISTEN_FDS == 0:
log.debug("create new socket")
socketserver.UnixStreamServer.server_bind(self)
else:
log.debug("rebind socket")
log.debug("address_family: %d ", self.address_family)
log.debug("socket_type: %d ", self.socket_type)
self.socket = socket.fromfd(3, self.address_family, self.socket_type)
server = SystemDActivationSocketServer('', DnfJsonRequestHandler)
pathlib.Path(SOCK_PATH).mkdir(parents=True, exist_ok=True)
server = SystemDActivationSocketServer(f"{SOCK_PATH}{SOCK_NAME}", DnfJsonRequestHandler)
server.serve_forever()