# -*- coding: utf-8 -*- """Deploy douyinWEB to Ubuntu via SSH (paramiko).""" from __future__ import annotations import sys import time import paramiko HOST = "107.173.30.245" PORT = 4014 USER = "eric" PASSWORD = "4669805" GIT_URL = "https://eric:Xiao4669805@gitea.dsx2020.com/eric/douyinWEB.git" APP_DIR = "/home/eric/douyinWEB" def run(client: paramiko.SSHClient, cmd: str, timeout: int = 600) -> tuple[int, str, str]: print(f"\n$ {cmd}", flush=True) stdin, 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() if out.strip(): print(out[-4000:], flush=True) if err.strip(): print("[stderr]", err[-2000:], flush=True) print(f"[exit {code}]", flush=True) return code, out, err 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=30, allow_agent=False, look_for_keys=False) print("connected", flush=True) # basic env run(client, "uname -a; whoami; pwd; df -h / | tail -1") run( client, "command -v git; command -v python3; command -v node; command -v npm; command -v ffmpeg; command -v curl", ) # clone or pull (force sync to remote main with data) run( client, f""" set -e if [ -d {APP_DIR}/.git ]; then cd {APP_DIR} git remote set-url origin '{GIT_URL}' git fetch origin 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 ls -la backend/data || true """.strip(), ) # install system deps if missing run( client, """ set -e export DEBIAN_FRONTEND=noninteractive sudo -n true 2>/dev/null && SUDO=sudo || SUDO= $SUDO apt-get update -y $SUDO apt-get install -y python3-pip python3-venv nodejs npm ffmpeg curl unzip ca-certificates python3 --version node --version || true npm --version || true ffmpeg -version | head -1 || true """.strip(), timeout=900, ) # pocketbase linux binary run( client, f""" set -e cd {APP_DIR}/pb if [ ! -x ./pocketbase ]; then curl -L -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 || true ls -la """.strip(), timeout=300, ) # python venv + deps 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, ) # frontend deps + build (production) run( client, f""" set -e cd {APP_DIR}/frontend npm install --no-fund --no-audit npm run build """.strip(), timeout=900, ) # stop old processes run( client, f""" 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(), ) # start services with nohup run( client, f""" set -e cd {APP_DIR} mkdir -p logs # PocketBase nohup ./pb/pocketbase serve --http=127.0.0.1:8090 > logs/pb.log 2>&1 & echo $! > logs/pb.pid # FastAPI 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 # Next.js production cd {APP_DIR}/frontend nohup npm run start -- --port 3000 --hostname 0.0.0.0 > ../logs/web.log 2>&1 & echo $! > ../logs/web.pid sleep 5 ss -lntp | grep -E ':3000|:8000|:8090' || netstat -lntp 2>/dev/null | grep -E ':3000|:8000|:8090' || true curl -s --max-time 8 http://127.0.0.1:8000/api/health || true echo curl -s -o /dev/null -w 'web:%{{http_code}}\\n' --max-time 15 http://127.0.0.1:3000/ || true echo DONE """.strip(), timeout=120, ) client.close() print("\nDeploy finished. Open http://107.173.30.245:3000 (if firewall allows)", flush=True) return 0 if __name__ == "__main__": try: raise SystemExit(main()) except Exception as exc: print("DEPLOY FAILED:", type(exc).__name__, exc, flush=True) raise