147 lines
5.0 KiB
Python
147 lines
5.0 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""Recreate ops-neko with full assign-parity env so Chrome actually starts.
|
|
|
|
ops compose on physical is still the old stub (no ENV_USER / managed policies).
|
|
Supervisor google-chrome.conf needs ENV_USER + ENV_DISPLAY or Chrome never launches → black screen.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import time
|
|
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
|
|
PHYS = ("107.173.30.245", 4014, "eric", "4669805")
|
|
ROOT = "/opt/douyinWEB"
|
|
|
|
|
|
def ssh():
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(*PHYS[:3], password=PHYS[3], timeout=45, allow_agent=False, look_for_keys=False)
|
|
return c
|
|
|
|
|
|
def run(c, cmd, t=180):
|
|
_i, o, e = c.exec_command(cmd, timeout=t)
|
|
return (o.read() + e.read()).decode("utf-8", "replace")
|
|
|
|
|
|
# Upload patch files then run recreate via python on host
|
|
REMOTE_FIX = r'''
|
|
import os, shutil, subprocess
|
|
from pathlib import Path
|
|
|
|
os.chdir("/opt/douyinWEB/backend")
|
|
sys_path_insert = "/opt/douyinWEB/backend"
|
|
import sys
|
|
sys.path.insert(0, sys_path_insert)
|
|
|
|
# Prefer in-tree modules if already updated; else rewrite compose from assign template.
|
|
from app import youtube_neko as yn
|
|
from app import ops_neko
|
|
|
|
# Force rewrite compose with shared compose_service_lines
|
|
text = ops_neko._render_compose(max(ops_neko.ops_neko_db.max_slot(), 1))
|
|
path = ops_neko._compose_path()
|
|
path.write_text(text, encoding="utf-8")
|
|
print("compose_head:")
|
|
print("\n".join(text.splitlines()[:45]))
|
|
|
|
# Recreate all ops containers, keep running set
|
|
max_slot = max(ops_neko.ops_neko_db.max_slot(), 1)
|
|
was = {i for i in range(1, max_slot+1) if ops_neko._container_running(i)}
|
|
# Always ensure slot 1 comes up for operator
|
|
was.add(1)
|
|
print("resume", sorted(was))
|
|
r = ops_neko.sync_compose(start_slots=was, force_recreate=True)
|
|
print("sync", r)
|
|
'''
|
|
|
|
|
|
def main():
|
|
c = ssh()
|
|
# First: check if local code has compose_service_lines; if not, scp files
|
|
has = run(
|
|
c,
|
|
"echo '4669805' | sudo -S -p '' grep -c compose_service_lines "
|
|
f"{ROOT}/backend/app/youtube_neko.py || true",
|
|
).strip()
|
|
print("has_compose_service_lines:", has, flush=True)
|
|
|
|
# Upload current local modules
|
|
from pathlib import Path
|
|
|
|
local = Path(__file__).resolve().parents[1]
|
|
sftp = c.open_sftp()
|
|
for rel in (
|
|
"backend/app/youtube_neko.py",
|
|
"backend/app/ops_neko.py",
|
|
"backend/app/ops_neko_db.py",
|
|
):
|
|
src = local / rel
|
|
dst = f"/tmp/_up_{Path(rel).name}"
|
|
with sftp.file(dst, "wb") as f:
|
|
f.write(src.read_bytes())
|
|
print("uploaded", rel, flush=True)
|
|
sftp.close()
|
|
|
|
print(
|
|
run(
|
|
c,
|
|
"echo '4669805' | sudo -S -p '' bash -lc '"
|
|
f"install -m 644 /tmp/_up_youtube_neko.py {ROOT}/backend/app/youtube_neko.py; "
|
|
f"install -m 644 /tmp/_up_ops_neko.py {ROOT}/backend/app/ops_neko.py; "
|
|
f"install -m 644 /tmp/_up_ops_neko_db.py {ROOT}/backend/app/ops_neko_db.py; "
|
|
"sed -i \"s/\\r$//\" "
|
|
f"{ROOT}/backend/app/youtube_neko.py {ROOT}/backend/app/ops_neko.py {ROOT}/backend/app/ops_neko_db.py; "
|
|
# recreate via python
|
|
f"cd {ROOT}/backend && ./../.venv/bin/python - <<\"PY\"\n"
|
|
"import sys\n"
|
|
"sys.path.insert(0, \".\")\n"
|
|
"from app import ops_neko\n"
|
|
"from app import ops_neko_db\n"
|
|
"max_slot = max(ops_neko_db.max_slot(), 1)\n"
|
|
"text = ops_neko._render_compose(max_slot)\n"
|
|
"ops_neko._compose_path().write_text(text, encoding=\"utf-8\")\n"
|
|
"print(\"ENV_USER in compose\", \"ENV_USER\" in text)\n"
|
|
"print(\"LIVE_OPS in compose\", \"NEKO_LIVE_OPS_EXTENSION\" in text)\n"
|
|
"print(\"\\n\".join(text.splitlines()[:55]))\n"
|
|
"was = {i for i in range(1, max_slot+1) if ops_neko._container_running(i)}\n"
|
|
"was.add(1)\n"
|
|
"print(\"resume\", sorted(was))\n"
|
|
"print(ops_neko.sync_compose(start_slots=was, force_recreate=True))\n"
|
|
"PY\n"
|
|
"'",
|
|
300,
|
|
),
|
|
flush=True,
|
|
)
|
|
|
|
time.sleep(8)
|
|
print(
|
|
run(
|
|
c,
|
|
"echo '4669805' | sudo -S -p '' bash -lc '"
|
|
"docker exec douyinweb-ops-neko-1 env | grep -E \"ENV_USER|ENV_DISPLAY|NEKO_LIVE|NEKO_MANAGED|NEKO_TAMPER|HOME|USER=\" | sort; "
|
|
"echo ---chrome-log---; "
|
|
"docker exec douyinweb-ops-neko-1 bash -lc \"tail -40 /var/log/neko/google-chrome.log 2>/dev/null || true\"; "
|
|
"echo ---procs---; "
|
|
"docker exec douyinweb-ops-neko-1 bash -lc \"command -v ps; ls /proc | head; pgrep -a chrome | head -10; pgrep -a Xvfb; pgrep -a openbox\"; "
|
|
"echo ---supervisorctl---; "
|
|
"docker exec douyinweb-ops-neko-1 bash -lc \"supervisorctl status 2>/dev/null || true\"; "
|
|
"'",
|
|
60,
|
|
),
|
|
flush=True,
|
|
)
|
|
c.close()
|
|
print("DONE — reopen https://dy.nomadro.com/ops-neko/1/", flush=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|