Netbox: Refactor (#11126)

* refactor install script

* refactor update script
This commit is contained in:
Chris
2026-01-24 17:14:55 -05:00
committed by GitHub
parent ebcbe90c99
commit d6b4cea3ba
2 changed files with 22 additions and 57 deletions

View File

@@ -28,46 +28,30 @@ function update_script() {
exit exit
fi fi
RELEASE=$(curl -fsSL https://api.github.com/repos/netbox-community/netbox/releases/latest | grep "tag_name" | awk '{print substr($2, 3, length($2)-4) }') if check_for_gh_release "netbox" "netbox-community/netbox"; then
if [[ ! -f /opt/${APP}_version.txt ]] || [[ "${RELEASE}" != "$(cat /opt/${APP}_version.txt)" ]]; then msg_info "Stopping Services"
msg_info "Stopping Service"
systemctl stop netbox netbox-rq systemctl stop netbox netbox-rq
msg_ok "Stopped Service" msg_ok "Stopped Services"
msg_info "Updating $APP to v${RELEASE}" msg_info "Backing up NetBox configurations"
mv /opt/netbox/ /opt/netbox-backup mv /opt/netbox/ /opt/netbox-backup
cd /opt msg_ok "Backed up NetBox configurations"
curl -fsSL "https://github.com/netbox-community/netbox/archive/refs/tags/v${RELEASE}.zip" -o $(basename "https://github.com/netbox-community/netbox/archive/refs/tags/v${RELEASE}.zip")
$STD unzip "v${RELEASE}.zip" CLEAN_INSTALL=1 fetch_and_deploy_gh_release "netbox" "netbox-community/netbox" "tarball"
mv /opt/netbox-${RELEASE}/ /opt/netbox/
cp -r /opt/netbox-backup/netbox/netbox/configuration.py /opt/netbox/netbox/netbox/ cp -r /opt/netbox-backup/netbox/netbox/configuration.py /opt/netbox/netbox/netbox/
cp -r /opt/netbox-backup/netbox/media/ /opt/netbox/netbox/ cp -r /opt/netbox-backup/netbox/{media,scripts,reports}/ /opt/netbox/netbox/
cp -r /opt/netbox-backup/netbox/scripts /opt/netbox/netbox/
cp -r /opt/netbox-backup/netbox/reports /opt/netbox/netbox/
cp -r /opt/netbox-backup/gunicorn.py /opt/netbox/ cp -r /opt/netbox-backup/gunicorn.py /opt/netbox/
[[ -f /opt/netbox-backup/local_requirements.txt ]] && cp -r /opt/netbox-backup/local_requirements.txt /opt/netbox/
if [ -f /opt/netbox-backup/local_requirements.txt ]; then [[ -f /opt/netbox-backup/netbox/netbox/ldap_config.py ]] && cp -r /opt/netbox-backup/netbox/netbox/ldap_config.py /opt/netbox/netbox/netbox/
cp -r /opt/netbox-backup/local_requirements.txt /opt/netbox/
fi
if [ -f /opt/netbox-backup/netbox/netbox/ldap_config.py ]; then
cp -r /opt/netbox-backup/netbox/netbox/ldap_config.py /opt/netbox/netbox/netbox/
fi
$STD /opt/netbox/upgrade.sh $STD /opt/netbox/upgrade.sh
rm -r "/opt/v${RELEASE}.zip"
rm -r /opt/netbox-backup rm -r /opt/netbox-backup
echo "${RELEASE}" >/opt/${APP}_version.txt
msg_ok "Updated $APP to v${RELEASE}"
msg_info "Starting Service" msg_info "Starting Services"
systemctl start netbox netbox-rq systemctl start netbox netbox-rq
msg_ok "Started Service" msg_ok "Started Services"
msg_ok "Updated successfully!" msg_ok "Updated successfully!"
else
msg_ok "No update required. ${APP} is already at v${RELEASE}"
fi fi
exit exit
} }

View File

@@ -27,6 +27,7 @@ $STD apt install -y \
msg_ok "Installed Dependencies" msg_ok "Installed Dependencies"
PG_VERSION="16" setup_postgresql PG_VERSION="16" setup_postgresql
PG_DB_NAME="netbox" PG_DB_USER="netbox" setup_postgresql_db
msg_info "Installing Python" msg_info "Installing Python"
$STD apt install -y \ $STD apt install -y \
@@ -36,42 +37,24 @@ $STD apt install -y \
python3-dev python3-dev
msg_ok "Installed Python" msg_ok "Installed Python"
msg_info "Setting up PostgreSQL" fetch_and_deploy_gh_release "netbox" "netbox-community/netbox" "tarball"
DB_NAME=netbox
DB_USER=netbox
DB_PASS=$(openssl rand -base64 18 | tr -dc 'a-zA-Z0-9' | cut -c1-13)
$STD sudo -u postgres psql -c "CREATE ROLE $DB_USER WITH LOGIN PASSWORD '$DB_PASS';"
$STD sudo -u postgres psql -c "CREATE DATABASE $DB_NAME WITH OWNER $DB_USER TEMPLATE template0;"
{
echo "Netbox-Credentials"
echo -e "Netbox Database User: \e[32m$DB_USER\e[0m"
echo -e "Netbox Database Password: \e[32m$DB_PASS\e[0m"
echo -e "Netbox Database Name: \e[32m$DB_NAME\e[0m"
} >>~/netbox.creds
msg_ok "Set up PostgreSQL"
msg_info "Installing NetBox (Patience)" msg_info "Configuring NetBox (Patience)"
cd /opt cd /opt/netbox
RELEASE=$(curl -fsSL https://api.github.com/repos/netbox-community/netbox/releases/latest | grep "tag_name" | awk '{print substr($2, 3, length($2)-4) }')
curl -fsSL "https://github.com/netbox-community/netbox/archive/refs/tags/v${RELEASE}.zip" -o "v${RELEASE}.zip"
$STD unzip "v${RELEASE}.zip"
mv /opt/netbox-"${RELEASE}"/ /opt/netbox
mkdir -p /opt/netbox/netbox/media mkdir -p /opt/netbox/netbox/media
$STD adduser --system --group netbox $STD adduser --system --group netbox
chown --recursive netbox /opt/netbox/netbox/media/ chown -R netbox /opt/netbox/netbox
chown --recursive netbox /opt/netbox/netbox/reports/
chown --recursive netbox /opt/netbox/netbox/scripts/
mv /opt/netbox/netbox/netbox/configuration_example.py /opt/netbox/netbox/netbox/configuration.py mv /opt/netbox/netbox/netbox/configuration_example.py /opt/netbox/netbox/netbox/configuration.py
SECRET_KEY=$(python3 /opt/netbox/netbox/generate_secret_key.py) SECRET_KEY=$(python3 /opt/netbox/netbox/generate_secret_key.py)
ESCAPED_SECRET_KEY=$(printf '%s\n' "$SECRET_KEY" | sed 's/[&/\]/\\&/g') ESCAPED_SECRET_KEY=$(printf '%s\n' "$SECRET_KEY" | sed 's/[&/\]/\\&/g')
sed -i 's/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = ["*"]/' /opt/netbox/netbox/netbox/configuration.py sed -i -e 's/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = ["*"]/' \
sed -i "s|SECRET_KEY = ''|SECRET_KEY = '${ESCAPED_SECRET_KEY}'|" /opt/netbox/netbox/netbox/configuration.py -e "s|SECRET_KEY = ''|SECRET_KEY = '${ESCAPED_SECRET_KEY}'|" \
sed -i "/DATABASES = {/,/}/s/'USER': '[^']*'/'USER': '$DB_USER'/" /opt/netbox/netbox/netbox/configuration.py -e "/DATABASES = {/,/}/s/'USER': '[^']*'/'USER': '$PG_DB_USER'/" \
sed -i "/DATABASES = {/,/}/s/'PASSWORD': '[^']*'/'PASSWORD': '$DB_PASS'/" /opt/netbox/netbox/netbox/configuration.py -e "/DATABASES = {/,/}/s/'PASSWORD': '[^']*'/'PASSWORD': '$PG_DB_PASS'/" /opt/netbox/netbox/netbox/configuration.py
$STD /opt/netbox/upgrade.sh $STD /opt/netbox/upgrade.sh
ln -s /opt/netbox/contrib/netbox-housekeeping.sh /etc/cron.daily/netbox-housekeeping ln -s /opt/netbox/contrib/netbox-housekeeping.sh /etc/cron.daily/netbox-housekeeping
@@ -86,10 +69,8 @@ mv /opt/netbox/contrib/gunicorn.py /opt/netbox/gunicorn.py
mv /opt/netbox/contrib/*.service /etc/systemd/system/ mv /opt/netbox/contrib/*.service /etc/systemd/system/
systemctl daemon-reload systemctl daemon-reload
systemctl enable -q --now netbox netbox-rq systemctl enable -q --now netbox netbox-rq
rm "/opt/v${RELEASE}.zip"
echo "${RELEASE}" >/opt/"${APPLICATION}"_version.txt
echo -e "Netbox Secret: \e[32m$SECRET_KEY\e[0m" >>~/netbox.creds echo -e "Netbox Secret: \e[32m$SECRET_KEY\e[0m" >>~/netbox.creds
msg_ok "Installed NetBox" msg_ok "Configured NetBox"
msg_info "Setting up Django Admin" msg_info "Setting up Django Admin"
DJANGO_USER=Admin DJANGO_USER=Admin