__netbox_{gunicorn,uwsgi}: add state parameter

Adds the --state parameter to both types. With it, the transition
between both types can be done smothly.
This commit is contained in:
matze 2020-09-06 13:44:03 +02:00
parent 3b07a660b3
commit c9e4e8d7dc
10 changed files with 244 additions and 75 deletions

View file

@ -1,5 +1,11 @@
#!/bin/sh -e
# control state
state="$(cat "$__object/parameter/state")"
case "$state" in
# install gunicorn
enabled|disabled)
curr_installed="$(cat "$__object/explorer/installed")"
should_installed="$(cat "$__object/explorer/should_installed")"
@ -8,9 +14,13 @@ if [ "$curr_installed" != "$should_installed" ]; then
# (re)installing gunicorn
echo "/opt/netbox/venv/bin/pip3 install 'gunicorn==$should_installed'"
do_restart=yes
if [ "$curr_installed" != "" ]; then
printf "updated %s to %s\n" "$curr_installed" "$should_installed" \
>> "$__messages_out"
else
printf "installed\n" >> "$__messages_out"
fi
do_restart=yes
fi
# configuration changes
@ -21,9 +31,20 @@ fi
# restart gunicorn
if [ "$do_restart" ]; then
if [ "$do_restart" ] && [ "$state" != "disabled" ]; then
cat << EOF
# Restart service
service gunicorn-netbox restart
EOF
fi
;;
# uninstall
absent)
# check if installed
if [ -s "$__object/explorer/installed" ]; then
# service already disabled
echo "/opt/netbox/venv/bin/pip3 uninstall -y gunicorn"
printf "uninstalled\n" >> "$__messages_out"
fi
esac

View file

@ -1,5 +1,5 @@
cdist-type__netbox_uwsgi(7)
===========================
cdist-type__netbox_gunicorn(7)
==============================
NAME
----
@ -22,6 +22,20 @@ None.
OPTIONAL PARAMETERS
-------------------
state
Represents the state of the Gunciron application. Defaults to ``enabled``.
enabled
The Gunicorn service is enabled and running.
disabled
The Gunicorn service is installed, but disabled.
absent
The uWSGI service is not installed and all configuration removed.
This type does not guarantee anything about the running state of the
service. To be sure about the service is stopped or not, use the type
:strong:`cdist-type__systemd_service`\ (7) after this execution.
bind-to
The hosts the gunicorn socket should be bind to. Formats are `IP`,
`IP:PORT`, `unix:PATH` and `fd://FD`. Parameter can be set a multiple
@ -35,13 +49,20 @@ None.
MESSAGES
--------
updated $old to $new
installed
The software was installed.
upgraded $old to $new
The version of the gunicorn software was updated from `$old` to `$new`.
configured
Configuration for gunicorn changed.
In both cases, it restarts the service to use the up-to-date version.
uninstalled
The Gunicorn application was removed.
In all cases where the application is still present, it restarts the service to
use the up-to-date version.
EXAMPLES
@ -59,6 +80,17 @@ EXAMPLES
--bind-to 0.0.0.0:8001 \
--bind-to 1.2.3.4:5678
# replace uwsgi with gunicorn
__netbox $args
require="__netbox" __netbox_uwsgi --state absent
# it should depend on __netbox_uwsgi if they use the same socket
require="__netbox_uwsgi" __netbox_gunicorn --state enabled
# be sure the service is disabled
__netbox $args
require="__netbox" __netbox_gunicorn --state disabled
require="__netbox_gunicorn" __systemd_service gunicorn-netbox --state stopped
SEE ALSO
--------

View file

@ -1,5 +1,31 @@
#!/bin/sh -e
# __netbox_gunicorn/manifest
# Check states
state=""
unit_state=""
param_state="$(cat "$__object/parameter/state")"
case "$param_state" in
enabled|disabled)
state="present"
unit_state="$param_state"
;;
absent)
state="absent"
unit_state="disabled"
;;
*)
# does not exist
printf "The state '%s' does not exist, can't continue!\n" "$param_state" >&2
exit 2
;;
esac
if [ "$state" = "present" ]; then
HOST=""
while read -r host; do
# shellcheck disable=SC2089
@ -17,7 +43,13 @@ __file /opt/netbox/gunicorn.py \
--mode 644 --owner netbox \
--source "$__object/files/gunicorn.py"
else
# absent config file
__file /opt/netbox/gunicorn.py --state absent
fi
# install service file
__systemd_unit gunicorn-netbox.service \
--source "$__type/files/netbox.service" \
--enablement-state enabled --restart
--state "$state" --enablement-state "$unit_state" \
--source "$__type/files/netbox.service" --restart

