# -*- coding: utf-8 -*- """Deploy ntfy topic dylive + all-events ntfy notify.""" import time from pathlib import Path import paramiko HOST = "107.173.30.245" REPO = "/opt/douyinWEB" LOCAL = Path(r"C:/Users/admin/Desktop/douyinWEB") FILES = [ "backend/app/notify.py", "backend/app/ops_panel.py", "backend/app/main.py", "frontend/src/app/ops/page.tsx", ] c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(HOST, 22, "root", "Xiao4669805", timeout=30, allow_agent=False, look_for_keys=False) def run(cmd: str, timeout: int = 180) -> str: _, o, e = c.exec_command(cmd, timeout=timeout) out = o.read().decode("utf-8", "replace").strip() err = e.read().decode("utf-8", "replace").strip() return (out + ("\n" + err if err else "")).strip() def patch_env(remote_path: str, key: str, val: str) -> None: content = run(f"test -f {remote_path} && cat {remote_path} || echo ''") lines = content.splitlines() seen = False out: list[str] = [] for line in lines: if line.strip().startswith(f"{key}="): out.append(f"{key}={val}") seen = True else: out.append(line) if not seen: out.append(f"{key}={val}") tmp = f"/tmp/ntfy_topic_{int(time.time())}.env" sftp = c.open_sftp() with sftp.file(tmp, "w") as f: f.write("\n".join(out).rstrip() + "\n") sftp.close() run(f"install -m 600 {tmp} {remote_path} && rm -f {tmp}") ts = int(time.time()) print("[backup]", run(f"cp -a {REPO}/backend/data/monitor.db /tmp/monitor.db.bak_ntfy_dylive_{ts}")) sftp = c.open_sftp() for rel in FILES: remote = f"{REPO}/{rel.replace(chr(92), '/')}" print("[upload]", rel) sftp.put(str(LOCAL / rel), remote) sftp.close() print("[patch ntfy.env]") patch_env("/etc/nomadro/ntfy.env", "NTFY_TOPIC", "dylive") print(run("grep NTFY_TOPIC /etc/nomadro/ntfy.env")) print("[verify]", run(f"grep -n 'NTFY_TOPIC = \"dylive\"\\|NTFY_TOPIC.*dylive' {REPO}/backend/app/notify.py {REPO}/backend/app/ops_panel.py")) print("[api restart]", run("systemctl restart douyinweb-api; sleep 5; systemctl is-active douyinweb-api")) print("[frontend build]", run(f"cd {REPO}/frontend && npm run build 2>&1 | tail -8")) print("[web restart]", run("systemctl restart douyinweb-web; sleep 3; systemctl is-active douyinweb-web")) print("[ops topic]", run("curl -s http://127.0.0.1:8014/api/ops -H 'Cookie: ' | python3 -c \"import sys,json; d=json.load(sys.stdin); print(d.get('ntfy',{}).get('topic'))\" 2>/dev/null || echo need-auth")) c.close() print("DONE")