141 lines
5.0 KiB
Python
141 lines
5.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Deploy Studio Live Chrome OOM mitigations to dylive (and dy). Never recreate Neko."""
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
PASS = "Xiao4669805"
|
|
|
|
FILES = [
|
|
("backend/neko/browser-launch.sh", "/opt/douyinWEB/backend/neko/browser-launch.sh", 0o755),
|
|
("backend/neko/live-ops-extension/content.js", "/opt/douyinWEB/backend/neko/live-ops-extension/content.js", 0o644),
|
|
("backend/neko/live-ops-extension/manifest.json", "/opt/douyinWEB/backend/neko/live-ops-extension/manifest.json", 0o644),
|
|
]
|
|
|
|
|
|
def connect(host: str):
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
for i in range(12):
|
|
try:
|
|
c.connect(
|
|
host,
|
|
22,
|
|
"root",
|
|
PASS,
|
|
timeout=60,
|
|
allow_agent=False,
|
|
look_for_keys=False,
|
|
banner_timeout=300,
|
|
)
|
|
return c
|
|
except Exception as e:
|
|
print(f"retry {host} {i}: {e}", flush=True)
|
|
time.sleep(2 + i)
|
|
raise SystemExit(f"ssh_fail {host}")
|
|
|
|
|
|
def put(c, local: Path, remote: str, mode: int) -> None:
|
|
raw = local.read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n")
|
|
sftp = c.open_sftp()
|
|
tmp = f"/tmp/{Path(remote).name}.oomfix"
|
|
with sftp.file(tmp, "wb") as f:
|
|
f.write(raw)
|
|
sftp.chmod(tmp, mode)
|
|
sftp.close()
|
|
_i, o, e = c.exec_command(f"cat {tmp} > {remote} && chmod {mode:o} {remote} && sed -i 's/\\r$//' {remote}", timeout=30)
|
|
o.channel.recv_exit_status()
|
|
print(f"wrote {remote} bytes={len(raw)}", flush=True)
|
|
|
|
|
|
REMOTE = r"""
|
|
set +e
|
|
# syntax check
|
|
sh -n /opt/douyinWEB/backend/neko/browser-launch.sh && echo launch_syntax_ok
|
|
grep -n 'renderer-process-limit=1\|prune_and_soft_reload\|max-old-space-size=160\|tameMedia\|1.4.0' \
|
|
/opt/douyinWEB/backend/neko/browser-launch.sh \
|
|
/opt/douyinWEB/backend/neko/live-ops-extension/content.js \
|
|
/opt/douyinWEB/backend/neko/live-ops-extension/manifest.json | head -20
|
|
|
|
# Only touch running nekos — restart remounts browser-launch (fixes //deleted) without recreate
|
|
for n in $(docker ps --filter name=douyinweb-neko --format '{{.Names}}' | sort -V); do
|
|
echo "=== remount+restart chrome path: $n ==="
|
|
# Prefer chrome restart first if mount not deleted
|
|
del=$(docker exec "$n" sh -c 'cat /proc/self/mountinfo | grep browser-launch | grep -c "//deleted"' || true)
|
|
echo deleted=$del
|
|
if [ "${del:-0}" != "0" ]; then
|
|
docker restart "$n"
|
|
sleep 14
|
|
else
|
|
docker exec "$n" sh -c '
|
|
rm -f /home/neko/.config/google-chrome/Singleton* 2>/dev/null
|
|
supervisorctl stop google-chrome || true
|
|
sleep 1
|
|
supervisorctl start google-chrome
|
|
sleep 10
|
|
supervisorctl status google-chrome
|
|
'
|
|
fi
|
|
docker exec "$n" sh -c '
|
|
grep -n "renderer-process-limit=1\|max-old-space-size=160\|prune_and_soft_reload" /etc/neko/browser-launch.sh | head -5
|
|
curl -sS -m 4 http://127.0.0.1:9222/json/version | head -c 120; echo
|
|
'
|
|
# Immediate tab prune (close google.com etc.)
|
|
docker exec "$n" sh -c 'DO_RELOAD=0 python3 - <<"PY"
|
|
import json, urllib.request
|
|
def tabs():
|
|
with urllib.request.urlopen("http://127.0.0.1:9222/json/list", timeout=3) as r:
|
|
return json.loads(r.read().decode())
|
|
def close(tid):
|
|
try: urllib.request.urlopen(f"http://127.0.0.1:9222/json/close/{tid}", timeout=2)
|
|
except Exception: pass
|
|
def is_live(u):
|
|
u=(u or "").lower(); return "studio.youtube.com" in u and "livestreaming" in u
|
|
def keep(u):
|
|
u=(u or "").lower()
|
|
if is_live(u): return True
|
|
if "accounts.google.com" in u and any(x in u for x in ("signin","accountchooser","identifier","challenge","servicelogin")): return True
|
|
return False
|
|
try:
|
|
pages=[t for t in tabs() if t.get("type")=="page"]
|
|
except Exception as e:
|
|
print("no_tabs", e); raise SystemExit(0)
|
|
live=[t for t in pages if is_live(t.get("url") or "")]
|
|
kid=(live[0].get("id") if live else "") or ""
|
|
closed=0
|
|
for t in pages:
|
|
tid=t.get("id") or ""; url=t.get("url") or ""
|
|
if kid and tid==kid: continue
|
|
if is_live(url) and kid and tid!=kid:
|
|
close(tid); closed+=1; continue
|
|
if keep(url): continue
|
|
close(tid); closed+=1
|
|
print("pruned", closed, "kept_live", bool(kid), "pages_left", len([t for t in tabs() if t.get("type")=="page"]))
|
|
for t in tabs():
|
|
if t.get("type")=="page":
|
|
print("TAB", (t.get("url") or "")[:90])
|
|
PY'
|
|
docker stats --no-stream --format '{{.Name}} mem={{.MemUsage}}' "$n"
|
|
done
|
|
free -m | head -2
|
|
echo HOST_DONE
|
|
"""
|
|
|
|
|
|
for label, host in (("dylive", "107.173.30.245"), ("dy", "108.174.50.68")):
|
|
print(f"\n######## {label} ########", flush=True)
|
|
c = connect(host)
|
|
for rel, remote, mode in FILES:
|
|
put(c, ROOT / rel, remote, mode)
|
|
_i, o, e = c.exec_command(REMOTE, timeout=300)
|
|
print((o.read() + e.read()).decode("utf-8", "replace"), flush=True)
|
|
c.close()
|
|
|
|
print("ALL_DONE", flush=True)
|