forked from yangjj-iso/NiuNiuJavaGuide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_deploy.py
More file actions
85 lines (73 loc) · 3.21 KB
/
auto_deploy.py
File metadata and controls
85 lines (73 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import paramiko, os, time, sys
SERVER = "154.219.109.125"
USER = "root"
PASSWORD = "YJJ20231124gyx@"
REMOTE_DIR = "/opt/javaguide"
LOCAL_TAR = os.path.join(os.environ.get("TEMP", "/tmp"), "javaguide-deploy.tar.gz")
def ssh_exec(ssh, cmd, timeout=300):
print(f" $ {cmd}")
stdin, stdout, stderr = ssh.exec_command(cmd, timeout=timeout)
out = stdout.read().decode()
err = stderr.read().decode()
code = stdout.channel.recv_exit_status()
if out.strip():
print(out.strip())
if err.strip() and code != 0:
print(f" [STDERR] {err.strip()}")
return code, out, err
def main():
print("=" * 50)
print("JavaGuide 自动部署")
print("=" * 50)
# 1. Connect
print(f"\n[1/5] 连接服务器 {SERVER}...")
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(SERVER, username=USER, password=PASSWORD, timeout=15)
sftp = ssh.open_sftp()
print(" 连接成功!")
# 2. Check & install Docker
print("\n[2/5] 检查 Docker 环境...")
code, out, _ = ssh_exec(ssh, "docker --version 2>/dev/null && docker compose version 2>/dev/null")
if code != 0 or "Docker" not in out:
print(" Docker 未安装,正在安装...")
ssh_exec(ssh, "curl -fsSL https://get.docker.com | sh", timeout=600)
ssh_exec(ssh, "systemctl enable docker && systemctl start docker")
ssh_exec(ssh, "apt-get update && apt-get install -y docker-compose-plugin 2>/dev/null || yum install -y docker-compose-plugin 2>/dev/null", timeout=300)
code, out, _ = ssh_exec(ssh, "docker --version && docker compose version")
if code != 0:
print(" Docker 安装失败!请手动安装。")
sys.exit(1)
print(" Docker 就绪!")
# 3. Upload files
print(f"\n[3/5] 上传项目文件...")
if not os.path.exists(LOCAL_TAR):
print(f" 本地打包文件不存在: {LOCAL_TAR}")
print(" 请先运行打包命令!")
sys.exit(1)
ssh_exec(ssh, f"mkdir -p {REMOTE_DIR}")
size_mb = os.path.getsize(LOCAL_TAR) / 1024 / 1024
print(f" 上传 {size_mb:.1f} MB...")
sftp.put(LOCAL_TAR, "/tmp/javaguide-deploy.tar.gz")
print(" 上传完成!")
# 4. Extract and build
print(f"\n[4/5] 解压并构建(首次构建需5-10分钟)...")
ssh_exec(ssh, f"cd {REMOTE_DIR} && tar -xzf /tmp/javaguide-deploy.tar.gz && rm /tmp/javaguide-deploy.tar.gz")
ssh_exec(ssh, f"cd {REMOTE_DIR} && docker compose down 2>/dev/null || true")
print(" 正在构建镜像并启动服务,请耐心等待...")
code, out, err = ssh_exec(ssh, f"cd {REMOTE_DIR} && docker compose up -d --build 2>&1", timeout=900)
if code != 0:
print(f" 构建失败!错误: {err}")
sys.exit(1)
# 5. Verify
print("\n[5/5] 等待服务启动...")
time.sleep(20)
ssh_exec(ssh, f"cd {REMOTE_DIR} && docker compose ps")
ssh_exec(ssh, "curl -s -o /dev/null -w 'HTTP状态码: %{http_code}' http://localhost:8080/api/auth/login 2>/dev/null || echo '服务启动中...'")
print("\n" + "=" * 50)
print(f"部署完成! 后端地址: http://{SERVER}:8080")
print("=" * 50)
sftp.close()
ssh.close()
if __name__ == "__main__":
main()