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

179 lines
6.4 KiB
Python

#!/usr/bin/env python3
"""Deploy audit bugfixes dual + revive hung dy API. Never recreate Neko."""
from __future__ import annotations
import io
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"
DY = "108.174.50.68"
DYLIVE = "107.173.30.245"
TGZ = "/tmp/dy_next_audit_fix.tgz"
FILES = [
"backend/app/youtube_relay_pipeline.py",
"backend/app/youtube_relay_status.py",
"backend/app/main.py",
"frontend/src/components/YtAssignPanel.tsx",
"frontend/src/lib/ytRelayStatus.ts",
"backend/tests/test_relay_paused_status.py",
]
def connect(host: str):
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for i in range(12):
try:
c.connect(
host,
22,
"root",
PASS,
timeout=60,
allow_agent=False,
look_for_keys=False,
banner_timeout=300,
)
return c
except Exception as e:
print(f"ssh_retry {host} {i}: {e}", flush=True)
time.sleep(3 + i)
raise SystemExit(f"ssh_fail {host}")
def run(c, cmd: str, t: int = 1200) -> tuple[int, str]:
print(f"\n===== {cmd[:100].replace(chr(10), ' ')} =====", flush=True)
_i, o, e = c.exec_command(cmd, timeout=t)
out = (o.read() + e.read()).decode("utf-8", "replace")
code = o.channel.recv_exit_status()
print(out[-14000:] if len(out) > 14000 else out, flush=True)
return code, out
def put_text(sftp, local: Path, remote: str) -> None:
raw = local.read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n")
with sftp.file(remote, "wb") as f:
f.write(raw)
for rel in FILES:
assert (ROOT / rel).is_file(), rel
# ---------- dy ----------
print("=== DY ===", flush=True)
dy = connect(DY)
run(
dy,
"ps -o pid,stat,etime,cmd -p $(pgrep -f 'uvicorn app.main' | head -1) 2>/dev/null; "
"systemctl is-active douyinweb-api; free -h | head -2",
)
sftp = dy.open_sftp()
for rel in FILES:
put_text(sftp, ROOT / rel, f"/tmp/{Path(rel).name}")
sftp.close()
code, _ = run(
dy,
rf"""
set -euo pipefail
install -m 0644 /tmp/youtube_relay_pipeline.py /opt/douyinWEB/backend/app/youtube_relay_pipeline.py
install -m 0644 /tmp/youtube_relay_status.py /opt/douyinWEB/backend/app/youtube_relay_status.py
install -m 0644 /tmp/main.py /opt/douyinWEB/backend/app/main.py
install -m 0644 /tmp/YtAssignPanel.tsx /opt/douyinWEB/frontend/src/components/YtAssignPanel.tsx
install -m 0644 /tmp/ytRelayStatus.ts /opt/douyinWEB/frontend/src/lib/ytRelayStatus.ts
install -m 0644 /tmp/test_relay_paused_status.py /opt/douyinWEB/backend/tests/test_relay_paused_status.py
cd /opt/douyinWEB/backend
./.venv/bin/python -c 'from app.youtube_relay_status import summarize_relay_group as s; r=s(group={{"id":"g","enabled":True,"has_stream_key":True,"stream_key":"k","featured":[{{"url":"https://live.douyin.com/1"}}]}}, st={{"status":"paused_exclusive","error":"独占","recent_log":[]}}, session_running=False, pipeline_enabled=True, lane_armed=True); assert r["business_status"]=="paused_exclusive"; print("status_ok")'
grep -n 'time_slot_mode on but no time_slot' app/youtube_relay_pipeline.py | head -1
grep -n '24/N 窗口限制' app/youtube_relay_pipeline.py | head -1
# revive hung API (D-state)
systemctl restart douyinweb-api
sleep 5
systemctl is-active douyinweb-api
curl -sS -m 15 -o /dev/null -w 'health:%{{http_code}}\n' http://127.0.0.1:8014/api/health
cd /opt/douyinWEB/frontend
NODE_OPTIONS='--max-old-space-size=1536' npm run build
systemctl restart douyinweb-web
sleep 2
systemctl is-active douyinweb-web
rm -f {TGZ}
tar -C /opt/douyinWEB/frontend -czf {TGZ} .next
ls -lh {TGZ}
curl -sS -m 15 -o /dev/null -w 'youtube:%{{http_code}}\n' http://127.0.0.1:4015/youtube
docker ps --filter name=douyinweb-neko --format '{{{{.Names}}}} {{{{.Status}}}}' | sort
echo DY_DONE
""",
t=1800,
)
if code != 0:
dy.close()
raise SystemExit(f"dy failed {code}")
sftp = dy.open_sftp()
buf = io.BytesIO()
sftp.getfo(TGZ, buf)
sftp.close()
dy.close()
print(f"tarball={buf.tell()}", flush=True)
# ---------- dylive ----------
print("=== DYLIVE ===", flush=True)
dl = connect(DYLIVE)
sftp = dl.open_sftp()
for rel in FILES:
put_text(sftp, ROOT / rel, f"/tmp/{Path(rel).name}")
buf.seek(0)
with sftp.file(TGZ, "wb") as f:
f.write(buf.getvalue())
sftp.close()
code, _ = run(
dl,
rf"""
set -euo pipefail
BEFORE=$(docker ps --filter name=douyinweb-neko --format '{{{{.Names}}}}' | sort | tr '\n' ' ')
echo BEFORE=$BEFORE
install -m 0644 /tmp/youtube_relay_pipeline.py /opt/douyinWEB/backend/app/youtube_relay_pipeline.py
install -m 0644 /tmp/youtube_relay_status.py /opt/douyinWEB/backend/app/youtube_relay_status.py
install -m 0644 /tmp/main.py /opt/douyinWEB/backend/app/main.py
install -m 0644 /tmp/YtAssignPanel.tsx /opt/douyinWEB/frontend/src/components/YtAssignPanel.tsx
install -m 0644 /tmp/ytRelayStatus.ts /opt/douyinWEB/frontend/src/lib/ytRelayStatus.ts
install -m 0644 /tmp/test_relay_paused_status.py /opt/douyinWEB/backend/tests/test_relay_paused_status.py
cd /opt/douyinWEB/backend
./.venv/bin/python -c 'from app.youtube_relay_status import summarize_relay_group as s; r=s(group={{"id":"g","enabled":True,"has_stream_key":True,"stream_key":"k","featured":[{{"url":"https://live.douyin.com/1"}}]}}, st={{"status":"paused_exclusive","error":"独占","recent_log":[]}}, session_running=False, pipeline_enabled=True, lane_armed=True); assert r["business_status"]=="paused_exclusive"; print("status_ok")'
systemctl restart douyinweb-api
sleep 4
systemctl is-active douyinweb-api
curl -sS -m 12 -o /dev/null -w 'health:%{{http_code}}\n' http://127.0.0.1:8014/api/health
cd /opt/douyinWEB/frontend
rm -rf .next.bak-audit
[ -d .next ] && mv .next .next.bak-audit || true
tar -xzf {TGZ}
test -d .next
systemctl restart douyinweb-web
sleep 2
systemctl is-active douyinweb-web
curl -sS -m 12 -o /dev/null -w 'youtube:%{{http_code}}\n' http://127.0.0.1:4015/youtube
AFTER=$(docker ps --filter name=douyinweb-neko --format '{{{{.Names}}}}' | sort | tr '\n' ' ')
echo AFTER=$AFTER
docker exec douyinweb-neko-6 supervisorctl status google-chrome 2>/dev/null || true
free -m | head -2
echo DYLIVE_DONE
""",
t=300,
)
dl.close()
if code != 0:
raise SystemExit(f"dylive failed {code}")
print("ALL_DONE", flush=True)