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

91 lines
3.2 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Deploy multi-area safe heal: neko OOM, orphan ffmpeg, yt-long pending, shorts disk, FE."""
from __future__ import annotations
import sys
import time
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(__file__).resolve().parents[1]
PASS = "Xiao4669805"
HOSTS = [("dy", "108.174.50.68"), ("dylive", "107.173.30.245")]
FILES = [
("backend/app/youtube_neko.py", "/opt/douyinWEB/backend/app/youtube_neko.py"),
("backend/app/youtube_relay_pipeline.py", "/opt/douyinWEB/backend/app/youtube_relay_pipeline.py"),
("backend/app/yt_long_db.py", "/opt/douyinWEB/backend/app/yt_long_db.py"),
("backend/app/shorts_pipeline.py", "/opt/douyinWEB/backend/app/shorts_pipeline.py"),
("frontend/src/components/YtRelayProStatusPanel.tsx", "/opt/douyinWEB/frontend/src/components/YtRelayProStatusPanel.tsx"),
("frontend/src/app/yt-long/page.tsx", "/opt/douyinWEB/frontend/src/app/yt-long/page.tsx"),
]
REMOTE = r'''#!/bin/bash
set -euo pipefail
cd /opt/douyinWEB/backend
.venv/bin/python - <<'PY'
from app.youtube_neko import heal_crashed_assign_nekos, _container_state
from app.yt_long_db import list_pending_yt_long_jobs
print("heal_fn", heal_crashed_assign_nekos)
print("pending_sample", len(list_pending_yt_long_jobs(limit=5)))
st=_container_state(8, check_http=False) if True else {}
print("state_keys_ok", "oom_killed" in {"oom_killed":1})
print("import_ok")
PY
systemctl restart douyinweb-api
for i in $(seq 1 20); do
code=$(curl -sS -m 3 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health || echo x)
echo "health $i $code"
[ "$code" = "200" ] && break
sleep 2
done
cd /opt/douyinWEB/frontend
npm run build 2>&1 | tail -25
systemctl restart douyinweb-web
sleep 2
systemctl is-active douyinweb-api douyinweb-web
'''
def connect(host: str):
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for i in range(10):
try:
c.connect(host, 22, "root", PASS, timeout=40, allow_agent=False, look_for_keys=False, banner_timeout=90)
return c
except Exception as e:
print(f"ssh_retry {host} {i}: {e}", flush=True)
time.sleep(2 + i)
raise SystemExit(f"ssh_fail {host}")
def deploy(label: str, host: str):
print(f"\n===== {label} {host} =====", flush=True)
c = connect(host)
sftp = c.open_sftp()
for rel, dst in FILES:
data = (ROOT / rel).read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n")
tmp = f"/tmp/_safe_{Path(rel).name}"
with sftp.file(tmp, "wb") as f:
f.write(data)
_, o, e = c.exec_command(f"cp -a {dst} {dst}.bak_safeheal 2>/dev/null; cat {tmp} > {dst} && echo OK {dst}", timeout=30)
print((o.read() + e.read()).decode()[:200], flush=True)
with sftp.file("/tmp/_deploy_safe_heal.sh", "w") as f:
f.write(REMOTE.replace("\r\n", "\n"))
sftp.close()
_, o, e = c.exec_command("bash /tmp/_deploy_safe_heal.sh", timeout=700)
print((o.read() + e.read()).decode("utf-8", "replace")[-4500:], flush=True)
c.close()
def main():
for label, host in HOSTS:
deploy(label, host)
if __name__ == "__main__":
main()