2017-04-19 14:48:21 +00:00
|
|
|
#!/bin/bash
|
2021-02-04 20:48:08 +00:00
|
|
|
# Runs on every start of the NetBox Docker container
|
2019-12-23 16:53:19 +00:00
|
|
|
|
|
|
|
|
# Stop when an error occures
|
2017-04-19 14:48:21 +00:00
|
|
|
set -e
|
2019-12-23 16:53:19 +00:00
|
|
|
|
2021-02-04 20:48:08 +00:00
|
|
|
# Allows NetBox to be run as non-root users
|
2019-11-26 11:09:26 +00:00
|
|
|
umask 002
|
2017-04-19 14:48:21 +00:00
|
|
|
|
2020-11-10 14:23:07 +00:00
|
|
|
# Load correct Python3 env
|
2021-02-08 11:04:22 +00:00
|
|
|
# shellcheck disable=SC1091
|
2020-11-10 14:23:07 +00:00
|
|
|
source /opt/netbox/venv/bin/activate
|
|
|
|
|
|
2019-12-23 16:53:19 +00: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 14:35:39 +00: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 16:53:19 +00:00
|
|
|
echo "⏳ Waiting on DB... (${CUR_DB_WAIT_TIME}s / ${MAX_DB_WAIT_TIME}s)"
|
|
|
|
|
sleep "${DB_WAIT_TIMEOUT}"
|
2021-02-08 11:16:04 +00:00
|
|
|
CUR_DB_WAIT_TIME=$((CUR_DB_WAIT_TIME + DB_WAIT_TIMEOUT))
|
2017-04-19 14:48:21 +00:00
|
|
|
done
|
2019-12-23 16:53:19 +00: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 09:31:32 +00: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
|
2023-02-23 07:37:53 +00:00
|
|
|
echo "⚙️ Building search index (lazy)"
|
|
|
|
|
./manage.py reindex --lazy
|
2021-05-05 09:31:32 +00:00
|
|
|
fi
|
2017-04-19 14:48:21 +00:00
|
|
|
|
2019-12-23 16:53:19 +00:00
|
|
|
# Create Superuser if required
|
2019-10-12 12:45:55 +00:00
|
|
|
if [ "$SKIP_SUPERUSER" == "true" ]; then
|
2019-10-13 12:03:22 +00:00
|
|
|
echo "↩️ Skip creating the superuser"
|
2019-10-12 12:45:55 +00:00
|
|
|
else
|
2026-01-08 07:14:59 +00:00
|
|
|
./manage.py shell --no-startup --no-imports --interface python \
|
2026-01-08 08:10:05 +00:00
|
|
|
</opt/netbox/super_user.py
|
2019-10-12 12:45:55 +00:00
|
|
|
fi
|
|
|
|
|
|
2017-11-29 14:08:55 +00:00
|
|
|
echo "✅ Initialisation is done."
|
2017-08-30 09:09:56 +00:00
|
|
|
|
2019-12-23 16:53:19 +00:00
|
|
|
# Launch whatever is passed by docker
|
2018-02-16 09:25:26 +00:00
|
|
|
# (i.e. the RUN instruction in the Dockerfile)
|
2021-04-15 16:18:00 +00:00
|
|
|
exec "$@"
|