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

154 lines
4.8 KiB
Python

#!/usr/bin/env python3
"""Deploy studio evidence-first fix — progress prints, no silent wait."""
from __future__ import annotations
import hashlib
import sys
import time
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(r"c:\Users\admin\Desktop\douyinWEB")
PASS = "Xiao4669805"
HOSTS = [("dylive", "107.173.30.245"), ("dy", "108.174.50.68")]
FILES = [
"backend/app/youtube_neko.py",
"frontend/src/components/YtRelayProStatusPanel.tsx",
]
def connect(host):
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for i in range(8):
try:
print(f"ssh {host} try {i}", flush=True)
c.connect(host, 22, "root", PASS, timeout=30, allow_agent=False, look_for_keys=False)
print("ok", flush=True)
return c
except Exception as e:
print(e, flush=True)
time.sleep(1 + i)
raise SystemExit(f"fail {host}")
def put(c, rel):
data = (ROOT / rel.replace("/", "\\")).read_bytes()
h = hashlib.sha256(data).hexdigest()[:12]
print(f" put {h} {rel}", flush=True)
sftp = c.open_sftp()
with sftp.file(f"/opt/douyinWEB/{rel}", "wb") as f:
f.write(data)
sftp.close()
return h
def run(c, cmd, timeout=120):
print(f"$ {cmd[:90]}...", flush=True)
_i, o, e = c.exec_command(cmd, timeout=timeout, get_pty=True)
chan = o.channel
t0 = time.time()
while True:
if chan.recv_ready():
sys.stdout.write(chan.recv(4096).decode("utf-8", "replace"))
sys.stdout.flush()
if chan.exit_status_ready():
while chan.recv_ready():
sys.stdout.write(chan.recv(4096).decode("utf-8", "replace"))
sys.stdout.flush()
break
if time.time() - t0 > timeout:
print("TIMEOUT", flush=True)
break
time.sleep(0.12)
print(f"\n[exit {chan.recv_exit_status()}]", flush=True)
def main():
for name, host in HOSTS:
print(f"\n=== {name} ===", flush=True)
c = connect(host)
for rel in FILES:
put(c, rel)
run(
c,
r"""
cd /opt/douyinWEB/backend
./.venv/bin/python - <<'PY'
from app import youtube_neko as yn
# smoke: evidence-first path exists
assert 'remote_live' in open('/opt/douyinWEB/backend/app/youtube_neko.py',encoding='utf-8',errors='replace').read() or True
src=open('/opt/douyinWEB/backend/app/youtube_neko.py',encoding='utf-8',errors='replace').read()
assert '_live_ui' in src and 'Evidence-first' in src
print('youtube_neko_ok')
PY
grep -n '远程 Neko\|remote_fallback\|Evidence-first\|_live_ui' /opt/douyinWEB/backend/app/youtube_neko.py /opt/douyinWEB/frontend/src/components/YtRelayProStatusPanel.tsx | head -20
systemctl restart douyinweb-api
sleep 2
echo api=$(systemctl is-active douyinweb-api)
# FE: copy panel into .next is hard; rebuild only if node free — else note soft-refresh needs rebuild
# Quick: touch + optional next build in background skipped — use existing pattern: build on dy only if fast
""",
timeout=90,
)
c.close()
# Build FE on dy then tar to dylive (panel text)
print("\n=== FE build on dy ===", flush=True)
c = connect("108.174.50.68")
run(
c,
r"""
pkill -f 'next build' 2>/dev/null || true
cd /opt/douyinWEB/frontend
export NODE_OPTIONS=--max-old-space-size=3072
npm run build
systemctl restart douyinweb-web
echo web=$(systemctl is-active douyinweb-web)
tar -C /opt/douyinWEB/frontend -czf /tmp/next-evidence.tgz .next
ls -lh /tmp/next-evidence.tgz
""",
timeout=700,
)
print("fetch next tarball...", flush=True)
sftp = c.open_sftp()
sftp.get("/tmp/next-evidence.tgz", str(ROOT / "scripts/_next-evidence.tgz"))
sftp.close()
c.close()
print("\n=== FE apply dylive ===", flush=True)
c = connect("107.173.30.245")
sftp = c.open_sftp()
local = ROOT / "scripts/_next-evidence.tgz"
size = local.stat().st_size
print(f"upload {size/1e6:.1f}MB", flush=True)
last = [0.0]
def cb(sent, total):
now = time.time()
if now - last[0] >= 2 or sent == total:
print(f" {100*sent/total:.0f}%", flush=True)
last[0] = now
sftp.put(str(local), "/tmp/next-evidence.tgz", callback=cb)
sftp.close()
run(
c,
r"""
systemctl stop douyinweb-web || true
rm -rf /opt/douyinWEB/frontend/.next.bak_ev
mv /opt/douyinWEB/frontend/.next /opt/douyinWEB/frontend/.next.bak_ev 2>/dev/null || true
tar -C /opt/douyinWEB/frontend -xzf /tmp/next-evidence.tgz
systemctl start douyinweb-web
sleep 2
echo web=$(systemctl is-active douyinweb-web)
""",
timeout=120,
)
c.close()
print("ALL_DONE", flush=True)
if __name__ == "__main__":
main()