121 lines
3.9 KiB
Python
121 lines
3.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Deploy youtube_neko start-fix + recreate stale dylive containers."""
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import httpx
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def ssh():
|
|
last = None
|
|
for i in range(6):
|
|
try:
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(
|
|
"107.173.30.245",
|
|
22,
|
|
"root",
|
|
"Xiao4669805",
|
|
timeout=45,
|
|
allow_agent=False,
|
|
look_for_keys=False,
|
|
banner_timeout=120,
|
|
)
|
|
return c
|
|
except Exception as ex:
|
|
last = ex
|
|
print("retry", i, ex, flush=True)
|
|
time.sleep(3)
|
|
raise SystemExit(last)
|
|
|
|
|
|
def run(c, cmd, t=360):
|
|
print(f"\n$ {cmd[:220]}", flush=True)
|
|
_i, o, e = c.exec_command(cmd, timeout=t)
|
|
code = o.channel.recv_exit_status()
|
|
out = (o.read() + e.read()).decode("utf-8", "replace")
|
|
print(out[-14000:] if len(out) > 14000 else out, flush=True)
|
|
return code, out
|
|
|
|
|
|
c = ssh()
|
|
sftp = c.open_sftp()
|
|
sftp.put(str(ROOT / "backend/app/youtube_neko.py"), "/tmp/youtube_neko.py")
|
|
sftp.put(str(ROOT / "backend/app/host_profile.py"), "/tmp/host_profile.py")
|
|
sftp.close()
|
|
|
|
run(
|
|
c,
|
|
r"""
|
|
set +e
|
|
install -m 0644 /tmp/youtube_neko.py /opt/douyinWEB/backend/app/youtube_neko.py
|
|
install -m 0644 /tmp/host_profile.py /opt/douyinWEB/backend/app/host_profile.py
|
|
# drop stale containers without published ports
|
|
docker rm -f douyinweb-neko-3 douyinweb-neko-7 douyinweb-neko-8 2>/dev/null
|
|
systemctl restart douyinweb-api
|
|
for i in $(seq 1 40); do
|
|
curl -sS -m 2 http://127.0.0.1:8014/api/health >/dev/null 2>&1 && break
|
|
sleep 1
|
|
done
|
|
CS=$(mktemp)
|
|
curl -sS -m 20 -c "$CS" -b "$CS" -X POST http://127.0.0.1:8014/api/auth/login \
|
|
-H 'Content-Type: application/json' \
|
|
-d '{"username":"hackrobot","password":"Xiao4669805@"}' >/dev/null
|
|
curl -sS -m 20 -b "$CS" http://127.0.0.1:8014/api/youtube/assign/groups | python3 -c '
|
|
import sys,json
|
|
rows=json.load(sys.stdin)
|
|
rows=rows if isinstance(rows,list) else rows.get("groups") or []
|
|
open("/tmp/en.txt","w").write("\n".join(
|
|
f"{g[\"id\"]} {int(g.get(\"neko_instance_id\") or 0)}"
|
|
for g in rows if g.get("neko_enabled") and int(g.get("neko_instance_id") or 0)>0
|
|
))
|
|
print(open("/tmp/en.txt").read())
|
|
'
|
|
while read -r GID NID; do
|
|
[[ -z "$GID" ]] && continue
|
|
echo "=== start $NID ==="
|
|
curl -sS -m 300 -b "$CS" -X POST "http://127.0.0.1:8014/api/youtube/assign/groups/${GID}/neko" \
|
|
-H 'Content-Type: application/json' \
|
|
-d '{"action":"start","enabled":true}' | python3 -c '
|
|
import sys,json
|
|
d=json.load(sys.stdin)
|
|
n=d.get("neko") or {}
|
|
print({k:n.get(k) for k in ("id","running","ready","status","message","studio_url")})
|
|
print("err", d.get("detail") or d.get("error"))
|
|
'
|
|
done < /tmp/en.txt
|
|
sleep 2
|
|
docker ps --format '{{.Names}} {{.Status}} {{.Ports}}' | grep douyinweb-neko
|
|
ss -lntp | grep -E ':(920[0-7])\b' | head
|
|
for p in 9202 9206; do curl -sS -m 5 -o /dev/null -w "p$p=%{http_code}\n" http://127.0.0.1:$p/; done
|
|
# host profile effects via API
|
|
curl -sS -m 10 -b "$CS" http://127.0.0.1:8014/api/ops/host-profile | python3 -c 'import sys,json;d=json.load(sys.stdin);print(d.get("effects",{}).get("vps"))'
|
|
rm -f "$CS"
|
|
""",
|
|
360,
|
|
)
|
|
c.close()
|
|
|
|
with httpx.Client(follow_redirects=False, timeout=25) as cl:
|
|
for u in [
|
|
"https://dylive.nomadro.com/neko/3/",
|
|
"https://dylive.nomadro.com/neko/7/",
|
|
"https://dy.nomadro.com/neko/1/",
|
|
"https://dy.nomadro.cn/ops-neko/1/",
|
|
"https://dylive.nomadro.com/api/health",
|
|
]:
|
|
try:
|
|
r = cl.get(u)
|
|
print(f"{r.status_code} {u}", flush=True)
|
|
except Exception as ex:
|
|
print(f"ERR {u} {ex}", flush=True)
|
|
print("DONE", flush=True)
|