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 680393de3d
commit 55b89d1aeb
5 changed files with 42 additions and 9 deletions

View File

@ -169,12 +169,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,-)
@ -206,8 +209,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,6 +209,19 @@ if __name__ == '__main__':
log.error(e) log.error(e)
sys.exit(1) sys.exit(1)
# Did systemd pass any extra fds (for socket activation)?
try:
fds = int(os.environ['LISTEN_FDS'])
except (ValueError, KeyError):
fds = 0
if fds == 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 # Setup the Unix Domain Socket, remove old one, set ownership and permissions
if os.path.exists(opts.socket): if os.path.exists(opts.socket):
os.unlink(opts.socket) os.unlink(opts.socket)

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