douyinWEB/scripts/_enable_tm_policies.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.8 KiB
Python

# -*- coding: utf-8 -*-
"""Enable managed policies + recreate Neko so Tampermonkey jsonImport runs."""
from __future__ import annotations
import sys
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
FILES = [
("backend/app/youtube_neko.py", "/opt/douyinWEB/backend/app/youtube_neko.py"),
("backend/app/config.py", "/opt/douyinWEB/backend/app/config.py"),
("backend/app/main.py", "/opt/douyinWEB/backend/app/main.py"),
("backend/app/session_auth.py", "/opt/douyinWEB/backend/app/session_auth.py"),
("backend/app/auth.py", "/opt/douyinWEB/backend/app/auth.py"),
(
"backend/neko/policies/policies.json",
"/opt/douyinWEB/backend/neko/policies/policies.json",
),
(
"backend/neko/provisioning/tampermonkey-provisioning.json",
"/opt/douyinWEB/backend/neko/provisioning/tampermonkey-provisioning.json",
),
(
"backend/neko/provisioning/youtube-studio-auto-dismiss.user.js",
"/opt/douyinWEB/backend/neko/provisioning/youtube-studio-auto-dismiss.user.js",
),
]
REMOTE = r"""#!/bin/bash
set -e
# ensure managed policies env for API process
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
EOF
systemctl daemon-reload
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
from app.config import settings
print('managed', settings.neko_managed_policies, youtube_neko._managed_policies_enabled())
groups = yt_assign_db.list_groups(include_urls=False)
desired = max([int(g.get('neko_instance_id') or 0) for g in groups] + [0])
# wipe empty broken TM dirs so force-install is clean
import shutil
from pathlib import Path
for i in range(1, desired+1):
p = Path(f'/opt/douyinWEB/backend/data/neko/profiles/{i}/Default')
for sub in ('Extensions', 'Local Extension Settings', 'Managed Extension Settings'):
d = p / sub / youtube_neko._TM_EXT_ID
if d.exists():
shutil.rmtree(d, ignore_errors=True)
print('cleared', d)
compose = youtube_neko._compose_path()
compose.write_text(youtube_neko._render_compose(desired), encoding='utf-8')
print('policy mount sample:')
print([ln for ln in compose.read_text().splitlines() if 'policies' in ln][:6])
code, out = youtube_neko._run(youtube_neko._compose_cmd('up', '-d', '--force-recreate', '--no-start'), timeout=600)
print('recreate', code, out[-300:])
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=90)
print('started', i)
PY
sleep 15
docker exec douyinweb-neko-1 sh -c 'ls /etc/opt/chrome/policies/managed; cat /etc/opt/chrome/policies/managed/policies.json | head -c 200; echo; curl -fsS http://127.0.0.1:18080/tampermonkey-provisioning.json | head -c 100; echo'
docker exec douyinweb-neko-1 supervisorctl status
# wait for chrome to pull TM
sleep 25
echo PROFILE_AFTER
ls /opt/douyinWEB/backend/data/neko/profiles/1/Default/Extensions 2>/dev/null || true
ls "/opt/douyinWEB/backend/data/neko/profiles/1/Default/Local Extension Settings" 2>/dev/null || true
ls "/opt/douyinWEB/backend/data/neko/profiles/1/Default/Managed Extension Settings" 2>/dev/null || true
python3 - <<'PY'
import json
from pathlib import Path
p=Path('/opt/douyinWEB/backend/data/neko/profiles/1/Default/Preferences')
if p.exists():
d=json.loads(p.read_text(encoding='utf-8', errors='replace'))
tm=((d.get('extensions') or {}).get('settings') or {}).get('dhdgffkkebhmkfjojejmpbldmpobfkfo')
print('tm', bool(tm), 'state', (tm or {}).get('state'), 'path', (tm or {}).get('path'))
else:
print('no prefs')
# look for script marker anywhere in profile
root=Path('/opt/douyinWEB/backend/data/neko/profiles/1')
hits=[]
for f in root.rglob('*'):
if not f.is_file() or f.stat().st_size>5_000_000: continue
try:
if b'YouTube Studio Auto Dismiss' in f.read_bytes():
hits.append(str(f))
if len(hits)>=5: break
except Exception:
pass
print('script_hits', hits)
PY
"""
ROOT = r"c:\Users\admin\Desktop\douyinWEB"
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()
for local, remote in FILES:
s.put(f"{ROOT}\\{local.replace('/', chr(92))}", remote)
print("put", local)
with s.file("/tmp/enable_tm_policies.sh", "w") as f:
f.write(REMOTE)
s.close()
_i, o, e = c.exec_command("bash /tmp/enable_tm_policies.sh", timeout=420)
o.channel.settimeout(420)
print(o.read().decode("utf-8", "replace"))
err = e.read().decode("utf-8", "replace")
if err.strip():
print("STDERR", err[:2000])
c.close()