diff --git a/create_admin.py b/create_admin.py new file mode 100644 index 000000000..d534c6696 --- /dev/null +++ b/create_admin.py @@ -0,0 +1,33 @@ +import sys +import os +import django + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sapl.settings") +django.setup() + +from django.contrib.auth.models import User + +def create_superuser(): + username = "admin" + password = os.environ['ADMIN_PASSWORD'] if 'ADMIN_PASSWORD' in os.environ else None + email = os.environ['ADMIN_EMAIL'] if 'ADMIN_EMAIL' in os.environ else '' + + if User.objects.filter(username=username).exists(): + # print("[SUPERUSER] User %s already exists. Exiting without change." % username) + sys.exit('ADMIN_USER_EXISTS') + else: + if not password: + # print("[SUPERUSER] Environment variable $ADMIN_PASSWORD for user %s was not set. Leaving..." % username) + sys.exit('MISSING_ADMIN_PASSWORD') + + print("[SUPERUSER] Creating superuser...") + + u = User.objects.create_superuser(username=username, password=password, email=email) + u.save() + + print("[SUPERUSER] Done.") + + sys.exit(0) + +if __name__ == '__main__': + create_superuser() diff --git a/start.sh b/start.sh index 0774b0e02..f421efa99 100755 --- a/start.sh +++ b/start.sh @@ -2,6 +2,7 @@ create_env() { + echo "[ENV FILE] creating .env file..." # check if file exists if [ -f "/var/interlegis/sapl/data/secret.key" ]; then KEY=`cat /var/interlegis/sapl/data/secret.key` @@ -10,7 +11,6 @@ create_env() { echo $KEY > data/secret.key fi - # TODO: rename env-test-bash to .env FILENAME="/var/interlegis/sapl/sapl/.env" if [ -z "${DATABASE_URL:-}" ]; then @@ -34,11 +34,11 @@ create_env() { echo "EMAIL_HOST = ""${EMAIL_HOST-''}" >> $FILENAME echo "EMAIL_HOST_USER = ""${EMAIL_HOST_USER-''}" >> $FILENAME echo "EMAIL_HOST_PASSWORD = ""${EMAIL_HOST_PASSWORD-''}" >> $FILENAME + + echo "[ENV FILE] done." } -echo "creating .env file..." create_env -echo "done." # # python3 gen-env.py @@ -50,4 +50,18 @@ python3 manage.py migrate python3 manage.py collectstatic --no-input python3 manage.py rebuild_index --noinput +user_created=$(python3 create_admin.py) + +echo $user_created + +#if [ $user_created -eq "ADMIN_USER_EXISTS" ]; then +# echo "[SUPERUSER CREATION] User admin already exists. Not creating" +#fi + +#if [ $user_created -eq "MISSING_ADMIN_PASSWORD" ]; then +# echo "[SUPERUSER] Environment variable $ADMIN_PASSWORD for superuser admin was not set. Leaving container" + # return -1 # TODO: Uncomment when in finally in prod. +#fi + + /bin/sh gunicorn_start.sh no-venv