# -*- coding: utf-8 -*- """Fix phys API if hung; deploy neko launch assets; import exclude; verify live.""" from __future__ import annotations import time from pathlib import Path import paramiko LOCAL = Path(__file__).resolve().parents[1] NEKO_FILES = [ "backend/neko/browser-launch.sh", "backend/neko/policies/policies.json", "backend/neko/provisioning/tampermonkey-provisioning.json", "backend/neko/provisioning/youtube-studio-auto-dismiss.user.js", ] def run(c, cmd, t=120): _, o, e = c.exec_command(cmd, timeout=t) return (o.read() + e.read()).decode("utf-8", "replace") def put_files(c, sudo=None): sftp = c.open_sftp() for rel in NEKO_FILES: tmp = f"/tmp/_nk_{Path(rel).name}" with sftp.file(tmp, "wb") as f: f.write((LOCAL / rel).read_bytes()) dst = f"/opt/douyinWEB/{rel}" if sudo: run(c, f"echo '{sudo}' | sudo -S -p '' bash -lc \"install -m 644 {tmp} {dst}; sed -i 's/\\r$//' {dst}; chmod +x {dst} 2>/dev/null || true\"", 30) else: run(c, f"install -m 644 {tmp} {dst}; sed -i 's/\\r$//' {dst}; chmod +x {dst} 2>/dev/null || true", 20) print("ok", rel) sftp.close() VERIFY = r''' import json,urllib.request,http.cookiejar,time for i in range(12): try: print("health", urllib.request.urlopen("http://127.0.0.1:8014/api/health", timeout=3).status) break except Exception as e: print("wait", i, type(e).__name__); time.sleep(2) 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) req=urllib.request.Request("http://127.0.0.1:8014/api/youtube/assign/timeslot-exclude/import-manual", data=json.dumps({"merge":True}).encode(), headers={"Content-Type":"application/json"}, method="POST") imp=json.loads(op.open(req, timeout=60).read()) print("import added", imp.get("added"), "count", imp.get("count")) ex=json.loads(op.open("http://127.0.0.1:8014/api/youtube/assign/timeslot-exclude", timeout=30).read()) print("exclude count", ex.get("count"), "effective", ex.get("effective_room_count")) try: req=urllib.request.Request("http://127.0.0.1:8014/api/ops/neko/push-catalog", data=b"{}", headers={"Content-Type":"application/json"}, method="POST") print("push", op.open(req, timeout=90).read()[:500]) except Exception as e: print("push", type(e).__name__, e) lm=json.loads(op.open("http://127.0.0.1:8014/api/youtube/live-map?force=1", timeout=150).read()) print("live_count", lm.get("live_count"), "source", lm.get("source")) for k,v in sorted((lm.get("entries") or {}).items()): if v.get("is_live") and str(k).startswith("assign:"): print(" LIVE", (v.get("title") or "")[:28], v.get("source")) ''' def verify(c, sudo=None): if sudo: sftp = c.open_sftp() with sftp.file("/tmp/_cont3.py", "w") as f: f.write(VERIFY) sftp.close() print(run(c, f"echo '{sudo}' | sudo -S -p '' /opt/douyinWEB/backend/.venv/bin/python /tmp/_cont3.py", 220)[:2500]) else: _, o, e = c.exec_command("/opt/douyinWEB/backend/.venv/bin/python -", timeout=220) o.channel.sendall(VERIFY.encode()) o.channel.shutdown_write() print((o.read() + e.read()).decode("utf-8", "replace")[:2500]) def main(): # PHYS c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect("107.173.30.245", 4014, "eric", "4669805", timeout=30) print("===== PHYS =====") print(run(c, "echo '4669805' | sudo -S -p '' bash -lc 'systemctl is-active douyinweb-api; ss -lntp | grep 8014 || true'", 30)) # bounce if health slow print(run(c, "echo '4669805' | sudo -S -p '' systemctl restart douyinweb-api", 40)) time.sleep(4) put_files(c, "4669805") verify(c, "4669805") c.close() # VPS c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect("107.173.30.245", 22, "root", "Xiao4669805", timeout=30) print("===== VPS =====") put_files(c) verify(c) c.close() print("DONE") if __name__ == "__main__": main()