From bbc61a4b043bd69f818935f73354d3ed3ea4a1cb Mon Sep 17 00:00:00 2001 From: Marie Backman Date: Thu, 13 Jun 2024 13:55:33 -0400 Subject: [PATCH 1/2] remove database flush command to make database persistent --- docker-entrypoint.sh | 4 ++-- live_data_server/plots/management/__init__.py | 0 .../plots/management/commands/__init__.py | 0 .../management/commands/ensure_adminuser.py | 22 +++++++++++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 live_data_server/plots/management/__init__.py create mode 100644 live_data_server/plots/management/commands/__init__.py create mode 100644 live_data_server/plots/management/commands/ensure_adminuser.py diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 97c2bab..57388d8 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -13,15 +13,15 @@ cd /var/www/livedata/app python manage.py collectstatic --noinput # migrate django models -python manage.py flush --no-input python manage.py makemigrations --noinput python manage.py migrate --noinput # create superuser -echo "from django.contrib.auth.models import User; User.objects.create_superuser('${DJANGO_SUPERUSER_USERNAME}', '${DJANGO_SUPERUSER_USERNAME}@example.com', '${DJANGO_SUPERUSER_PASSWORD}')" | python manage.py shell +#echo "from django.contrib.auth.models import User; User.objects.create_superuser('${DJANGO_SUPERUSER_USERNAME}', '${DJANGO_SUPERUSER_USERNAME}@example.com', '${DJANGO_SUPERUSER_PASSWORD}')" | python manage.py shell # Create the webcache python manage.py createcachetable webcache +python manage.py ensure_adminuser --username=${DJANGO_SUPERUSER_USERNAME} --email='workflow@example.com' --password=${DJANGO_SUPERUSER_USERNAME} # run application gunicorn live_data_server.wsgi:application -w 2 -b :8000 --reload diff --git a/live_data_server/plots/management/__init__.py b/live_data_server/plots/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/live_data_server/plots/management/commands/__init__.py b/live_data_server/plots/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/live_data_server/plots/management/commands/ensure_adminuser.py b/live_data_server/plots/management/commands/ensure_adminuser.py new file mode 100644 index 0000000..0a015df --- /dev/null +++ b/live_data_server/plots/management/commands/ensure_adminuser.py @@ -0,0 +1,22 @@ +# taken from +# https://stackoverflow.com/questions/39744593/how-to-create-a-django-superuser-if-it-doesnt-exist-non-interactively +from django.contrib.auth import get_user_model +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + help = "Creates an admin user non-interactively if it doesn't exist" + + def add_arguments(self, parser): + parser.add_argument("--username", help="Admin's username") + parser.add_argument("--email", help="Admin's email") + parser.add_argument("--password", help="Admin's password") + + def handle(self, *args, **options): + User = get_user_model() + if not User.objects.filter(username=options["username"]).exists(): + User.objects.create_superuser( + username=options["username"], + email=options["email"], + password=options["password"], + ) From 3f901ec0ba5a268c26e7573904fc2c1d0fff230a Mon Sep 17 00:00:00 2001 From: Marie Backman Date: Thu, 13 Jun 2024 15:10:45 -0400 Subject: [PATCH 2/2] Fix copy paste error --- docker-entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 57388d8..2125ca7 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -21,7 +21,7 @@ python manage.py migrate --noinput # Create the webcache python manage.py createcachetable webcache -python manage.py ensure_adminuser --username=${DJANGO_SUPERUSER_USERNAME} --email='workflow@example.com' --password=${DJANGO_SUPERUSER_USERNAME} +python manage.py ensure_adminuser --username=${DJANGO_SUPERUSER_USERNAME} --email='workflow@example.com' --password=${DJANGO_SUPERUSER_PASSWORD} # run application gunicorn live_data_server.wsgi:application -w 2 -b :8000 --reload