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

175 lines
5.6 KiB
Python

#!/usr/bin/env python3
"""Post-reboot recover: core sites only. Never start Neko. Live flush."""
from __future__ import annotations
import sys
import time
import httpx
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace", line_buffering=True)
HOST = "107.173.30.245"
def ssh(port=22, user="root", pw="Xiao4669805", tries=20):
last = None
for i in range(tries):
try:
print(f"ssh :{port} try={i} ...", flush=True)
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
HOST,
port,
user,
pw,
timeout=20,
banner_timeout=30,
auth_timeout=30,
allow_agent=False,
look_for_keys=False,
)
print(f"ssh :{port} OK", flush=True)
return c
except Exception as ex:
last = ex
print(f"ssh :{port} fail {type(ex).__name__}: {ex}", flush=True)
time.sleep(3)
raise SystemExit(f"ssh dead: {last}")
def run(c, cmd, t=120, label=""):
print(f"\n[{label}] $ {cmd[:200]}", flush=True)
_i, o, e = c.exec_command(cmd, timeout=t)
# poll for output
buf = b""
deadline = time.time() + t
while True:
if o.channel.recv_ready():
chunk = o.channel.recv(8192)
buf += chunk
sys.stdout.write(chunk.decode("utf-8", "replace"))
sys.stdout.flush()
if o.channel.recv_stderr_ready():
chunk = o.channel.recv_stderr(8192)
buf += chunk
sys.stdout.write(chunk.decode("utf-8", "replace"))
sys.stdout.flush()
if o.channel.exit_status_ready():
# drain
while o.channel.recv_ready():
chunk = o.channel.recv(8192)
buf += chunk
sys.stdout.write(chunk.decode("utf-8", "replace"))
sys.stdout.flush()
while o.channel.recv_stderr_ready():
chunk = o.channel.recv_stderr(8192)
buf += chunk
sys.stdout.write(chunk.decode("utf-8", "replace"))
sys.stdout.flush()
code = o.channel.recv_exit_status()
print(f"\n[{label}] → {code}", flush=True)
return code, buf.decode("utf-8", "replace")
if time.time() > deadline:
print(f"\n[{label}] TIMEOUT", flush=True)
return -1, buf.decode("utf-8", "replace")
time.sleep(0.15)
def probe():
print("\n=== PUBLIC ===", flush=True)
with httpx.Client(follow_redirects=False, timeout=15) as cl:
for u in [
"https://dylive.nomadro.com/api/health",
"https://dylive.nomadro.com/youtube",
"https://dy.nomadro.com/api/health",
"https://dy.nomadro.com/",
]:
try:
r = cl.get(u)
print(f"{u} -> {r.status_code} loc={(r.headers.get('location') or '')[:40]}", flush=True)
except Exception as ex:
print(f"{u} ERR {ex}", flush=True)
print("=== post-reboot recover (no neko start) ===", flush=True)
v = ssh(22)
run(v, "uptime; free -h | head -2; hostname", 30, "host")
# If docker came back and auto-started old containers, STOP them only — do not start any
run(
v,
"systemctl is-active docker; "
"if systemctl is-active docker >/dev/null 2>&1; then "
" docker ps --format '{{.Names}} {{.Status}}' 2>/dev/null | head -40; "
" ids=$(docker ps -q --filter name=douyinweb-neko 2>/dev/null); "
" if [ -n \"$ids\" ]; then echo STOPPING_NEKO; docker stop -t 2 $ids; fi; "
" for id in $(docker ps -q --filter name=douyinweb-neko -a 2>/dev/null); do "
" docker update --restart=no $id >/dev/null 2>&1 || true; "
" done; "
"fi; "
"free -h | head -2",
90,
"neko-stop-only",
)
# Core services
run(
v,
"systemctl start seaweed-master seaweed-volume seaweed-filer seaweed-s3 frpp nginx caddy "
"douyinweb-pb douyinweb-yt douyinweb-api douyinweb-web 'wg-quick@wg-dy' 2>&1; "
"sleep 3; "
"systemctl is-active caddy nginx frpp douyinweb-api douyinweb-web douyinweb-yt douyinweb-pb docker; "
"ss -lntp | grep -E ':(443|80|8014|4015|4014|8020)\\b' || true; "
"free -h | head -2; uptime",
120,
"start-core",
)
run(
v,
"curl -sS -m 12 http://127.0.0.1:8014/api/health | head -c 300; 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 'yt=%{http_code}\\n' http://127.0.0.1:4015/youtube; "
"curl -sS -m 10 -o /dev/null -w 'caddy=%{http_code}\\n' -k -H 'Host: dylive.nomadro.com' https://127.0.0.1/api/health; "
"wg show wg-dy | head -14",
50,
"local",
)
v.close()
# Physical guest
try:
g = ssh(4014, "eric", "4669805")
run(
g,
"echo 4669805 | sudo -S -p '' bash -lc '"
"wg-quick up wg-dy 2>/dev/null || wg-quick down wg-dy; wg-quick up wg-dy; "
"systemctl start douyinweb-api douyinweb-web; sleep 2; "
"wg show wg-dy | head -12; "
"systemctl is-active douyinweb-api douyinweb-web; "
"curl -sS -m 8 http://127.0.0.1:8014/api/health | head -c 180; echo"
"'",
90,
"guest",
)
g.close()
except Exception as ex:
print(f"guest skip: {ex}", flush=True)
v = ssh(22)
run(
v,
"ping -c 3 -W 2 10.8.0.2 | tail -5; "
"curl -sS -m 10 -o /dev/null -w 'via_wg=%{http_code}\\n' http://10.8.0.2:8014/api/health",
30,
"tunnel",
)
v.close()
probe()
print("DONE", flush=True)