#!/bin/bash # Runs on every start of the NetBox Docker container # Stop when an error occures set -e # Allows NetBox to be run as non-root users umask 002 # Load correct Python3 env # shellcheck disable=SC1091 source /opt/netbox/venv/bin/activate # Try to connect to the DB DB_WAIT_TIMEOUT=${DB_WAIT_TIMEOUT-3} MAX_DB_WAIT_TIME=${MAX_DB_WAIT_TIME-30} CUR_DB_WAIT_TIME=0 while ! /opt/netbox/netbox/manage.py showmigrations >/dev/null 2>&1 && [ "${CUR_DB_WAIT_TIME}" -lt "${MAX_DB_WAIT_TIME}" ]; do echo "⏳ Waiting on DB... (${CUR_DB_WAIT_TIME}s / ${MAX_DB_WAIT_TIME}s)" sleep "${DB_WAIT_TIMEOUT}" CUR_DB_WAIT_TIME=$((CUR_DB_WAIT_TIME + DB_WAIT_TIMEOUT)) done if [ "${CUR_DB_WAIT_TIME}" -ge "${MAX_DB_WAIT_TIME}" ]; then echo "❌ Waited ${MAX_DB_WAIT_TIME}s or more for the DB to become ready." exit 1 fi # Check if update is needed if ! /opt/netbox/netbox/manage.py migrate --check >/dev/null 2>&1; then echo "⚙️ Applying database migrations" /opt/netbox/netbox/manage.py migrate --no-input echo "⚙️ Running trace_paths" /opt/netbox/netbox/manage.py trace_paths --no-input echo "⚙️ Removing stale content types" /opt/netbox/netbox/manage.py remove_stale_contenttypes --no-input echo "⚙️ Removing expired user sessions" /opt/netbox/netbox/manage.py clearsessions fi # Create Superuser if required if [ "$SKIP_SUPERUSER" == "true" ]; then echo "↩️ Skip creating the superuser" else if [ -z ${SUPERUSER_NAME+x} ]; then SUPERUSER_NAME='admin' fi if [ -z ${SUPERUSER_EMAIL+x} ]; then SUPERUSER_EMAIL='admin@example.com' fi if [ -f "/run/secrets/superuser_password" ]; then SUPERUSER_PASSWORD="$(