人生轨迹功能完善

This commit is contained in:
2025-12-23 22:10:25 +08:00
parent 97abbefaa3
commit 56cacb7163
12 changed files with 310 additions and 38 deletions
+44 -10
View File
@@ -71,20 +71,47 @@ def run_command(cmd, cwd=None, shell=True, capture=True):
return False, "", str(e)
def exec_ssh_cmd(cmd):
def exec_ssh_cmd(cmd, timeout=30):
"""通过SSH执行远程命令"""
ssh_cmd = f'ssh {USERNAME}@{SERVER_IP} "{cmd}"'
return run_command(ssh_cmd)
ssh_cmd = f'ssh -o ConnectTimeout=10 -o BatchMode=yes {USERNAME}@{SERVER_IP} "{cmd}"'
try:
result = subprocess.run(
ssh_cmd,
shell=True,
capture_output=True,
text=True,
timeout=timeout
)
return result.returncode == 0, result.stdout.strip(), result.stderr.strip()
except subprocess.TimeoutExpired:
log_error(f"SSH命令超时: {cmd}")
return False, "", "命令执行超时"
except Exception as e:
return False, "", str(e)
def scp_upload(local_path, remote_path, recursive=False):
"""通过SCP上传文件或目录"""
r_flag = "-r" if recursive else ""
scp_cmd = f'scp {r_flag} "{local_path}" {USERNAME}@{SERVER_IP}:{remote_path}'
success, stdout, stderr = run_command(scp_cmd)
if not success:
log_error(f"SCP上传失败: {stderr}")
return success
scp_cmd = f'scp -o ConnectTimeout=10 -o BatchMode=yes {r_flag} "{local_path}" {USERNAME}@{SERVER_IP}:{remote_path}'
try:
result = subprocess.run(
scp_cmd,
shell=True,
capture_output=True,
text=True,
timeout=120 # 上传文件给更长时间
)
if result.returncode != 0:
log_error(f"SCP上传失败: {result.stderr}")
return False
return True
except subprocess.TimeoutExpired:
log_error("SCP上传超时")
return False
except Exception as e:
log_error(f"SCP上传异常: {e}")
return False
def check_env():
@@ -136,11 +163,15 @@ def deploy():
# 1. 创建远程目录
log_info("创建远程目录...")
exec_ssh_cmd(f"mkdir -p {release_path}")
success, _, err = exec_ssh_cmd(f"mkdir -p {release_path}")
if not success:
log_error(f"创建远程目录失败: {err}")
sys.exit(1)
# 2. 上传文件
log_info("上传文件到服务器...")
for item in DIST_DIR.iterdir():
log_info(f" 上传: {item.name}")
if item.is_file():
if not scp_upload(item, f"{release_path}/"):
log_error("文件上传失败")
@@ -160,7 +191,10 @@ def deploy():
# 检查目标路径是否为普通目录
exec_ssh_cmd(f"if [ -d '{LINK_PATH}' ] && [ ! -L '{LINK_PATH}' ]; then mv '{LINK_PATH}' '{LINK_PATH}_backup_$(date +%s)'; fi")
# 创建软链接
exec_ssh_cmd(f"ln -snf '{release_path}' '{LINK_PATH}'")
success, _, err = exec_ssh_cmd(f"ln -snf '{release_path}' '{LINK_PATH}'")
if not success:
log_error(f"切换版本失败: {err}")
sys.exit(1)
log_info(f"部署完成!当前版本指向: {release_path}")