# -*- coding: utf-8 -*- from __future__ import annotations import time from pathlib import Path import paramiko REPO = Path(__file__).resolve().parents[3] HOST, USER, PASS = "82.157.112.245", "ubuntu", "Xiao4669805" SRC = REPO / "apps/api/services/ebook_svc/routers/public_config.py" def main() -> int: text = SRC.read_text(encoding="utf-8").replace("ebook_svc.", "app.") if "ebook_svc." in text: raise SystemExit("rewrite failed") c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(HOST, username=USER, password=PASS, timeout=60, allow_agent=False, look_for_keys=False) sftp = c.open_sftp() with sftp.file("/tmp/public_config.py", "w") as rf: rf.write(text) sftp.close() cmd = r""" set -e sudo cp /tmp/public_config.py /opt/nomadmini/backend/app/routers/public_config.py sudo systemctl restart nomadmini-api sleep 3 systemctl is-active nomadmini-api curl -sk -X POST https://api.nomadro.cn/api/config/pull -H 'Content-Type: application/json' -d '{}' | python3 -c "import sys,json;d=json.load(sys.stdin);c=d['config'];print('POST_PULL',c.get('version'),c.get('modules',{}).get('nav'))" curl -sk -X POST https://api.nomadro.cn/api/config -H 'Content-Type: application/json' -d '{}' | python3 -c "import sys,json;d=json.load(sys.stdin);print('POST_CFG',d['config'].get('version'))" rm -f /tmp/public_config.py """ _, stdout, _ = c.exec_command(cmd, timeout=90, get_pty=True) while not stdout.channel.exit_status_ready(): if stdout.channel.recv_ready(): print(stdout.channel.recv(65536).decode("utf-8", "replace"), end="", flush=True) time.sleep(0.05) while stdout.channel.recv_ready(): print(stdout.channel.recv(65536).decode("utf-8", "replace"), end="", flush=True) code = stdout.channel.recv_exit_status() c.close() return code if __name__ == "__main__": raise SystemExit(main())