86 lines
2.8 KiB
Python
86 lines
2.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Deploy Shorts channel live analytics."""
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
|
|
HOST = "107.173.30.245"
|
|
USER = "root"
|
|
PASSWORD = "Xiao4669805"
|
|
REMOTE = "/opt/douyinWEB"
|
|
LOCAL = Path(__file__).resolve().parents[1]
|
|
FILES = [
|
|
"backend/app/db.py",
|
|
"backend/app/main.py",
|
|
"backend/app/youtube_live.py",
|
|
"frontend/src/app/shorts/page.tsx",
|
|
"frontend/src/app/sessions/page.tsx",
|
|
]
|
|
|
|
|
|
def run(c, cmd, timeout=420):
|
|
print("$", cmd[:180], flush=True)
|
|
chan = c.get_transport().open_session()
|
|
chan.settimeout(timeout)
|
|
chan.get_pty()
|
|
chan.exec_command(cmd)
|
|
buf = []
|
|
while True:
|
|
if chan.recv_ready():
|
|
buf.append(chan.recv(65535).decode("utf-8", "replace"))
|
|
if chan.exit_status_ready() and not chan.recv_ready():
|
|
break
|
|
time.sleep(0.25)
|
|
while chan.recv_ready():
|
|
buf.append(chan.recv(65535).decode("utf-8", "replace"))
|
|
print("".join(buf)[-2200:], flush=True)
|
|
return chan.recv_exit_status()
|
|
|
|
|
|
def main():
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(HOST, username=USER, password=PASSWORD, timeout=30)
|
|
sftp = c.open_sftp()
|
|
for rel in FILES:
|
|
print("upload", rel, flush=True)
|
|
sftp.put(str(LOCAL / rel), f"{REMOTE}/{rel}")
|
|
sftp.close()
|
|
run(
|
|
c,
|
|
"systemctl kill -s SIGKILL douyinweb-api || true; sleep 1; "
|
|
"systemctl reset-failed douyinweb-api || true; "
|
|
"systemctl start douyinweb-api; sleep 5; systemctl is-active douyinweb-api",
|
|
timeout=90,
|
|
)
|
|
py = r"""
|
|
from app import db, youtube_live
|
|
from app.session_auth import create_session_token, get_admin_credentials, SESSION_COOKIE
|
|
import httpx, asyncio
|
|
db.init_db()
|
|
user,_=get_admin_credentials(); tok=create_session_token(user)
|
|
async def go():
|
|
m=await youtube_live.build_live_map(force=True)
|
|
print('live_count', m.get('live_count'), 'sync', m.get('shorts_live_sync'))
|
|
asyncio.get_event_loop().run_until_complete(go())
|
|
with httpx.Client(base_url='http://127.0.0.1:8014', timeout=90, cookies={SESSION_COOKIE: tok}) as c:
|
|
r=c.get('/api/shorts/live-analytics')
|
|
print('analytics', r.status_code)
|
|
d=r.json()
|
|
st=(d.get('stats') or {}).get('overview') or {}
|
|
print('sessions', st.get('sessions'), 'hours', st.get('total_hours'), 'live_now', d.get('live_now'), 'channels', len(d.get('channels') or []))
|
|
"""
|
|
run(c, "cd /opt/douyinWEB/backend && .venv/bin/python - <<'PY'\n" + py + "\nPY\n", timeout=180)
|
|
if run(c, "cd /opt/douyinWEB/frontend && npm run build", timeout=420) != 0:
|
|
raise SystemExit(1)
|
|
run(c, "systemctl restart douyinweb-web --no-block; sleep 3; systemctl is-active douyinweb-api douyinweb-web")
|
|
c.close()
|
|
print("DONE")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|