games_app/deploy/vps-hardening/remote-bootstrap-env-db.sh
Your Name 5ce6a6811a
Some checks failed
CI / test-and-build (push) Failing after 25s
Add Gitea CI/CD stack, tloto-deploy, and tagged Compose images
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-17 19:56:39 +02:00

112 lines
3.9 KiB
Bash

#!/usr/bin/env bash
# Create fresh .env + Postgres DB for Tloto on new VPS (repo already synced).
set -euo pipefail
cd "${HOME}/games_app"
log() { printf '\n==> %s\n' "$*"; }
command -v python3 >/dev/null || sudo apt-get install -y python3
command -v openssl >/dev/null
if [[ -f .env ]]; then
log "Removing previous incomplete .env for clean regenerate"
rm -f .env
fi
log "Generating BIP39 mnemonic inside Docker"
MNEMO="$(sudo docker run --rm -w /tmp node:20-alpine sh -c \
'npm init -y >/dev/null 2>&1 && npm install bip39@3.1.0 >/dev/null 2>&1 && node -e "process.stdout.write(require(\"bip39\").generateMnemonic(256))"')"
WORD_COUNT="$(echo "$MNEMO" | wc -w | tr -d ' ')"
[[ "$WORD_COUNT" == "24" ]] || { echo "Mnemonic word count=$WORD_COUNT expected 24"; exit 1; }
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)"
cp .env.example .env
chmod 600 .env
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(lambda m: 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")
PY
grep '^INITIAL_SETUP_TOKEN=' .env | tee "${HOME}/tloto-initial-setup-token.txt" >/dev/null
chmod 600 "${HOME}/tloto-initial-setup-token.txt"
echo "SETUP_TOKEN_SAVED=~/tloto-initial-setup-token.txt"
log "Postgres password + database"
# Use file-based psql to avoid shell escaping issues
umask 077
PASSFILE="$(mktemp)"
printf '%s\n' "$PGPASS" >"$PASSFILE"
sudo -u postgres psql -v ON_ERROR_STOP=1 -c "ALTER USER postgres PASSWORD '$(cat "$PASSFILE")';"
rm -f "$PASSFILE"
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 games_platform_db"
else
log "games_platform_db already exists"
fi
PG_HBA="$(ls /etc/postgresql/*/main/pg_hba.conf | head -n1)"
if [[ -n "$PG_HBA" ]] && ! sudo grep -q 'tloto-docker-local' "$PG_HBA"; then
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
log "pg_hba.conf updated for localhost scram"
fi
log "Env + DB ready"
echo "Setup URL: http://191.215.41.53:9080/setup"
echo "Token: $(cat "${HOME}/tloto-initial-setup-token.txt")"