Files
2026-07-27 14:07:26 +08:00

62 lines
2.1 KiB
Python

import paramiko
import time
host = 'hs.yifenbao.cn'
port = 22
username = 'root'
password = 'Torch1112'
deploy_path = '/www/huishou'
local_zip = 'D:/Trae/huishou/deploy.zip'
remote_zip = f'{deploy_path}/deploy.zip'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
print("Step 1: 连接服务器...")
ssh.connect(host, port, username, password, timeout=30)
print("连接成功")
print("\nStep 2: 清理服务器目录...")
stdin, stdout, stderr = ssh.exec_command(f'mkdir -p {deploy_path} && rm -rf {deploy_path}/admin {deploy_path}/dist {deploy_path}/server {deploy_path}/*.yml {deploy_path}/*.dockerignore {deploy_path}/Dockerfile {deploy_path}/deploy.zip 2>/dev/null')
stdout.channel.recv_exit_status()
print("清理完成")
print("\nStep 3: 上传部署文件...")
sftp = ssh.open_sftp()
sftp.put(local_zip, remote_zip)
sftp.close()
print("上传完成")
print("\nStep 4: 解压文件...")
stdin, stdout, stderr = ssh.exec_command(f'cd {deploy_path} && unzip -o deploy.zip && rm deploy.zip')
exit_code = stdout.channel.recv_exit_status()
if exit_code == 0:
print("解压完成")
else:
print(f"解压失败,退出码: {exit_code}")
print(f"错误信息: {stderr.read().decode()}")
print("\nStep 5: 启动Docker容器...")
stdin, stdout, stderr = ssh.exec_command(f'cd {deploy_path} && docker stop huishou huishou-backend 2>/dev/null; docker rm huishou huishou-backend 2>/dev/null; docker-compose up -d')
stdout.channel.recv_exit_status()
print("Docker启动命令已执行")
print("\nStep 6: 等待容器启动...")
time.sleep(15)
print("\nStep 7: 检查容器状态...")
stdin, stdout, stderr = ssh.exec_command('docker ps | grep huishou')
output = stdout.read().decode()
if output:
print("容器状态:")
print(output)
else:
print("容器未启动或名称不正确")
stdin, stdout, stderr = ssh.exec_command('docker ps -a | grep huishou')
print("所有容器:")
print(stdout.read().decode())
finally:
ssh.close()
print("\n部署完成!")