#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Deploy realtime ops-neko sync to physical + VPS.""" from __future__ import annotations import sys from pathlib import Path import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") LOCAL = Path(__file__).resolve().parents[1] FILES = [ "backend/app/ops_neko_sync.py", "backend/app/ops_neko.py", "backend/app/main.py", "frontend/src/app/neko/page.tsx", "frontend/src/components/YtAssignPanel.tsx", "frontend/src/components/OpsNekoHubCredentialsCard.tsx", ] def put(c, root, sudo_pw=None): sftp = c.open_sftp() for rel in FILES: src = LOCAL / rel tmp = f"/tmp/_rt_{Path(rel).name}" with sftp.file(tmp, "wb") as f: f.write(src.read_bytes()) dst = f"{root}/{rel}" if sudo_pw: cmd = ( f"echo '{sudo_pw}' | sudo -S -p '' bash -lc " f"\"mkdir -p $(dirname {dst}); install -m 644 {tmp} {dst}; sed -i 's/\\r$//' {dst}\"" ) else: cmd = f"mkdir -p $(dirname {dst}); install -m 644 {tmp} {dst}; sed -i 's/\\r$//' {dst}" _i, o, e = c.exec_command(cmd, timeout=30) (o.read() + e.read()) print("ok", rel, flush=True) sftp.close() def main(): 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") _i, o, e = p.exec_command( "echo '4669805' | sudo -S -p '' bash -lc '" "systemctl restart douyinweb-api; " "cd /opt/douyinWEB/frontend && npm run build >/tmp/fe_rt.log 2>&1 && echo FE_OK || (tail -20 /tmp/fe_rt.log; echo FE_FAIL); " "systemctl restart douyinweb-web; " "sleep 2; systemctl is-active douyinweb-api; " "curl -sS -m 8 -b /tmp/c -c /tmp/c -X POST http://127.0.0.1:8014/api/auth/login " " -H \"Content-Type: application/json\" " " -d \"{\\\"username\\\":\\\"hackrobot\\\",\\\"password\\\":\\\"Xiao4669805@\\\"}\" >/dev/null; " "curl -sS -m 8 -b /tmp/c http://127.0.0.1:8014/api/ops/neko/sync-status" "'", timeout=600, ) print((o.read() + e.read()).decode("utf-8", "replace"), flush=True) 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") _i, o, e = v.exec_command( "systemctl restart douyinweb-api; " "cd /opt/douyinWEB/frontend && npm run build >/tmp/fe_rt.log 2>&1 && echo FE_OK || (tail -20 /tmp/fe_rt.log; echo FE_FAIL); " "systemctl restart douyinweb-web; " "sleep 2; curl -sS -m 12 -o /dev/null -w 'dylive=%{http_code}\\n' https://dylive.nomadro.com/api/health", timeout=600, ) print((o.read() + e.read()).decode("utf-8", "replace"), flush=True) v.close() print("DONE", flush=True) if __name__ == "__main__": main()