douyinWEB/scripts/_deploy_yt_neko.py

103 lines
2.9 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy YouTube Neko per-channel feature to dy.nomadro.live."""
import sys
import time
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
HOST = "108.174.50.68"
APP = "/opt/douyinWEB"
ROOT = Path(__file__).resolve().parents[1]
FILES = [
"backend/app/youtube_neko.py",
"backend/app/yt_assign_db.py",
"backend/app/main.py",
"backend/app/models.py",
"backend/app/config.py",
"frontend/src/components/YtRelayProStatusPanel.tsx",
"frontend/src/components/YtAssignPanel.tsx",
]
REMOTE_SH = r"""#!/usr/bin/env bash
set -euo pipefail
source /etc/douyinweb.env
echo "=== docker ==="
if ! command -v docker >/dev/null 2>&1; then
apt-get update -y
apt-get install -y docker.io docker-compose-plugin || apt-get install -y docker.io
systemctl enable --now docker
fi
docker --version
echo "=== restart api ==="
systemctl restart douyinweb-api
sleep 5
systemctl is-active douyinweb-api
echo "=== migrate neko ids ==="
cd /opt/douyinWEB/backend
.venv/bin/python - <<'PY'
from app import yt_assign_db
from app.db import init_db
init_db()
groups = yt_assign_db.list_groups(include_urls=False)
print("groups", len(groups), "neko_ids", [g.get("neko_instance_id") for g in groups])
PY
echo "=== build web ==="
cd /opt/douyinWEB/frontend && npm run build
systemctl restart douyinweb-web
sleep 4
systemctl is-active douyinweb-web
echo "=== neko status ==="
curl -sS http://127.0.0.1:${API_PORT:-8014}/api/youtube/neko/status | head -c 400
echo
echo DONE
"""
def main() -> int:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(HOST, 22, "root", "Xiao4669805", timeout=45, allow_agent=False, look_for_keys=False)
sftp = c.open_sftp()
for rel in FILES:
local = ROOT / rel
remote = f"{APP}/{rel.replace(chr(92), '/')}"
print(f"upload {rel}")
sftp.put(str(local), remote)
with sftp.file("/tmp/deploy_yt_neko.sh", "w") as f:
f.write(REMOTE_SH)
sftp.chmod("/tmp/deploy_yt_neko.sh", 0o755)
sftp.close()
chan = c.get_transport().open_session()
chan.settimeout(900)
chan.get_pty(width=200, height=50)
chan.exec_command("bash /tmp/deploy_yt_neko.sh")
while True:
if chan.recv_ready():
print(chan.recv(65535).decode("utf-8", "replace"), end="", flush=True)
if chan.recv_stderr_ready():
print(chan.recv_stderr(65535).decode("utf-8", "replace"), end="", flush=True)
if chan.exit_status_ready() and not chan.recv_ready() and not chan.recv_stderr_ready():
break
time.sleep(0.3)
while chan.recv_ready():
print(chan.recv(65535).decode("utf-8", "replace"), end="", flush=True)
code = chan.recv_exit_status()
c.close()
print(f"\nexit {code}")
return code
if __name__ == "__main__":
raise SystemExit(main())