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

246 lines
8.3 KiB
Python

#!/usr/bin/env python3
"""Sync SIGILL chrome flags + API memory hard-cap to dy.nomadro.live; recover neko/7."""
from __future__ import annotations
import sys
import time
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
HOST = "108.174.50.68"
PASS = "Xiao4669805"
LOCAL = Path(__file__).resolve().parents[1] / "backend" / "neko" / "browser-launch.sh"
def connect(retries: int = 10):
last = None
for i in range(retries):
try:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
HOST,
22,
"root",
PASS,
timeout=45,
allow_agent=False,
look_for_keys=False,
banner_timeout=180,
auth_timeout=60,
)
return c
except Exception as e:
last = e
print(f"ssh_retry {i}: {e}", flush=True)
time.sleep(2 + i)
raise SystemExit(f"ssh_fail {last}")
def run(c, cmd: str, t: int = 300) -> str:
print("\n=====", cmd[:100].replace("\n", " "), flush=True)
_i, o, e = c.exec_command(cmd, timeout=t)
out = (o.read() + e.read()).decode("utf-8", "replace")
print(out[-16000:] if len(out) > 16000 else out, flush=True)
return out
text = LOCAL.read_text(encoding="utf-8")
for needle in ("no-wasm-simd", "PassageEmbeddings", "no-turbofan", "WebAssemblySimd"):
assert needle in text, f"missing {needle}"
c = connect()
# --- 0) inventory ---
run(
c,
r"""
set +e
echo '=== host ==='
hostname; free -h | head -3
docker ps -a --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}' | sort
systemctl is-active douyinweb-api douyinweb-web 2>/dev/null
systemctl show douyinweb-api -p MemoryCurrent,MemoryHigh,MemoryMax,OOMScoreAdjust 2>/dev/null
""",
)
# --- 1) API memory hard-cap (same as dylive) ---
run(
c,
r"""
set -euo pipefail
mkdir -p /etc/systemd/system/douyinweb-api.service.d
cat > /etc/systemd/system/douyinweb-api.service.d/oom-protect-chrome.conf <<'EOF'
[Service]
MemoryAccounting=yes
MemoryHigh=700M
MemoryMax=900M
MemorySwapMax=256M
OOMPolicy=kill
OOMScoreAdjust=400
EOF
systemctl daemon-reload
systemctl restart douyinweb-api.service
sleep 5
systemctl is-active douyinweb-api.service
systemctl show douyinweb-api -p MemoryCurrent,MemoryHigh,MemoryMax,MemorySwapMax,OOMScoreAdjust,OOMPolicy
curl -sS -m 10 http://127.0.0.1:8014/api/health | head -c 220; echo
""",
)
# --- 2) upload browser-launch, overwrite mounted inode ---
sftp = c.open_sftp()
tmp = "/tmp/browser-launch-dy-sigill.sh"
with sftp.file(tmp, "wb") as f:
f.write(text.encode("utf-8"))
sftp.chmod(tmp, 0o755)
sftp.close()
run(
c,
rf"""
set -euo pipefail
mkdir -p /opt/douyinWEB/backend/neko
# ensure capacity so neko-7 exists
cd /opt/douyinWEB/backend
set -a; source /etc/douyinweb.env 2>/dev/null || true; set +a
./.venv/bin/python - <<'PY' || true
from app import youtube_neko as yn
print(yn.ensure_capacity(8, start_instances=set(), reconcile_running=False))
PY
# start neko-7 if not running
docker start douyinweb-neko-7 2>/dev/null || true
sleep 3
docker ps -a --filter name=douyinweb-neko-7 --format '{{.Names}} {{.Status}}'
# find mounted inode and overwrite in-place (Docker file bind keeps old inode after rename)
CTR_INO=$(docker exec douyinweb-neko-7 sh -c "ls -li /etc/neko/browser-launch.sh" 2>/dev/null | awk '{{print $1}}' || true)
echo CTR_INO=$CTR_INO
HOST_MATCH=""
if [ -n "$CTR_INO" ]; then
HOST_MATCH=$(ls -li /opt/douyinWEB/backend/neko/browser-launch.sh* 2>/dev/null | awk -v i="$CTR_INO" '$1==i {{print $NF; exit}}' || true)
fi
echo HOST_MATCH=$HOST_MATCH
if [ -n "$HOST_MATCH" ]; then
cat {tmp} > "$HOST_MATCH"
fi
cat {tmp} > /opt/douyinWEB/backend/neko/browser-launch.sh
chmod 755 /opt/douyinWEB/backend/neko/browser-launch.sh
# if still not visible, force-recreate neko-7 to remount
if ! docker exec douyinweb-neko-7 grep -q no-turbofan /etc/neko/browser-launch.sh 2>/dev/null; then
echo RECREATE_NEKO7
COMPOSE=$(docker inspect douyinweb-neko-7 --format '{{{{index .Config.Labels "com.docker.compose.project.working_dir"}}}}' 2>/dev/null || true)
PROJ=$(docker inspect douyinweb-neko-7 --format '{{{{index .Config.Labels "com.docker.compose.project"}}}}' 2>/dev/null || true)
SVC=$(docker inspect douyinweb-neko-7 --format '{{{{index .Config.Labels "com.docker.compose.service"}}}}' 2>/dev/null || true)
echo COMPOSE=$COMPOSE PROJ=$PROJ SVC=$SVC
if [ -n "$COMPOSE" ] && [ -n "$SVC" ]; then
cd "$COMPOSE"
docker compose -p "$PROJ" up -d --force-recreate --no-deps "$SVC"
else
docker stop douyinweb-neko-7 || true
docker rm douyinweb-neko-7 || true
cd /opt/douyinWEB/backend
set -a; source /etc/douyinweb.env 2>/dev/null || true; set +a
./.venv/bin/python - <<'PY'
from app import youtube_neko as yn
print(yn.ensure_capacity(8, start_instances={{7}}, reconcile_running=True))
PY
fi
sleep 12
fi
docker exec douyinweb-neko-7 grep -n 'no-turbofan\|PassageEmbeddings\|no-wasm-simd\|renderer-process-limit' /etc/neko/browser-launch.sh
docker exec douyinweb-neko-7 supervisorctl restart google-chrome
sleep 12
docker exec douyinweb-neko-7 supervisorctl status google-chrome
docker exec douyinweb-neko-7 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 "js-flags|turbofan|Passage|renderer-process|wasm|max-old"; break ;;
esac
done'
# recover tabs: close crash junk, open studio or youtube home light
docker exec douyinweb-neko-7 python3 -c '
import json, urllib.request, time
for i in range(40):
try:
urllib.request.urlopen("http://127.0.0.1:9222/json/version", timeout=2).read(); print("devtools_ok"); break
except Exception:
time.sleep(1)
else:
raise SystemExit("no_devtools")
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":
continue
tid=t.get("id") or ""
url=t.get("url") or ""
title=t.get("title") or ""
# close Aw Snap / heavy youtube.com homepage spam; keep one studio if present
if tid and ("chrome-error://" in url or "Aw, Snap" in title or url.rstrip("/") in ("https://www.youtube.com","https://youtube.com","https://www.google.com")):
try:
urllib.request.urlopen("http://127.0.0.1:9222/json/close/"+tid, timeout=3).read()
print("CLOSE", title[:40], url[:80])
except Exception as e:
print("close_err", e)
# prefer open studio livestreaming blank dashboard of creator studio
S="https://studio.youtube.com/"
req=urllib.request.Request("http://127.0.0.1:9222/json/new?"+S, method="PUT")
try:
print("OPEN", urllib.request.urlopen(req, timeout=15).read()[:100])
except Exception as e:
print("open_err", e)
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 "")[:60], "|", (t.get("url") or "")[:110])
'
# oom protect neko chrome pids
for pid in $(docker top douyinweb-neko-7 -eo pid 2>/dev/null | awk "NR>1{{print \$1}}"); do
echo -800 > /proc/$pid/oom_score_adj 2>/dev/null || true
done
echo oom_adj_done
""",
)
# --- 3) also patch all other running nekos' visible launch script + optional restart ---
run(
c,
rf"""
set +e
# sync file for future starts
cat {tmp} > /opt/douyinWEB/backend/neko/browser-launch.sh
# for each running neko, overwrite its mounted inode
for name in $(docker ps --filter name=douyinweb-neko --format '{{{{.Names}}}}'); do
ino=$(docker exec "$name" sh -c 'ls -li /etc/neko/browser-launch.sh' 2>/dev/null | awk '{{print $1}}')
[ -z "$ino" ] && continue
match=$(ls -li /opt/douyinWEB/backend/neko/browser-launch.sh* 2>/dev/null | awk -v i="$ino" '$1==i {{print $NF; exit}}')
if [ -n "$match" ]; then
cat {tmp} > "$match"
echo patched_inode $name $match
fi
if ! docker exec "$name" grep -q no-turbofan /etc/neko/browser-launch.sh 2>/dev/null; then
echo STALE $name
else
echo OK_VISIBLE $name
fi
done
echo '=== mem ==='
free -h | head -3
docker stats --no-stream --format '{{{{.Name}}}} {{{{.MemUsage}}}}' | head -20
curl -sk -m 10 -o /dev/null -w 'pub_neko7:%{{http_code}}\n' https://dy.nomadro.live/neko/7/
dmesg -T 2>/dev/null | grep -iE 'invalid opcode|oom-kill' | tail -5
""",
)
c.close()
print("DONE")