145 lines
6.2 KiB
Python
145 lines
6.2 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""Deploy assign_lives hub sync + receiving-first live-map; push catalog; verify."""
|
|
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]
|
|
FILES = {
|
|
"/opt/douyinWEB/backend/app/youtube_live.py": (ROOT / "backend/app/youtube_live.py").read_bytes(),
|
|
"/opt/douyinWEB/backend/app/ops_neko.py": (ROOT / "backend/app/ops_neko.py").read_bytes(),
|
|
"/opt/douyinWEB/backend/app/main.py": (ROOT / "backend/app/main.py").read_bytes(),
|
|
"/opt/douyinWEB/youtube-svc/src/youtube.mjs": (ROOT / "youtube-svc/src/youtube.mjs").read_bytes(),
|
|
}
|
|
|
|
|
|
def run(c, cmd, t=120):
|
|
_, o, e = c.exec_command(cmd, timeout=t)
|
|
o.channel.settimeout(t)
|
|
try:
|
|
return (o.read() + e.read()).decode("utf-8", "replace")
|
|
except Exception as ex:
|
|
return str(ex)
|
|
|
|
|
|
def deploy(port, user, pw, sudo=False):
|
|
print(f"\n===== {user}:{port} =====")
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect("107.173.30.245", port, user, pw, timeout=30, allow_agent=False, look_for_keys=False)
|
|
sftp = c.open_sftp()
|
|
for i, (dst, data) in enumerate(FILES.items()):
|
|
tmp = f"/tmp/_dep_{i}"
|
|
with sftp.file(tmp, "wb") as f:
|
|
f.write(data)
|
|
if sudo:
|
|
run(c, f"echo '{pw}' | sudo -S -p '' bash -lc \"cat {tmp} > {dst}\"", 30)
|
|
else:
|
|
run(c, f"cat {tmp} > {dst}", 20)
|
|
sftp.close()
|
|
restart = "systemctl restart douyinweb-yt; systemctl restart douyinweb-api"
|
|
if sudo:
|
|
run(c, f"echo '{pw}' | sudo -S -p '' bash -lc {repr(restart)}", 50)
|
|
else:
|
|
run(c, restart + " >/tmp/r.log 2>&1 &", 15)
|
|
for i in range(16):
|
|
time.sleep(2)
|
|
h = run(c, "curl -sS -m 3 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health || echo x").strip()
|
|
print("health", h)
|
|
if h.endswith("200"):
|
|
break
|
|
if i == 7:
|
|
kill = "systemctl kill -s SIGKILL douyinweb-api; systemctl reset-failed douyinweb-api; systemctl start douyinweb-api"
|
|
if sudo:
|
|
run(c, f"echo '{pw}' | sudo -S -p '' bash -lc {repr(kill)}", 40)
|
|
else:
|
|
run(c, kill, 40)
|
|
c.close()
|
|
|
|
|
|
def push_and_verify():
|
|
# physical: push catalog
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect("107.173.30.245", 4014, "eric", "4669805", timeout=25)
|
|
print("\n===== push catalog =====")
|
|
print(
|
|
run(
|
|
c,
|
|
"echo '4669805' | sudo -S -p '' bash -lc \"curl -sS -m 60 -X POST -H 'Cookie: ' "
|
|
"http://127.0.0.1:8014/api/ops/neko/push-catalog\"",
|
|
90,
|
|
)[:1500]
|
|
)
|
|
# login + push via python with session
|
|
script = r'''
|
|
import json,urllib.request,http.cookiejar
|
|
cj=http.cookiejar.CookieJar(); op=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
|
|
req=urllib.request.Request("http://127.0.0.1:8014/api/auth/login", data=json.dumps({"username":"hackrobot","password":"Xiao4669805@"}).encode(), headers={"Content-Type":"application/json"}, method="POST")
|
|
print("login", op.open(req,timeout=10).status)
|
|
req=urllib.request.Request("http://127.0.0.1:8014/api/ops/neko/push-catalog", data=b"{}", headers={"Content-Type":"application/json"}, method="POST")
|
|
raw=op.open(req, timeout=90).read()
|
|
print("push", raw[:800])
|
|
req=urllib.request.Request("http://127.0.0.1:8014/api/youtube/live-map?force=1")
|
|
p=json.loads(op.open(req, timeout=150).read())
|
|
print("phys live_count", p.get("live_count"), p.get("source"))
|
|
for k,v in sorted((p.get("entries") or {}).items()):
|
|
if v.get("is_live") and str(k).startswith("assign:"):
|
|
print(" LIVE", (v.get("title") or k)[:28], v.get("source"))
|
|
'''
|
|
sftp = c.open_sftp()
|
|
with sftp.file("/tmp/_pushv.py", "w") as f:
|
|
f.write(script)
|
|
sftp.close()
|
|
print(run(c, "echo '4669805' | sudo -S -p '' /opt/douyinWEB/backend/.venv/bin/python /tmp/_pushv.py", 220)[:2500])
|
|
c.close()
|
|
|
|
# vps verify
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect("107.173.30.245", 22, "root", "Xiao4669805", timeout=25)
|
|
print("\n===== vps after sync =====")
|
|
print(run(c, "/opt/douyinWEB/backend/.venv/bin/python /tmp/_pushv.py", 220)[:2500])
|
|
# write same script on vps
|
|
sftp = c.open_sftp()
|
|
with sftp.file("/tmp/_pushv.py", "w") as f:
|
|
f.write(script.replace("push-catalog", "ops/neko/sync-status").replace(
|
|
'req=urllib.request.Request("http://127.0.0.1:8014/api/ops/neko/push-catalog", data=b"{}", headers={"Content-Type":"application/json"}, method="POST")\nraw=op.open(req, timeout=90).read()\nprint("push", raw[:800])\n',
|
|
'print("skip push on hub")\n'
|
|
))
|
|
sftp.close()
|
|
# simpler vps-only verify
|
|
vscript = r'''
|
|
import json,urllib.request,http.cookiejar,sqlite3
|
|
cj=http.cookiejar.CookieJar(); op=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
|
|
req=urllib.request.Request("http://127.0.0.1:8014/api/auth/login", data=json.dumps({"username":"hackrobot","password":"Xiao4669805@"}).encode(), headers={"Content-Type":"application/json"}, method="POST")
|
|
print("login", op.open(req,timeout=10).status)
|
|
con=sqlite3.connect("/opt/douyinWEB/backend/data/monitor.db")
|
|
rows=list(con.execute("select ts,youtube_channel_id,on_air,live_status,detect_source,neko_ref from yt_studio_samples where on_air=1 order by ts desc limit 10"))
|
|
print("hub on_air samples", len(rows))
|
|
for r in rows:
|
|
print(" ", r)
|
|
p=json.loads(op.open("http://127.0.0.1:8014/api/youtube/live-map?force=1", timeout=150).read())
|
|
print("vps live_count", p.get("live_count"), p.get("source"))
|
|
for k,v in sorted((p.get("entries") or {}).items()):
|
|
if v.get("is_live") and str(k).startswith("assign:"):
|
|
print(" LIVE", (v.get("title") or k)[:28], v.get("source"), (v.get("live_url") or "")[:55])
|
|
'''
|
|
with sftp.file("/tmp/_vpsv.py", "w") as f:
|
|
f.write(vscript)
|
|
sftp.close()
|
|
print(run(c, "/opt/douyinWEB/backend/.venv/bin/python /tmp/_vpsv.py", 220)[:3000])
|
|
c.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
deploy(22, "root", "Xiao4669805", sudo=False)
|
|
deploy(4014, "eric", "4669805", sudo=True)
|
|
push_and_verify()
|