import paramiko host = '8.140.221.49' username = 'root' private_key_path = r'C:\Users\FZX\.ssh\FzxBdsh-ECS.pem' ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(host, username=username, key_filename=private_key_path, timeout=30) print('='*80) print('安装兼容的Maven版本') print('='*80) # 1. 检查当前Maven版本 print('\n1. 检查当前Maven版本...') cmd = 'mvn -v' stdin, stdout, stderr = ssh.exec_command(cmd) result = stdout.read().decode() print(f'当前Maven版本: {result}') # 2. 下载并安装Maven 3.8.8 print('\n2. 下载Maven 3.8.8...') cmd = 'cd /opt && wget -q https://dlcdn.apache.org/maven/maven-3/3.8.8/binaries/apache-maven-3.8.8-bin.tar.gz' stdin, stdout, stderr = ssh.exec_command(cmd, timeout=120) result = stdout.read().decode() print(f'下载结果: {result}') # 3. 解压Maven print('\n3. 解压Maven...') cmd = 'cd /opt && tar -xzf apache-maven-3.8.8-bin.tar.gz' stdin, stdout, stderr = ssh.exec_command(cmd) result = stdout.read().decode() print(f'解压结果: {result}') # 4. 设置环境变量 print('\n4. 设置环境变量...') cmd = 'export PATH=/opt/apache-maven-3.8.8/bin:$PATH && mvn -v' stdin, stdout, stderr = ssh.exec_command(cmd) result = stdout.read().decode() print(f'新Maven版本: {result}') # 5. 使用新版本构建common-service print('\n5. 使用新版本构建common-service...') cmd = 'export PATH=/opt/apache-maven-3.8.8/bin:$PATH && cd /opt/Fzxpodman/services/common-service && mvn clean install -DskipTests -q' stdin, stdout, stderr = ssh.exec_command(cmd, timeout=180) result = stdout.read().decode() print(f'构建输出: {result}') # 6. 检查构建是否成功 cmd = 'echo $?' stdin, stdout, stderr = ssh.exec_command(cmd) exit_code = stdout.read().decode().strip() if exit_code != '0': print(f'\n构建失败,退出码: {exit_code}') ssh.close() exit(1) print('common-service构建成功!') # 7. 构建user-service print('\n6. 构建user-service...') cmd = 'export PATH=/opt/apache-maven-3.8.8/bin:$PATH && cd /opt/Fzxpodman/services/user-service && mvn clean package -DskipTests -q' stdin, stdout, stderr = ssh.exec_command(cmd, timeout=180) result = stdout.read().decode() print(f'构建输出: {result}') # 8. 检查构建是否成功 cmd = 'echo $?' stdin, stdout, stderr = ssh.exec_command(cmd) exit_code = stdout.read().decode().strip() if exit_code != '0': print(f'\n构建失败,退出码: {exit_code}') ssh.close() exit(1) print('user-service构建成功!') # 9. 重新构建容器镜像 print('\n7. 重新构建容器镜像...') cmd = 'cd /opt/Fzxpodman/services/user-service && podman build -t localhost/fzx-aliyun-dev_user-service:latest .' stdin, stdout, stderr = ssh.exec_command(cmd, timeout=120) result = stdout.read().decode()[-1000:] print(f'镜像构建输出: {result}') # 10. 重启用户服务 print('\n8. 重启用户服务...') cmd = 'cd /opt/Fzxpodman/infrastructure/compose && podman-compose -f podman-compose.aliyun-dev.yml up -d user-service' stdin, stdout, stderr = ssh.exec_command(cmd, timeout=120) result = stdout.read().decode()[-1000:] print(f'启动输出: {result}') ssh.close() print('\n' + '='*80) print('操作完成!') print('='*80)