2017-04-19 16:48:21 +02:00
|
|
|
#!/bin/bash
|
2021-02-04 21:48:08 +01:00
|
|
|
# Runs on every start of the NetBox Docker container
|
2019-12-23 17:53:19 +01:00
|
|
|
|
|
|
|
# Stop when an error occures
|
2017-04-19 16:48:21 +02:00
|
|
|
set -e
|
2019-12-23 17:53:19 +01:00
|
|
|
|
2021-02-04 21:48:08 +01:00
|
|
|
# Allows NetBox to be run as non-root users
|
2019-11-26 12:09:26 +01:00
|
|
|
umask 002
|
2017-04-19 16:48:21 +02:00
|
|
|
|
2020-11-10 15:23:07 +01:00
|
|
|
# Load correct Python3 env
|
2021-02-08 12:04:22 +01:00
|
|
|
# shellcheck disable=SC1091
|
2020-11-10 15:23:07 +01:00
|
|
|
source /opt/netbox/venv/bin/activate
|
|
|
|
|
2019-12-23 17:53:19 +01:00
|
|
|
# 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
|
2021-09-02 16:35:39 +02:00
|
|
|
while [ "${CUR_DB_WAIT_TIME}" -lt "${MAX_DB_WAIT_TIME}" ]; do
|
|
|
|
# Read and truncate connection error tracebacks to last line by default
|
|
|
|
exec {psfd}< <(./manage.py showmigrations 2>&1)
|
|
|
|
read -rd '' DB_ERR <&$psfd || :
|
|
|
|
exec {psfd}<&-
|
|
|
|
wait $! && break
|
|
|
|
if [ -n "$DB_WAIT_DEBUG" ]; then
|
|
|
|
echo "$DB_ERR"
|
|
|
|
else
|
|
|
|
readarray -tn 0 DB_ERR_LINES <<<"$DB_ERR"
|
|
|
|
echo "${DB_ERR_LINES[@]: -1}"
|
|
|
|
echo "[ Use DB_WAIT_DEBUG=1 in netbox.env to print full traceback for errors here ]"
|
|
|
|
fi
|
2019-12-23 17:53:19 +01:00
|
|
|
echo "⏳ Waiting on DB... (${CUR_DB_WAIT_TIME}s / ${MAX_DB_WAIT_TIME}s)"
|
|
|
|
sleep "${DB_WAIT_TIMEOUT}"
|
2021-02-08 12:16:04 +01:00
|
|
|
CUR_DB_WAIT_TIME=$((CUR_DB_WAIT_TIME + DB_WAIT_TIMEOUT))
|
2017-04-19 16:48:21 +02:00
|
|
|
done
|
2019-12-23 17:53:19 +01:00
|
|
|
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
|
2021-05-05 11:31:32 +02:00
|
|
|
# Check if update is needed
|
|
|
|
if ! ./manage.py migrate --check >/dev/null 2>&1; then
|
|
|
|
echo "⚙️ Applying database migrations"
|
|
|
|
./manage.py migrate --no-input
|
|
|
|
echo "⚙️ Running trace_paths"
|
|
|
|
./manage.py trace_paths --no-input
|
|
|
|
echo "⚙️ Removing stale content types"
|
|
|
|
./manage.py remove_stale_contenttypes --no-input
|
|
|
|
echo "⚙️ Removing expired user sessions"
|
|
|
|
./manage.py clearsessions
|
|
|
|
fi
|
2017-04-19 16:48:21 +02:00
|
|
|
|
2019-12-23 17:53:19 +01:00
|
|
|
# Create Superuser if required
|
2019-10-12 14:45:55 +02:00
|
|
|
if [ "$SKIP_SUPERUSER" == "true" ]; then
|
2019-10-13 14:03:22 +02:00
|
|
|
echo "↩️ Skip creating the superuser"
|
2019-10-12 14:45:55 +02:00
|
|
|
else
|
|
|
|
if [ -z ${SUPERUSER_NAME+x} ]; then
|
|
|
|
SUPERUSER_NAME='admin'
|
2017-12-13 15:50:30 +01:00
|
|
|
fi
|
2019-10-12 14:45:55 +02:00
|
|
|
if [ -z ${SUPERUSER_EMAIL+x} ]; then
|
|
|
|
SUPERUSER_EMAIL='admin@example.com'
|
|
|
|
fi
|
2020-05-13 14:44:41 +02:00
|
|
|
if [ -f "/run/secrets/superuser_password" ]; then
|
2021-02-08 12:16:04 +01:00
|
|
|
SUPERUSER_PASSWORD="$(</run/secrets/superuser_password)"
|
2020-05-13 14:44:41 +02:00
|
|
|
elif [ -z ${SUPERUSER_PASSWORD+x} ]; then
|
|
|
|
SUPERUSER_PASSWORD='admin'
|
2019-10-12 14:45:55 +02:00
|
|
|
fi
|
2020-05-13 14:44:41 +02:00
|
|
|
if [ -f "/run/secrets/superuser_api_token" ]; then
|
2021-02-08 12:16:04 +01:00
|
|
|
SUPERUSER_API_TOKEN="$(</run/secrets/superuser_api_token)"
|
2020-05-13 14:44:41 +02:00
|
|
|
elif [ -z ${SUPERUSER_API_TOKEN+x} ]; then
|
|
|
|
SUPERUSER_API_TOKEN='0123456789abcdef0123456789abcdef01234567'
|
2017-12-13 15:50:30 +01:00
|
|
|
fi
|
2017-04-21 13:43:44 +02:00
|
|
|
|
2021-02-08 12:16:04 +01:00
|
|
|
./manage.py shell --interface python <<END
|
2017-04-21 13:43:44 +02:00
|
|
|
from django.contrib.auth.models import User
|
2017-11-29 15:08:55 +01:00
|
|
|
from users.models import Token
|
2017-04-21 13:43:44 +02:00
|
|
|
if not User.objects.filter(username='${SUPERUSER_NAME}'):
|
2017-11-29 15:08:55 +01:00
|
|
|
u=User.objects.create_superuser('${SUPERUSER_NAME}', '${SUPERUSER_EMAIL}', '${SUPERUSER_PASSWORD}')
|
|
|
|
Token.objects.create(user=u, key='${SUPERUSER_API_TOKEN}')
|
2017-04-21 13:43:44 +02:00
|
|
|
END
|
2017-04-19 16:48:21 +02:00
|
|
|
|
2019-10-12 14:45:55 +02:00
|
|
|
echo "💡 Superuser Username: ${SUPERUSER_NAME}, E-Mail: ${SUPERUSER_EMAIL}"
|
|
|
|
fi
|
|
|
|
|
2022-08-09 08:18:20 +02:00
|
|
|
# Print warning if startup scripts (and initializers) would've been run # Remove for next release
|
2019-02-28 09:20:10 +01:00
|
|
|
if [ "$SKIP_STARTUP_SCRIPTS" == "true" ]; then
|
2022-08-09 08:18:20 +02:00
|
|
|
# Nothing to do
|
|
|
|
echo "" # Empty block not allowed
|
2019-02-28 09:20:10 +01:00
|
|
|
else
|
2022-08-09 08:18:20 +02:00
|
|
|
echo "⚠️⚠️⚠️ WARNING: The initializers have been moved to a plugin. See release notes."
|
|
|
|
echo "⚠️⚠️⚠️ Set environment variable 'SKIP_STARTUP_SCRIPTS' to 'true' to remove this warning."
|
2019-02-28 09:20:10 +01:00
|
|
|
fi
|
2018-01-24 09:21:55 +01:00
|
|
|
|
2017-11-29 15:08:55 +01:00
|
|
|
echo "✅ Initialisation is done."
|
2017-08-30 11:09:56 +02:00
|
|
|
|
2019-12-23 17:53:19 +01:00
|
|
|
# Launch whatever is passed by docker
|
2018-02-16 10:25:26 +01:00
|
|
|
# (i.e. the RUN instruction in the Dockerfile)
|
2021-04-15 18:18:00 +02:00
|
|
|
exec "$@"
|