Add auto-creation of admin user via entrypoint
This commit is contained in:
parent
4b1f09a2a5
commit
bc518a1fbf
19
core/management/commands/ensure_admin.py
Normal file
19
core/management/commands/ensure_admin.py
Normal 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."))
|
||||
@ -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
|
||||
Loading…
x
Reference in New Issue
Block a user