douyinWEB/scripts/_deploy_dylive_latest_neko.py
2026-09-13 22:10:49 -05:00

124 lines
4.6 KiB
Python

#!/usr/bin/env python3
"""Deploy latest origin/test1 to dylive VPS and force-recreate Neko containers."""
from __future__ import annotations
import sys
import time
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
HOST = "107.173.30.245"
USER = "root"
PASSWORD = "Xiao4669805"
REPO = "/opt/douyinWEB"
REMOTE = "https://eric:Xiao4669805@gitea.dsx2020.com/eric/douyinWEB.git"
BRANCH = "test1"
def run(c: paramiko.SSHClient, cmd: str, timeout: int = 600) -> tuple[int, str]:
print(f"\n$ {cmd[:240]}{'…' if len(cmd) > 240 else ''}", flush=True)
_i, o, e = c.exec_command(cmd, timeout=timeout)
code = o.channel.recv_exit_status()
out = (o.read() + e.read()).decode("utf-8", "replace")
if out.strip():
print(out[-8000:] if len(out) > 8000 else out, flush=True)
print(f"→ exit={code}", flush=True)
return code, out
def main() -> int:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(HOST, username=USER, password=PASSWORD, timeout=60, banner_timeout=120, allow_agent=False, look_for_keys=False)
try:
ts = time.strftime("%Y%m%d_%H%M%S")
run(
c,
f"cd {REPO} && cp -a backend/data/monitor.db backend/data/monitor.db.bak.{ts} 2>/dev/null || true; "
f"ls -la backend/data/monitor.db | head -1",
timeout=30,
)
code, _ = run(
c,
f"set -e; cd {REPO}; "
f"git remote set-url origin '{REMOTE}'; "
f"git fetch origin {BRANCH}; "
f"git reset --hard HEAD; "
f"git clean -fd -e backend/data -e frontend/.next -e node_modules -e frontend/node_modules "
f"-e backend/.venv -e .venv -e '*.db' -e '*.db-*'; "
f"git checkout -f -B {BRANCH} origin/{BRANCH}; "
f"git reset --hard origin/{BRANCH}; "
f"git log -1 --oneline; git status -sb; "
f"test \"$(git rev-parse HEAD)\" = \"$(git rev-parse origin/{BRANCH})\"",
timeout=300,
)
if code != 0:
print("FATAL: git sync failed", flush=True)
return 1
run(
c,
f"cd {REPO}/backend; "
f"(test -x .venv/bin/pip && .venv/bin/pip install -r requirements.txt -q) || "
f"(test -x ../.venv/bin/pip && ../.venv/bin/pip install -r requirements.txt -q) || true; "
f"chmod +x {REPO}/backend/neko/browser-launch.sh 2>/dev/null || true; "
f"sed -i 's/\\r$//' {REPO}/backend/neko/browser-launch.sh 2>/dev/null || true; "
f"echo deps_ok",
timeout=300,
)
run(
c,
"systemctl restart douyinweb-api douyinweb-pb douyinweb-yt 2>/dev/null || systemctl restart douyinweb-api; "
"sleep 3; systemctl is-active douyinweb-api",
timeout=90,
)
code, _ = run(
c,
f"set -e; cd {REPO}/frontend; npm ci --prefer-offline 2>/dev/null || npm install; npm run build",
timeout=900,
)
if code != 0:
print("FATAL: frontend build failed", flush=True)
return 1
run(c, "systemctl restart douyinweb-web; sleep 2; systemctl is-active douyinweb-api douyinweb-web", timeout=90)
# Force recreate Neko on VPS (allowed)
code, out = run(
c,
f"cd {REPO}/backend && .venv/bin/python - <<'PY'\n"
"from app import youtube_neko, yt_assign_db\n"
"groups = yt_assign_db.list_groups(include_urls=False)\n"
"print('groups', [(g.get('name'), g.get('neko_instance_id'), g.get('neko_enabled')) for g in groups])\n"
"print('desired', youtube_neko._desired_from_db())\n"
"print('docker', youtube_neko._docker_available())\n"
"r = youtube_neko.recreate_all_instances(start_enabled=True)\n"
"print('recreate', r)\n"
"PY",
timeout=1200,
)
if code != 0:
print("WARN: neko recreate command failed", flush=True)
run(
c,
"docker ps --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}' 2>/dev/null | head -30; "
"curl -sS -m 15 http://127.0.0.1:8014/api/health | head -c 500; echo; "
"curl -sS -m 20 -o /dev/null -w 'dylive=%{http_code}\\n' https://dylive.nomadro.com/api/health; "
f"cd {REPO} && git rev-parse --short HEAD && test -f backend/data/monitor.db && echo monitor_db=ok",
timeout=60,
)
print("\nDONE dylive latest + neko recreate", flush=True)
return 0
finally:
c.close()
if __name__ == "__main__":
raise SystemExit(main())