douyinWEB/scripts/_deploy_shellcrash_sh2_ui.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

121 lines
4.4 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy SH2-style ShellCrash panel."""
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()
for rel in (
"backend/app/shellcrash_hub.py",
"backend/app/main.py",
"frontend/src/app/network/page.tsx",
"frontend/next.config.mjs",
):
data = (ROOT / rel).read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n")
name = Path(rel).name
with sftp.file(f"/tmp/_up_{name}", "wb") as f:
f.write(data)
print("put", rel, flush=True)
fe = b"""#!/bin/bash
cd /opt/douyinWEB/frontend || exit 1
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-scui.log 2>&1
echo EXIT:$? >>/tmp/fe-scui.log
"""
with sftp.file("/tmp/_fe_scui.sh", "wb") as f:
f.write(fe)
sftp.close()
print(
run(
g,
f"echo '{GUEST_PW}' | sudo -S -p '' bash -c '"
"install -m 644 /tmp/_up_shellcrash_hub.py /opt/douyinWEB/backend/app/shellcrash_hub.py; "
"install -m 644 /tmp/_up_main.py /opt/douyinWEB/backend/app/main.py; "
"install -m 644 /tmp/_up_page.tsx /opt/douyinWEB/frontend/src/app/network/page.tsx; "
"install -m 644 /tmp/_up_next.config.mjs /opt/douyinWEB/frontend/next.config.mjs; "
"mkdir -p /etc/ShellCrash/yamls /etc/ShellCrash/configs /etc/ShellCrash/jsons; "
"if [ -f /opt/douyinWEB/backend/app/clash_assets/clash.yaml ] && [ ! -s /etc/ShellCrash/yamls/config.yaml ]; then "
" install -m 644 /opt/douyinWEB/backend/app/clash_assets/clash.yaml /etc/ShellCrash/yamls/config.yaml; "
"fi; "
"systemctl restart douyinweb-api; sleep 2; systemctl is-active douyinweb-api; "
"chmod 755 /tmp/_fe_scui.sh; rm -f /tmp/fe-scui.log; "
"systemctl stop douyinweb-web || true; "
"nohup /bin/bash /tmp/_fe_scui.sh >/dev/null 2>&1 & echo BUILD'"
),
flush=True,
)
g.close()
for i in range(50):
time.sleep(12)
g = ssh_guest()
st = run(g, "grep '^EXIT:' /tmp/fe-scui.log 2>/dev/null | tail -1")
g.close()
print(f"fe[{i}] {st.strip()}", 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 -40 /tmp/fe-scui.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 '' systemctl start douyinweb-web; sleep 3; systemctl is-active douyinweb-web"), flush=True)
g.close()
with httpx.Client(base_url="https://dy.nomadro.com", timeout=60.0, follow_redirects=False) as client:
client.post("/api/auth/login", json={"username": "hackrobot", "password": "Xiao4669805@"})
print("page", client.get("/network").status_code, flush=True)
files = client.get("/api/network/shellcrash/files?root=shellcrash-yamls")
print("files", files.status_code, flush=True)
try:
print("file_count", len((files.json() or {}).get("files") or []), flush=True)
except Exception:
print(files.text[:200], flush=True)
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())