134 lines
4.1 KiB
Bash
Executable File
134 lines
4.1 KiB
Bash
Executable File
#!/bin/sh -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'
|
|
|
|
EOF
|
|
|
|
# Stop everything in the pyenv to update
|
|
cat << EOF
|
|
# Try to stop everything in the venv (ignore non-existant services)
|
|
systemctl -q stop netbox gunicorn-netbox uwsgi-netbox || true
|
|
|
|
# kill and poll till all venv processes end
|
|
# at least if they are called with full path in the cmd (like in the services)
|
|
pids="\$( ps -axo pid,cmd | awk '\$2 ~ "^/opt/netbox/venv/"{print \$1}' )"
|
|
if [ "\$pids" ]; then
|
|
kill \$pids
|
|
while ps -axo pid,cmd | awk '\$2 ~ "^/opt/netbox/venv/"{print \$1}' | grep -q . ; do
|
|
sleep 0.5
|
|
done
|
|
fi
|
|
|
|
EOF
|
|
|
|
cat << EOF
|
|
# backup requirement files
|
|
if [ -f /opt/netbox/requirements.txt ]; then
|
|
cp /opt/netbox/requirements.txt /opt/netbox/old-requirements.txt
|
|
else
|
|
# preseve file-not-found errors and warnings
|
|
touch /opt/netbox/old-requirements.txt
|
|
fi
|
|
cp '$src/requirements.txt' /opt/netbox/
|
|
|
|
# Deploy sources and restore configuration.
|
|
rm -rf '$install_dir'
|
|
cp -r '$src/netbox' '$install_dir'
|
|
# force links to the cdist directory
|
|
ln -fs /opt/netbox/cdist/configuration.py '$install_dir/netbox/configuration.py'
|
|
ln -fs /opt/netbox/cdist/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
|
|
|
|
# Uninstall packages not required anymore
|
|
# if versions not shortend, they will be ignored by pip, but not by comm
|
|
# all of this could be done with grep, too, but it's still must be shortend with awk
|
|
awk -F== '{print $1}' '/opt/netbox/requirements.txt' | sort > "\$tmpdir/curr-reqs.txt"
|
|
awk -F== '{print $1}' '/opt/netbox/old-requirements.txt' | sort > "\$tmpdir/old-reqs.txt"
|
|
comm -23 "\$tmpdir/old-reqs.txt" "\$tmpdir/curr-reqs.txt" \
|
|
| xargs /opt/netbox/venv/bin/pip3 uninstall -qy
|
|
|
|
# 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 -q
|
|
EOF
|
|
|
|
if [ -f "$__object/parameter/ldap-server" ]; then
|
|
echo "/opt/netbox/venv/bin/pip3 install -q django-auth-ldap"
|
|
else
|
|
echo "/opt/netbox/venv/bin/pip3 uninstall -qy django-auth-ldap"
|
|
fi
|
|
|
|
cat << EOF
|
|
# Set final permissions.
|
|
chown -R netbox /opt/netbox
|
|
|
|
# NetBox manage scripts
|
|
# 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
|
|
# Delete any stale content types
|
|
sudo -u netbox /opt/netbox/venv/bin/python3 /opt/netbox/netbox/manage.py remove_stale_contenttypes --no-input
|
|
# Delete any expired user sessions
|
|
sudo -u netbox /opt/netbox/venv/bin/python3 /opt/netbox/netbox/manage.py clearsessions
|
|
# Clear all cached data
|
|
sudo -u netbox /opt/netbox/venv/bin/python3 /opt/netbox/netbox/manage.py invalidate all
|
|
|
|
# Remove temporary working directory.
|
|
cd /
|
|
rm -rf "\$tmpdir"
|
|
|
|
# Save version after successful installation
|
|
printf "%s\\n" "$VERSION" > /opt/netbox/cdist/version
|
|
EOF
|
|
|
|
# meta
|
|
printf "installed %s\n" "$VERSION" >> "$__messages_out"
|
|
changes=yes
|
|
fi
|
|
|
|
# check if configuration changed
|
|
if grep -q "^__file/opt/netbox/" "$__messages_in"; then
|
|
# meta
|
|
printf "configured\n" >> "$__messages_out"
|
|
changes=yes
|
|
fi
|
|
|
|
|
|
# check for changes
|
|
if [ "$changes" = "yes" ]; then
|
|
# to avoid database corruption at config changes, both services must be
|
|
# stopped at the same time (noted in the manual, too).
|
|
cat << EOF
|
|
# Restart service. All required services are included with netbox.service.
|
|
systemctl restart netbox
|
|
EOF
|
|
fi
|