Last active
September 21, 2023 11:18
-
-
Save fbouynot/6979dffc0631c811cb07a9d2b857ac14 to your computer and use it in GitHub Desktop.
Upgrade Netbox to the latest stable version.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# This program is free software: you can redistribute it and/or modify it | |
# under the terms of the GNU General Public License as published by the | |
# Free Software Foundation, either version 3 of the License, or (at your | |
# option) any later version. Please see LICENSE.txt at the top level of | |
# the source code distribution for details. | |
# | |
# @package netbox_upgrade.sh | |
# @author <[email protected]> | |
# @link https://gist.github.com/fbouynot/6979dffc0631c811cb07a9d2b857ac14 | |
# @copyright <[email protected]> | |
# | |
# Upgrade Netbox to the latest stable version. | |
# | |
# -e: When a command fails, bash exits instead of continuing with the rest of the script | |
# -u: This will make the script fail, when accessing an unset variable | |
# -o pipefail: This will ensure that a pipeline command is treated as failed, even if one command in the pipeline fails | |
set -euo pipefail | |
# Enable debug mode by running your script as TRACE=1 ./script.sh instead of ./script.sh | |
if [[ "${TRACE-0}" == "1" ]] | |
then | |
set -o xtrace | |
fi | |
# Define constants | |
readonly PROGNAME="${0##*/}" | |
readonly VERSION='1.0.1' | |
# Help function: print the help message | |
help() { | |
cat << EOF | |
Usage: ${PROGNAME} [-Vh] | |
Upgrade Netbox to the latest stable version. | |
Options: | |
-h --help Print this message and exit | |
-V --version Print the version and exit | |
EOF | |
exit 2 | |
} | |
# Version function: print the version and license | |
version() { | |
cat << EOF | |
${PROGNAME} version ${VERSION} under GPLv3 licence. | |
EOF | |
exit 2 | |
} | |
# Deal with arguments | |
while [[ $# -gt 0 ]] | |
do | |
key="${1}" | |
case "${key}" in | |
-h|--help) | |
help | |
;; | |
-V|--version) | |
version | |
;; | |
*) | |
;; | |
esac | |
shift # consume $1 | |
done | |
# Change directory to base script directory | |
cd "$(dirname "$0")" | |
# Main function where we can write code | |
main() { | |
# Get current Netbox version | |
local netbox_version | |
netbox_version=$(grep VERSION /opt/netbox/netbox/netbox/settings.py | head -n 1 | cut -d "'" -f 2) | |
# Get latest Netbox version | |
curl https://api.github.com/repos/netbox-community/netbox/releases -s > releases.json | |
local release_index=0 | |
while [[ ($(jq -r ".[${release_index}].draft" < releases.json) == true) || ($(jq -r ".[${release_index}].prerelease" < releases.json) == true) ]] | |
do | |
((++release_index)) | |
done | |
local netbox_latest | |
netbox_latest=$(jq -r ".[${release_index}].tag_name" < releases.json | cut -c2-) | |
rm -f releases.json | |
if [[ "${netbox_version}" != "${netbox_latest}" ]] | |
then | |
echo "Upgrading Netbox v${netbox_version} to Netbox v${netbox_latest}: " | |
# Download latest Netbox version | |
wget https://github.com/netbox-community/netbox/archive/v"${netbox_latest}".tar.gz | |
# Extract in /opt | |
tar -xzf v"${netbox_latest}".tar.gz -C /opt | |
rm -f v"${netbox_latest}".tar.gz | |
# Symoblic link of /opt/netbox with the new folder | |
ln -sfn /opt/netbox-"${netbox_latest}"/ /opt/netbox | |
# Copy files to keep | |
cp /opt/netbox-"${netbox_version}"/local_requirements.txt /opt/netbox/ | |
cp /opt/netbox-"${netbox_version}"/netbox/netbox/configuration.py /opt/netbox/netbox/netbox/ | |
cp -prf /opt/netbox-"${netbox_version}"/netbox/media/ /opt/netbox/netbox/ | |
cp -rf /opt/netbox-"${netbox_version}"/netbox/scripts /opt/netbox/netbox/ | |
cp -rf /opt/netbox-"${netbox_version}"/netbox/reports /opt/netbox/netbox/ | |
cp -f /opt/netbox-"${netbox_version}"/gunicorn.py /opt/netbox/ | |
cp /opt/netbox-"${netbox_version}"/netbox/netbox/samlgetgroups.py /opt/netbox/netbox/netbox/ | |
# Change setting.py for SAML | |
sed -i -r "/^ 'social_core.pipeline.user.user_details',/a\ \ \ \ 'netbox.samlgetgroups.set_role'," /opt/netbox/netbox/netbox/settings.py | |
cd /opt/netbox | |
./upgrade.sh | |
echo "systemctl restart netbox netbox-rq" | |
systemctl restart netbox netbox-rq | |
# Remove oldest versions, keep the latest 4 | |
ls -dt /opt/netbox-* | tail -n +5 | xargs rm -rf | |
echo "Services restart done." | |
echo "Upgrade done." | |
else | |
echo "Netbox is up to date (v${netbox_version})" | |
fi | |
exit 0 | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment