# -*- coding: utf-8 -*- """Deploy douyinWEB to Ubuntu VPS via SSH (paramiko).""" from __future__ import annotations import paramiko HOST = "107.173.30.245" PORT = 22 USER = "root" PASSWORD = "Xiao4669805" GIT_URL = "https://eric:Xiao4669805@gitea.dsx2020.com/eric/douyinWEB.git" APP_DIR = "/opt/douyinWEB" def run(client: paramiko.SSHClient, cmd: str, timeout: int = 900) -> tuple[int, str]: print(f"\n$ {cmd[:200]}{'...' if len(cmd) > 200 else ''}", flush=True) _, stdout, stderr = client.exec_command(cmd, timeout=timeout, get_pty=True) out = stdout.read().decode("utf-8", errors="replace") err = stderr.read().decode("utf-8", errors="replace") code = stdout.channel.recv_exit_status() text = (out or "") + (("\n" + err) if err.strip() else "") if text.strip(): print(text[-5000:], flush=True) print(f"[exit {code}]", flush=True) return code, text def main() -> int: client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) print(f"connecting {USER}@{HOST}:{PORT} ...", flush=True) client.connect( HOST, port=PORT, username=USER, password=PASSWORD, timeout=45, allow_agent=False, look_for_keys=False, banner_timeout=45, ) print("connected", flush=True) run(client, "uname -a; whoami; df -h / | tail -1") run(client, "command -v git; command -v python3; command -v node; command -v npm; command -v ffmpeg; command -v curl; command -v unzip") code, _ = run( client, f""" set -e export DEBIAN_FRONTEND=noninteractive apt-get update -y apt-get install -y git python3 python3-pip python3-venv curl unzip ca-certificates ffmpeg # Node 20 if missing/old if ! command -v node >/dev/null 2>&1 || [ "$(node -v | tr -d v | cut -d. -f1)" -lt 18 ]; then curl -fsSL https://deb.nodesource.com/setup_20.x | bash - apt-get install -y nodejs fi python3 --version node --version npm --version ffmpeg -version | head -1 """.strip(), timeout=1200, ) if code != 0: print("WARN: apt/node install returned non-zero, continuing", flush=True) run( client, f""" set -e if [ -d {APP_DIR}/.git ]; then cd {APP_DIR} git remote set-url origin '{GIT_URL}' git fetch --all git reset --hard origin/main else rm -rf {APP_DIR} git clone '{GIT_URL}' {APP_DIR} fi cd {APP_DIR} git log -1 --oneline ls -la backend/data | head """.strip(), ) run( client, f""" set -e cd {APP_DIR}/pb if [ ! -x ./pocketbase ]; then curl -L --fail -o pb.zip https://github.com/pocketbase/pocketbase/releases/download/v0.25.8/pocketbase_0.25.8_linux_amd64.zip unzip -o pb.zip pocketbase chmod +x pocketbase rm -f pb.zip fi ./pocketbase --version """.strip(), timeout=300, ) run( client, f""" set -e cd {APP_DIR}/backend python3 -m venv .venv . .venv/bin/activate pip install -U pip pip install -r requirements.txt """.strip(), timeout=600, ) run( client, f""" set -e cd {APP_DIR}/frontend npm install --no-fund --no-audit npm run build """.strip(), timeout=1200, ) run( client, """ set +e pkill -f 'pocketbase serve' || true pkill -f 'uvicorn app.main:app' || true pkill -f 'next start' || true pkill -f 'next-server' || true sleep 2 echo stopped """.strip(), ) code, text = run( client, f""" set -e cd {APP_DIR} mkdir -p logs nohup ./pb/pocketbase serve --http=127.0.0.1:8090 > logs/pb.log 2>&1 & echo $! > logs/pb.pid cd {APP_DIR}/backend nohup .venv/bin/python -m uvicorn app.main:app --host 0.0.0.0 --port 8000 > ../logs/api.log 2>&1 & echo $! > ../logs/api.pid cd {APP_DIR}/frontend nohup npm run start -- --hostname 0.0.0.0 --port 3000 > ../logs/web.log 2>&1 & echo $! > ../logs/web.pid sleep 8 ss -lntp | grep -E ':3000|:8000|:8090' || true echo '--- health ---' curl -s --max-time 10 http://127.0.0.1:8000/api/health || true echo echo '--- web ---' curl -s -o /dev/null -w 'web:%{{http_code}}\\n' --max-time 20 http://127.0.0.1:3000/ || true echo '--- api log tail ---' tail -n 30 {APP_DIR}/logs/api.log || true echo '--- web log tail ---' tail -n 30 {APP_DIR}/logs/web.log || true """.strip(), timeout=180, ) client.close() print("\n=== DEPLOY DONE ===", flush=True) print("Frontend: http://107.173.30.245:3000/", flush=True) print("API: http://107.173.30.245:8000/docs", flush=True) print("PB: http://107.173.30.245:8090/_/ (bound 127.0.0.1 by default)", flush=True) return 0 if code == 0 else 1 if __name__ == "__main__": try: raise SystemExit(main()) except Exception as exc: print("DEPLOY FAILED:", type(exc).__name__, exc, flush=True) raise