Add support for systemd socket activation

Instead of enabling lorax-composer.service enable lorax-composer.socket
and it will start lorax-composer on first access to
/run/weldr/api.socket
This commit is contained in:
Brian C. Lane 2018-04-26 14:29:12 -07:00
parent 081da8859a
commit 48e318b391
5 changed files with 42 additions and 9 deletions

View File

@ -170,12 +170,15 @@ getent passwd weldr >/dev/null 2>&1 || useradd -r -g weldr -d / -s /sbin/nologin
%post composer %post composer
%systemd_post lorax-composer.service %systemd_post lorax-composer.service
%systemd_post lorax-composer.socket
%preun composer %preun composer
%systemd_preun lorax-composer.service %systemd_preun lorax-composer.service
%systemd_preun lorax-composer.socket
%postun composer %postun composer
%systemd_postun_with_restart lorax-composer.service %systemd_postun_with_restart lorax-composer.service
%systemd_postun_with_restart lorax-composer.socket
%files %files
%defattr(-,root,root,-) %defattr(-,root,root,-)
@ -207,8 +210,10 @@ getent passwd weldr >/dev/null 2>&1 || useradd -r -g weldr -d / -s /sbin/nologin
%{python3_sitelib}/pylorax/api/* %{python3_sitelib}/pylorax/api/*
%{_sbindir}/lorax-composer %{_sbindir}/lorax-composer
%{_unitdir}/lorax-composer.service %{_unitdir}/lorax-composer.service
%{_unitdir}/lorax-composer.socket
%dir %{_datadir}/lorax/composer %dir %{_datadir}/lorax/composer
%{_datadir}/lorax/composer/* %{_datadir}/lorax/composer/*
%{_tmpfilesdir}/lorax-composer.conf
%files -n composer-cli %files -n composer-cli
%{_bindir}/composer-cli %{_bindir}/composer-cli

View File

@ -8,7 +8,9 @@ import sys
# config file # config file
data_files = [("/etc/lorax", ["etc/lorax.conf"]), data_files = [("/etc/lorax", ["etc/lorax.conf"]),
("/etc/lorax", ["etc/composer.conf"]), ("/etc/lorax", ["etc/composer.conf"]),
("/usr/lib/systemd/system", ["systemd/lorax-composer.service"])] ("/usr/lib/systemd/system", ["systemd/lorax-composer.service",
"systemd/lorax-composer.socket"]),
("/usr/lib/tmpfiles.d/", ["systemd/lorax-composer.conf"])]
# shared files # shared files
for root, dnames, fnames in os.walk("share"): for root, dnames, fnames in os.walk("share"):

View File

@ -209,14 +209,27 @@ if __name__ == '__main__':
log.error(e) log.error(e)
sys.exit(1) sys.exit(1)
# Setup the Unix Domain Socket, remove old one, set ownership and permissions # Did systemd pass any extra fds (for socket activation)?
if os.path.exists(opts.socket): try:
os.unlink(opts.socket) fds = int(os.environ['LISTEN_FDS'])
listener = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) except (ValueError, KeyError):
listener.bind(opts.socket) fds = 0
os.chmod(opts.socket, 0o660)
os.chown(opts.socket, 0, gid) if fds == 1:
listener.listen(1) # Inherit the fd passed by systemd
listener = socket.fromfd(3, socket.AF_UNIX, socket.SOCK_STREAM)
elif fds > 1:
log.error("lorax-composer only supports inheriting 1 fd from systemd.")
sys.exit(1)
else:
# Setup the Unix Domain Socket, remove old one, set ownership and permissions
if os.path.exists(opts.socket):
os.unlink(opts.socket)
listener = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
listener.bind(opts.socket)
os.chmod(opts.socket, 0o660)
os.chown(opts.socket, 0, gid)
listener.listen(1)
start_queue_monitor(server.config["COMPOSER_CFG"], uid, gid) start_queue_monitor(server.config["COMPOSER_CFG"], uid, gid)

View File

@ -0,0 +1 @@
d /run/weldr 750 root weldr

View File

@ -0,0 +1,12 @@
[Unit]
Description=lorax-composer socket activation
[Socket]
ListenStream=/run/weldr/api.socket
SocketUser=root
SocketGroup=weldr
SocketMode=0660
DirectoryMode=0750
[Install]
WantedBy=sockets.target