101 lines
4.0 KiB
Python
101 lines
4.0 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""Deploy yt_long_db.py + yt_long_pipeline.py to physical + VPS (API restart only)."""
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
LOCAL = Path(__file__).resolve().parents[1]
|
|
FILES = [
|
|
"backend/app/yt_long_db.py",
|
|
"backend/app/yt_long_pipeline.py",
|
|
"backend/tests/test_quota_defaults.py",
|
|
]
|
|
|
|
|
|
def put(c: paramiko.SSHClient, root: str, sudo: str | None = None) -> None:
|
|
sftp = c.open_sftp()
|
|
for rel in FILES:
|
|
tmp = f"/tmp/_ylq_{Path(rel).name}"
|
|
data = (LOCAL / rel).read_bytes()
|
|
with sftp.file(tmp, "wb") as f:
|
|
f.write(data)
|
|
dst = f"{root}/{rel}"
|
|
parent = str(Path(dst).parent).replace("\\", "/")
|
|
if sudo:
|
|
cmd = (
|
|
f"echo '{sudo}' | sudo -S -p '' bash -lc "
|
|
f"\"mkdir -p '{parent}'; install -m 644 {tmp} {dst}; sed -i 's/\\r$//' {dst}\""
|
|
)
|
|
else:
|
|
cmd = f"mkdir -p '{parent}'; install -m 644 {tmp} {dst}; sed -i 's/\\r$//' {dst}"
|
|
_, o, e = c.exec_command(cmd, timeout=40)
|
|
err = (e.read() + o.read()).decode("utf-8", "replace")
|
|
print("ok" if not err.strip() or "password" in err.lower() else err[-200:], rel, flush=True)
|
|
sftp.close()
|
|
|
|
|
|
def restart_api(c: paramiko.SSHClient, sudo: str | None = None) -> None:
|
|
inner = (
|
|
"systemctl restart douyinweb-api; sleep 2; systemctl is-active douyinweb-api; "
|
|
"curl -sS -m 8 -o /dev/null -w 'health=%{http_code}\\n' http://127.0.0.1:8014/api/health || true"
|
|
)
|
|
cmd = f"echo '{sudo}' | sudo -S -p '' bash -lc {repr(inner)}" if sudo else inner
|
|
_, o, e = c.exec_command(cmd, timeout=60)
|
|
print((o.read() + e.read()).decode("utf-8", "replace"), flush=True)
|
|
|
|
|
|
def verify(c: paramiko.SSHClient, sudo: str | None = None) -> None:
|
|
py = 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=20).status)
|
|
# save 9
|
|
req2=urllib.request.Request('http://127.0.0.1:8014/api/yt-long/settings', data=json.dumps({'yt_long_channel_daily_max': 9}).encode(), headers={'Content-Type':'application/json'}, method='POST')
|
|
r=json.loads(op.open(req2, timeout=30).read())
|
|
print('save_settings', r.get('settings',{}).get('yt_long_channel_daily_max'), 'top', r.get('channel_daily_max'))
|
|
# status again
|
|
st=json.loads(op.open('http://127.0.0.1:8014/api/yt-long/status', timeout=30).read())
|
|
print('status', st.get('settings',{}).get('yt_long_channel_daily_max'), 'top', st.get('channel_daily_max'))
|
|
"""
|
|
sftp = c.open_sftp()
|
|
with sftp.file("/tmp/_v_ylq.py", "w") as f:
|
|
f.write(py)
|
|
sftp.close()
|
|
if sudo:
|
|
cmd = f"echo '{sudo}' | sudo -S -p '' /opt/douyinWEB/backend/.venv/bin/python /tmp/_v_ylq.py"
|
|
else:
|
|
cmd = "/opt/douyinWEB/backend/.venv/bin/python /tmp/_v_ylq.py"
|
|
_, o, e = c.exec_command(cmd, timeout=90)
|
|
print((o.read() + e.read()).decode("utf-8", "replace"), flush=True)
|
|
|
|
|
|
def main() -> None:
|
|
p = paramiko.SSHClient()
|
|
p.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
p.connect("107.173.30.245", 4014, "eric", "4669805", timeout=40, allow_agent=False, look_for_keys=False)
|
|
print("===== PHYS =====", flush=True)
|
|
put(p, "/opt/douyinWEB", "4669805")
|
|
restart_api(p, "4669805")
|
|
verify(p, "4669805")
|
|
p.close()
|
|
|
|
v = paramiko.SSHClient()
|
|
v.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
v.connect("107.173.30.245", 22, "root", "Xiao4669805", timeout=40, allow_agent=False, look_for_keys=False)
|
|
print("===== VPS =====", flush=True)
|
|
put(v, "/opt/douyinWEB")
|
|
restart_api(v)
|
|
verify(v)
|
|
v.close()
|
|
print("DONE", flush=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|