Add auto-creation of admin user via entrypoint

This commit is contained in:
Flatlogic Bot 2026-01-31 08:09:47 +00:00
parent 4b1f09a2a5
commit bc518a1fbf
2 changed files with 23 additions and 0 deletions

View File

@ -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."))

View File

@ -32,6 +32,10 @@ python manage.py migrate
echo "Collecting static files..." echo "Collecting static files..."
python manage.py collectstatic --noinput python manage.py collectstatic --noinput
# Ensure Admin User
echo "Ensuring admin user exists..."
python manage.py ensure_admin
# Start Gunicorn # Start Gunicorn
echo "Starting Gunicorn..." echo "Starting Gunicorn..."
exec gunicorn config.wsgi:application --bind 0.0.0.0:8000 --workers 3 exec gunicorn config.wsgi:application --bind 0.0.0.0:8000 --workers 3