#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Deploy timeslot exclude list to physical + VPS (API + frontend build).""" from __future__ import annotations import sys import time from pathlib import Path import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") LOCAL = Path(__file__).resolve().parents[1] FILES = [ "backend/app/yt_assign_db.py", "backend/app/youtube_relay_pipeline.py", "backend/app/main.py", "frontend/src/components/YtAssignPanel.tsx", "backend/tests/test_timeslot_exclude.py", ] def put(c: paramiko.SSHClient, root: str, sudo: str | None = None) -> None: sftp = c.open_sftp() for rel in FILES: tmp = f"/tmp/_tex_{Path(rel).name}" data = (LOCAL / rel).read_bytes() with sftp.file(tmp, "wb") as f: f.write(data) dst = f"{root}/{rel}" parent = str(Path(dst).parent).replace("\\", "/") if sudo: cmd = ( f"echo '{sudo}' | sudo -S -p '' bash -lc " f"\"mkdir -p '{parent}'; install -m 644 {tmp} {dst}; sed -i 's/\\r$//' {dst}\"" ) else: cmd = f"mkdir -p '{parent}'; install -m 644 {tmp} {dst}; sed -i 's/\\r$//' {dst}" _, o, e = c.exec_command(cmd, timeout=40) err = e.read().decode("utf-8", "replace") if err.strip() and "password" not in err.lower(): print("warn", rel, err[-200:], flush=True) else: print("ok", rel, flush=True) sftp.close() def build_restart(c: paramiko.SSHClient, sudo: str | None = None) -> None: inner = ( "cd /opt/douyinWEB/frontend && npm run build >/tmp/fe_tex.log 2>&1 " "&& echo FE_OK || (tail -40 /tmp/fe_tex.log; echo FE_FAIL); " "systemctl restart douyinweb-api douyinweb-web; " "sleep 2; " "systemctl is-active douyinweb-api; systemctl is-active douyinweb-web; " "curl -sS -m 5 -o /dev/null -w 'health=%{http_code}\\n' http://127.0.0.1:8014/api/health || true" ) cmd = f"echo '{sudo}' | sudo -S -p '' bash -lc {repr(inner)}" if sudo else inner _, o, e = c.exec_command(cmd, timeout=900) print((o.read() + e.read()).decode("utf-8", "replace"), flush=True) def verify(c: paramiko.SSHClient, sudo: str | None = None) -> None: py = r""" import json,urllib.request,http.cookiejar cj=http.cookiejar.CookieJar(); op=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) req=urllib.request.Request('http://127.0.0.1:8014/api/auth/login', data=json.dumps({'username':'hackrobot','password':'Xiao4669805@'}).encode(), headers={'Content-Type':'application/json'}, method='POST') print('login', op.open(req, timeout=15).status) raw=op.open('http://127.0.0.1:8014/api/youtube/assign/timeslot-exclude', timeout=30).read() p=json.loads(raw) print('exclude_ok', p.get('ok'), 'count', p.get('count'), 'effective', p.get('effective_room_count')) """ if sudo: sftp = c.open_sftp() with sftp.file("/tmp/_v_tex.py", "w") as f: f.write(py) sftp.close() cmd = f"echo '{sudo}' | sudo -S -p '' /opt/douyinWEB/backend/.venv/bin/python /tmp/_v_tex.py" else: # write via stdin cmd = "/opt/douyinWEB/backend/.venv/bin/python -" if sudo: _, o, e = c.exec_command(cmd, timeout=60) print((o.read() + e.read()).decode("utf-8", "replace"), flush=True) else: _, o, e = c.exec_command(cmd, timeout=60) o.channel.sendall(py.encode()) o.channel.shutdown_write() print((o.read() + e.read()).decode("utf-8", "replace"), flush=True) def main() -> None: # Physical first p = paramiko.SSHClient() p.set_missing_host_key_policy(paramiko.AutoAddPolicy()) p.connect("107.173.30.245", 4014, "eric", "4669805", timeout=40, allow_agent=False, look_for_keys=False) print("===== PHYS =====", flush=True) put(p, "/opt/douyinWEB", "4669805") build_restart(p, "4669805") verify(p, "4669805") p.close() v = paramiko.SSHClient() v.set_missing_host_key_policy(paramiko.AutoAddPolicy()) v.connect("107.173.30.245", 22, "root", "Xiao4669805", timeout=40, allow_agent=False, look_for_keys=False) print("===== VPS =====", flush=True) put(v, "/opt/douyinWEB") build_restart(v) verify(v) v.close() print("DONE", flush=True) if __name__ == "__main__": main()