douyinWEB/scripts/_deploy_sh2_exact_tm.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

172 lines
6.5 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy exact sh2 Neko stack; remove baked dismiss; wait for TM jsonImport."""
from __future__ import annotations
import sys
import tarfile
import tempfile
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(r"c:\Users\admin\Desktop\douyinWEB")
# normalize LF on shell
launch = ROOT / "backend/neko/browser-launch.sh"
launch.write_bytes(launch.read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n"))
INCLUDE = [
"backend/neko/browser-launch.sh",
"backend/neko/google-chrome.conf",
"backend/neko/chromium.conf",
"backend/neko/policies",
"backend/neko/provisioning",
"backend/neko/live-ops-extension",
"backend/neko/tampermonkey-seed",
"backend/neko/no-managed-policies",
"backend/neko/render_tampermonkey_provisioning.py",
"backend/app/youtube_neko.py",
"backend/app/config.py",
]
fd, tmp = tempfile.mkstemp(suffix=".tgz")
import os
os.close(fd)
tgz = Path(tmp)
with tarfile.open(tgz, "w:gz") as tar:
for rel in INCLUDE:
tar.add(ROOT / rel, arcname=rel)
print("pack", rel)
REMOTE = r"""#!/bin/bash
set -e
cd /opt/douyinWEB
tar xzf /tmp/neko-sh2-exact.tgz
# remove invented file if any
rm -f /opt/douyinWEB/backend/neko/live-ops-extension/auto-dismiss.js
chmod +x /opt/douyinWEB/backend/neko/browser-launch.sh
sed -i 's/\r$//' /opt/douyinWEB/backend/neko/browser-launch.sh
# keep sh2 default managed policies=1
mkdir -p /etc/systemd/system/douyinweb-api.service.d
cat >/etc/systemd/system/douyinweb-api.service.d/neko-sh2.conf <<EOF
[Service]
Environment=DY_NEKO_MANAGED_POLICIES=1
Environment=DY_NEKO_TAMPERMONKEY_REPAIR=1
EOF
systemctl daemon-reload
systemctl restart douyinweb-api
sleep 3
cd /opt/douyinWEB/backend
.venv/bin/python - <<'PY'
import shutil
from pathlib import Path
from app import youtube_neko, yt_assign_db
from app.config import settings
print('managed', settings.neko_managed_policies, youtube_neko._managed_policies_enabled())
assert youtube_neko._managed_policies_enabled(), 'must be managed=1 like sh2'
# wipe TM state so policy jsonImport can land cleanly
for i in (1, 2):
p = Path(f'/opt/douyinWEB/backend/data/neko/profiles/{i}/Default')
for sub in (
f'Extensions/{youtube_neko._TM_EXT_ID}',
f'Local Extension Settings/{youtube_neko._TM_EXT_ID}',
f'Managed Extension Settings/{youtube_neko._TM_EXT_ID}',
):
d = p / sub
if d.exists():
shutil.rmtree(d, ignore_errors=True)
print('cleared', d)
# strip TM prefs entry so force-install is clean
pref = p / 'Preferences'
if pref.exists():
import json
try:
data = json.loads(pref.read_text(encoding='utf-8'))
settings_map = ((data.get('extensions') or {}).get('settings') or {})
if youtube_neko._TM_EXT_ID in settings_map:
settings_map.pop(youtube_neko._TM_EXT_ID, None)
pref.write_text(json.dumps(data, ensure_ascii=False, separators=(',', ':')), encoding='utf-8')
print('cleared prefs TM', i)
except Exception as e:
print('prefs skip', i, e)
groups = yt_assign_db.list_groups(include_urls=False)
desired = max(int(g.get('neko_instance_id') or 0) for g in groups) or 2
compose = youtube_neko._compose_path()
compose.write_text(youtube_neko._render_compose(desired), encoding='utf-8')
# confirm policy mount + no auto-dismiss in live-ops
text = compose.read_text()
assert '/policies:' in text and 'no-managed-policies' not in text
assert 'live-ops-extension' in text and 'tampermonkey-seed' in text and 'browser-launch' in text
code, out = youtube_neko._run(youtube_neko._compose_cmd('up', '-d', '--force-recreate', '--no-start'), timeout=600)
print('recreate', code, out[-200:])
start = {int(g.get('neko_instance_id') or 0) for g in groups if g.get('neko_enabled')}
for i in sorted(start):
youtube_neko._run(['docker', 'start', youtube_neko._container_name(i)], timeout=90)
print('started', i)
PY
sleep 20
# wait for chrome + policy import
for n in 1 2 3 4 5 6; do
sleep 10
echo "=== wait ${n}0s ==="
for id in 1 2; do
name=douyinweb-neko-$id
docker ps --format '{{.Names}}' | grep -qx $name || continue
docker exec $name sh -c '
echo CTR=$HOSTNAME
supervisorctl status tampermonkey-provisioning google-chrome | cat
curl -fsS http://127.0.0.1:18080/tampermonkey-provisioning.json | python3 -c "import sys,json; d=json.load(sys.stdin); print(d[\"scripts\"][0][\"name\"], d[\"settings\"][\"runtime_content_mode\"])"
ls /etc/opt/chrome/policies/managed/
# sh2 verify storage check
for base in /home/neko/.config/google-chrome/Default /home/neko/.config/chromium/Default; do
for area in "Local Extension Settings" "Managed Extension Settings"; do
dir="$base/$area/dhdgffkkebhmkfjojejmpbldmpobfkfo"
if [ -d "$dir" ] && grep -R -a -l "YouTube Studio Auto Dismiss" "$dir" 2>/dev/null | head -1; then
echo FOUND_SCRIPT
fi
done
done
# no auto-dismiss in load-extension
for f in /proc/[0-9]*/cmdline; do tr "\0" " " < "$f"; echo; done 2>/dev/null | grep load-extension | head -2
test ! -f /etc/neko/live-ops-extension/auto-dismiss.js && echo no_baked_dismiss_ok
' || true
done
if docker exec douyinweb-neko-2 sh -c 'grep -R -a -l "YouTube Studio Auto Dismiss" /home/neko/.config/google-chrome/Default/Local\ Extension\ Settings/dhdgffkkebhmkfjojejmpbldmpobfkfo /home/neko/.config/google-chrome/Default/Managed\ Extension\ Settings/dhdgffkkebhmkfjojejmpbldmpobfkfo 2>/dev/null | head -1' | grep -q .; then
echo 'SCRIPT_IMPORTED_OK'
break
fi
done
# final state
cd /opt/douyinWEB/backend && .venv/bin/python - <<'PY'
from app import youtube_neko, yt_assign_db
print(youtube_neko.sync_for_groups(yt_assign_db.list_groups(False)))
for i in youtube_neko.list_instances(2):
print(i.get('id'), i.get('status'), i.get('running'), i.get('studio_url'))
PY
"""
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect("108.174.50.68", 22, "root", "Xiao4669805", timeout=60, allow_agent=False, look_for_keys=False)
s = c.open_sftp()
s.put(str(tgz), "/tmp/neko-sh2-exact.tgz")
print("uploaded", tgz.stat().st_size)
with s.file("/tmp/neko_sh2_exact.sh", "w") as f:
f.write(REMOTE)
s.close()
_i, o, e = c.exec_command("bash /tmp/neko_sh2_exact.sh", timeout=600)
o.channel.settimeout(600)
print(o.read().decode("utf-8", "replace"))
err = e.read().decode("utf-8", "replace")
if err.strip():
print("STDERR", err[:2500])
c.close()
tgz.unlink(missing_ok=True)