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

171 lines
5.6 KiB
Python

#!/usr/bin/env python3
"""Emergency: docker start stopped nekos on dy (no recreate). Then finish dylive deploy carefully."""
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/app/yt_assign_db.py",
"backend/app/yt_stream_key_db.py",
"backend/app/youtube_relay_status.py",
"backend/app/main.py",
"frontend/src/lib/ytRelayStatus.ts",
"frontend/src/components/YtAssignPanel.tsx",
"frontend/src/components/YtRelayProStatusPanel.tsx",
"frontend/src/app/youtube/keys/page.tsx",
"frontend/src/app/youtube-relay-log/page.tsx",
]
def connect(host: str):
last = None
for i in range(10):
try:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
host,
22,
"root",
PASS,
timeout=60,
allow_agent=False,
look_for_keys=False,
banner_timeout=240,
)
return c
except Exception as e:
last = e
print("retry", i, e, flush=True)
time.sleep(3 + i)
raise SystemExit(last)
def run(c, cmd, t=600):
_i, o, e = c.exec_command(cmd, timeout=t)
out = (o.read() + e.read()).decode("utf-8", "replace")
code = o.channel.recv_exit_status()
print(out[-8000:] if len(out) > 8000 else out, flush=True)
return code, out
print("===== revive dy neko 2/3 =====", flush=True)
dy = connect("108.174.50.68")
run(
dy,
r"""
set +e
echo before:
docker ps -a --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}' | sort
# ONLY start — never rm/recreate
docker start douyinweb-neko-2 douyinweb-neko-3
sleep 8
echo after:
docker ps -a --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}' | sort
free -h | head -2
# confirm profiles mounts still there
docker inspect douyinweb-neko-2 --format '{{range .Mounts}}{{.Source}} -> {{.Destination}}{{println}}{{end}}' | grep profiles || true
""",
)
dy.close()
print("===== dylive: API only first, build with swap pressure care =====", flush=True)
dl = connect("107.173.30.245")
_, before = run(dl, "docker ps --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}' | sort; free -h | head -2")
sftp = dl.open_sftp()
for rel in FILES:
local = ROOT / rel
remote = f"/opt/douyinWEB/{rel}"
run(dl, f"mkdir -p {'/'.join(remote.split('/')[:-1])}")
print("put", rel, flush=True)
sftp.put(str(local), remote)
sftp.close()
# API restart only — skip npm build on low-mem host; rsync .next from dy instead
run(
dl,
r"""
set -euo pipefail
cd /opt/douyinWEB/backend
./.venv/bin/python -c 'from app import yt_assign_db; gs=yt_assign_db.list_groups(include_urls=False, include_secret=True); print(len(gs), sum(1 for g in gs if g.get("stream_key")))'
systemctl restart douyinweb-api
sleep 4
systemctl is-active douyinweb-api
docker ps --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}' | sort
""",
)
dl.close()
# Copy built frontend artifacts from dy -> dylive to avoid OOM build on 3.8G
print("===== copy .next from dy to dylive =====", flush=True)
dy = connect("108.174.50.68")
# tar .next + relevant static on dy, stream via sftp is heavy; use scp-like paramiko
run(
dy,
r"""
set -euo pipefail
cd /opt/douyinWEB/frontend
tar -czf /tmp/dy-next-keys.tgz .next/static .next/server .next/BUILD_ID 2>/dev/null || tar -czf /tmp/dy-next-keys.tgz .next
ls -lh /tmp/dy-next-keys.tgz
""",
)
# download to local then upload to dylive
sftp = dy.open_sftp()
local_tgz = ROOT / "scripts" / "_dy_next_keys.tgz"
sftp.get("/tmp/dy-next-keys.tgz", str(local_tgz))
sftp.close()
dy.close()
print("downloaded", local_tgz.stat().st_size, flush=True)
dl = connect("107.173.30.245")
_, before2 = run(dl, "docker ps --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}' | sort")
sftp = dl.open_sftp()
sftp.put(str(local_tgz), "/tmp/dy-next-keys.tgz")
# also ensure source files for keys page exist
for rel in [
"frontend/src/app/youtube/keys/page.tsx",
"frontend/src/components/YtAssignPanel.tsx",
"frontend/src/components/YtRelayProStatusPanel.tsx",
]:
sftp.put(str(ROOT / rel), f"/opt/douyinWEB/{rel}")
sftp.close()
run(
dl,
r"""
set -euo pipefail
cd /opt/douyinWEB/frontend
# backup then extract built assets only
mkdir -p .next
tar -xzf /tmp/dy-next-keys.tgz -C /opt/douyinWEB/frontend
systemctl restart douyinweb-web
sleep 3
systemctl is-active douyinweb-web
docker ps --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}' | sort
curl -sk -o /dev/null -w 'keys:%{http_code}\n' https://dylive.nomadro.com/youtube/keys
curl -sk -o /dev/null -w 'yt:%{http_code}\n' https://dylive.nomadro.com/youtube
""",
)
_, after2 = run(dl, "docker ps --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}' | sort")
before_up = {ln.split()[0] for ln in before2.splitlines() if " Up " in ln and "neko" in ln}
after_up = {ln.split()[0] for ln in after2.splitlines() if " Up " in ln and "neko" in ln}
lost = before_up - after_up
if lost:
print("CRITICAL dylive neko lost", lost)
# try start only
run(dl, "docker start " + " ".join(lost) + "; sleep 5; docker ps --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}' | sort")
dl.close()
# final dy check
dy = connect("108.174.50.68")
run(dy, "docker ps --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}' | sort; curl -sk -o /dev/null -w 'keys:%{http_code}\\n' https://dy.nomadro.live/youtube/keys")
dy.close()
print("DONE")