__systemd_unit improvements (#606)

* __systemd_unit: Move systemctl detection to manifest

* __systemd_unit: Restart the unit if inactive

Until now, the --restart parameter caused the unit to be restarted
only when the unit file has changed. This commit modifies --restart
behavior so that the unit is also restarted when the unit is inactive.

* __systemd_unit: Do not create unit file when source is empty
This commit is contained in:
lubo 2017-12-29 17:06:48 +01:00 committed by Darko Poljak
parent b4e98d4ab4
commit 3397bcbf9b
4 changed files with 43 additions and 19 deletions

View file

@ -0,0 +1,21 @@
#!/bin/sh
#
# 2017 Ľubomír Kučera <lubomir.kucera.jr at gmail.com>
#
# This file is part of cdist.
#
# cdist is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cdist is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
systemctl is-active "${__object_id}" || true

View file

@ -18,14 +18,6 @@
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
systemctl_present=$(cat "${__object}/explorer/systemctl-present")
if [ "${systemctl_present}" -ne 0 ]; then
echo "systemctl does not seem to be present on this system" >&2
exit 1
fi
name="${__object_id}"
state=$(cat "${__object}/parameter/state")
current_enablement_state=$(cat "${__object}/explorer/enablement-state")
@ -38,6 +30,15 @@ if [ "${state}" = "absent" ]; then
exit 0
fi
unit_status=$(cat "${__object}/explorer/unit-status")
if [ -f "${__object}/parameter/restart" ]; then
if grep -q "^__file/etc/systemd/system/${name}" "${__messages_in}" || \
[ "${unit_status}" != "active" ]; then
echo "systemctl restart ${name} || true"
fi
fi
desired_enablement_state=$(cat "${__object}/parameter/enablement-state")
if [ "${current_enablement_state}" = "${desired_enablement_state}" ]; then

View file

@ -45,7 +45,7 @@ BOOLEAN PARAMETERS
------------------
restart
Restart the unit on change.
Restart the unit on unit file change or when the unit is inactive.
MESSAGES
--------

View file

@ -18,22 +18,24 @@
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
systemctl_present=$(cat "${__object}/explorer/systemctl-present")
if [ "${systemctl_present}" -ne 0 ]; then
echo "systemctl does not seem to be present on this system" >&2
exit 1
fi
name="${__object_id}"
source=$(cat "${__object}/parameter/source")
state=$(cat "${__object}/parameter/state")
onchange() {
echo -n "systemctl daemon-reload"
if [ -f "${__object}/parameter/restart" ]; then
echo -n " && (systemctl restart ${name} || true)"
if [ -z "${source}" ] && [ "${state}" != "absent" ]; then
exit 0
fi
echo
}
__config_file "/etc/systemd/system/${name}" \
--mode 644 \
--onchange "$(onchange)" \
--onchange "systemctl daemon-reload" \
--source "${source}" \
--state "${state}"