From 3397bcbf9bc651f274dad2dcd7556a3afba12129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=BDubom=C3=ADr=20Ku=C4=8Dera?= Date: Fri, 29 Dec 2017 17:06:48 +0100 Subject: [PATCH] __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 --- .../type/__systemd_unit/explorer/unit-status | 21 ++++++++++++++++++ cdist/conf/type/__systemd_unit/gencode-remote | 17 +++++++------- cdist/conf/type/__systemd_unit/man.rst | 2 +- cdist/conf/type/__systemd_unit/manifest | 22 ++++++++++--------- 4 files changed, 43 insertions(+), 19 deletions(-) create mode 100644 cdist/conf/type/__systemd_unit/explorer/unit-status diff --git a/cdist/conf/type/__systemd_unit/explorer/unit-status b/cdist/conf/type/__systemd_unit/explorer/unit-status new file mode 100644 index 00000000..b68e5169 --- /dev/null +++ b/cdist/conf/type/__systemd_unit/explorer/unit-status @@ -0,0 +1,21 @@ +#!/bin/sh +# +# 2017 Ľubomír Kučera +# +# 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 . +# + +systemctl is-active "${__object_id}" || true diff --git a/cdist/conf/type/__systemd_unit/gencode-remote b/cdist/conf/type/__systemd_unit/gencode-remote index 6ea3ddaa..c608d9b3 100644 --- a/cdist/conf/type/__systemd_unit/gencode-remote +++ b/cdist/conf/type/__systemd_unit/gencode-remote @@ -18,14 +18,6 @@ # along with cdist. If not, see . # -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 diff --git a/cdist/conf/type/__systemd_unit/man.rst b/cdist/conf/type/__systemd_unit/man.rst index c624e91e..88da6b30 100644 --- a/cdist/conf/type/__systemd_unit/man.rst +++ b/cdist/conf/type/__systemd_unit/man.rst @@ -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 -------- diff --git a/cdist/conf/type/__systemd_unit/manifest b/cdist/conf/type/__systemd_unit/manifest index 10dc4f0c..d5f04ee6 100644 --- a/cdist/conf/type/__systemd_unit/manifest +++ b/cdist/conf/type/__systemd_unit/manifest @@ -18,22 +18,24 @@ # along with cdist. If not, see . # +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)" - fi - - echo -} +if [ -z "${source}" ] && [ "${state}" != "absent" ]; then + exit 0 +fi __config_file "/etc/systemd/system/${name}" \ --mode 644 \ - --onchange "$(onchange)" \ + --onchange "systemctl daemon-reload" \ --source "${source}" \ --state "${state}"