2016-07-01 13:21:51 -04:00
|
|
|
#!/bin/bash
|
2016-06-29 15:43:42 -04:00
|
|
|
# This script will prepare NetBox to run after the code has been upgraded to
|
|
|
|
# its most recent release.
|
|
|
|
|
2021-07-19 15:13:30 -04:00
|
|
|
# This script will invoke Python with the value of the PYTHON environment
|
2024-01-04 09:47:55 -05:00
|
|
|
# variable (if set), or fall back to "python3". Note that NetBox v4.0+ requires
|
|
|
|
# Python 3.10 or later.
|
2021-07-19 15:13:30 -04:00
|
|
|
|
2019-05-02 13:02:53 -05:00
|
|
|
cd "$(dirname "$0")"
|
2023-12-21 22:13:40 +02:00
|
|
|
|
|
|
|
NETBOX_VERSION="$(grep ^VERSION netbox/netbox/settings.py | cut -d\' -f2)"
|
|
|
|
echo "You are installing (or upgrading to) NetBox version ${NETBOX_VERSION}"
|
|
|
|
|
2020-02-26 13:54:07 -05:00
|
|
|
VIRTUALENV="$(pwd -P)/venv"
|
2021-07-19 15:13:30 -04:00
|
|
|
PYTHON="${PYTHON:-python3}"
|
2020-02-26 13:54:07 -05:00
|
|
|
|
2022-02-17 10:31:28 -05:00
|
|
|
# Validate the minimum required Python version
|
2024-01-04 09:47:55 -05:00
|
|
|
COMMAND="${PYTHON} -c 'import sys; exit(1 if sys.version_info < (3, 10) else 0)'"
|
2022-02-17 10:31:28 -05:00
|
|
|
PYTHON_VERSION=$(eval "${PYTHON} -V")
|
|
|
|
eval $COMMAND || {
|
|
|
|
echo "--------------------------------------------------------------------"
|
|
|
|
echo "ERROR: Unsupported Python version: ${PYTHON_VERSION}. NetBox requires"
|
2024-01-04 09:47:55 -05:00
|
|
|
echo "Python 3.10 or later. To specify an alternate Python executable, set"
|
2022-02-17 10:31:28 -05:00
|
|
|
echo "the PYTHON environment variable. For example:"
|
|
|
|
echo ""
|
2024-01-04 09:47:55 -05:00
|
|
|
echo " sudo PYTHON=/usr/bin/python3.10 ./upgrade.sh"
|
2022-02-17 10:31:28 -05:00
|
|
|
echo ""
|
|
|
|
echo "To show your current Python version: ${PYTHON} -V"
|
|
|
|
echo "--------------------------------------------------------------------"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
echo "Using ${PYTHON_VERSION}"
|
|
|
|
|
2020-02-26 13:54:07 -05:00
|
|
|
# Remove the existing virtual environment (if any)
|
|
|
|
if [ -d "$VIRTUALENV" ]; then
|
|
|
|
COMMAND="rm -rf ${VIRTUALENV}"
|
|
|
|
echo "Removing old virtual environment..."
|
|
|
|
eval $COMMAND
|
2020-02-26 15:26:12 -05:00
|
|
|
else
|
|
|
|
WARN_MISSING_VENV=1
|
2020-02-26 13:54:07 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Create a new virtual environment
|
2021-07-19 15:13:30 -04:00
|
|
|
COMMAND="${PYTHON} -m venv ${VIRTUALENV}"
|
2020-02-26 13:54:07 -05:00
|
|
|
echo "Creating a new virtual environment at ${VIRTUALENV}..."
|
2020-02-26 15:34:42 -05:00
|
|
|
eval $COMMAND || {
|
2020-02-26 15:40:05 -05:00
|
|
|
echo "--------------------------------------------------------------------"
|
|
|
|
echo "ERROR: Failed to create the virtual environment. Check that you have"
|
2020-03-04 12:20:06 -05:00
|
|
|
echo "the required system packages installed and the following path is"
|
|
|
|
echo "writable: ${VIRTUALENV}"
|
2020-02-26 15:40:05 -05:00
|
|
|
echo "--------------------------------------------------------------------"
|
2020-02-26 15:26:12 -05:00
|
|
|
exit 1
|
2020-02-26 15:34:42 -05:00
|
|
|
}
|
2017-10-09 17:40:15 -04:00
|
|
|
|
2020-02-26 13:54:07 -05:00
|
|
|
# Activate the virtual environment
|
|
|
|
source "${VIRTUALENV}/bin/activate"
|
2016-06-29 15:43:42 -04:00
|
|
|
|
2021-02-09 13:32:29 -05:00
|
|
|
# Upgrade pip
|
|
|
|
COMMAND="pip install --upgrade pip"
|
|
|
|
echo "Updating pip ($COMMAND)..."
|
|
|
|
eval $COMMAND || exit 1
|
|
|
|
pip -V
|
|
|
|
|
2020-03-06 13:59:19 -05:00
|
|
|
# Install necessary system packages
|
2021-02-09 13:32:29 -05:00
|
|
|
COMMAND="pip install wheel"
|
2020-03-06 13:59:19 -05:00
|
|
|
echo "Installing Python system packages ($COMMAND)..."
|
|
|
|
eval $COMMAND || exit 1
|
|
|
|
|
2020-03-09 14:22:27 -04:00
|
|
|
# Install required Python packages
|
2021-02-09 13:32:29 -05:00
|
|
|
COMMAND="pip install -r requirements.txt"
|
2020-03-09 14:22:27 -04:00
|
|
|
echo "Installing core dependencies ($COMMAND)..."
|
2020-03-06 10:26:12 -05:00
|
|
|
eval $COMMAND || exit 1
|
2019-10-23 17:12:32 -04:00
|
|
|
|
2020-03-09 14:22:27 -04:00
|
|
|
# Install optional packages (if any)
|
2020-07-27 12:01:21 -04:00
|
|
|
if [ -s "local_requirements.txt" ]; then
|
2021-02-09 13:32:29 -05:00
|
|
|
COMMAND="pip install -r local_requirements.txt"
|
2020-03-09 14:22:27 -04:00
|
|
|
echo "Installing local dependencies ($COMMAND)..."
|
|
|
|
eval $COMMAND || exit 1
|
2020-07-27 12:01:21 -04:00
|
|
|
elif [ -f "local_requirements.txt" ]; then
|
|
|
|
echo "Skipping local dependencies (local_requirements.txt is empty)"
|
2020-03-09 14:22:27 -04:00
|
|
|
else
|
|
|
|
echo "Skipping local dependencies (local_requirements.txt not found)"
|
|
|
|
fi
|
|
|
|
|
2016-06-29 15:43:42 -04:00
|
|
|
# Apply any database migrations
|
2020-02-26 13:54:07 -05:00
|
|
|
COMMAND="python3 netbox/manage.py migrate"
|
2017-05-18 14:53:35 -04:00
|
|
|
echo "Applying database migrations ($COMMAND)..."
|
2020-03-06 10:26:12 -05:00
|
|
|
eval $COMMAND || exit 1
|
2016-06-29 15:43:42 -04:00
|
|
|
|
2020-10-07 10:23:15 -04:00
|
|
|
# Trace any missing cable paths (not typically needed)
|
|
|
|
COMMAND="python3 netbox/manage.py trace_paths --no-input"
|
|
|
|
echo "Checking for missing cable paths ($COMMAND)..."
|
|
|
|
eval $COMMAND || exit 1
|
|
|
|
|
2021-07-19 14:18:08 -04:00
|
|
|
# Build the local documentation
|
|
|
|
COMMAND="mkdocs build"
|
|
|
|
echo "Building documentation ($COMMAND)..."
|
|
|
|
eval $COMMAND || exit 1
|
|
|
|
|
2016-06-29 15:43:42 -04:00
|
|
|
# Collect static files
|
2020-02-26 13:54:07 -05:00
|
|
|
COMMAND="python3 netbox/manage.py collectstatic --no-input"
|
2017-04-27 14:42:52 -04:00
|
|
|
echo "Collecting static files ($COMMAND)..."
|
2020-03-06 10:26:12 -05:00
|
|
|
eval $COMMAND || exit 1
|
2020-01-15 08:57:00 -05:00
|
|
|
|
2020-02-26 13:54:07 -05:00
|
|
|
# Delete any stale content types
|
|
|
|
COMMAND="python3 netbox/manage.py remove_stale_contenttypes --no-input"
|
|
|
|
echo "Removing stale content types ($COMMAND)..."
|
2020-03-06 10:26:12 -05:00
|
|
|
eval $COMMAND || exit 1
|
2020-02-26 13:54:07 -05:00
|
|
|
|
2023-02-20 09:39:33 -05:00
|
|
|
# Rebuild the search cache (lazily)
|
|
|
|
COMMAND="python3 netbox/manage.py reindex --lazy"
|
|
|
|
echo "Rebuilding search cache ($COMMAND)..."
|
|
|
|
eval $COMMAND || exit 1
|
|
|
|
|
2020-03-04 10:29:22 -05:00
|
|
|
# Delete any expired user sessions
|
|
|
|
COMMAND="python3 netbox/manage.py clearsessions"
|
|
|
|
echo "Removing expired user sessions ($COMMAND)..."
|
2020-03-06 10:26:12 -05:00
|
|
|
eval $COMMAND || exit 1
|
2020-03-04 10:29:22 -05:00
|
|
|
|
2020-03-06 15:07:29 -05:00
|
|
|
if [ -v WARN_MISSING_VENV ]; then
|
2020-02-26 15:40:05 -05:00
|
|
|
echo "--------------------------------------------------------------------"
|
|
|
|
echo "WARNING: No existing virtual environment was detected. A new one has"
|
|
|
|
echo "been created. Update your systemd service files to reflect the new"
|
2020-11-17 12:01:10 -05:00
|
|
|
echo "Python and gunicorn executables. (If this is a new installation,"
|
|
|
|
echo "this warning can be ignored.)"
|
2020-03-04 12:20:06 -05:00
|
|
|
echo ""
|
|
|
|
echo "netbox.service ExecStart:"
|
|
|
|
echo " ${VIRTUALENV}/bin/gunicorn"
|
|
|
|
echo ""
|
|
|
|
echo "netbox-rq.service ExecStart:"
|
|
|
|
echo " ${VIRTUALENV}/bin/python"
|
2020-03-04 17:19:16 -05:00
|
|
|
echo ""
|
|
|
|
echo "After modifying these files, reload the systemctl daemon:"
|
|
|
|
echo " > systemctl daemon-reload"
|
2020-02-26 15:40:05 -05:00
|
|
|
echo "--------------------------------------------------------------------"
|
2020-02-26 15:26:12 -05:00
|
|
|
fi
|
|
|
|
|
2020-02-26 13:54:07 -05:00
|
|
|
echo "Upgrade complete! Don't forget to restart the NetBox services:"
|
2020-03-04 17:19:16 -05:00
|
|
|
echo " > sudo systemctl restart netbox netbox-rq"
|