Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for systemd socket activation #136

Merged
merged 1 commit into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
],
extras_require={
'prometheus': ['prometheus_client'],
'systemd': ['systemd_python'],
},
packages=find_packages(),
package_data={'xandikos': ['templates/*.html']},
Expand Down
48 changes: 46 additions & 2 deletions xandikos/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,36 @@
)

import shutil
import socket
import urllib.parse

try:
import systemd.daemon
except ImportError:
systemd_imported = False

def get_systemd_listen_sockets() -> List[socket.socket]:
raise NotImplementedError
else:
systemd_imported = True

def get_systemd_listen_sockets() -> List[socket.socket]:
socks = []
for fd in systemd.daemon.listen_fds():
for family in (socket.AF_UNIX, socket.AF_INET, socket.AF_INET6):
if systemd.daemon.is_socket(fd, family=family,
type=socket.SOCK_STREAM,
listening=True):
sock = socket.fromfd(fd, family, socket.SOCK_STREAM)
socks.append(sock)
break
else:
raise RuntimeError(
"socket family must be AF_INET, AF_INET6, or AF_UNIX; "
"socket type must be SOCK_STREAM; and it must be listening"
)
return socks

from xandikos import __version__ as xandikos_version
from xandikos import (
access,
Expand Down Expand Up @@ -1178,6 +1206,13 @@ def main(argv):
)

access_group = parser.add_argument_group(title="Access Options")
if systemd_imported:
access_group.add_argument(
"--no-detect-systemd",
action="store_false",
dest="detect_systemd",
help="Disable systemd detection and socket activation.",
)
access_group.add_argument(
"-l",
"--listen-address",
Expand Down Expand Up @@ -1289,15 +1324,23 @@ def main(argv):
async def xandikos_handler(request):
return await main_app.aiohttp_handler(request, options.route_prefix)

if "/" in options.listen_address:
if getattr(options, "detect_systemd", False) and systemd.daemon.booted():
listen_socks = get_systemd_listen_sockets()
socket_path = None
listen_address = None
listen_port = None
logging.info("Receiving file descriptors from systemd socket activation")
elif "/" in options.listen_address:
socket_path = options.listen_address
listen_address = None
listen_port = None # otherwise aiohttp also listens on its default host
listen_socks = []
logging.info("Listening on unix domain socket %s", socket_path)
else:
listen_address = options.listen_address
listen_port = options.port
socket_path = None
listen_socks = []
logging.info("Listening on %s:%s", listen_address, options.port)

from aiohttp import web
Expand Down Expand Up @@ -1340,7 +1383,8 @@ async def redirect_to_subprefix(request):
else:
avahi_register(options.port, options.route_prefix)

web.run_app(app, port=listen_port, host=listen_address, path=socket_path)
web.run_app(app, port=listen_port, host=listen_address, path=socket_path,
sock=listen_socks)


if __name__ == "__main__":
Expand Down