From f4f6fa5962e5b3f08040d7db871a08b2d079a550 Mon Sep 17 00:00:00 2001 From: Markos Gogoulos Date: Sun, 1 Aug 2021 18:56:38 +0300 Subject: [PATCH] optimize migrations service to not run all actions every time (#229) * optimize migrations service to not run all actions every time * set random pass for Docker setup, but also allow for env set --- Dockerfile | 2 +- deploy/docker/prestart.sh | 31 ++++++++++++++++++++----------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0791c82..197433d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -29,8 +29,8 @@ FROM python:3.8-slim-buster as runtime-image ENV PYTHONUNBUFFERED=1 ENV PYTHONDONTWRITEBYTECODE=1 ENV ADMIN_USER='admin' -ENV ADMIN_PASSWORD='mediacms' ENV ADMIN_EMAIL='admin@localhost' +#ENV ADMIN_PASSWORD='uncomment_and_set_password_here' # See: https://github.com/celery/celery/issues/6285#issuecomment-715316219 ENV CELERY_APP='cms' diff --git a/deploy/docker/prestart.sh b/deploy/docker/prestart.sh index f40b71a..a512792 100755 --- a/deploy/docker/prestart.sh +++ b/deploy/docker/prestart.sh @@ -4,20 +4,29 @@ RANDOM_ADMIN_PASS=`python -c "import secrets;chars = 'abcdefghijklmnopqrstuvwxyz ADMIN_PASSWORD=${ADMIN_PASSWORD:-$RANDOM_ADMIN_PASS} if [ X"$ENABLE_MIGRATIONS" = X"yes" ]; then + echo "Running migrations service" python manage.py migrate - python manage.py loaddata fixtures/encoding_profiles.json - python manage.py loaddata fixtures/categories.json + EXISTING_INSTALLATION=`echo "from users.models import User; print(User.objects.exists())" |python manage.py shell` + if [ "$EXISTING_INSTALLATION" = "True" ]; then + echo "Loaddata has already run" + else + echo "Running loaddata and creating admin user" + python manage.py loaddata fixtures/encoding_profiles.json + python manage.py loaddata fixtures/categories.json + + # post_save, needs redis to succeed (ie. migrate depends on redis) + DJANGO_SUPERUSER_PASSWORD=$ADMIN_PASSWORD python manage.py createsuperuser \ + --no-input \ + --username=admin \ + --email=$ADMIN_EMAIL \ + --database=default || true + echo "Created admin user with password: $ADMIN_PASSWORD" + + fi + echo "RUNNING COLLECTSTATIC" + python manage.py collectstatic --noinput - echo "Admin Password: $ADMIN_PASSWORD" - - # post_save, needs redis to succeed (ie. migrate depends on redis) - DJANGO_SUPERUSER_PASSWORD=$ADMIN_PASSWORD python manage.py createsuperuser \ - --no-input \ - --username=$ADMIN_USER \ - --email=$ADMIN_EMAIL \ - --database=default || true - # echo "Updating hostname ..." # TODO: Get the FRONTEND_HOST from cms/local_settings.py # echo "from django.contrib.sites.models import Site; Site.objects.update(name='$FRONTEND_HOST', domain='$FRONTEND_HOST')" | python manage.py shell