Introduce $SKIP_SUPERUSER

This adds a new variable to skip the creation of the superuser.
That is useful for LDAP and for production environments.

Fixes #160
This commit is contained in:
Christian Mäder 2019-10-12 14:45:55 +02:00
parent 5ae5977717
commit 20c234a96e
2 changed files with 31 additions and 24 deletions

View File

@ -7,31 +7,31 @@ while ! ./manage.py migrate 2>&1; do
sleep 3 sleep 3
done done
# create superuser silently if [ "$SKIP_SUPERUSER" == "true" ]; then
if [ -z ${SUPERUSER_NAME+x} ]; then echo "↩️ Skipp creating the superuser"
SUPERUSER_NAME='admin' else
fi if [ -z ${SUPERUSER_NAME+x} ]; then
if [ -z ${SUPERUSER_EMAIL+x} ]; then SUPERUSER_NAME='admin'
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
fi if [ -z ${SUPERUSER_EMAIL+x} ]; then
if [ -z ${SUPERUSER_API_TOKEN+x} ]; then SUPERUSER_EMAIL='admin@example.com'
if [ -f "/run/secrets/superuser_api_token" ]; then fi
SUPERUSER_API_TOKEN="$(< /run/secrets/superuser_api_token)" if [ -z ${SUPERUSER_PASSWORD+x} ]; then
else if [ -f "/run/secrets/superuser_password" ]; then
SUPERUSER_API_TOKEN='0123456789abcdef0123456789abcdef01234567' 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
fi fi
fi
echo "💡 Username: ${SUPERUSER_NAME}, E-Mail: ${SUPERUSER_EMAIL}" ./manage.py shell --interface python << END
./manage.py shell --interface python << END
from django.contrib.auth.models import User from django.contrib.auth.models import User
from users.models import Token from users.models import Token
if not User.objects.filter(username='${SUPERUSER_NAME}'): if not User.objects.filter(username='${SUPERUSER_NAME}'):
@ -39,8 +39,11 @@ if not User.objects.filter(username='${SUPERUSER_NAME}'):
Token.objects.create(user=u, key='${SUPERUSER_API_TOKEN}') Token.objects.create(user=u, key='${SUPERUSER_API_TOKEN}')
END END
echo "💡 Superuser Username: ${SUPERUSER_NAME}, E-Mail: ${SUPERUSER_EMAIL}"
fi
if [ "$SKIP_STARTUP_SCRIPTS" == "true" ]; then if [ "$SKIP_STARTUP_SCRIPTS" == "true" ]; then
echo "☇ Skipping startup scripts" echo "↩️ Skipping startup scripts"
else else
for script in /opt/netbox/startup_scripts/*.py; do for script in /opt/netbox/startup_scripts/*.py; do
echo "⚙️ Executing '$script'" echo "⚙️ Executing '$script'"
@ -55,4 +58,6 @@ echo "✅ Initialisation is done."
# launch whatever is passed by docker # launch whatever is passed by docker
# (i.e. the RUN instruction in the Dockerfile) # (i.e. the RUN instruction in the Dockerfile)
exec ${@} #
# shellcheck disable=SC2068
exec $@

2
env/netbox.env vendored
View File

@ -20,6 +20,8 @@ REDIS_DATABASE=0
REDIS_CACHE_DATABASE=1 REDIS_CACHE_DATABASE=1
REDIS_SSL=false REDIS_SSL=false
SECRET_KEY=r8OwDznj!!dci#P9ghmRfdu1Ysxm0AiPeDCQhKE+N_rClfWNj SECRET_KEY=r8OwDznj!!dci#P9ghmRfdu1Ysxm0AiPeDCQhKE+N_rClfWNj
SKIP_STARTUP_SCRIPTS=false
SKIP_SUPERUSER=false
SUPERUSER_NAME=admin SUPERUSER_NAME=admin
SUPERUSER_EMAIL=admin@example.com SUPERUSER_EMAIL=admin@example.com
SUPERUSER_PASSWORD=admin SUPERUSER_PASSWORD=admin