"""Deploy with non-blocking command pattern (avoid paramiko read hang).""" import paramiko import time HOST = "107.173.30.245" USER = "root" PASSWORD = "Xiao4669805" REPO = "/opt/douyinWEB" LOG = "/tmp/dylive_deploy.log" c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(HOST, 22, USER, PASSWORD, timeout=30, allow_agent=False, look_for_keys=False) def start(cmd: str) -> None: # run in background, append to log wrapped = f"nohup bash -lc {repr(cmd)} >>{LOG} 2>&1 & echo $!" _, out, _ = c.exec_command(wrapped, timeout=30) print("started pid:", out.read().decode().strip()) def wait_done(marker: str, timeout: int = 600) -> str: deadline = time.time() + timeout while time.time() < deadline: _, out, _ = c.exec_command(f"tail -n 40 {LOG}", timeout=30) text = out.read().decode("utf-8", "replace") if marker in text: return text time.sleep(3) _, out, _ = c.exec_command(f"tail -n 80 {LOG}", timeout=30) return out.read().decode("utf-8", "replace") _, out, _ = c.exec_command(f"rm -f {LOG}; echo START > {LOG}", timeout=20) out.read() print("[1] git pull") start( f"cd {REPO} && GIT_TERMINAL_PROMPT=0 git pull origin dev && echo PULL_OK || echo PULL_FAIL" ) print(wait_done("PULL_", 300)) print("[2] restart api + build + web") start( f"systemctl restart douyinweb-api; sleep 3; " f"cd {REPO}/frontend && npm run build && " f"systemctl restart douyinweb-web; " f"echo BUILD_OK; " f"systemctl is-active douyinweb-api; systemctl is-active douyinweb-web; " f"cd {REPO} && git log --oneline -1; echo DEPLOY_DONE" ) print(wait_done("DEPLOY_DONE", 600)) c.close() print("DONE")