View file

@ -0,0 +1 @@
enabled

View file

@ -0,0 +1 @@
state

View file

@ -1,5 +1,11 @@
#!/bin/sh -e
# control state
state="$(cat "$__object/parameter/state")"
case "$state" in
# install uwsgi
enabled|disabled)
# not installed
if ! [ -s "$__object/explorer/installed" ]; then
echo "/opt/netbox/venv/bin/pip3 install uwsgi"
@ -21,9 +27,21 @@ fi
# restart uwsgi
if [ "$do_restart" ]; then
if [ "$do_restart" ] && [ "$state" != "disabled" ]; then
cat << EOF
# Restart service
service uwsgi-netbox restart
EOF
fi
;;
# uninstall
absent)
# check if installed
if [ -s "$__object/explorer/installed" ]; then
# service already disabled
echo "/opt/netbox/venv/bin/pip3 uninstall -y uwsgi"
printf "uninstalled\n" >> "$__messages_out"
fi
;;
esac

View file

@ -23,6 +23,21 @@ None.
OPTIONAL PARAMETERS
-------------------
state
Represents the state of the uWSGI application. Defaults to ``enabled``.
enabled
The uWSGI service is enabled and running.
disabled
The uWSGI service is installed, but disabled.
absent
The uWSGI service is not installed and all configuration removed.
This type does not guarantee anything about the running state of the
service. To be sure about the service is stopped or not, use the type
:strong:`cdist-type__systemd_service`\ (7) after this execution.
bind-to
The socket uwsgi should bind to. Must be UNIX/TCP for the uwsgi protocol.
Defaults to ``127.0.0.1:3031``. Can be set multiple times.
@ -62,7 +77,11 @@ upgraded
configured
The uwsgi configuration got updated.
In all cases, it restarts the service to use the up-to-date version.
uninstalled
The uWSGI application was removed.
In all cases where the application is still present, it restarts the service to
use the up-to-date version.
EXAMPLES
@ -90,6 +109,17 @@ EXAMPLES
__netbox $args
require="__netbox" __netbox_uwsgi --serve-static --http-bind 0.0.0.0:80
# replace gunicorn with uwsgi
__netbox $args
require="__netbox" __netbox_gunicorn --state absent
# it should depend on __netbox_gunicorn if they use the same socket
require="__netbox_gunicorn" __netbox_uwsgi --state enabled
# be sure the service is disabled
__netbox $args
require="__netbox" __netbox_uwsgi --state disabled
require="__netbox_uwsgi" __systemd_service uwsgi-netbox --state stopped
SEE ALSO
--------

View file

@ -1,5 +1,31 @@
#!/bin/sh -e
# __netbox_uwsgi/manifest
# Check states
state=""
unit_state=""
param_state="$(cat "$__object/parameter/state")"
case "$param_state" in
enabled|disabled)
state="present"
unit_state="$param_state"
;;
absent)
state="absent"
unit_state="disabled"
;;
*)
# does not exist
printf "The state '%s' does not exist, can't continue!\n" "$param_state" >&2
exit 2
;;
esac
if [ "$state" = "present" ]; then
# *bind* parameters are directly processed in the gen script
if [ -f "$__object/parameter/serve-static" ]; then
STATIC_MAP="yes"
@ -16,7 +42,13 @@ __file /opt/netbox/uwsgi.ini \
--mode 644 --owner netbox \
--source "$__object/files/uwsgi.ini"
else
# absent config file
__file /opt/netbox/uwsgi.ini --state absent
fi
# install service file
__systemd_unit uwsgi-netbox.service \
--source "$__type/files/netbox.service" \
--enablement-state enabled --restart
--state "$state" --enablement-state "$unit_state" \
--source "$__type/files/netbox.service" --restart

View file

@ -0,0 +1 @@
enabled

View file

@ -0,0 +1 @@
state