__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 #!/bin/sh -e
curr_installed="$(cat "$__object/explorer/installed")" # control state
should_installed="$(cat "$__object/explorer/should_installed")" state="$(cat "$__object/parameter/state")"
# gunicorn version change case "$state" in
if [ "$curr_installed" != "$should_installed" ]; then # install gunicorn
# (re)installing gunicorn enabled|disabled)
echo "/opt/netbox/venv/bin/pip3 install 'gunicorn==$should_installed'" curr_installed="$(cat "$__object/explorer/installed")"
should_installed="$(cat "$__object/explorer/should_installed")"
do_restart=yes # gunicorn version change
printf "updated %s to %s\n" "$curr_installed" "$should_installed" \ if [ "$curr_installed" != "$should_installed" ]; then
>> "$__messages_out" # (re)installing gunicorn
fi echo "/opt/netbox/venv/bin/pip3 install 'gunicorn==$should_installed'"
# configuration changes if [ "$curr_installed" != "" ]; then
if grep -q "^__file/opt/netbox/gunicorn.py:" "$__messages_in"; then printf "updated %s to %s\n" "$curr_installed" "$should_installed" \
do_restart=yes >> "$__messages_out"
printf "configured\n" >> "$__messages_out" else
fi 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 # restart gunicorn
if [ "$do_restart" ]; then if [ "$do_restart" ] && [ "$state" != "disabled" ]; then
cat << EOF cat << EOF
# Restart service # Restart service
service gunicorn-netbox restart service gunicorn-netbox restart
EOF 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 NAME
---- ----
@ -22,6 +22,20 @@ None.
OPTIONAL PARAMETERS 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 bind-to
The hosts the gunicorn socket should be bind to. Formats are `IP`, 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 `IP:PORT`, `unix:PATH` and `fd://FD`. Parameter can be set a multiple
@ -35,13 +49,20 @@ None.
MESSAGES 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`. The version of the gunicorn software was updated from `$old` to `$new`.
configured configured
Configuration for gunicorn changed. 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 EXAMPLES
@ -59,6 +80,17 @@ EXAMPLES
--bind-to 0.0.0.0:8001 \ --bind-to 0.0.0.0:8001 \
--bind-to 1.2.3.4:5678 --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 SEE ALSO
-------- --------

View file

@ -1,23 +1,55 @@
#!/bin/sh -e #!/bin/sh -e
# __netbox_gunicorn/manifest
HOST="" # Check states
while read -r host; do state=""
# shellcheck disable=SC2089 unit_state=""
HOST="$HOST '$host'," param_state="$(cat "$__object/parameter/state")"
done < "$__object/parameter/bind-to"
# shellcheck disable=SC2090
export HOST
# process template case "$param_state" in
mkdir "$__object/files" enabled|disabled)
"$__type/files/gunicorn.py.sh" > "$__object/files/gunicorn.py" 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 # install service file
__systemd_unit gunicorn-netbox.service \ __systemd_unit gunicorn-netbox.service \
--source "$__type/files/netbox.service" \ --state "$state" --enablement-state "$unit_state" \
--enablement-state enabled --restart --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 #!/bin/sh -e
# not installed # control state
if ! [ -s "$__object/explorer/installed" ]; then state="$(cat "$__object/parameter/state")"
echo "/opt/netbox/venv/bin/pip3 install uwsgi"
do_restart=yes
printf "installed\n" >> "$__messages_out"
# updates available case "$state" in
elif [ -s "$__object/explorer/upgradeable" ]; then # install uwsgi
echo "/opt/netbox/venv/bin/pip3 install --upgrade uwsgi" enabled|disabled)
do_restart=yes # not installed
printf "upgraded\n" >> "$__messages_out" if ! [ -s "$__object/explorer/installed" ]; then
fi echo "/opt/netbox/venv/bin/pip3 install uwsgi"
do_restart=yes
printf "installed\n" >> "$__messages_out"
# changed configuration # updates available
if grep -q "^__file/opt/netbox/uwsgi.ini:" "$__messages_in"; then elif [ -s "$__object/explorer/upgradeable" ]; then
do_restart=yes echo "/opt/netbox/venv/bin/pip3 install --upgrade uwsgi"
printf "configured\n" >> "$__messages_out" do_restart=yes
fi 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 # restart uwsgi
if [ "$do_restart" ]; then if [ "$do_restart" ] && [ "$state" != "disabled" ]; then
cat << EOF cat << EOF
# Restart service # Restart service
service uwsgi-netbox restart service uwsgi-netbox restart
EOF 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 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 bind-to
The socket uwsgi should bind to. Must be UNIX/TCP for the uwsgi protocol. 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. Defaults to ``127.0.0.1:3031``. Can be set multiple times.
@ -62,7 +77,11 @@ upgraded
configured configured
The uwsgi configuration got updated. 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 EXAMPLES
@ -90,6 +109,17 @@ EXAMPLES
__netbox $args __netbox $args
require="__netbox" __netbox_uwsgi --serve-static --http-bind 0.0.0.0:80 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 SEE ALSO
-------- --------

View file

@ -1,22 +1,54 @@
#!/bin/sh -e #!/bin/sh -e
# __netbox_uwsgi/manifest
# *bind* parameters are directly processed in the gen script # Check states
if [ -f "$__object/parameter/serve-static" ]; then state=""
STATIC_MAP="yes" unit_state=""
export STATIC_MAP 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 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 # install service file
__systemd_unit uwsgi-netbox.service \ __systemd_unit uwsgi-netbox.service \
--source "$__type/files/netbox.service" \ --state "$state" --enablement-state "$unit_state" \
--enablement-state enabled --restart --source "$__type/files/netbox.service" --restart

View file

@ -0,0 +1 @@
enabled

View file

@ -0,0 +1 @@
state