2017-04-19 09:48:21 -05:00
|
|
|
#!/bin/bash
|
2019-12-23 10:53:19 -06:00
|
|
|
# Runs on every start of the Netbox Docker container
|
|
|
|
|
|
|
|
# Stop when an error occures
|
2017-04-19 09:48:21 -05:00
|
|
|
set -e
|
2019-12-23 10:53:19 -06:00
|
|
|
|
|
|
|
# Allows Netbox to be run as non-root users
|
2019-11-26 05:09:26 -06:00
|
|
|
umask 002
|
2017-04-19 09:48:21 -05:00
|
|
|
|
2019-12-23 10:53:19 -06: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
|
|
|
|
while ! ./manage.py migrate 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 ))
|
2017-04-19 09:48:21 -05:00
|
|
|
done
|
2019-12-23 10:53:19 -06: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
|
2017-04-19 09:48:21 -05:00
|
|
|
|
2019-12-23 10:53:19 -06:00
|
|
|
# Create Superuser if required
|
2019-10-12 07:45:55 -05:00
|
|
|
if [ "$SKIP_SUPERUSER" == "true" ]; then
|
2019-10-13 07:03:22 -05:00
|
|
|
echo "↩️ Skip creating the superuser"
|
2019-10-12 07:45:55 -05:00
|
|
|
else
|
|
|
|
if [ -z ${SUPERUSER_NAME+x} ]; then
|
|
|
|
SUPERUSER_NAME='admin'
|
2017-12-13 08:50:30 -06:00
|
|
|
fi
|
2019-10-12 07:45:55 -05:00
|
|
|
if [ -z ${SUPERUSER_EMAIL+x} ]; then
|
|
|
|
SUPERUSER_EMAIL='admin@example.com'
|
|
|
|
fi
|
|
|
|
if [ -z ${SUPERUSER_PASSWORD+x} ]; then
|
|
|
|
if [ -f "/run/secrets/superuser_password" ]; then
|
|
|
|
SUPERUSER_PASSWORD="$(< /run/secrets/superuser_password)"
|
|
|
|
else
|
|
|
|
SUPERUSER_PASSWORD='admin'
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if [ -z ${SUPERUSER_API_TOKEN+x} ]; then
|
|
|
|
if [ -f "/run/secrets/superuser_api_token" ]; then
|
|
|
|
SUPERUSER_API_TOKEN="$(< /run/secrets/superuser_api_token)"
|
|
|
|
else
|
|
|
|
SUPERUSER_API_TOKEN='0123456789abcdef0123456789abcdef01234567'
|
|
|
|
fi
|
2017-12-13 08:50:30 -06:00
|
|
|
fi
|
2017-04-21 06:43:44 -05:00
|
|
|
|
2019-10-12 07:45:55 -05:00
|
|
|
./manage.py shell --interface python << END
|
2017-04-21 06:43:44 -05:00
|
|
|
from django.contrib.auth.models import User
|
2017-11-29 08:08:55 -06:00
|
|
|
from users.models import Token
|
2017-04-21 06:43:44 -05:00
|
|
|
if not User.objects.filter(username='${SUPERUSER_NAME}'):
|
2017-11-29 08:08:55 -06:00
|
|
|
u=User.objects.create_superuser('${SUPERUSER_NAME}', '${SUPERUSER_EMAIL}', '${SUPERUSER_PASSWORD}')
|
|
|
|
Token.objects.create(user=u, key='${SUPERUSER_API_TOKEN}')
|
2017-04-21 06:43:44 -05:00
|
|
|
END
|
2017-04-19 09:48:21 -05:00
|
|
|
|
2019-10-12 07:45:55 -05:00
|
|
|
echo "💡 Superuser Username: ${SUPERUSER_NAME}, E-Mail: ${SUPERUSER_EMAIL}"
|
|
|
|
fi
|
|
|
|
|
2019-12-23 10:53:19 -06:00
|
|
|
# Run the startup scripts (and initializers)
|
2019-02-28 02:20:10 -06:00
|
|
|
if [ "$SKIP_STARTUP_SCRIPTS" == "true" ]; then
|
2019-10-12 07:45:55 -05:00
|
|
|
echo "↩️ Skipping startup scripts"
|
2019-02-28 02:20:10 -06:00
|
|
|
else
|
2019-12-14 11:16:31 -06:00
|
|
|
echo "import runpy; runpy.run_path('../startup_scripts')" | ./manage.py shell --interface python
|
2019-02-28 02:20:10 -06:00
|
|
|
fi
|
2018-01-24 02:21:55 -06:00
|
|
|
|
2019-12-23 10:53:19 -06:00
|
|
|
# Copy static files
|
2017-08-30 04:09:24 -05:00
|
|
|
./manage.py collectstatic --no-input
|
2017-04-19 09:48:21 -05:00
|
|
|
|
2017-11-29 08:08:55 -06:00
|
|
|
echo "✅ Initialisation is done."
|
2017-08-30 04:09:56 -05:00
|
|
|
|
2019-12-23 10:53:19 -06:00
|
|
|
# Launch whatever is passed by docker
|
2018-02-16 03:25:26 -06:00
|
|
|
# (i.e. the RUN instruction in the Dockerfile)
|
2019-10-12 07:45:55 -05:00
|
|
|
#
|
|
|
|
# shellcheck disable=SC2068
|
|
|
|
exec $@
|