cdist-contrib/type/__netbox/gencode-remote
Matthias Stecher 314a0d4d8e __netbox: kill all running venv processes
To avoid aborts because of the python venv could not be updated by
killing all processes that uses the venv.

It will be done all times to prevent any error, because it could not be
reliably detected if the type installs or updates NetBox.
2020-09-05 13:53:05 +02:00

115 lines
3 KiB
Bash
Executable file

#!/bin/sh
echo "set -e"
old_version="$(cat "$__object/explorer/version")"
VERSION=$(cat "$__object/parameter/version")
src="netbox-$VERSION"
archive="v$VERSION.tar.gz"
url="https://github.com/netbox-community/netbox/archive/$archive"
install_dir=/opt/netbox/netbox
if [ "$VERSION" != "$old_version" ]; then
cat << EOF
# Ensure that coreutils is installed.
if [ ! -x \$(which mktemp) ]; then
echo "mktemp is not available on the remote host." >&2
exit 1
fi
# Create temporary working directory.
tmpdir=\$(mktemp -d)
cd "\$tmpdir"
# Download and extract sources.
curl -sS -L '$url' > '$archive'
tar xf '$archive'
# Save cdist-upload configuration file.
cp '$install_dir/cdist/configuration.py' "\$tmpdir/configuration.py"
cp '$install_dir/cdist/ldap_config.py' "\$tmpdir/ldap_config.py"
EOF
# Stop everything in the pyenv to update
cat << EOF
# Try to kill everything in the venv
systemctl stop netbox gunicorn-netbox uwsgi-netbox || true
ps -axo pid,cmd | awk '\$2 ~ "^/opt/netbox/venv/"{print \$1}' | xargs kill || true
EOF
cat << EOF
# Deploy sources and restore configuration.
rm -r '$install_dir'
cp -r '$src/netbox' '$install_dir'
mkdir '$install_dir/cdist'
cp '$src/requirements.txt' /opt/netbox/ # backup dependency info
cp \$tmpdir/configuration.py '$install_dir/netbox/configuration.py'
cp \$tmpdir/ldap_config.py '$install_dir/netbox/ldap_config.py'
# Setup & enter python virtualenv.
# forcing python3 to be sure (till python4 gets released ..)
virtualenv -p python3 /opt/netbox/venv
# Install python dependencies.
# avoid gunicorn, because it will be done in an other type
grep -v "^gunicorn==" "\$tmpdir/$src/requirements.txt" \
| xargs /opt/netbox/venv/bin/pip3 install
EOF
if [ -f "$__object/parameter/ldap-server" ]; then
echo "/opt/netbox/venv/bin/pip3 install django-auth-ldap"
else
echo "/opt/netbox/venv/bin/pip3 uninstall django-auth-ldap"
fi
cat << EOF
# Set final permissions.
chown -R netbox /opt/netbox
# Run database migrations.
sudo -u netbox /opt/netbox/venv/bin/python3 /opt/netbox/netbox/manage.py migrate
# Generate static assets.
sudo -u netbox /opt/netbox/venv/bin/python3 /opt/netbox/netbox/manage.py collectstatic --no-input
# Remove temporary working directory.
cd /
rm -r "\$tmpdir"
# Save version after successful installation
printf "%s\\n" "$VERSION" > '$install_dir/cdist/version'
EOF
# meta
printf "installed %s\n" "$VERSION" >> "$__messages_out"
changes=yes
# check if configuration changed
elif grep -q "^__file/opt/netbox/" "$__messages_in"; then
# check if coping is required
if grep -q "^__file/opt/netbox/netbox/cdist/" "$__messages_in"; then
cat << EOF
# Copy configuration
cp '$install_dir/cdist/configuration.py' '$install_dir/netbox/configuration.py'
cp '$install_dir/cdist/ldap_config.py' '$install_dir/netbox/ldap_config.py'
EOF
fi
# meta
printf "configured\n" >> "$__messages_out"
changes=yes
fi
# check for changes
if [ "$changes" = "yes" ]; then
cat << EOF
# Restart service.
service netbox restart
service netbox-rq restart
EOF
fi