65 lines
1.8 KiB
Bash
65 lines
1.8 KiB
Bash
#!/bin/sh
|
|
set -e
|
|
|
|
echo "==> Waiting for PostgreSQL..."
|
|
python - <<'PY'
|
|
import os
|
|
import time
|
|
|
|
import psycopg
|
|
|
|
timeout = int(os.getenv("DATABASE_WAIT_TIMEOUT", "60"))
|
|
deadline = time.monotonic() + timeout
|
|
last_error = None
|
|
|
|
while time.monotonic() < deadline:
|
|
try:
|
|
if os.getenv("DATABASE_URL"):
|
|
connection = psycopg.connect(os.environ["DATABASE_URL"], connect_timeout=5)
|
|
else:
|
|
connection = psycopg.connect(
|
|
dbname=os.environ["POSTGRES_DB"],
|
|
user=os.environ["POSTGRES_USER"],
|
|
password=os.environ["POSTGRES_PASSWORD"],
|
|
host=os.environ["POSTGRES_HOST"],
|
|
port=os.getenv("POSTGRES_PORT", "5432"),
|
|
connect_timeout=5,
|
|
)
|
|
connection.close()
|
|
break
|
|
except psycopg.OperationalError as exc:
|
|
last_error = exc
|
|
time.sleep(2)
|
|
else:
|
|
raise SystemExit(f"PostgreSQL was not ready after {timeout}s: {last_error}")
|
|
PY
|
|
|
|
if [ "${RUN_STARTUP_TASKS:-0}" = "1" ]; then
|
|
echo "==> Applying database migrations..."
|
|
python manage.py migrate --noinput
|
|
|
|
seed_dir="${PRODUCTION_SEED_DIR:-/app/production-seed}"
|
|
seed_available=0
|
|
if [ -f "$seed_dir/manifest.json" ]; then
|
|
echo "==> Importing production seed (idempotent)..."
|
|
python manage.py import_production_seed "$seed_dir"
|
|
seed_available=1
|
|
elif [ "${REQUIRE_PRODUCTION_SEED:-1}" = "1" ]; then
|
|
echo "ERROR: Required production seed is missing from $seed_dir" >&2
|
|
exit 1
|
|
fi
|
|
|
|
if [ "$seed_available" = "0" ]; then
|
|
echo "==> Bootstrapping required application data..."
|
|
python manage.py bootstrap
|
|
else
|
|
echo "==> Production seed is authoritative; skipping bootstrap mutations."
|
|
fi
|
|
|
|
echo "==> Collecting static files..."
|
|
python manage.py collectstatic --noinput
|
|
fi
|
|
|
|
echo "==> Starting: $*"
|
|
exec "$@"
|