Merge pull request #678 from devinsolutions/improve-systemd-unit

Improve __systemd_unit
This commit is contained in:
Darko Poljak 2018-07-18 07:41:41 +02:00 committed by GitHub
commit e1f5fbf7b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 12 deletions

View File

@ -25,22 +25,32 @@ current_enablement_state=$(cat "${__object}/explorer/enablement-state")
if [ "${state}" = "absent" ]; then if [ "${state}" = "absent" ]; then
if [ ! -z "${current_enablement_state}" ]; then if [ ! -z "${current_enablement_state}" ]; then
echo "systemctl --now disable ${name}" echo "systemctl --now disable ${name}"
echo "rm -f /etc/systemd/system/${name}"
echo "systemctl daemon-reload"
fi fi
exit 0 exit 0
fi fi
unit_status=$(cat "${__object}/explorer/unit-status") unit_status=$(cat "${__object}/explorer/unit-status")
desired_enablement_state=$(cat "${__object}/parameter/enablement-state")
if [ "${current_enablement_state}" = "masked" ] && \
[ "${desired_enablement_state}" != "masked" ]; then
echo "systemctl unmask ${name}"
fi
if [ -f "${__object}/parameter/restart" ]; then if [ -f "${__object}/parameter/restart" ]; then
if grep -q "^__file/etc/systemd/system/${name}" "${__messages_in}" || \ if [ "${desired_enablement_state}" = "masked" ]; then
if [ "${unit_status}" = "active" ]; then
echo "systemctl stop ${name}"
fi
elif grep -q "^__file/etc/systemd/system/${name}" "${__messages_in}" || \
[ "${unit_status}" != "active" ]; then [ "${unit_status}" != "active" ]; then
echo "systemctl restart ${name} || true" echo "systemctl restart ${name} || true"
fi fi
fi fi
desired_enablement_state=$(cat "${__object}/parameter/enablement-state")
if [ "${current_enablement_state}" = "${desired_enablement_state}" ]; then if [ "${current_enablement_state}" = "${desired_enablement_state}" ]; then
exit 0 exit 0
fi fi
@ -56,6 +66,9 @@ case "${desired_enablement_state}" in
disabled) disabled)
echo "systemctl disable ${name}" echo "systemctl disable ${name}"
;; ;;
masked)
echo "systemctl mask ${name}"
;;
*) *)
echo "Unsupported unit status: ${desired_enablement_state}" >&2 echo "Unsupported unit status: ${desired_enablement_state}" >&2
exit 1 exit 1

View File

@ -9,9 +9,10 @@ cdist-type__systemd_unit - Install a systemd unit
DESCRIPTION DESCRIPTION
----------- -----------
This type can install, enable and start a systemd unit. This is particularly This type manages systemd units in ``/etc/systemd/system/``. It can install,
useful on systems which take advantage of systemd heavily (e.g., CoreOS). For enable and start a systemd unit. This is particularly useful on systems which
more information about systemd units, see SYSTEMD.UNIT(5). take advantage of systemd heavily (e.g., CoreOS). For more information about
systemd units, see SYSTEMD.UNIT(5).
REQUIRED PARAMETERS REQUIRED PARAMETERS
------------------- -------------------
@ -22,12 +23,14 @@ OPTIONAL PARAMETERS
------------------- -------------------
enablement-state enablement-state
'enabled' or 'disabled', where: 'enabled', 'disabled' or 'masked', where:
enabled enabled
enables the unit enables the unit
disabled disabled
disables the unit disables the unit
masked
masks the unit
source source
Path to the config file. If source is '-' (dash), take what was written to Path to the config file. If source is '-' (dash), take what was written to
@ -37,15 +40,17 @@ state
'present' or 'absent', defaults to 'present' where: 'present' or 'absent', defaults to 'present' where:
present present
the unit is installed, enabled and started the unit (or its mask) is installed
absent absent
the unit is stopped, disabled and uninstalled The unit is stopped, disabled and uninstalled. If the unit was masked,
the mask is removed.
BOOLEAN PARAMETERS BOOLEAN PARAMETERS
------------------ ------------------
restart restart
Restart the unit on unit file change or when the unit is inactive. Start the unit if it was inactive. Restart the unit if the unit file
changed. Stop the unit if new ``enablement-state`` is ``masked``.
MESSAGES MESSAGES
-------- --------

View File

@ -29,8 +29,11 @@ fi
name="${__object_id}" name="${__object_id}"
source=$(cat "${__object}/parameter/source") source=$(cat "${__object}/parameter/source")
state=$(cat "${__object}/parameter/state") state=$(cat "${__object}/parameter/state")
enablement_state=$(cat "${__object}/parameter/enablement-state")
if [ -z "${source}" ] && [ "${state}" != "absent" ]; then # The unit must be disabled before removing its unit file. The unit file is
# therefore removed by gencode-remote of this type, not here.
if [ -z "${source}" ] || [ "${state}" = "absent" ]; then
exit 0 exit 0
fi fi
@ -39,8 +42,17 @@ if [ "${source}" = "-" ]; then
source="${__object}/stdin" source="${__object}/stdin"
fi fi
unitfile_state="${state}"
if [ "${enablement_state}" = "masked" ]; then
# Masking creates a symlink from /etc/systemd/system/<unit> to /dev/null.
# This process fails with "Failed to execute operation: Invalid argument"
# if file /etc/systemd/system/<unit> already exists. We must therefore
# remove it.
unitfile_state="absent"
fi
__config_file "/etc/systemd/system/${name}" \ __config_file "/etc/systemd/system/${name}" \
--mode 644 \ --mode 644 \
--onchange "systemctl daemon-reload" \ --onchange "systemctl daemon-reload" \
--source "${source}" \ --source "${source}" \
--state "${state}" --state "${unitfile_state}"