-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
66 lines (54 loc) · 1.76 KB
/
Copy pathdocker-entrypoint.sh
File metadata and controls
66 lines (54 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/sh
set -e
cd /app
if [ -z "${DATABASE_URL:-}" ] || [ -z "$(printf '%s' "$DATABASE_URL" | tr -d '[:space:]')" ]; then
echo "ERROR: DATABASE_URL is required." >&2
echo " Use docker compose (see README) or pass -e DATABASE_URL=postgres://user:pass@host:5432/db" >&2
exit 1
fi
/opt/venv/bin/python <<'PY'
import os
import sys
import time
from urllib.parse import urlparse
from sqlalchemy import create_engine, text
from sqlalchemy.pool import NullPool
def get_url() -> str:
url = (os.environ.get("DATABASE_URL") or "").strip()
if url.startswith("postgres://"):
return "postgresql+psycopg://" + url[len("postgres://") :]
if url.startswith("postgresql://") and "+psycopg" not in url:
return "postgresql+psycopg://" + url[len("postgresql://") :]
return url
def db_host_label() -> str:
raw = (os.environ.get("DATABASE_URL") or "").strip()
parsed = urlparse(raw.replace("postgres://", "postgresql://", 1))
return parsed.hostname or raw
url = get_url()
attempts = 30
delay = 2
last_error = None
for attempt in range(1, attempts + 1):
try:
engine = create_engine(url, poolclass=NullPool)
with engine.connect() as conn:
conn.execute(text("SELECT 1"))
sys.exit(0)
except Exception as exc:
last_error = exc
if attempt < attempts:
time.sleep(delay)
host = db_host_label()
print(
f"ERROR: Could not connect to Postgres at host '{host}' after {attempts * delay}s.",
file=sys.stderr,
)
print(
" Ensure the postgres service is running on the same Docker network (use docker compose).",
file=sys.stderr,
)
print(f" Last error: {last_error}", file=sys.stderr)
sys.exit(1)
PY
/opt/venv/bin/alembic upgrade head
cd /app/web && exec npm run start -- -H 0.0.0.0 -p 3000