平台化管理Linux环境的客户端程序的部署、更新、以及进程关闭
平台是用SSH实现,前端是avalon,数据库mysql客户端程序(Monitor, CLientSetup)是spring boot实现平台操作界面如下:
客户端程序访问路径为 http://ip:8034,
获取版本方式为 http://ip:8034/Deploy/Version
心跳地址为 http://ip:8034/Deploy/ReturnOk
1. 更新状态访问版本地址,返回该ip对应的版本,落入数据库,更新页面信息
2. 部署以及更新应用所有服务器路径都在ftpuser用户目录下,即/STRESS
应用源码放在服务器A上,为Monitor.tar,上传脚本Deploy.sh也在服务器A上,
上传脚本如下:
IP=$1
echo "put tar to "${IP}
ftp -n<<!
open ${IP}
user ftpuser 1qaz@WSX
binary
prompt
mkdir tools
cd tools
put Monitor.tar
put ClientSetup.tar
put clientDeploy.sh
#chmod 777 clientDeploy.sh
close
bye
!
平台调用上传脚本把源码从服务器A传到目标服务器B上。
平台再调用服务器B上脚本tools/clientDeploy.sh
cd tools
rm -rf Monitor
rm -rf ClientSetup
tar -xvf Monitor.tar
tar -xvf ClientSetup.tar
cd ~/tools/Monitor
sh restart.sh
cd ~/tools/ClientSetup
sh restart.sh
cd ~/tools
rm -rf Monitor.tar
rm -rf ClientSetup.tar
平台调用java代码
public String DeployEnvAgency(String ip) throws IOException, InterruptedException
{
RemoteShellTool tool = new RemoteShellTool("172.16.103.126", "ftpuser",
"******", "utf-8");
String result = tool.exec("sh Deploy.sh "+ip);
System.out.print("result:"+result);
RemoteShellTool tool2 = new RemoteShellTool(ip, "ftpuser",
"******", "utf-8");
String result2 = tool2.exec("sh tools/clientDeploy.sh");
System.out.print("result2:"+result2);
return "ok";
}
调用RemoteShellTool类如下:
package com.ymt.testplatform.util;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
public class RemoteShellTool {
private Connection conn;
private String ipAddr;
private String charset = Charset.defaultCharset().toString();
private String userName;
private String password;
public RemoteShellTool(String ipAddr, String charset) {
this.ipAddr = ipAddr;
if (charset != null) {
this.charset = charset;
}
}
public RemoteShellTool(String ipAddr, String userName, String password,
String charset) {
this.ipAddr = ipAddr;
this.userName = userName;
this.password = password;
if (charset != null) {
this.charset = charset;
}
}
public boolean login() throws IOException {
conn = new Connection(ipAddr);
conn.connect(); // 连接
return conn.authenticateWithPassword(userName, password); // 认证
}
public boolean loginCentos() throws IOException
{
conn = new Connection(ipAddr);
conn.connect(); // 连接
String [] pass = {"*****","*****","******","******","******"};
for (String pa : pass) {
if(conn.authenticateWithPassword("root", pa))
{
this.userName = "root";
this.password = pa;
return true;
}
}
return false;
}
public boolean login(String userName,String password) throws IOException {
conn = new Connection(ipAddr);
conn.connect(); // 连接
this.userName = userName;
this.password = password;
return conn.authenticateWithPassword(userName, password); // 认证
}
public String exec(String cmds) {
InputStream in = null;
String result = "";
try {
if (this.login()) {
Session session = conn.openSession(); // 打开一个会话
session.execCommand(cmds);
in = session.getStdout();
result = this.processStdout(in, this.charset);
session.close();
conn.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
return result;
}
public String processStdout(InputStream in, String charset) {
byte[] buf = new byte[1024];
StringBuffer sb = new StringBuffer();
try {
while (in.read(buf) != -1) {
sb.append(new String(buf, charset));
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
/**
* @param args
*/
public static void main(String[] args) {
RemoteShellTool tool = new RemoteShellTool("172.16.103.126", "ftpuser",
"*****", "utf-8");
String result = tool.exec("sh Deploy.sh 172.16.103.121");
System.out.print("result:"+result);
RemoteShellTool tool2 = new RemoteShellTool("172.16.103.121", "ftpuser",
"*****", "utf-8");
String result2 = tool2.exec("sh tools/clientDeploy.sh");
System.out.print("result2:"+result2);
}
}
3. 关闭进程
java代码:
public String killClient(String ip) throws IOException, InterruptedException
{
RemoteShellTool tool = new RemoteShellTool(ip,"utf-8");
tool.loginCentos();
tool.exec("kill -9 $(ps -ef|grep ClientSetup|gawk '$0 !~/grep/ {print $2}'|tr -s '\n''')");
tool.exec("kill -9 $(ps -ef|grep SpringMVCDemo|gawk '$0 !~/grep/ {print $2}'