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

110 lines
3.6 KiB
Python

#!/usr/bin/env python3
"""Fix dy NAT1TO1 regression + restart chrome on running nekos with new flags + verify health."""
from __future__ import annotations
import sys
import time
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
"108.174.50.68",
22,
"root",
"Xiao4669805",
timeout=45,
allow_agent=False,
look_for_keys=False,
banner_timeout=180,
)
def run(cmd: str, t: int = 240) -> str:
print("\n=====", cmd[:90].replace("\n", " "), flush=True)
_i, o, e = c.exec_command(cmd, timeout=t)
out = (o.read() + e.read()).decode("utf-8", "replace")
print(out[-12000:] if len(out) > 12000 else out, flush=True)
return out
run(
r"""
set -euo pipefail
# force public NAT, never 198.18
grep -q '^DY_NEKO_NAT1TO1=' /etc/douyinweb.env \
&& sed -i 's/^DY_NEKO_NAT1TO1=.*/DY_NEKO_NAT1TO1=108.174.50.68/' /etc/douyinweb.env \
|| echo 'DY_NEKO_NAT1TO1=108.174.50.68' >> /etc/douyinweb.env
grep -q '^DY_NEKO_VPS_NAT1TO1=' /etc/douyinweb.env \
&& sed -i 's/^DY_NEKO_VPS_NAT1TO1=.*/DY_NEKO_VPS_NAT1TO1=108.174.50.68/' /etc/douyinweb.env \
|| echo 'DY_NEKO_VPS_NAT1TO1=108.174.50.68' >> /etc/douyinweb.env
cd /opt/douyinWEB/backend
set -a; source /etc/douyinweb.env; set +a
export DY_NEKO_NAT1TO1=108.174.50.68
export DY_NEKO_VPS_NAT1TO1=108.174.50.68
./.venv/bin/python - <<'PY'
from app import youtube_neko as yn
from pathlib import Path
# rewrite compose with correct NAT; do not start extras
out = yn.ensure_capacity(8, start_instances=set(), reconcile_running=False)
print(out)
text = Path(yn._compose_path()).read_text(encoding='utf-8')
for ln in text.splitlines():
if 'NAT' in ln.upper() or 'nat1' in ln.lower():
print('compose', ln.strip()[:140])
PY
# restart chrome on all running nekos so new flags apply
for name in $(docker ps --filter name=douyinweb-neko --format '{{.Names}}'); do
echo "RESTART_CHROME $name"
docker exec "$name" supervisorctl restart google-chrome || true
done
sleep 10
for name in $(docker ps --filter name=douyinweb-neko --format '{{.Names}}'); do
echo "== $name =="
docker exec "$name" supervisorctl status google-chrome || true
docker exec "$name" sh -c 'for p in /proc/[0-9]*; do
cmd=$(tr "\0" " " < "$p/cmdline" 2>/dev/null)
case "$cmd" in
*remote-debugging-port=9222*) echo "$cmd" | tr " " "\n" | grep -E "no-turbofan|no-wasm|Passage|renderer-process" | head -5; break ;;
esac
done' || true
# oom protect
for pid in $(docker top "$name" -eo pid 2>/dev/null | awk 'NR>1{print $1}'); do
echo -800 > /proc/$pid/oom_score_adj 2>/dev/null || true
done
done
# reopen studio on neko-7
docker exec douyinweb-neko-7 python3 -c '
import json,urllib.request,time
S="https://studio.youtube.com/channel/UC6ZbNjX9jg6-AeQrqwday8Q/livestreaming"
for i in range(30):
try:
urllib.request.urlopen("http://127.0.0.1:9222/json/version",timeout=2).read(); break
except Exception:
time.sleep(1)
req=urllib.request.Request("http://127.0.0.1:9222/json/new?"+S, method="PUT")
print(urllib.request.urlopen(req,timeout=15).read()[:80])
time.sleep(5)
tabs=json.loads(urllib.request.urlopen("http://127.0.0.1:9222/json/list",timeout=5).read())
for t in tabs:
if t.get("type")=="page":
print("TAB",(t.get("title") or "")[:55], "|", (t.get("url") or "")[:100])
'
sleep 3
curl -sS -m 10 http://127.0.0.1:8014/api/health | head -c 300; echo
systemctl show douyinweb-api -p MemoryHigh,MemoryMax,OOMScoreAdjust
free -h | head -3
docker stats --no-stream --format '{{.Name}} {{.MemUsage}}' | head -10
grep -n NAT1TO1 /etc/douyinweb.env
"""
)
c.close()
print("DONE")