douyinWEB/scripts/_deploy_time_slot_armed.py
2026-09-13 22:10:49 -05:00

76 lines
3.1 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy time_slot_armed switch for assign cards (API + FE)."""
from __future__ import annotations
import sys
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(r"c:\Users\admin\Desktop\douyinWEB")
FILES = [
("backend/app/yt_assign_db.py", "/tmp/_dy_yt_assign_db.py"),
("backend/app/models.py", "/tmp/_dy_models.py"),
("backend/app/main.py", "/tmp/_dy_main.py"),
("frontend/src/components/YtAssignPanel.tsx", "/tmp/_dy_YtAssignPanel.tsx"),
]
REMOTE = r'''#!/bin/bash
set -euo pipefail
python3 - <<'PY'
from pathlib import Path
pairs=[
('/tmp/_dy_yt_assign_db.py','/opt/douyinWEB/backend/app/yt_assign_db.py'),
('/tmp/_dy_models.py','/opt/douyinWEB/backend/app/models.py'),
('/tmp/_dy_main.py','/opt/douyinWEB/backend/app/main.py'),
('/tmp/_dy_YtAssignPanel.tsx','/opt/douyinWEB/frontend/src/components/YtAssignPanel.tsx'),
]
for s,d in pairs:
p=Path(s); dst=Path(d)
dst.write_bytes(p.read_bytes().replace(b'\r\n',b'\n').replace(b'\r',b'\n'))
print('ok', dst, dst.stat().st_size)
PY
python3 -m py_compile /opt/douyinWEB/backend/app/yt_assign_db.py /opt/douyinWEB/backend/app/models.py /opt/douyinWEB/backend/app/main.py && echo py_ok
grep -n 'time_slot_armed\|group_eligible_for_time_slot' /opt/douyinWEB/backend/app/yt_assign_db.py | head -15
systemctl restart douyinweb-api.service
sleep 5
systemctl is-active douyinweb-api.service
# FE unit rebuild
if systemctl list-unit-files | grep -q douyinweb-web; then
cd /opt/douyinWEB/frontend && npm run build 2>&1 | tail -25
systemctl restart douyinweb-web.service
sleep 3
systemctl is-active douyinweb-web.service
fi
curl -sS -m 8 http://127.0.0.1:8014/api/ping; echo
'''
def deploy(label, host, port, user, pw, sudo=False, build_fe=True):
print(f"\n===== {label} =====")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(host, port, user, pw, timeout=45, allow_agent=False, look_for_keys=False)
sftp = c.open_sftp()
for local, remote in FILES:
data = (ROOT / local).read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n")
with sftp.file(remote, "wb") as f:
f.write(data)
script = REMOTE if build_fe else REMOTE.replace(
"if systemctl list-unit-files | grep -q douyinweb-web; then\n cd /opt/douyinWEB/frontend && npm run build 2>&1 | tail -25\n systemctl restart douyinweb-web.service\n sleep 3\n systemctl is-active douyinweb-web.service\nfi\n",
"systemctl restart douyinweb-web.service 2>/dev/null || true\n",
)
with sftp.file("/tmp/_dy_slot_armed.sh", "w") as f:
f.write(script.replace("\r\n", "\n"))
sftp.close()
cmd = f"echo '{pw}' | sudo -S -p '' bash /tmp/_dy_slot_armed.sh" if sudo else "bash /tmp/_dy_slot_armed.sh"
_, o, e = c.exec_command(cmd, timeout=600)
print((o.read() + e.read()).decode("utf-8", "replace"))
c.close()
deploy("vps-dylive", "107.173.30.245", 22, "root", "Xiao4669805", sudo=False)
deploy("phys-dy", "107.173.30.245", 4014, "eric", "4669805", sudo=True)
print("DONE")