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

138 lines
4.8 KiB
Python

#!/usr/bin/env python3
"""Short-step post-reboot recover. Reconnect every command. No Neko start."""
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 connect(port=22, user="root", pw="Xiao4669805"):
for i in range(12):
try:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
HOST,
port,
user,
pw,
timeout=25,
banner_timeout=40,
auth_timeout=25,
allow_agent=False,
look_for_keys=False,
)
print(f" connected :{port}", flush=True)
return c
except Exception as ex:
print(f" connect fail {i}: {ex}", flush=True)
time.sleep(2)
raise RuntimeError("cannot connect")
def step(title: str, cmd: str, t: int = 60, port=22, user="root", pw="Xiao4669805"):
print(f"\n## {title}", flush=True)
print(f"$ {cmd[:180]}", flush=True)
c = connect(port, user, pw)
try:
_i, o, e = c.exec_command(cmd, timeout=t)
out = b""
while not o.channel.exit_status_ready():
if o.channel.recv_ready():
chunk = o.channel.recv(4096)
out += chunk
sys.stdout.write(chunk.decode("utf-8", "replace"))
sys.stdout.flush()
if o.channel.recv_stderr_ready():
chunk = o.channel.recv_stderr(4096)
out += chunk
sys.stdout.write(chunk.decode("utf-8", "replace"))
sys.stdout.flush()
time.sleep(0.1)
rest = o.read() + e.read()
if rest:
sys.stdout.write(rest.decode("utf-8", "replace"))
sys.stdout.flush()
code = o.channel.recv_exit_status()
print(f"→ {code}", flush=True)
return code
finally:
c.close()
# Wait a bit for boot settle
print("boot settle 8s...", flush=True)
time.sleep(8)
step("uptime", "uptime; free -h | head -2")
step(
"disable docker autostart neko restart policy + stop running neko",
"systemctl stop docker.socket 2>/dev/null; "
"systemctl start docker; sleep 2; "
"docker ps -a --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}' || true; "
"for c in $(docker ps -aq --filter name=douyinweb-neko); do docker update --restart=no $c; docker stop -t 1 $c; done; "
"docker ps --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}' || echo no_running_neko; "
"free -h | head -2",
t=90,
)
step("start seaweed+frpp+nginx", "systemctl start seaweed-master seaweed-volume seaweed-filer seaweed-s3 frpp nginx; systemctl is-active seaweed-s3 frpp nginx")
step("start douyinweb", "systemctl start douyinweb-pb douyinweb-yt douyinweb-api douyinweb-web; sleep 4; systemctl is-active douyinweb-api douyinweb-web douyinweb-yt douyinweb-pb")
step("start caddy+wg", "systemctl start caddy 'wg-quick@wg-dy'; sleep 2; systemctl is-active caddy; wg show wg-dy | head -10 || true")
step(
"listen+health",
"ss -lntp | grep -E ':(443|80|8014|4015|4014)\\b' || true; "
"curl -sS -m 10 http://127.0.0.1:8014/api/health | head -c 250; echo; "
"curl -sS -m 8 -o /dev/null -w 'web=%{http_code} yt=' http://127.0.0.1:4015/; "
"curl -sS -m 8 -o /dev/null -w '%{http_code}\\n' http://127.0.0.1:4015/youtube; "
"curl -sS -m 8 -o /dev/null -w 'caddy=%{http_code}\\n' -k -H 'Host: dylive.nomadro.com' https://127.0.0.1/api/health",
t=50,
)
print("\n## guest FRP", flush=True)
try:
step(
"guest wg+api",
"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; "
"systemctl is-active douyinweb-api douyinweb-web; "
"curl -sS -m 8 http://127.0.0.1:8014/api/health | head -c 160; echo; "
"wg show wg-dy | head -10'",
t=80,
port=4014,
user="eric",
pw="4669805",
)
except Exception as ex:
print(f"guest fail: {ex}", flush=True)
step(
"tunnel",
"ping -c 2 -W 2 10.8.0.2 | tail -3; curl -sS -m 8 -o /dev/null -w 'via_wg=%{http_code}\\n' http://10.8.0.2:8014/api/health",
t=25,
)
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}", flush=True)
except Exception as ex:
print(f"{u} ERR {ex}", flush=True)
print("DONE", flush=True)