From 20c234a96eb80e3c337c777e1909849536f56c6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=A4der?= Date: Sat, 12 Oct 2019 14:45:55 +0200 Subject: [PATCH 1/2] 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 --- docker/docker-entrypoint.sh | 53 ++++++++++++++++++++----------------- env/netbox.env | 2 ++ 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh index 8922a5d..effedee 100755 --- a/docker/docker-entrypoint.sh +++ b/docker/docker-entrypoint.sh @@ -7,31 +7,31 @@ 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='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' +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='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 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}'): @@ -39,8 +39,11 @@ if not User.objects.filter(username='${SUPERUSER_NAME}'): 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'" @@ -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 $@ diff --git a/env/netbox.env b/env/netbox.env index 0b02958..4a75905 100644 --- a/env/netbox.env +++ b/env/netbox.env @@ -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=admin@example.com SUPERUSER_PASSWORD=admin From 79d349133e8ae62b11bf618480d62303543946d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Sun, 13 Oct 2019 14:03:22 +0200 Subject: [PATCH 2/2] fix typo --- docker/docker-entrypoint.sh | 2 +- env/netbox.env | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh index effedee..2e6a8de 100755 --- a/docker/docker-entrypoint.sh +++ b/docker/docker-entrypoint.sh @@ -8,7 +8,7 @@ while ! ./manage.py migrate 2>&1; do done if [ "$SKIP_SUPERUSER" == "true" ]; then - echo "↩️ Skipp creating the superuser" + echo "↩️ Skip creating the superuser" else if [ -z ${SUPERUSER_NAME+x} ]; then SUPERUSER_NAME='admin' diff --git a/env/netbox.env b/env/netbox.env index 4a75905..8f1f69b 100644 --- a/env/netbox.env +++ b/env/netbox.env @@ -21,8 +21,8 @@ REDIS_CACHE_DATABASE=1 REDIS_SSL=false SECRET_KEY=r8OwDznj!!dci#P9ghmRfdu1Ysxm0AiPeDCQhKE+N_rClfWNj SKIP_STARTUP_SCRIPTS=false -SKIP_SUPERUSER=false -SUPERUSER_NAME=admin +SKIP_SUPERUSER=true +SUPERUSER_NAME=admin2 SUPERUSER_EMAIL=admin@example.com SUPERUSER_PASSWORD=admin SUPERUSER_API_TOKEN=0123456789abcdef0123456789abcdef01234567