Merge branch 'type/__systemd_service' into 'master'
[__systemd_service] new type to manage the state of systemd services See merge request ungleich-public/cdist!844
This commit is contained in:
commit
221c3820ca
6 changed files with 256 additions and 0 deletions
43
cdist/conf/type/__systemd_service/explorer/state
Executable file
43
cdist/conf/type/__systemd_service/explorer/state
Executable file
|
@ -0,0 +1,43 @@
|
||||||
|
#!/bin/sh -e
|
||||||
|
# explorer/state
|
||||||
|
#
|
||||||
|
# 2020 Matthias Stecher <matthiasstecher at gmx.de>
|
||||||
|
#
|
||||||
|
# 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/>.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Check if the service is running or stopped.
|
||||||
|
#
|
||||||
|
# The explorer must check before if the service exist, because 'systemctl is-active'
|
||||||
|
# will return "inactive" even if there is no service there:
|
||||||
|
# systemctl cat foo # does not exist
|
||||||
|
# systemctl is-active foo # is "inactive"
|
||||||
|
|
||||||
|
|
||||||
|
# get name of the service
|
||||||
|
if [ -f "$__object/parameter/name" ]; then
|
||||||
|
name="$(cat "$__object/parameter/name")"
|
||||||
|
else
|
||||||
|
name="$__object_id"
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
# check if the service exist, else exit without output (also if systemd doesn't exist)
|
||||||
|
# do not exit here with an error code, will be done in the gencode-remote script
|
||||||
|
systemctl cat "$name" > /dev/null 2>&1 || exit 0
|
||||||
|
|
||||||
|
# print if the service is running or not
|
||||||
|
systemctl is-active -q "$name" && printf "running" || printf "stopped"
|
98
cdist/conf/type/__systemd_service/gencode-remote
Executable file
98
cdist/conf/type/__systemd_service/gencode-remote
Executable file
|
@ -0,0 +1,98 @@
|
||||||
|
#!/bin/sh -e
|
||||||
|
# gencode-remote
|
||||||
|
#
|
||||||
|
# 2020 Matthias Stecher <matthiasstecher at gmx.de>
|
||||||
|
#
|
||||||
|
# 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/>.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Checks the given state of the service and set it to the given
|
||||||
|
# state. Optionally, it executes the action if service running.
|
||||||
|
|
||||||
|
|
||||||
|
# get name of the service
|
||||||
|
name="$__object/parameter/name"
|
||||||
|
if [ -f "$name" ]; then
|
||||||
|
name="$(cat "$name")"
|
||||||
|
else
|
||||||
|
name="$__object_id"
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
# read current status and parameters
|
||||||
|
state="$(cat "$__object/explorer/state")"
|
||||||
|
should="$(cat "$__object/parameter/state")"
|
||||||
|
|
||||||
|
# if systemd/service does not exist
|
||||||
|
if [ -z "$state" ]; then
|
||||||
|
printf "systemd or service '%s' does not exist!\n" "$name" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
# save the action required
|
||||||
|
required_action=""
|
||||||
|
|
||||||
|
# check the state of the service that should be
|
||||||
|
if [ "$state" != "$should" ]; then
|
||||||
|
# select what to do to get the $should state
|
||||||
|
case "$should" in
|
||||||
|
running)
|
||||||
|
if [ "$state" = "stopped" ]; then required_action="start"; fi
|
||||||
|
;;
|
||||||
|
|
||||||
|
stopped)
|
||||||
|
if [ "$state" = "running" ]; then required_action="stop"; fi
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
|
||||||
|
# check if the action can be achieved if given
|
||||||
|
if [ -f "$__object/parameter/action" ] \
|
||||||
|
&& [ -z "$required_action" ] && [ "$state" = "running" ]; then
|
||||||
|
|
||||||
|
# there must be an action
|
||||||
|
action="$(cat "$__object/parameter/action")"
|
||||||
|
|
||||||
|
# select the action to the required element
|
||||||
|
case "$action" in
|
||||||
|
restart)
|
||||||
|
required_action="restart"
|
||||||
|
;;
|
||||||
|
|
||||||
|
reload)
|
||||||
|
required_action="reload"
|
||||||
|
;;
|
||||||
|
|
||||||
|
*)
|
||||||
|
printf "action '%s' does not exist!" "$action" >&2
|
||||||
|
exit 2
|
||||||
|
esac
|
||||||
|
|
||||||
|
# Make a special check: only do this action if a dependency did something
|
||||||
|
# it is required that the dependencies write there action to $__messages_in
|
||||||
|
if [ -f "$__object/parameter/if-required" ]; then
|
||||||
|
# exit here if there are no changes from the dependencies affected (nothing to do)
|
||||||
|
if ! grep -q -f "$__object/require" "$__messages_in"; then exit 0; fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# print the execution command if a action given
|
||||||
|
if [ -n "$required_action" ]; then
|
||||||
|
# also print it as message
|
||||||
|
echo "$required_action" >> "$__messages_out"
|
||||||
|
echo "systemctl $required_action '$name'"
|
||||||
|
fi
|
110
cdist/conf/type/__systemd_service/man.rst
Normal file
110
cdist/conf/type/__systemd_service/man.rst
Normal file
|
@ -0,0 +1,110 @@
|
||||||
|
cdist-type__systemd-service(7)
|
||||||
|
==============================
|
||||||
|
|
||||||
|
NAME
|
||||||
|
----
|
||||||
|
cdist-type__systemd-service - Controls a systemd service state
|
||||||
|
|
||||||
|
DESCRIPTION
|
||||||
|
-----------
|
||||||
|
This type controls systemd services to define a state of the service,
|
||||||
|
or an action like reloading or restarting. It is useful to reload a
|
||||||
|
service after configuration applied or shutdown one service.
|
||||||
|
|
||||||
|
The activation or deactivation is out of scope. Look for the
|
||||||
|
:strong:`cdist-type__systemd_util`\ (7) type instead.
|
||||||
|
|
||||||
|
REQUIRED PARAMETERS
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
None.
|
||||||
|
|
||||||
|
OPTIONAL PARAMETERS
|
||||||
|
-------------------
|
||||||
|
|
||||||
|
name
|
||||||
|
String which will used as name instead of the object id.
|
||||||
|
|
||||||
|
state
|
||||||
|
The state which the service should be in:
|
||||||
|
|
||||||
|
running
|
||||||
|
Service should run (default)
|
||||||
|
|
||||||
|
stoppend
|
||||||
|
Service should stopped
|
||||||
|
|
||||||
|
action
|
||||||
|
Executes an action on on the service. It will only execute it if the
|
||||||
|
service keeps the state **running**. There are following actions, where:
|
||||||
|
|
||||||
|
reload
|
||||||
|
Reloads the service
|
||||||
|
|
||||||
|
restart
|
||||||
|
Restarts the service
|
||||||
|
|
||||||
|
BOOLEAN PARAMETERS
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
if-required
|
||||||
|
Only execute the action if minimum one required type outputs a message to
|
||||||
|
**$__messages_out**. Through this, the action should only executed if a
|
||||||
|
dependency did something. The action will not executed if no dependencies
|
||||||
|
given.
|
||||||
|
|
||||||
|
MESSAGES
|
||||||
|
--------
|
||||||
|
|
||||||
|
start
|
||||||
|
Started the service
|
||||||
|
|
||||||
|
stop
|
||||||
|
Stopped the service
|
||||||
|
|
||||||
|
restart
|
||||||
|
Restarted the service
|
||||||
|
|
||||||
|
reload
|
||||||
|
Reloaded the service
|
||||||
|
|
||||||
|
ABORTS
|
||||||
|
------
|
||||||
|
Aborts in following cases:
|
||||||
|
|
||||||
|
systemd or the service does not exist
|
||||||
|
|
||||||
|
EXAMPLES
|
||||||
|
--------
|
||||||
|
.. code-block:: sh
|
||||||
|
|
||||||
|
# service must run
|
||||||
|
__systemd_service nginx
|
||||||
|
|
||||||
|
# service must stopped
|
||||||
|
__systemd_service sshd \
|
||||||
|
--state stopped
|
||||||
|
|
||||||
|
# restart the service
|
||||||
|
__systemd_service apache2 \
|
||||||
|
--action restart
|
||||||
|
|
||||||
|
# makes sure the service exist with an alternative name
|
||||||
|
__systemd_service foo \
|
||||||
|
--name sshd
|
||||||
|
|
||||||
|
# reload the service for a modified configuration file
|
||||||
|
# only reloads the service if the file really changed
|
||||||
|
require="__config_file/etc/foo.conf" __systemd_service foo \
|
||||||
|
--action reload --if-required
|
||||||
|
|
||||||
|
AUTHORS
|
||||||
|
-------
|
||||||
|
Matthias Stecher <matthiasstecher at gmx.de>
|
||||||
|
|
||||||
|
COPYRIGHT
|
||||||
|
---------
|
||||||
|
Copyright \(C) 2020 Matthias Stecher. 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.
|
1
cdist/conf/type/__systemd_service/parameter/boolean
Normal file
1
cdist/conf/type/__systemd_service/parameter/boolean
Normal file
|
@ -0,0 +1 @@
|
||||||
|
if-required
|
|
@ -0,0 +1 @@
|
||||||
|
running
|
3
cdist/conf/type/__systemd_service/parameter/optional
Normal file
3
cdist/conf/type/__systemd_service/parameter/optional
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
name
|
||||||
|
state
|
||||||
|
action
|
Loading…
Reference in a new issue