Skip to content

Commit

Permalink
Introduce $SKIP_SUPERUSER
Browse files Browse the repository at this point in the history
This adds a new variable to skip the creation of the superuser.
That is useful for LDAP and for production environments.

Fixes #160
  • Loading branch information
cimnine committed Oct 12, 2019
1 parent 5ae5977 commit 20c234a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
53 changes: 29 additions & 24 deletions docker/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,43 @@ while ! ./manage.py migrate 2>&1; do
sleep 3
done

# create superuser silently
if [ -z ${SUPERUSER_NAME+x} ]; then
SUPERUSER_NAME='admin'
fi
if [ -z ${SUPERUSER_EMAIL+x} ]; then
SUPERUSER_EMAIL='[email protected]'
fi
if [ -z ${SUPERUSER_PASSWORD+x} ]; then
if [ -f "/run/secrets/superuser_password" ]; then
SUPERUSER_PASSWORD="$(< /run/secrets/superuser_password)"
else
SUPERUSER_PASSWORD='admin'
if [ "$SKIP_SUPERUSER" == "true" ]; then
echo "↩️ Skipp creating the superuser"
else
if [ -z ${SUPERUSER_NAME+x} ]; then
SUPERUSER_NAME='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'
if [ -z ${SUPERUSER_EMAIL+x} ]; then
SUPERUSER_EMAIL='[email protected]'
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
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 users.models import Token
if not User.objects.filter(username='${SUPERUSER_NAME}'):
u=User.objects.create_superuser('${SUPERUSER_NAME}', '${SUPERUSER_EMAIL}', '${SUPERUSER_PASSWORD}')
Token.objects.create(user=u, key='${SUPERUSER_API_TOKEN}')
END

echo "💡 Superuser Username: ${SUPERUSER_NAME}, E-Mail: ${SUPERUSER_EMAIL}"
fi

if [ "$SKIP_STARTUP_SCRIPTS" == "true" ]; then
echo " Skipping startup scripts"
echo "↩️ Skipping startup scripts"
else
for script in /opt/netbox/startup_scripts/*.py; do
echo "⚙️ Executing '$script'"
Expand All @@ -55,4 +58,6 @@ echo "✅ Initialisation is done."

# launch whatever is passed by docker
# (i.e. the RUN instruction in the Dockerfile)
exec ${@}
#
# shellcheck disable=SC2068
exec $@
2 changes: 2 additions & 0 deletions env/netbox.env
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ REDIS_DATABASE=0
REDIS_CACHE_DATABASE=1
REDIS_SSL=false
SECRET_KEY=r8OwDznj!!dci#P9ghmRfdu1Ysxm0AiPeDCQhKE+N_rClfWNj
SKIP_STARTUP_SCRIPTS=false
SKIP_SUPERUSER=false
SUPERUSER_NAME=admin
SUPERUSER_EMAIL=[email protected]
SUPERUSER_PASSWORD=admin
Expand Down

0 comments on commit 20c234a

Please sign in to comment.