douyinWEB/scripts/_deploy_neko_sh2_assets.py
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 22:42:40 -05:00

124 lines
4.5 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy sh2-aligned Neko assets (Tampermonkey + userscript + live-ops) to US VPS."""
from __future__ import annotations
import os
import sys
import tarfile
import tempfile
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(__file__).resolve().parents[1]
HOST = "108.174.50.68"
PASS = "Xiao4669805"
REMOTE = "/opt/douyinWEB"
INCLUDE = [
"backend/neko",
"backend/app/youtube_neko.py",
"backend/app/config.py",
"backend/app/main.py",
"backend/app/session_auth.py",
]
def _add(tar: tarfile.TarFile, rel: str) -> None:
path = ROOT / rel
if not path.exists():
raise SystemExit(f"missing {path}")
tar.add(path, arcname=rel.replace("\\", "/"))
print("pack", rel)
def main() -> None:
# Normalize shell script LF before pack
launch = ROOT / "backend" / "neko" / "browser-launch.sh"
text = launch.read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n")
launch.write_bytes(text)
fd, tmp = tempfile.mkstemp(suffix=".tgz")
os.close(fd)
tgz = Path(tmp)
try:
with tarfile.open(tgz, "w:gz") as tar:
for rel in INCLUDE:
_add(tar, rel)
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)
sftp = c.open_sftp()
remote_tgz = "/tmp/douyinweb-neko-sh2.tgz"
print("upload", tgz.stat().st_size, "bytes")
sftp.put(str(tgz), remote_tgz)
sftp.close()
cmd = r"""
set -e
cd /opt/douyinWEB
tar xzf /tmp/douyinweb-neko-sh2.tgz
chmod +x /opt/douyinWEB/backend/neko/browser-launch.sh
# force recreate so volume mounts apply
systemctl restart douyinweb-api
sleep 3
systemctl is-active douyinweb-api
cd /opt/douyinWEB/backend
.venv/bin/python - <<'PY'
from app import youtube_neko, yt_assign_db
print('assets', youtube_neko._asset_root())
print('seed', (youtube_neko._asset_root()/'tampermonkey-seed/Default/Extensions').is_dir())
print('script', (youtube_neko._asset_root()/'provisioning/youtube-studio-auto-dismiss.user.js').is_file())
groups = yt_assign_db.list_groups(include_urls=False)
# recreate compose + restart enabled instances
r = youtube_neko.sync_for_groups(groups)
print('sync', r)
# force recreate containers for new mounts
import subprocess
desired = max([int(g.get('neko_instance_id') or 0) for g in groups] + [0])
if desired:
compose = youtube_neko._compose_path()
# rewrite compose even if identical timestamps
compose.write_text(youtube_neko._render_compose(desired), encoding='utf-8')
cmd = youtube_neko._compose_cmd('up', '-d', '--force-recreate', '--no-start')
code, out = youtube_neko._run(cmd, timeout=600)
print('recreate', code, out[-400:])
start = {int(g.get('neko_instance_id') or 0) for g in groups if g.get('neko_enabled')}
for i in sorted(start):
if i:
youtube_neko._run(['docker', 'start', youtube_neko._container_name(i)], timeout=60)
print('started', i)
st = youtube_neko.status_summary(desired=desired)
print('status assets', st.get('assets'))
for inst in st.get('instances') or []:
print('inst', inst.get('id'), inst.get('status'), 'running', inst.get('running'), 'ready', inst.get('ready'))
PY
# verify mounts inside running container
for id in 1 2; do
name=douyinweb-neko-$id
if docker inspect $name >/dev/null 2>&1; then
echo "--- $name mounts ---"
docker inspect $name --format '{{range .Mounts}}{{.Source}} -> {{.Destination}}{{println}}{{end}}' | grep -E 'tampermonkey|browser-launch|live-ops|provisioning|policies|supervisord' || true
if docker ps --format '{{.Names}}' | grep -qx $name; then
echo "--- $name seed check ---"
docker exec $name sh -c 'ls /etc/neko/tampermonkey-seed/Default/Extensions 2>/dev/null; test -f /etc/neko/browser-launch.sh && echo browser_launch_ok; test -f /var/www/provisioning/youtube-studio-auto-dismiss.user.js && echo userscript_ok; test -f /etc/neko/live-ops-extension/manifest.json && echo live_ops_ok; ss -ltn 2>/dev/null | grep 18080 || netstat -ltn 2>/dev/null | grep 18080 || echo provisioning_port_check'
fi
fi
done
"""
_i, o, e = c.exec_command(cmd, timeout=600)
print(o.read().decode("utf-8", "replace"))
err = e.read().decode("utf-8", "replace")
if err.strip():
print("STDERR", err[:2000])
c.close()
finally:
tgz.unlink(missing_ok=True)
if __name__ == "__main__":
main()