# -*- 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/services/weapp_switches.py" def main() -> int: data = SRC.read_text(encoding="utf-8").replace("ebook_svc.", "app.").encode("utf-8") 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/weapp_switches.py", "wb") as rf: rf.write(data) sftp.close() cmd = r""" set -e sudo cp /tmp/weapp_switches.py /opt/nomadmini/backend/app/services/weapp_switches.py cd /opt/nomadmini/backend .venv/bin/python -c 'from app.services.weapp_switches import read_weapp_switches; print("ok", read_weapp_switches()["bookAudit"])' sudo systemctl restart nomadmini-api sleep 2 systemctl is-active nomadmini-api rm -f /tmp/weapp_switches.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())