__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,29 +1,50 @@
#!/bin/sh -e
curr_installed="$(cat "$__object/explorer/installed")"
should_installed="$(cat "$__object/explorer/should_installed")"
# control state
state="$(cat "$__object/parameter/state")"
# gunicorn version change
if [ "$curr_installed" != "$should_installed" ]; then
# (re)installing gunicorn
echo "/opt/netbox/venv/bin/pip3 install 'gunicorn==$should_installed'"
case "$state" in
# install gunicorn
enabled|disabled)
curr_installed="$(cat "$__object/explorer/installed")"
should_installed="$(cat "$__object/explorer/should_installed")"
do_restart=yes
printf "updated %s to %s\n" "$curr_installed" "$should_installed" \
>> "$__messages_out"
fi
# gunicorn version change
if [ "$curr_installed" != "$should_installed" ]; then
# (re)installing gunicorn
echo "/opt/netbox/venv/bin/pip3 install 'gunicorn==$should_installed'"
# configuration changes
if grep -q "^__file/opt/netbox/gunicorn.py:" "$__messages_in"; then
do_restart=yes
printf "configured\n" >> "$__messages_out"
fi
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
if grep -q "^__file/opt/netbox/gunicorn.py:" "$__messages_in"; then
do_restart=yes
printf "configured\n" >> "$__messages_out"
fi
# restart gunicorn
if [ "$do_restart" ]; then
cat << EOF
# restart gunicorn
if [ "$do_restart" ] && [ "$state" != "disabled" ]; then
cat << EOF
# Restart service
service gunicorn-netbox restart
EOF
fi
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,23 +1,55 @@
#!/bin/sh -e
# __netbox_gunicorn/manifest
HOST=""
while read -r host; do
# shellcheck disable=SC2089
HOST="$HOST '$host',"
done < "$__object/parameter/bind-to"
# shellcheck disable=SC2090
export HOST
# Check states
state=""
unit_state=""
param_state="$(cat "$__object/parameter/state")"
# process template
mkdir "$__object/files"
"$__type/files/gunicorn.py.sh" > "$__object/files/gunicorn.py"
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
HOST="$HOST '$host',"
done < "$__object/parameter/bind-to"
# shellcheck disable=SC2090
export HOST
# process template
mkdir "$__object/files"
"$__type/files/gunicorn.py.sh" > "$__object/files/gunicorn.py"
# gunicorn config file
__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
# gunicorn config file
__file /opt/netbox/gunicorn.py \
--mode 644 --owner netbox \
--source "$__object/files/gunicorn.py"
# 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,29 +1,47 @@
#!/bin/sh -e
# not installed
if ! [ -s "$__object/explorer/installed" ]; then
echo "/opt/netbox/venv/bin/pip3 install uwsgi"
do_restart=yes
printf "installed\n" >> "$__messages_out"
# control state
state="$(cat "$__object/parameter/state")"
# updates available
elif [ -s "$__object/explorer/upgradeable" ]; then
echo "/opt/netbox/venv/bin/pip3 install --upgrade uwsgi"
do_restart=yes
printf "upgraded\n" >> "$__messages_out"
fi
case "$state" in
# install uwsgi
enabled|disabled)
# not installed
if ! [ -s "$__object/explorer/installed" ]; then
echo "/opt/netbox/venv/bin/pip3 install uwsgi"
do_restart=yes
printf "installed\n" >> "$__messages_out"
# changed configuration
if grep -q "^__file/opt/netbox/uwsgi.ini:" "$__messages_in"; then
do_restart=yes
printf "configured\n" >> "$__messages_out"
fi
# updates available
elif [ -s "$__object/explorer/upgradeable" ]; then
echo "/opt/netbox/venv/bin/pip3 install --upgrade uwsgi"
do_restart=yes
printf "upgraded\n" >> "$__messages_out"
fi
# changed configuration
if grep -q "^__file/opt/netbox/uwsgi.ini:" "$__messages_in"; then
do_restart=yes
printf "configured\n" >> "$__messages_out"
fi
# restart uwsgi
if [ "$do_restart" ]; then
cat << EOF
# restart uwsgi
if [ "$do_restart" ] && [ "$state" != "disabled" ]; then
cat << EOF
# Restart service
service uwsgi-netbox restart
EOF
fi
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,22 +1,54 @@
#!/bin/sh -e
# __netbox_uwsgi/manifest
# *bind* parameters are directly processed in the gen script
if [ -f "$__object/parameter/serve-static" ]; then
STATIC_MAP="yes"
export STATIC_MAP
# 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"
export STATIC_MAP
fi
# process template
mkdir "$__object/files"
"$__type/files/uwsgi.ini.sh" > "$__object/files/uwsgi.ini"
# uwsgi config file
# TODO maybe patching with __key_value cause of .ini ?
__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
# process template
mkdir "$__object/files"
"$__type/files/uwsgi.ini.sh" > "$__object/files/uwsgi.ini"
# uwsgi config file
# TODO maybe patching with __key_value cause of .ini ?
__file /opt/netbox/uwsgi.ini \
--mode 644 --owner netbox \
--source "$__object/files/uwsgi.ini"
# 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