Linux机器上实现程序自动部署以及更新

本文介绍了如何在Linux环境中实现程序的自动部署和更新。通过SSH连接,使用前端 Avalon 和 MySQL 数据库,客户端程序访问指定URL获取版本信息和发送心跳。更新过程包括:检查最新版本、将源码从服务器A传输到B,并调用服务器上的脚本进行部署。
摘要由CSDN通过智能技术生成

平台化管理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}'
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值