From bc518a1fbf7089fe9db66f132968ad6c3956512a Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Sat, 31 Jan 2026 08:09:47 +0000 Subject: [PATCH] Add auto-creation of admin user via entrypoint --- core/management/commands/ensure_admin.py | 19 +++++++++++++++++++ entrypoint.sh | 4 ++++ 2 files changed, 23 insertions(+) create mode 100644 core/management/commands/ensure_admin.py diff --git a/core/management/commands/ensure_admin.py b/core/management/commands/ensure_admin.py new file mode 100644 index 0000000..cd9108a --- /dev/null +++ b/core/management/commands/ensure_admin.py @@ -0,0 +1,19 @@ +from django.core.management.base import BaseCommand +from django.contrib.auth import get_user_model +import os + +class Command(BaseCommand): + help = 'Ensures an admin user exists (idempotent)' + + def handle(self, *args, **options): + User = get_user_model() + username = os.environ.get('ADMIN_USER', 'admin') + email = os.environ.get('ADMIN_EMAIL', 'admin@example.com') + password = os.environ.get('ADMIN_PASS', 'admin') + + if not User.objects.filter(username=username).exists(): + self.stdout.write(f"Creating superuser '{username}'...") + User.objects.create_superuser(username, email, password) + self.stdout.write(self.style.SUCCESS(f"Superuser '{username}' created successfully.")) + else: + self.stdout.write(self.style.SUCCESS(f"Superuser '{username}' already exists. Skipping creation.")) diff --git a/entrypoint.sh b/entrypoint.sh index fc68099..966749d 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -32,6 +32,10 @@ python manage.py migrate echo "Collecting static files..." python manage.py collectstatic --noinput +# Ensure Admin User +echo "Ensuring admin user exists..." +python manage.py ensure_admin + # Start Gunicorn echo "Starting Gunicorn..." exec gunicorn config.wsgi:application --bind 0.0.0.0:8000 --workers 3 \ No newline at end of file