2020-09-05 20:28:48 +00:00
|
|
|
#!/bin/sh -e
|
2020-08-16 13:18:47 +00:00
|
|
|
|
|
|
|
old_version="$(cat "$__object/explorer/version")"
|
2020-07-20 15:20:33 +00:00
|
|
|
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
|
|
|
|
|
2020-08-16 13:18:47 +00:00
|
|
|
if [ "$VERSION" != "$old_version" ]; then
|
|
|
|
cat << EOF
|
2020-07-20 15:20:33 +00:00
|
|
|
# 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.
|
2020-08-22 19:50:54 +00:00
|
|
|
curl -sS -L '$url' > '$archive'
|
2020-07-20 15:20:33 +00:00
|
|
|
tar xf '$archive'
|
|
|
|
|
2020-09-05 11:53:05 +00:00
|
|
|
|
2020-10-17 09:08:11 +00:00
|
|
|
# virtualenv is given already by __pyvenv, just using it
|
|
|
|
|
2020-10-05 16:26:47 +00:00
|
|
|
# backup requirement files
|
2020-10-08 17:54:48 +00:00
|
|
|
if [ -f /opt/netbox/requirements.txt ]; then
|
2020-10-17 09:08:11 +00:00
|
|
|
mv /opt/netbox/requirements.txt /opt/netbox/old-requirements.txt
|
2020-10-05 16:26:47 +00:00
|
|
|
else
|
|
|
|
# preseve file-not-found errors and warnings
|
|
|
|
touch /opt/netbox/old-requirements.txt
|
|
|
|
fi
|
|
|
|
cp '$src/requirements.txt' /opt/netbox/
|
|
|
|
|
|
|
|
# Uninstall packages not required anymore
|
2020-10-11 14:50:22 +00:00
|
|
|
# if versions not be shortend, they will be ignored by pip, but not by comm
|
2020-10-05 16:26:47 +00:00
|
|
|
# all of this could be done with grep, too, but it's still must be shortend with awk
|
2020-10-17 09:08:11 +00:00
|
|
|
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"
|
2020-10-11 14:50:22 +00:00
|
|
|
comm -23 "\$tmpdir/old-reqs.txt" "\$tmpdir/curr-reqs.txt" > "\$tmpdir/pip-uninstall.txt"
|
|
|
|
|
|
|
|
# only uninstall if something is available (to avoid errors cause of this)
|
|
|
|
if [ -s "\$tmpdir/pip-uninstall.txt" ]; then
|
|
|
|
/opt/netbox/venv/bin/pip3 uninstall -qy -r "\$tmpdir/pip-uninstall.txt"
|
|
|
|
fi
|
2020-10-05 16:26:47 +00:00
|
|
|
|
2020-07-20 15:20:33 +00:00
|
|
|
# Install python dependencies.
|
2020-08-26 18:01:23 +00:00
|
|
|
# avoid gunicorn, because it will be done in an other type
|
|
|
|
grep -v "^gunicorn==" "\$tmpdir/$src/requirements.txt" \
|
2020-10-06 15:29:31 +00:00
|
|
|
| xargs /opt/netbox/venv/bin/pip3 install -q
|
2020-07-21 05:46:45 +00:00
|
|
|
EOF
|
|
|
|
|
2020-08-16 13:18:47 +00:00
|
|
|
if [ -f "$__object/parameter/ldap-server" ]; then
|
2020-10-06 15:29:31 +00:00
|
|
|
echo "/opt/netbox/venv/bin/pip3 install -q django-auth-ldap"
|
2020-08-26 15:39:19 +00:00
|
|
|
else
|
2020-10-06 15:29:31 +00:00
|
|
|
echo "/opt/netbox/venv/bin/pip3 uninstall -qy django-auth-ldap"
|
2020-08-16 13:18:47 +00:00
|
|
|
fi
|
2020-07-20 15:20:33 +00:00
|
|
|
|
2020-08-16 13:18:47 +00:00
|
|
|
cat << EOF
|
2020-10-11 14:50:22 +00:00
|
|
|
|
2020-10-17 09:08:11 +00:00
|
|
|
# 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'
|
|
|
|
|
2020-07-20 15:20:33 +00:00
|
|
|
# Set final permissions.
|
|
|
|
chown -R netbox /opt/netbox
|
|
|
|
|
2020-10-17 09:08:11 +00:00
|
|
|
|
2020-09-28 18:04:22 +00:00
|
|
|
# NetBox manage scripts
|
2020-07-20 15:20:33 +00:00
|
|
|
# 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
|
2020-09-28 18:04:22 +00:00
|
|
|
# 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
|
2020-07-20 15:20:33 +00:00
|
|
|
|
|
|
|
# Remove temporary working directory.
|
|
|
|
cd /
|
2020-10-06 15:29:31 +00:00
|
|
|
rm -rf "\$tmpdir"
|
2020-07-20 15:20:33 +00:00
|
|
|
|
2020-08-16 13:18:47 +00:00
|
|
|
# Save version after successful installation
|
2020-09-28 17:49:29 +00:00
|
|
|
printf "%s\\n" "$VERSION" > /opt/netbox/cdist/version
|
2020-10-17 08:16:25 +00:00
|
|
|
|
2020-08-16 13:18:47 +00:00
|
|
|
EOF
|
|
|
|
|
|
|
|
# meta
|
2020-08-22 07:19:12 +00:00
|
|
|
printf "installed %s\n" "$VERSION" >> "$__messages_out"
|
2020-08-16 13:18:47 +00:00
|
|
|
changes=yes
|
2020-09-28 17:49:29 +00:00
|
|
|
fi
|
2020-08-16 13:18:47 +00:00
|
|
|
|
|
|
|
# check if configuration changed
|
2020-09-28 17:49:29 +00:00
|
|
|
if grep -q "^__file/opt/netbox/" "$__messages_in"; then
|
2020-08-16 13:18:47 +00:00
|
|
|
# meta
|
2020-08-27 17:46:09 +00:00
|
|
|
printf "configured\n" >> "$__messages_out"
|
2020-08-16 13:18:47 +00:00
|
|
|
changes=yes
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2020-10-17 08:16:25 +00:00
|
|
|
# Check for changes
|
2020-08-16 13:18:47 +00:00
|
|
|
if [ "$changes" = "yes" ]; then
|
2020-10-17 08:16:25 +00:00
|
|
|
# After the upstream upgrade.sh script, it's ok to migrate while the
|
|
|
|
# application is running ;)
|
|
|
|
|
|
|
|
# restarting after changes
|
2020-08-22 07:19:12 +00:00
|
|
|
cat << EOF
|
2020-09-09 17:08:46 +00:00
|
|
|
# Restart service. All required services are included with netbox.service.
|
|
|
|
systemctl restart netbox
|
2020-07-20 15:20:33 +00:00
|
|
|
EOF
|
2020-08-16 13:18:47 +00:00
|
|
|
fi
|