dynamicweb/entrypoint.sh
Sajeyks 619a20394e Add docker compose stack for test deployments
Runs postgres, redis, the web app and a celery worker. Dev defaults live
in .env.docker with an optional .env override. The entrypoint generates
and persists the Django secret key and waits for postgres; the web
service migrates and creates a Site row for every configured domain.
SQL dumps placed in db-init/ are restored on first start and are
git-ignored.
2026-09-30 14:54:38 +03:00

50 lines
1.5 KiB
Bash
Executable file

#!/bin/sh
set -ue
cd /usr/src/app/
# Persist a generated Django secret key in a shared volume so that every
# container (web, celery) uses the same one across restarts.
if [ -z "${DJANGO_SECRET_KEY:-}" ]; then
KEY_FILE="${SECRET_KEY_FILE:-/data/secret-key}"
mkdir -p "$(dirname "$KEY_FILE")"
if [ ! -s "$KEY_FILE" ]; then
TMP_KEY="$KEY_FILE.$$"
python -c 'import random,string;print("".join(random.SystemRandom().choice(string.ascii_letters+string.digits) for _ in range(64)))' > "$TMP_KEY"
# hard link is atomic: only one container wins the race
ln "$TMP_KEY" "$KEY_FILE" 2>/dev/null || true
rm -f "$TMP_KEY"
fi
DJANGO_SECRET_KEY="$(cat "$KEY_FILE")"
export DJANGO_SECRET_KEY
fi
cat > dynamicweb/settings/dynamic.py <<PYEOF
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '${POSTGRES_DB:-app}',
'USER': '${POSTGRES_USER:-app}',
'PASSWORD': '${POSTGRES_PASSWORD:-app}',
'HOST': '${POSTGRES_HOST:-db}',
'PORT': '${POSTGRES_PORT:-5432}',
}
}
PYEOF
# Wait for postgres to accept connections
python - <<'PYEOF'
import os, socket, sys, time
host = os.environ.get('POSTGRES_HOST', 'db')
port = int(os.environ.get('POSTGRES_PORT', 5432))
for _ in range(60):
try:
socket.create_connection((host, port), 2).close()
sys.exit(0)
except OSError:
time.sleep(1)
sys.exit("postgres at %s:%s not reachable" % (host, port))
PYEOF
exec "$@"