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

100 lines
3.2 KiB
Python

#!/usr/bin/env python3
"""Aggressive revive: kill docker/neko, free RAM, start caddy+api+web for real."""
from __future__ import annotations
import sys
import time
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
def ssh():
for i in range(10):
try:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
"107.173.30.245",
22,
"root",
"Xiao4669805",
timeout=40,
banner_timeout=120,
allow_agent=False,
look_for_keys=False,
)
print("ssh", i, flush=True)
return c
except Exception as ex:
print("retry", i, ex, flush=True)
time.sleep(3)
raise SystemExit("ssh fail")
def run(c, cmd, t=120):
print(f"\n$ {cmd[:220]}", flush=True)
_i, o, e = c.exec_command(cmd, timeout=t)
code = o.channel.recv_exit_status()
out = (o.read() + e.read()).decode("utf-8", "replace")
print((out[-6000:] if len(out) > 6000 else out) or f"(empty {code})", flush=True)
return code
c = ssh()
# What eats RAM?
run(c, "ps aux --sort=-%mem | head -20; echo ---; timeout 15 docker ps -a --format '{{.Names}} {{.Status}}' 2>/dev/null | head -30; echo ---; free -h; uptime", 50)
# Hard stop all docker containers
run(
c,
"timeout 60 docker stop $(timeout 20 docker ps -q) 2>/dev/null; "
"timeout 30 docker kill $(timeout 15 docker ps -q) 2>/dev/null; "
"echo DOCKER_DOWN; free -h | head -2; uptime",
150,
)
# Kill leftover chrome / next zombies if any
run(
c,
"pkill -9 chrome 2>/dev/null; pkill -9 chromium 2>/dev/null; "
"pkill -9 -f 'next-server|uvicorn app.main' 2>/dev/null; "
"sleep 2; free -h | head -2; uptime",
40,
)
# Start only what dylive+edge need, with longer TimeoutStart for caddy
run(
c,
"mkdir -p /etc/systemd/system/caddy.service.d; "
"printf '[Service]\\nTimeoutStartSec=180\\n' > /etc/systemd/system/caddy.service.d/timeout.conf; "
"systemctl daemon-reload; "
"systemctl reset-failed caddy douyinweb-api douyinweb-web 2>/dev/null; "
"systemctl start seaweed-master seaweed-volume seaweed-filer seaweed-s3 2>/dev/null; "
"systemctl start douyinweb-pb douyinweb-yt; "
"systemctl start douyinweb-api; sleep 3; "
"systemctl start douyinweb-web; sleep 3; "
"systemctl start caddy nginx frpp; sleep 3; "
"systemctl is-active caddy nginx frpp douyinweb-api douyinweb-web douyinweb-yt douyinweb-pb; "
"ss -lntp | grep -E ':(443|80|8014|4015|4014|8092|8020|8333)\\b' || true",
200,
)
run(
c,
"curl -sS -m 10 http://127.0.0.1:8014/api/health; echo; "
"curl -sS -m 10 -o /dev/null -w 'web=%{http_code}\\n' http://127.0.0.1:4015/; "
"curl -sS -m 10 -o /dev/null -w 'caddy_local=%{http_code}\\n' -k https://127.0.0.1/ -H 'Host: dylive.nomadro.com'; "
"journalctl -u caddy -n 25 --no-pager; "
"journalctl -u douyinweb-api -n 20 --no-pager",
60,
)
# WG again
run(c, "wg-quick down wg-dy 2>/dev/null; wg-quick up wg-dy; sleep 3; wg show wg-dy | head -18; ping -c 2 -W 2 10.8.0.2 | tail -3", 40)
c.close()
print("DONE", flush=True)