#!/usr/bin/env python3 # -*- coding: utf-8 -*- """新 git clone 后:修正属主、创建 .venv、安装 requirements(需与 deploy_live_paramiko 相同环境变量)。""" from __future__ import annotations import os import sys try: import paramiko except ImportError: print("请先安装: pip install paramiko", file=sys.stderr) sys.exit(1) HOST = os.environ.get("LIVE_DEPLOY_HOST", "192.168.21.105") USER = os.environ.get("LIVE_DEPLOY_USER", "live") PASS = os.environ.get("LIVE_DEPLOY_PASS", "12345678") REMOTE_BASE = os.environ.get("LIVE_DEPLOY_PATH", "/home/live/douyinyoutube").rstrip("/") def main() -> int: c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(HOST, username=USER, password=PASS, timeout=30, banner_timeout=40, auth_timeout=30) try: def run(title: str, cmd: str, timeout: int = 600) -> int: print(f"\n=== {title} ===") wrapped = ( "export LC_ALL=C.UTF-8 LANG=C.UTF-8; " f"cd {REMOTE_BASE} 2>/dev/null || exit 9; " + cmd ) _, stdout, stderr = c.exec_command(wrapped, timeout=timeout, get_pty=False) out = (stdout.read() or b"").decode("utf-8", "replace") err = (stderr.read() or b"").decode("utf-8", "replace") code = stdout.channel.recv_exit_status() if out.strip(): print(out.rstrip()) if err.strip(): print(err.rstrip(), file=sys.stderr) print(f"exit={code}") return code code = run( "chown (root 克隆后必做)", f"echo '{PASS}' | sudo -S chown -R {USER}:{USER} {REMOTE_BASE} && echo chown_ok", timeout=60, ) if code != 0: return 1 run("chmod +x 常用脚本", "chmod +x start.sh web.sh dev.sh 2>/dev/null; chmod +x scripts/launch.py 2>/dev/null; echo chmod_ok", timeout=20) venv_cmd = ( f"if [ -x .venv/bin/pip ]; then echo venv_exists; " f"else python3 -m venv .venv && echo venv_created; fi && " f".venv/bin/pip install -q --upgrade pip && " f".venv/bin/pip install -r requirements.txt" ) code = run("venv + pip install -r requirements.txt", venv_cmd, timeout=900) if code != 0: return 2 print("\n完成。接着在本机执行: python tools/deploy_live_paramiko.py") return 0 finally: c.close() if __name__ == "__main__": raise SystemExit(main())