From 0847b27f6a4c6e2453fffd272b3cea431dbc074a Mon Sep 17 00:00:00 2001 From: James Shubin Date: Wed, 14 May 2025 21:21:27 -0400 Subject: [PATCH] modules: misc: Add a pattern for systemd daemon reload It would be really nice if systemd actually had an API for getting events on this. --- modules/misc/main.mcl | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/modules/misc/main.mcl b/modules/misc/main.mcl index 5d3ecbac..6fa06bec 100644 --- a/modules/misc/main.mcl +++ b/modules/misc/main.mcl @@ -30,6 +30,7 @@ import "golang" import "golang/strings" import "iter" +import "local" import "net" import "os" import "world/collect" @@ -190,3 +191,33 @@ mac-address={{ .mac }} svc "NetworkManager" { } } + +# systemd_daemon_reload creates an exec which runs a systemctl daemon-reload if +# one is needed. The exec resource uses the input name, to make it easy to +# attach an edge to it. +# TODO: can we use: systemctl show foo.service --property=NeedDaemonReload +class systemd_daemon_reload($name) { + $vardir = local.vardir("misc/") + $mtime_file = "${vardir}daemon-reload" + + exec "${name}" { + cmd => "/usr/bin/systemctl", + args => [ + "daemon-reload", + ], + + donecmd => "/usr/bin/date --utc > ${mtime_file}", + doneshell => "/usr/bin/bash", + + watchfiles => [ + "/lib/systemd/system/", + "/etc/systemd/system/", + "${mtime_file}", + ], + + # If we have any file that's newer than our mtime, the run. + # NOTE: We grep since find doesn't return a useful return code. + ifcmd => "/usr/bin/test ! -e '${mtime_file}' || /usr/bin/find /lib/systemd/system/ /etc/systemd/system/ -type f -name '*.service' -newer '${mtime_file}' | /usr/bin/grep -q .", + ifshell => "/usr/bin/bash", + } +}