diff --git a/.env.sample b/.env.sample index 1e4a12c..43bd3d1 100644 --- a/.env.sample +++ b/.env.sample @@ -66,3 +66,9 @@ OIDC_AUTH_REQUEST_EXTRA_PARAMS='{"acr_values": "eidas1"}' LOGGING_LEVEL_HANDLERS_CONSOLE=INFO LOGGING_LEVEL_LOGGERS_ROOT=INFO LOGGING_LEVEL_LOGGERS_APP=INFO + +############################################################################## +# MIGRATIONS +############################################################################## +# Set to false to disable automatic migrations on backend startup +# AUTO_MIGRATIONS=true diff --git a/README.md b/README.md index 36ff97f..49e32b7 100644 --- a/README.md +++ b/README.md @@ -20,11 +20,11 @@ * `abra app new lasuite-docs --secrets` * `abra app config ` * `abra app deploy ` -* `abra app cmd backend migrate` -You should then be able to visit the landing page of your app, but not yet to login. To login, you need to deploy and integrate single sign on (described below in the "Configure Authentication" section). +You should then be able to visit the landing page of your app, but not yet to login. To login, you need to deploy and integrate single sign on (described below in the "Configure Authentication" section). -Minio buckets are created automatically on first deploy. To manually trigger: `abra app cmd minio minio_initialize` +* Migrations run automatically on backend startup. To trigger manually: `abra app cmd backend migrate` +* Minio buckets are created automatically on first deploy. To manually trigger: `abra app cmd minio minio_initialize` ## Configure Authentication diff --git a/abra.sh b/abra.sh index c0b0c3e..2cb0ec8 100755 --- a/abra.sh +++ b/abra.sh @@ -4,6 +4,7 @@ export ABRA_ENTRYPOINT_VERSION=v5 export NGINX_CONF_VERSION=v3 export PG_BACKUP_VERSION=v3 export MINIO_INITIALIZE_VERSION=v1 +export MIGRATE_VERSION=v1 environment() { # this exports all the secrets as environment variables @@ -11,8 +12,7 @@ environment() { } migrate() { - environment - python manage.py migrate --noinput + /migrate.sh } minio_initialize() { diff --git a/compose.yml b/compose.yml index 60d778d..7052859 100644 --- a/compose.yml +++ b/compose.yml @@ -91,7 +91,7 @@ services: labels: - "traefik.enable=false" - "coop-cloud.${STACK_NAME}.timeout=${TIMEOUT:-120}" - - "coop-cloud.${STACK_NAME}.version=0.2.7+v4.5.0" + - "coop-cloud.${STACK_NAME}.version=0.2.8+v4.5.0" user: "${DOCKER_USER:-1000}" healthcheck: test: ["CMD", "curl", "-f", "http://localhost:8080"] @@ -103,9 +103,10 @@ services: backend: image: lasuite/impress-backend:v4.5.0 networks: - - backend + - backend environment: <<: [*common-env, *postgres-env, *yprovider-env] + AUTO_MIGRATIONS: "${AUTO_MIGRATIONS:-true}" healthcheck: test: ["CMD", "/abra-entrypoint.sh", "python", "manage.py", "check"] interval: 15s @@ -114,11 +115,15 @@ services: start_period: 10s user: "${DOCKER_USER:-1000}" command: ["gunicorn", "-c", "/usr/local/etc/gunicorn/impress.py", "impress.wsgi:application"] - entrypoint: ["/abra-entrypoint.sh", "/usr/local/bin/entrypoint"] + entrypoint: > + sh -c "if [ \"$$AUTO_MIGRATIONS\" = \"true\" ]; then /migrate.sh; fi && exec /abra-entrypoint.sh /usr/local/bin/entrypoint \"$$@\"" -- configs: - source: abra_entrypoint target: /abra-entrypoint.sh mode: 0555 + - source: migrate + target: /migrate.sh + mode: 0555 secrets: - django_sk - django_sp @@ -275,6 +280,9 @@ configs: minio_initialize: name: ${STACK_NAME}_minio_initialize_${MINIO_INITIALIZE_VERSION} file: minio-initialize.sh + migrate: + name: ${STACK_NAME}_migrate_${MIGRATE_VERSION} + file: migrate.sh secrets: django_sk: diff --git a/migrate.sh b/migrate.sh new file mode 100644 index 0000000..52b5435 --- /dev/null +++ b/migrate.sh @@ -0,0 +1,26 @@ +#!/bin/sh +set -e + +# Load secrets into environment +source /abra-entrypoint.sh -e + +# Wait for database to be ready (up to 30 seconds) +i=0 +while ! python manage.py check --database default 2>/dev/null; do + i=$((i+1)) + if [ "$i" -ge 30 ]; then + echo "migrate: timed out waiting for database" >&2 + exit 1 + fi + sleep 1 +done + +# Idempotent: skip if no pending migrations +if python manage.py migrate --check > /dev/null 2>&1; then + echo "migrate: no pending migrations, skipping" + exit 0 +fi + +echo "migrate: applying pending migrations..." +python manage.py migrate --noinput +echo "migrate: done"