Some checks failed
CI / test-and-build (push) Failing after 25s
Co-authored-by: Cursor <cursoragent@cursor.com>
140 lines
4.7 KiB
Bash
140 lines
4.7 KiB
Bash
#!/usr/bin/env bash
|
|
# Phase A bootstrap on new VPS — run as deploy with sudo.
|
|
set -euo pipefail
|
|
|
|
APP_DIR="${HOME}/games_app"
|
|
REPO_URL="${REPO_URL:-https://github.com/japhni/games-project.git}"
|
|
BRANCH="${BRANCH:-master}"
|
|
|
|
log() { printf '\n==> %s\n' "$*"; }
|
|
|
|
log "PostgreSQL status"
|
|
sudo systemctl enable --now postgresql
|
|
sudo systemctl is-active postgresql
|
|
sudo ss -tlnp | grep 5432 || true
|
|
|
|
log "Clone or update repo"
|
|
if [[ ! -d "$APP_DIR/.git" ]]; then
|
|
git clone --branch "$BRANCH" "$REPO_URL" "$APP_DIR"
|
|
else
|
|
git -C "$APP_DIR" fetch origin
|
|
git -C "$APP_DIR" checkout "$BRANCH"
|
|
git -C "$APP_DIR" pull --ff-only origin "$BRANCH" || true
|
|
fi
|
|
cd "$APP_DIR"
|
|
|
|
if [[ ! -f .env ]]; then
|
|
log "Creating .env with fresh secrets"
|
|
cp .env.example .env
|
|
chmod 600 .env
|
|
|
|
ENC="$(openssl rand -hex 32)"
|
|
JWT="$(openssl rand -base64 48 | tr -d '\n=/+' | head -c 48)"
|
|
REDIS="$(openssl rand -hex 32)"
|
|
SETUP="$(openssl rand -hex 24)"
|
|
BREAK="$(openssl rand -hex 32)"
|
|
PGPASS="$(openssl rand -hex 24)"
|
|
|
|
# BIP39 24 words via Node in Docker (no host Node required)
|
|
MNEMO="$(sudo docker run --rm node:20-alpine node -e "
|
|
const crypto=require('crypto');
|
|
const words=require('fs').readFileSync(0,'utf8').trim().split(/\s+/);
|
|
" 2>/dev/null || true)"
|
|
|
|
# Generate mnemonic with bip39 from docker + npm package
|
|
MNEMO="$(sudo docker run --rm node:20-alpine sh -c 'npm install -g bip39@3 >/dev/null 2>&1 && node -e "const b=require(\"bip39\"); process.stdout.write(b.generateMnemonic(256));"')"
|
|
|
|
# Escape for sed replacements — use Python for safe .env patching
|
|
export ENC JWT REDIS SETUP BREAK PGPASS MNEMO
|
|
python3 - <<'PY'
|
|
import os, re, pathlib
|
|
path = pathlib.Path(".env")
|
|
text = path.read_text(encoding="utf-8")
|
|
|
|
def set_kv(text, key, value):
|
|
line = f"{key}={value}"
|
|
pat = re.compile(rf"^{re.escape(key)}=.*$", re.M)
|
|
if pat.search(text):
|
|
return pat.sub(line, text)
|
|
return text.rstrip() + "\n" + line + "\n"
|
|
|
|
pairs = {
|
|
"POSTGRES_USER": "postgres",
|
|
"POSTGRES_PASSWORD": os.environ["PGPASS"],
|
|
"POSTGRES_DB": "games_platform_db",
|
|
"DATABASE_PASSWORD": os.environ["PGPASS"],
|
|
"DATABASE_URL": f"postgresql://postgres:{os.environ['PGPASS']}@host.docker.internal:5432/games_platform_db",
|
|
"REDIS_PASSWORD": os.environ["REDIS"],
|
|
"JWT_SECRET": os.environ["JWT"],
|
|
"ENCRYPTION_KEY": os.environ["ENC"],
|
|
"PLATFORM_MNEMONIC": os.environ["MNEMO"],
|
|
"INITIAL_SETUP_TOKEN": os.environ["SETUP"],
|
|
"BREAK_GLASS_TOKEN": os.environ["BREAK"],
|
|
"APP_PUBLIC_URL": "http://191.215.41.53:9080",
|
|
"CORS_ORIGINS": "http://191.215.41.53:9080",
|
|
"VITE_API_BASE_URL": "http://191.215.41.53:9080",
|
|
"VITE_WS_ENABLED": "true",
|
|
"VITE_WS_URL": "ws://191.215.41.53:9080/ws/v1/games/candle-predict",
|
|
"TRON_NODE_URL": "https://api.trongrid.io",
|
|
"TRON_CONFIRMATIONS": "20",
|
|
"BSC_RPC_URL": "https://bsc-dataseed.binance.org",
|
|
"BSC_CONFIRMATIONS": "15",
|
|
"ALLOW_WITHDRAWALS": "false",
|
|
"SWEEP_ENABLED": "false",
|
|
"RUN_MIGRATE_ON_START": "0",
|
|
"CERTBOT_DOMAIN1": "tloto.net",
|
|
"CERTBOT_DOMAIN2": "www.tloto.net",
|
|
}
|
|
for k, v in pairs.items():
|
|
text = set_kv(text, k, v)
|
|
path.write_text(text, encoding="utf-8")
|
|
print("Wrote .env (secrets not printed)")
|
|
PY
|
|
else
|
|
log ".env already exists — leaving it unchanged"
|
|
fi
|
|
|
|
# Load password for DB bootstrap without printing
|
|
PGPASS="$(python3 - <<'PY'
|
|
import re, pathlib
|
|
m=re.search(r'^POSTGRES_PASSWORD=(.*)$', pathlib.Path('.env').read_text(), re.M)
|
|
print(m.group(1).strip() if m else '')
|
|
PY
|
|
)"
|
|
|
|
log "Configure Postgres role + database"
|
|
sudo -u postgres psql -v ON_ERROR_STOP=1 <<SQL
|
|
DO \$\$
|
|
BEGIN
|
|
-- Ensure password for postgres role matches .env
|
|
EXECUTE format('ALTER USER postgres PASSWORD %L', '${PGPASS}');
|
|
END
|
|
\$\$;
|
|
SELECT 'postgres role password updated';
|
|
SQL
|
|
|
|
if ! sudo -u postgres psql -tAc "SELECT 1 FROM pg_database WHERE datname='games_platform_db'" | grep -q 1; then
|
|
sudo -u postgres createdb -O postgres games_platform_db
|
|
log "Created database games_platform_db"
|
|
else
|
|
log "Database games_platform_db already exists"
|
|
fi
|
|
|
|
# Allow password auth from localhost (Docker host.docker.internal)
|
|
PG_HBA="$(ls /etc/postgresql/*/main/pg_hba.conf | head -n1)"
|
|
if [[ -n "$PG_HBA" ]] && ! sudo grep -q 'tloto-docker-local' "$PG_HBA"; then
|
|
log "Adding scram localhost lines to pg_hba.conf"
|
|
sudo tee -a "$PG_HBA" >/dev/null <<'HBA'
|
|
|
|
# tloto-docker-local
|
|
host all all 127.0.0.1/32 scram-sha-256
|
|
host all all ::1/128 scram-sha-256
|
|
HBA
|
|
sudo systemctl reload postgresql
|
|
fi
|
|
|
|
log "Phase A done"
|
|
echo "APP_DIR=$APP_DIR"
|
|
echo "NEXT: sudo docker compose run --rm --no-deps --build backend node dist/db/migrate.js"
|
|
grep -E '^(INITIAL_SETUP_TOKEN|APP_PUBLIC_URL)=' .env
|