douyinWEB/scripts/_tmp_deploy_watches.py
2026-09-13 22:10:49 -05:00

98 lines
3.6 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy WG watch fix + Neko health watch to physical host. No business switches."""
from __future__ import annotations
import sys
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(r"c:\Users\admin\Desktop\douyinWEB")
FILES = [
("scripts/linux/wireguard_watch.sh", "/opt/douyinWEB/scripts/linux/wireguard_watch.sh", 0o755),
("scripts/linux/neko_health_watch.sh", "/opt/douyinWEB/scripts/linux/neko_health_watch.sh", 0o755),
("scripts/linux/api_health_watch.sh", "/opt/douyinWEB/scripts/linux/api_health_watch.sh", 0o755),
]
NEKO_UNIT = """[Unit]
Description=douyinWEB Neko HTTP soft-heal watchdog
After=docker.service network-online.target
Wants=network-online.target
[Service]
Type=simple
Environment=DY_NEKO_WATCH_INTERVAL=30
Environment=DY_NEKO_WATCH_FAILS=3
Environment=DY_NEKO_WATCH_COOLDOWN=300
Environment=DY_NEKO_WATCH_ASSIGN_MAX=8
Environment=DY_NEKO_WATCH_OPS_MAX=8
ExecStart=/bin/bash /opt/douyinWEB/scripts/linux/neko_health_watch.sh
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
"""
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect("107.173.30.245", 4014, "eric", "4669805", timeout=40, allow_agent=False, look_for_keys=False)
pw = "4669805"
sftp = c.open_sftp()
for rel, dest, mode in FILES:
data = (ROOT / rel).read_bytes().replace(b"\r\n", b"\n")
tmp = f"/tmp/_watch_{Path(rel).name}"
with sftp.file(tmp, "wb") as f:
f.write(data)
_, o, e = c.exec_command(
f"echo '{pw}' | sudo -S -p '' bash -lc {repr(f'install -D -m {mode:o} {tmp} {dest}; ls -la {dest}')}",
timeout=30,
)
print((o.read() + e.read()).decode())
with sftp.file("/tmp/_neko_watch.service", "w") as f:
f.write(NEKO_UNIT.replace("\r\n", "\n"))
sftp.close()
REMOTE = r'''
set +e
install -m 644 /tmp/_neko_watch.service /etc/systemd/system/douyinweb-neko-watch.service
# Fix WG watch: executable + resilient script already installed; drop empty Environment= that clears vars oddly
mkdir -p /etc/systemd/system/douyinweb-wireguard-watch.service.d
cat > /etc/systemd/system/douyinweb-wireguard-watch.service.d/aggressive.conf <<'EOF'
[Service]
Environment="WIREGUARD_WATCH_IFACES=wg-dy wg-dy-cn"
Environment=WIREGUARD_WATCH_INTERVAL=15
Environment=WIREGUARD_STALE_SECONDS=90
Environment=WIREGUARD_RESTART_COOLDOWN=60
Restart=always
RestartSec=5
EOF
chmod +x /opt/douyinWEB/scripts/linux/wireguard_watch.sh /opt/douyinWEB/scripts/linux/neko_health_watch.sh /opt/douyinWEB/scripts/linux/api_health_watch.sh
bash -n /opt/douyinWEB/scripts/linux/wireguard_watch.sh && echo wg_syntax_ok
bash -n /opt/douyinWEB/scripts/linux/neko_health_watch.sh && echo neko_syntax_ok
systemctl daemon-reload
systemctl reset-failed douyinweb-wireguard-watch douyinweb-neko-watch douyinweb-api-watch 2>/dev/null
systemctl enable douyinweb-wireguard-watch douyinweb-neko-watch douyinweb-api-watch
systemctl restart douyinweb-wireguard-watch
systemctl restart douyinweb-neko-watch
systemctl start douyinweb-api-watch
sleep 3
systemctl is-active douyinweb-wireguard-watch douyinweb-neko-watch douyinweb-api-watch
journalctl -u douyinweb-wireguard-watch -n 8 --no-pager
journalctl -u douyinweb-neko-watch -n 12 --no-pager
# once probe
/opt/douyinWEB/scripts/linux/wireguard_watch.sh once || true
wg show wg-dy-cn | egrep 'handshake|transfer' || true
'''
sftp = c.open_sftp()
with sftp.file("/tmp/_deploy_watches.sh", "w") as f:
f.write(REMOTE.replace("\r\n", "\n"))
sftp.close()
_, o, e = c.exec_command(f"echo '{pw}' | sudo -S -p '' bash /tmp/_deploy_watches.sh", timeout=90)
print((o.read() + e.read()).decode("utf-8", "replace"))
c.close()