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

178 lines
5.9 KiB
Python

#!/usr/bin/env python3
"""Fix Docker file-bind inode trap: patch the inode neko-12 still mounts, or recreate."""
from __future__ import annotations
import sys
import time
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
LOCAL = Path(__file__).resolve().parents[1] / "backend" / "neko" / "browser-launch.sh"
text = LOCAL.read_text(encoding="utf-8")
assert "no-wasm-simd" in text
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
"107.173.30.245",
22,
"root",
"Xiao4669805",
timeout=45,
allow_agent=False,
look_for_keys=False,
banner_timeout=120,
)
sftp = c.open_sftp()
def run(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[-14000:] if len(out) > 14000 else out, flush=True)
return out
# Compare inodes / hashes
run(
r"""
set +e
echo HOST_CUR:
ls -li /opt/douyinWEB/backend/neko/browser-launch.sh
md5sum /opt/douyinWEB/backend/neko/browser-launch.sh
echo HOST_BAKS:
ls -li /opt/douyinWEB/backend/neko/browser-launch.sh.bak-sigill* 2>/dev/null | tail -10
echo CTR:
docker exec douyinweb-neko-12 sh -c 'ls -li /etc/neko/browser-launch.sh; md5sum /etc/neko/browser-launch.sh; grep -n max-old /etc/neko/browser-launch.sh'
"""
)
# Write patched bytes into EVERY bak + current path (overwrite in place, no rename)
tmp = "/tmp/browser-launch-final.sh"
with sftp.file(tmp, "wb") as f:
f.write(text.encode("utf-8"))
sftp.chmod(tmp, 0o755)
run(
rf"""
set -euo pipefail
# Overwrite current path in-place (same inode if possible via cat redirect onto existing)
# First: identify which host inode matches container
CTR_INO=$(docker exec douyinweb-neko-12 sh -c "ls -li /etc/neko/browser-launch.sh" | awk '{{print $1}}')
echo CTR_INO=$CTR_INO
HOST_MATCH=$(ls -li /opt/douyinWEB/backend/neko/browser-launch.sh* 2>/dev/null | awk -v i="$CTR_INO" '$1==i {{print $NF; exit}}')
echo HOST_MATCH=$HOST_MATCH
if [ -n "$HOST_MATCH" ]; then
# in-place overwrite keeping inode: use dd/cat truncating same file
cat {tmp} > "$HOST_MATCH"
echo overwritten_match
ls -li "$HOST_MATCH"
md5sum "$HOST_MATCH"
fi
# also ensure canonical path has content (may be different inode)
cat {tmp} > /opt/douyinWEB/backend/neko/browser-launch.sh
chmod 755 /opt/douyinWEB/backend/neko/browser-launch.sh
echo AFTER_CTR:
docker exec douyinweb-neko-12 sh -c 'ls -li /etc/neko/browser-launch.sh; grep -n "no-wasm-simd\|WebAssemblySimd\|max-old" /etc/neko/browser-launch.sh'
"""
)
# If container still stale, recreate neko-12 via API helper / compose
out = run(
r"""
set +e
if docker exec douyinweb-neko-12 grep -q no-wasm-simd /etc/neko/browser-launch.sh; then
echo PATCH_VISIBLE
else
echo PATCH_NOT_VISIBLE_RECREATE
cd /opt/douyinWEB/backend
set -a; source /etc/douyinweb.env; set +a
./.venv/bin/python - <<'PY'
from app import youtube_neko as yn
# recreate only instance 12 if API exists
for name in ("recreate_instance", "restart_instance", "ensure_instance"):
fn = getattr(yn, name, None)
print("has", name, bool(fn))
# force recreate via docker compose
import subprocess
from pathlib import Path
cp = yn._compose_path()
print("compose", cp)
# docker compose up -d --force-recreate this service
svc = None
for cand in ("neko-12", "douyinweb-neko-12", "neko_12"):
pass
# inspect compose service names
text = Path(cp).read_text(encoding="utf-8")
for line in text.splitlines():
if "neko-12" in line or "container_name: douyinweb-neko-12" in line:
print("line", line.strip()[:120])
PY
# docker compose force recreate by container name
COMPOSE=$(docker inspect douyinweb-neko-12 --format '{{index .Config.Labels "com.docker.compose.project.working_dir"}}')
PROJ=$(docker inspect douyinweb-neko-12 --format '{{index .Config.Labels "com.docker.compose.project"}}')
SVC=$(docker inspect douyinweb-neko-12 --format '{{index .Config.Labels "com.docker.compose.service"}}')
echo COMPOSE=$COMPOSE PROJ=$PROJ SVC=$SVC
if [ -n "$COMPOSE" ] && [ -n "$SVC" ]; then
cd "$COMPOSE"
# keep profile volume — just recreate container to remount file inode
docker compose -p "$PROJ" up -d --force-recreate --no-deps "$SVC"
else
docker stop douyinweb-neko-12
docker rm douyinweb-neko-12
# regenerate via ensure_capacity
cd /opt/douyinWEB/backend
set -a; source /etc/douyinweb.env; set +a
./.venv/bin/python - <<'PY'
from app import youtube_neko as yn
print(yn.ensure_capacity(12, start_instances={12}, reconcile_running=True))
PY
fi
sleep 15
docker ps --filter name=douyinweb-neko-12 --format '{{.Names}} {{.Status}}'
docker exec douyinweb-neko-12 grep -n 'no-wasm-simd\|max-old' /etc/neko/browser-launch.sh
fi
"""
)
# restart chrome + open studio
run(
r"""
set +e
docker exec douyinweb-neko-12 supervisorctl restart google-chrome || true
sleep 12
docker exec douyinweb-neko-12 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|wasm|WebAssembly|max-old"; break ;;
esac
done'
docker exec douyinweb-neko-12 python3 -c '
import json,urllib.request,time
S="https://studio.youtube.com/channel/UCfP5vcTQEfrHc13yTmmokQw/livestreaming"
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")
req=urllib.request.Request("http://127.0.0.1:9222/json/new?"+S, method="PUT")
print("new", urllib.request.urlopen(req,timeout=15).read()[:100])
time.sleep(8)
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 "")[:70], "|", (t.get("url") or "")[:130])
'
free -h | head -3
"""
)
sftp.close()
c.close()
print("DONE")