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

148 lines
4.6 KiB
Python

#!/usr/bin/env python3
"""Deploy Neko mem_limit (1536m) without recreating containers / touching Chrome profiles.
Uses `docker update` so running Studio + Google login stay intact.
"""
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]
def ssh():
last = None
for i in range(6):
try:
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,
)
return c
except Exception as ex:
last = ex
print("retry", i, ex, flush=True)
time.sleep(3)
raise SystemExit(last)
def run(c, cmd, t=360):
print(f"\n$ {cmd[:240]}", flush=True)
_i, o, e = c.exec_command(cmd, timeout=t)
code = o.channel.recv_exit_status()
out = (o.read() + e.read()).decode("utf-8", "replace")
print(out[-16000:] if len(out) > 16000 else out, flush=True)
return code, out
c = ssh()
sftp = c.open_sftp()
sftp.put(str(ROOT / "backend/app/youtube_neko.py"), "/tmp/youtube_neko.py")
sftp.put(str(ROOT / "backend/app/config.py"), "/tmp/config.py")
sftp.close()
code, _ = run(
c,
r"""
set -euo pipefail
# --- BEFORE: fingerprint login profile (must not change) ---
PROF12=/opt/douyinWEB/backend/data/neko/profiles/12
echo '=== BEFORE profile12 fingerprint ==='
ls -la "$PROF12/Default/Cookies" "$PROF12/Default/Login Data" 2>/dev/null || ls -la "$PROF12/Default/" | head -20
python3 - <<'PY'
from pathlib import Path
p=Path("/opt/douyinWEB/backend/data/neko/profiles/12/Default")
for name in ("Cookies","Login Data","Preferences"):
f=p/name
if f.exists():
st=f.stat()
print(f"{name}\tsize={st.st_size}\tmtime={int(st.st_mtime)}")
else:
print(f"{name}\tMISSING")
open("/tmp/neko12_fp_before.txt","w").write(
"\n".join(
f"{n}\t{(p/n).stat().st_size if (p/n).exists() else -1}"
for n in ("Cookies","Login Data")
)
)
PY
install -m 0644 /tmp/youtube_neko.py /opt/douyinWEB/backend/app/youtube_neko.py
install -m 0644 /tmp/config.py /opt/douyinWEB/backend/app/config.py
systemctl restart douyinweb-api
for i in $(seq 1 45); do
curl -sS -m 2 http://127.0.0.1:8014/api/health >/dev/null 2>&1 && break
sleep 1
done
curl -sS -m 5 http://127.0.0.1:8014/api/health || true
# Rewrite compose YAML only (no force-recreate — preserve Chrome)
cd /opt/douyinWEB/backend
./.venv/bin/python - <<'PY'
from app import youtube_neko
# desired from DB assignments; do not start/stop
d = max(youtube_neko._desired_from_db(), 12)
r = youtube_neko.ensure_capacity(d, start_instances=set(), reconcile_running=False)
print(r)
# show mem_limit lines landed in compose
text = youtube_neko._compose_path().read_text(encoding="utf-8")
print("mem_limit_lines", sum(1 for ln in text.splitlines() if "mem_limit" in ln))
print([ln.strip() for ln in text.splitlines() if "mem" in ln.lower()][:8])
PY
# Live apply hard cap WITHOUT recreate (login/tabs stay)
echo '=== docker update mem on all neko containers ==='
for name in $(docker ps -a --filter name=douyinweb-neko- --format '{{.Names}}'); do
echo "update $name"
docker update --memory 1536m --memory-swap 1536m "$name"
done
echo '=== AFTER limits ==='
docker stats --no-stream --format 'table {{.Name}}\t{{.MemUsage}}\t{{.MemPerc}}' $(docker ps -q --filter name=douyinweb-neko) 2>/dev/null || true
docker inspect douyinweb-neko-12 --format 'Memory={{.HostConfig.Memory}} MemorySwap={{.HostConfig.MemorySwap}}' || true
echo '=== AFTER profile12 fingerprint ==='
python3 - <<'PY'
from pathlib import Path
p=Path("/opt/douyinWEB/backend/data/neko/profiles/12/Default")
for name in ("Cookies","Login Data","Preferences"):
f=p/name
if f.exists():
st=f.stat()
print(f"{name}\tsize={st.st_size}\tmtime={int(st.st_mtime)}")
else:
print(f"{name}\tMISSING")
before=Path("/tmp/neko12_fp_before.txt").read_text().strip().splitlines()
after={n: ((p/n).stat().st_size if (p/n).exists() else -1) for n in ("Cookies","Login Data")}
ok=True
for line in before:
n,s=line.split("\t")
if after.get(n)!=int(s):
print("MISMATCH", n, "before", s, "after", after.get(n))
ok=False
print("LOGIN_FILES_INTACT" if ok else "LOGIN_FILES_CHANGED")
PY
free -h
echo DONE
""",
t=240,
)
c.close()
raise SystemExit(code)