Files

91 lines
2.3 KiB
Bash

#!/bin/sh
set -e
echo "==> VpnPanel starting"
DB_HOST="${POSTGRES_HOST:-postgres}"
DB_PORT="${POSTGRES_PORT:-5432}"
DB_USER="${POSTGRES_USER:-vpn}"
DB_PASS="${POSTGRES_PASSWORD:-vpn}"
DB_NAME="${POSTGRES_DB:-vpn_panel}"
export DATABASE_URL="$(
POSTGRES_HOST="$DB_HOST" \
POSTGRES_PORT="$DB_PORT" \
POSTGRES_USER="$DB_USER" \
POSTGRES_PASSWORD="$DB_PASS" \
POSTGRES_DB="$DB_NAME" \
python - <<'PY'
import os
from urllib.parse import quote_plus
host = os.environ["POSTGRES_HOST"]
port = os.environ["POSTGRES_PORT"]
user = os.environ["POSTGRES_USER"]
password = os.environ["POSTGRES_PASSWORD"]
db = os.environ["POSTGRES_DB"]
print(
f"postgresql+asyncpg://{quote_plus(user)}:{quote_plus(password)}"
f"@{host}:{port}/{db}"
)
PY
)"
echo "DB target: ${DB_USER}@${DB_HOST}:${DB_PORT}/${DB_NAME}"
echo "DNS check..."
python - <<'PY'
import os
import socket
host = os.environ.get("POSTGRES_HOST", "postgres")
try:
infos = socket.getaddrinfo(host, None)
addrs = sorted({i[4][0] for i in infos})
print(f"Resolved {host} -> {', '.join(addrs)}")
except Exception as exc: # noqa: BLE001
print(f"WARNING: cannot resolve '{host}': {exc}")
PY
echo "Waiting for database..."
python - <<'PY'
import asyncio
import os
import sys
from sqlalchemy import text
from sqlalchemy.ext.asyncio import create_async_engine
async def wait_db(retries: int = 60) -> None:
url = os.environ["DATABASE_URL"]
engine = create_async_engine(url)
for i in range(retries):
try:
async with engine.connect() as conn:
await conn.execute(text("SELECT 1"))
await engine.dispose()
print("Database is ready")
return
except Exception as exc: # noqa: BLE001
print(f"DB not ready ({i + 1}/{retries}): {exc}")
await asyncio.sleep(2)
await engine.dispose()
print("ERROR: database did not become ready", file=sys.stderr)
print("HINT: both panel and postgres must be on dokploy-network", file=sys.stderr)
print("HINT: in Dokploy set POSTGRES_HOST if DB service name differs", file=sys.stderr)
sys.exit(1)
asyncio.run(wait_db())
PY
echo "Running migrations..."
alembic upgrade head
echo "Bootstrapping admin..."
python -m app.bootstrap
echo "Starting uvicorn..."
exec "$@"