douyinWEB/scripts/_repair_wg_watch_fe.py
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 22:42:40 -05:00

180 lines
5.9 KiB
Python

# -*- coding: utf-8 -*-
"""Repair watch unit + FE build; verify WG-primary checklist."""
from __future__ import annotations
import sys
import time
from pathlib import Path
import httpx
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(__file__).resolve().parents[1]
VPS = "107.173.30.245"
GUEST_PW = "4669805"
def ssh_guest(tries: int = 8):
last = None
for i in range(tries):
try:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
VPS,
4014,
"eric",
GUEST_PW,
timeout=40,
banner_timeout=80,
allow_agent=False,
look_for_keys=False,
)
return c
except Exception as ex:
last = ex
print(f"frp fail{i} {type(ex).__name__}", flush=True)
time.sleep(2 + i)
raise last # type: ignore[misc]
def run(c, cmd: str, timeout: int = 120) -> str:
_, o, e = c.exec_command(cmd, timeout=timeout)
return (o.read() + e.read()).decode("utf-8", "replace")
def main() -> int:
g = ssh_guest()
sftp = g.open_sftp()
watch = (ROOT / "scripts/linux/wireguard_watch.sh").read_bytes()
watch = watch.replace(b"\r\n", b"\n").replace(b"\r", b"\n")
with sftp.file("/tmp/_up_wireguard_watch.sh", "wb") as f:
f.write(watch)
hub = (ROOT / "backend/app/network_hub.py").read_bytes()
with sftp.file("/tmp/_up_network_hub.py", "wb") as f:
f.write(hub)
page = (ROOT / "frontend/src/app/network/page.tsx").read_bytes()
with sftp.file("/tmp/_up_page.tsx", "wb") as f:
f.write(page)
fe = b"""#!/bin/bash
set -e
cd /opt/douyinWEB/frontend
export DY_INTERNAL_API_URL=http://127.0.0.1:8014
export DY_PUBLIC_BASE_URL=https://dy.nomadro.com
npm run build >/tmp/fe-wg.log 2>&1
echo EXIT:$? >>/tmp/fe-wg.log
"""
with sftp.file("/tmp/_fe_build.sh", "wb") as f:
f.write(fe.replace(b"\r\n", b"\n"))
sftp.close()
print(
run(
g,
f"echo '{GUEST_PW}' | sudo -S -p '' bash -c '"
"set -e; "
"install -m 755 /tmp/_up_wireguard_watch.sh /opt/douyinWEB/scripts/linux/wireguard_watch.sh; "
"install -m 644 /tmp/_up_network_hub.py /opt/douyinWEB/backend/app/network_hub.py; "
"install -m 644 /tmp/_up_page.tsx /opt/douyinWEB/frontend/src/app/network/page.tsx; "
"chmod 755 /tmp/_fe_build.sh; "
"file /opt/douyinWEB/scripts/linux/wireguard_watch.sh; "
"systemctl reset-failed douyinweb-wireguard-watch || true; "
"systemctl restart douyinweb-wireguard-watch; "
"sleep 2; "
"systemctl is-active douyinweb-wireguard-watch; "
"journalctl -u douyinweb-wireguard-watch -n 25 --no-pager; "
"/opt/douyinWEB/scripts/linux/wireguard_watch.sh once; "
"systemctl restart douyinweb-api; "
"sleep 2; "
"systemctl is-active douyinweb-api wg-quick@wg-dy frpp"
"'",
90,
),
flush=True,
)
# start FE build as root via absolute bash
print(
run(
g,
f"echo '{GUEST_PW}' | sudo -S -p '' bash -c '"
"systemctl stop douyinweb-web || true; "
"rm -f /tmp/fe-wg.log; "
"nohup /bin/bash /tmp/_fe_build.sh >/dev/null 2>&1 & echo BUILD_PID:$!'"
),
flush=True,
)
g.close()
for i in range(50):
time.sleep(12)
g = ssh_guest()
st = run(g, "tail -3 /tmp/fe-wg.log 2>/dev/null; grep '^EXIT:' /tmp/fe-wg.log 2>/dev/null | tail -1")
g.close()
print(f"fe[{i}] {st.strip()[-200:]}", flush=True)
if "EXIT:0" in st:
break
if "EXIT:" in st and "EXIT:0" not in st:
g = ssh_guest()
print(run(g, "tail -60 /tmp/fe-wg.log"), flush=True)
g.close()
raise SystemExit("fe fail")
else:
raise SystemExit("fe timeout")
g = ssh_guest()
print(
run(
g,
f"echo '{GUEST_PW}' | sudo -S -p '' bash -c '"
"systemctl start douyinweb-web; sleep 3; "
"systemctl is-active douyinweb-web douyinweb-wireguard-watch douyinweb-api'"
),
flush=True,
)
g.close()
print("=== checklist ===", flush=True)
with httpx.Client(base_url="https://dy.nomadro.com", timeout=25.0, follow_redirects=False) as client:
client.post("/api/auth/login", json={"username": "hackrobot", "password": "Xiao4669805@"})
j = client.get("/api/network").json()
roles = j.get("path_roles") or {}
print("policy", roles.get("policy"), flush=True)
print("app", roles.get("app_https"), flush=True)
print("ssh", roles.get("ops_ssh"), flush=True)
wg = j.get("wireguard") or {}
frp = j.get("frp") or {}
print(
"wg role=",
wg.get("role"),
"fresh=",
wg.get("handshake_fresh"),
"age=",
wg.get("handshake_age_sec"),
"watch=",
(wg.get("watch") or {}).get("unit"),
flush=True,
)
print("frp role=", frp.get("role"), "purpose=", frp.get("purpose"), flush=True)
cl = j.get("physical_checklist") or {}
print("ok=", cl.get("ok"), "failed=", cl.get("failed"), flush=True)
for c in cl.get("checks") or []:
mark = "OK" if c.get("ok") else "!!"
print(f" [{mark}] {c.get('name')}: {c.get('detail')}", flush=True)
print("page", client.get("/network").status_code, flush=True)
print("health", client.get("/api/health").status_code, flush=True)
# dylive untouched
print("dylive", httpx.get("https://dylive.nomadro.com/api/health", timeout=15).status_code, flush=True)
print("DONE", flush=True)
return 0
if __name__ == "__main__":
raise SystemExit(main())