标签:部署服务器 循环 java 使用 col invoke googl hostname 完成后
1. 背景介绍
在实际开发中,经常要一边开发一边测试,经常为了一个功能而不断更新版本,这些情况都会涉及到频繁的打包、部署;手动打包的涉及到项目上传、打包、发布等很多重复工作;
那么,有一种工具能够实现,将代码提交到git后就自动打包部署,替换手动认为打包,答案是肯定的:Jenkins ,当然除了Jenkins以外,也还有其他的工具可以实现自动化部署;
1 #!/usr/bin/env python 2 #coding=utf-8 3 4 import paramiko 5 6 import sys 7 8 9 10 #shell 传入参数 11 12 sourcePath = sys.argv[1]; #源目录 13 14 targetPath = sys.argv[2]; #目标目录 15 16 jarName = sys.argv[3]; #文件名 17 18 runPort = sys.argv[4]; #运行端口 19 20 21 22 #部署服务器配置 23 24 serveHost = ["192.168.208.131","192.168.208.134","192.168.208.135"]; #目标服务器IP 25 26 serveKey = {"192.168.208.131":"?","192.168.208.133":"?","192.168.208.134":"?","192.168.208.135":"?"}; #目标服务器密码 27 28 serveUser = {"192.168.208.131":"march","192.168.208.133":"march","192.168.208.134":"march","192.168.208.135":"march"}; #目标服务器登录用户名 29 30 serveSSHPort = {"192.168.208.131":5321,"192.168.208.133":5321,"192.168.208.134":5321,"192.168.208.135":5321}; #目标服务器SSH 端口 31 32 33 34 #监听invoke_shell 交互动态结果 35 36 #cmd 交互对象,endParam 结尾对象 37 38 def mutualResult(cmd,endParam): 39 40 buff = ""; 41 42 while not buff.endswith(endParam): 43 44 line = cmd.recv(9999); 45 46 try: 47 48 #进行异常捕捉,如果解码有问题,则换一种解码方式 49 50 buff += line.decode("utf8"); 51 52 except Exception : 53 54 buff += line.decode("gb18030"); 55 56 return buff; 57 58 59 60 #kill 运行端口 61 62 def killPort(ssh,port): 63 64 print ("run cmd >> " + "netstat -tunlp | grep "+port+" | awk ‘{print $7}‘ | awk -F‘/‘ ‘{print $1}‘"); 65 66 stdin, stdout, stderr = ssh.exec_command("netstat -tunlp | grep "+port+" | awk ‘{print $7}‘ | awk -F‘/‘ ‘{print $1}‘"); 67 68 pid = stdout.read().decode(); 69 70 print(pid) 71 72 if pid: 73 74 print ("run cmd >> 关闭 java进程 进程ID: "+pid); 75 76 ssh.exec_command("kill -9 "+pid); 77 78 79 80 def deploy(): 81 82 print("开始部署"+jarName); 83 84 for ip in serveHost: 85 86 print ("连接服务器IP:"+ip); 87 88 #连接服务器 89 90 ssh =paramiko.SSHClient(); 91 92 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()); 93 94 ssh.connect(hostname=ip,port=serveSSHPort[ip],username=serveUser[ip],password=serveKey[ip]); 95 96 cmd = ssh.invoke_shell(); 97 98 #连接交互 99 100 print (mutualResult(cmd,"$ ")); 101 102 print ("开始复制"+jarName+" >> 服务:"+ip); 103 104 print ("run cmd >> scp -r -P 5321 march@192.168.208.133:/var/lib/jenkins/workspace/" +sourcePath+ "/target/"+jarName+" "+ targetPath +"\n"); 105 106 cmd.send("scp -r -P 5321 march@192.168.208.133:/var/lib/jenkins/workspace/" +sourcePath+ "/target/"+jarName+" "+ targetPath +"\n"); 107 108 #监听交互并输入密码 109 110 mutualResult(cmd,"‘s password: "); 111 112 cmd.send("?\n"); 113 114 mutualResult(cmd,"$ "); 115 116 cmd.close(); 117 118 print("run cmd >> scp suceess"); 119 120 121 122 #kill运行进程 123 124 killPort(ssh,runPort); 125 126 127 128 #启动服务 129 130 cmd = ssh.invoke_shell(); 131 132 mutualResult(cmd,"$ "); 133 134 print ("run cmd >> " + "nohup java -jar " + targetPath + "/" + jarName +" --server.port=" +runPort +" > " + targetPath + "/" + jarName + ".log 2>&1 & \n"); 135 136 print ("等待服务器:" + ip + "启动"); 137 138 cmd.send("nohup java -jar " + targetPath + "/" + jarName +" --server.port=" +runPort +" > " + targetPath + "/" + jarName + ".log 2>&1 & \n"); 139 140 mutualResult(cmd,"$ "); 141 142 print ("核心服务:" + ip + "发布成功"); 143 144 145 146 cmd.close(); 147 148 ssh.close(); 149 150 151 152 if __name__ =="__deploy__": 153 154 deploy() 155 156 deploy()
DevOps_remote.sh脚本文件
1 #!/bin/bash 2 3 python3 /var/lib/jenkins/workspace/dev_remote.py $1 $2 $3 $4
配置发布通知邮箱
使用参数构建:
配置:
代码审查结果:
pom.xml配置:
<dependency> <groupId>com.puppycrawl.tools</groupId> <artifactId>checkstyle</artifactId> <version>8.33</version> </dependency> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <version>3.1.1</version> <configuration> <consoleOutput>true</consoleOutput> <outputFileFormat>xml</outputFileFormat> <!-- <configLocation>sun_checks.xml</configLocation>--> <configLocation>google_checks.xml</configLocation> <linkXRef>false</linkXRef> </configuration> <dependencies> <dependency> <groupId>com.puppycrawl.tools</groupId> <artifactId>checkstyle</artifactId> <version>8.33</version> </dependency> </dependencies> </plugin> <reporting> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId> <version>3.1.1</version> <configuration> <configLocation>google_checks.xml</configLocation> </configuration> </plugin> </plugins> </reporting>
Jenkins配置:
代码审查结果:
GITHub自动推送
标签:部署服务器 循环 java 使用 col invoke googl hostname 完成后
原文地址:https://www.cnblogs.com/march74/p/13264263.html