mirror of https://github.com/elunez/eladmin
更改命令执行方式为shell,避免因为无法获得环境变量而无法启动程序的问题
parent
0cbc6e55e4
commit
1cec669c4a
|
@ -9,8 +9,13 @@ import me.zhengjie.modules.mnt.domain.Deploy;
|
||||||
import me.zhengjie.modules.mnt.domain.DeployHistory;
|
import me.zhengjie.modules.mnt.domain.DeployHistory;
|
||||||
import me.zhengjie.modules.mnt.domain.ServerDeploy;
|
import me.zhengjie.modules.mnt.domain.ServerDeploy;
|
||||||
import me.zhengjie.modules.mnt.repository.DeployRepository;
|
import me.zhengjie.modules.mnt.repository.DeployRepository;
|
||||||
import me.zhengjie.modules.mnt.service.*;
|
import me.zhengjie.modules.mnt.service.DeployHistoryService;
|
||||||
import me.zhengjie.modules.mnt.service.dto.*;
|
import me.zhengjie.modules.mnt.service.DeployService;
|
||||||
|
import me.zhengjie.modules.mnt.service.ServerDeployService;
|
||||||
|
import me.zhengjie.modules.mnt.service.dto.AppDto;
|
||||||
|
import me.zhengjie.modules.mnt.service.dto.DeployDto;
|
||||||
|
import me.zhengjie.modules.mnt.service.dto.DeployQueryCriteria;
|
||||||
|
import me.zhengjie.modules.mnt.service.dto.ServerDeployDto;
|
||||||
import me.zhengjie.modules.mnt.service.mapper.DeployMapper;
|
import me.zhengjie.modules.mnt.service.mapper.DeployMapper;
|
||||||
import me.zhengjie.modules.mnt.util.ExecuteShellUtil;
|
import me.zhengjie.modules.mnt.util.ExecuteShellUtil;
|
||||||
import me.zhengjie.modules.mnt.util.ScpClientUtil;
|
import me.zhengjie.modules.mnt.util.ScpClientUtil;
|
||||||
|
@ -26,9 +31,8 @@ import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Propagation;
|
import org.springframework.transaction.annotation.Propagation;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.sql.Timestamp;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -42,7 +46,7 @@ import java.util.Set;
|
||||||
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
|
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
|
||||||
public class DeployServiceImpl implements DeployService {
|
public class DeployServiceImpl implements DeployService {
|
||||||
|
|
||||||
private final String FILE_SEPARATOR = File.separatorChar + "";
|
private final String FILE_SEPARATOR = "/";
|
||||||
|
|
||||||
private final DeployRepository deployRepository;
|
private final DeployRepository deployRepository;
|
||||||
|
|
||||||
|
@ -163,6 +167,7 @@ public class DeployServiceImpl implements DeployService {
|
||||||
boolean result = checkIsRunningStatus(port, executeShellUtil);
|
boolean result = checkIsRunningStatus(port, executeShellUtil);
|
||||||
sb.append("服务器:").append(deployDTO.getName()).append("<br>应用:").append(app.getName());
|
sb.append("服务器:").append(deployDTO.getName()).append("<br>应用:").append(app.getName());
|
||||||
sendResultMsg(result, sb);
|
sendResultMsg(result, sb);
|
||||||
|
executeShellUtil.close();
|
||||||
}
|
}
|
||||||
return "部署结束";
|
return "部署结束";
|
||||||
}
|
}
|
||||||
|
@ -178,7 +183,7 @@ public class DeployServiceImpl implements DeployService {
|
||||||
private void backupApp(ExecuteShellUtil executeShellUtil, String ip, String fileSavePath, String appName, String backupPath, Long id) {
|
private void backupApp(ExecuteShellUtil executeShellUtil, String ip, String fileSavePath, String appName, String backupPath, Long id) {
|
||||||
String deployDate = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN);
|
String deployDate = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN);
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
if (!backupPath.endsWith(FILE_SEPARATOR)) {
|
if (!backupPath.endsWith(FILE_SEPARATOR)&&!backupPath.endsWith("\\")) {
|
||||||
backupPath += FILE_SEPARATOR;
|
backupPath += FILE_SEPARATOR;
|
||||||
}
|
}
|
||||||
backupPath += appName + FILE_SEPARATOR + deployDate + "\n";
|
backupPath += appName + FILE_SEPARATOR + deployDate + "\n";
|
||||||
|
@ -219,8 +224,8 @@ public class DeployServiceImpl implements DeployService {
|
||||||
* @return true 正在运行 false 已经停止
|
* @return true 正在运行 false 已经停止
|
||||||
*/
|
*/
|
||||||
private boolean checkIsRunningStatus(int port, ExecuteShellUtil executeShellUtil) {
|
private boolean checkIsRunningStatus(int port, ExecuteShellUtil executeShellUtil) {
|
||||||
String statusResult = executeShellUtil.executeForResult(String.format("fuser -n tcp %d", port));
|
String result = executeShellUtil.executeForResult(String.format("fuser -n tcp %d", port));
|
||||||
return !"".equals(statusResult.trim());
|
return result.indexOf("/tcp:")>0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendMsg(String msg, MsgType msgType) {
|
private void sendMsg(String msg, MsgType msgType) {
|
||||||
|
@ -248,16 +253,15 @@ public class DeployServiceImpl implements DeployService {
|
||||||
sendMsg(sb.toString(), MsgType.ERROR);
|
sendMsg(sb.toString(), MsgType.ERROR);
|
||||||
}
|
}
|
||||||
log.info(sb.toString());
|
log.info(sb.toString());
|
||||||
|
executeShellUtil.close();
|
||||||
}
|
}
|
||||||
return "执行完毕";
|
return "执行完毕";
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean checkFile(ExecuteShellUtil executeShellUtil, AppDto appDTO) {
|
private boolean checkFile(ExecuteShellUtil executeShellUtil, AppDto appDTO) {
|
||||||
String sb = "find " +
|
StringBuilder sb = new StringBuilder("find ").append(appDTO.getDeployPath()).append(" -name ").append(appDTO.getName());
|
||||||
appDTO.getDeployPath() +
|
String result = executeShellUtil.executeForResult(sb.toString());
|
||||||
" -name " +
|
return result.indexOf("/tcp:")>0;
|
||||||
appDTO.getName();
|
|
||||||
return executeShellUtil.executeShell(sb);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -282,6 +286,7 @@ public class DeployServiceImpl implements DeployService {
|
||||||
boolean result = checkIsRunningStatus(app.getPort(), executeShellUtil);
|
boolean result = checkIsRunningStatus(app.getPort(), executeShellUtil);
|
||||||
sendResultMsg(result, sb);
|
sendResultMsg(result, sb);
|
||||||
log.info(sb.toString());
|
log.info(sb.toString());
|
||||||
|
executeShellUtil.close();
|
||||||
}
|
}
|
||||||
return "执行完毕";
|
return "执行完毕";
|
||||||
}
|
}
|
||||||
|
@ -312,6 +317,7 @@ public class DeployServiceImpl implements DeployService {
|
||||||
sendMsg(sb.toString(), MsgType.INFO);
|
sendMsg(sb.toString(), MsgType.INFO);
|
||||||
}
|
}
|
||||||
log.info(sb.toString());
|
log.info(sb.toString());
|
||||||
|
executeShellUtil.close();
|
||||||
}
|
}
|
||||||
return "执行完毕";
|
return "执行完毕";
|
||||||
}
|
}
|
||||||
|
@ -320,7 +326,7 @@ public class DeployServiceImpl implements DeployService {
|
||||||
public String serverReduction(DeployHistory resources) {
|
public String serverReduction(DeployHistory resources) {
|
||||||
Long deployId = resources.getDeployId();
|
Long deployId = resources.getDeployId();
|
||||||
Deploy deployInfo = deployRepository.findById(deployId).orElseGet(Deploy::new);
|
Deploy deployInfo = deployRepository.findById(deployId).orElseGet(Deploy::new);
|
||||||
Timestamp deployDate = resources.getDeployDate();
|
String deployDate = DateUtil.format(resources.getDeployDate(), DatePattern.PURE_DATETIME_PATTERN);
|
||||||
App app = deployInfo.getApp();
|
App app = deployInfo.getApp();
|
||||||
if (app == null) {
|
if (app == null) {
|
||||||
sendMsg("应用信息不存在:" + resources.getAppName(), MsgType.ERROR);
|
sendMsg("应用信息不存在:" + resources.getAppName(), MsgType.ERROR);
|
||||||
|
@ -363,6 +369,7 @@ public class DeployServiceImpl implements DeployService {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("服务器:").append(ip).append("<br>应用:").append(resources.getAppName());
|
sb.append("服务器:").append(ip).append("<br>应用:").append(resources.getAppName());
|
||||||
sendResultMsg(result, sb);
|
sendResultMsg(result, sb);
|
||||||
|
executeShellUtil.close();
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,106 +1,88 @@
|
||||||
package me.zhengjie.modules.mnt.util;
|
package me.zhengjie.modules.mnt.util;
|
||||||
|
|
||||||
import com.jcraft.jsch.*;
|
import cn.hutool.core.io.IoUtil;
|
||||||
|
import com.jcraft.jsch.ChannelShell;
|
||||||
|
import com.jcraft.jsch.JSch;
|
||||||
|
import com.jcraft.jsch.Session;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.*;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行shell命令
|
* 执行shell命令
|
||||||
|
*
|
||||||
* @author: ZhangHouYing
|
* @author: ZhangHouYing
|
||||||
* @date: 2019/8/10
|
* @date: 2019/8/10
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class ExecuteShellUtil {
|
public class ExecuteShellUtil {
|
||||||
|
|
||||||
private String ipAddress;
|
|
||||||
|
|
||||||
private String username;
|
|
||||||
|
|
||||||
private String password;
|
|
||||||
|
|
||||||
public final int DEFAULT_SSH_PORT = 22;
|
public final int DEFAULT_SSH_PORT = 22;
|
||||||
|
|
||||||
private Vector<String> stdout;
|
private Vector<String> stdout;
|
||||||
|
|
||||||
|
Session session;
|
||||||
|
|
||||||
public ExecuteShellUtil(final String ipAddress, final String username, final String password) {
|
public ExecuteShellUtil(final String ipAddress, final String username, final String password) {
|
||||||
this.ipAddress = ipAddress;
|
try {
|
||||||
this.username = username;
|
JSch jsch = new JSch();
|
||||||
this.password = password;
|
session = jsch.getSession(username, ipAddress, DEFAULT_SSH_PORT);
|
||||||
stdout = new Vector<String>();
|
session.setPassword(password);
|
||||||
|
session.setConfig("StrictHostKeyChecking", "no");
|
||||||
|
session.connect(3000);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int execute(final String command) {
|
public int execute(final String command) {
|
||||||
int returnCode = 0;
|
int returnCode = 0;
|
||||||
JSch jsch = new JSch();
|
ChannelShell channel = null;
|
||||||
|
PrintWriter printWriter = null;
|
||||||
|
BufferedReader input = null;
|
||||||
|
stdout = new Vector<String>();
|
||||||
try {
|
try {
|
||||||
Session session = jsch.getSession(username, ipAddress, DEFAULT_SSH_PORT);
|
channel = (ChannelShell) session.openChannel("shell");
|
||||||
session.setPassword(password);
|
|
||||||
session.setConfig("StrictHostKeyChecking", "no");
|
|
||||||
session.connect(30000);
|
|
||||||
|
|
||||||
Channel channel = session.openChannel("exec");
|
|
||||||
((ChannelExec) channel).setCommand(command);
|
|
||||||
|
|
||||||
channel.setInputStream(null);
|
|
||||||
BufferedReader input = new BufferedReader(new InputStreamReader(channel.getInputStream()));
|
|
||||||
|
|
||||||
channel.connect();
|
channel.connect();
|
||||||
log.info("The remote command is: " + command);
|
input = new BufferedReader(new InputStreamReader(channel.getInputStream()));
|
||||||
|
printWriter = new PrintWriter(channel.getOutputStream());
|
||||||
|
printWriter.println(command);
|
||||||
|
printWriter.println("exit");
|
||||||
|
printWriter.flush();
|
||||||
|
log.info("The remote command is: ");
|
||||||
String line;
|
String line;
|
||||||
while ((line = input.readLine()) != null) {
|
while ((line = input.readLine()) != null) {
|
||||||
stdout.add(line);
|
stdout.add(line);
|
||||||
|
System.out.println(line);
|
||||||
}
|
}
|
||||||
input.close();
|
|
||||||
|
|
||||||
if (channel.isClosed()) {
|
|
||||||
returnCode = channel.getExitStatus();
|
|
||||||
}
|
|
||||||
channel.disconnect();
|
|
||||||
session.disconnect();
|
|
||||||
} catch (JSchException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
return -1;
|
||||||
|
}finally {
|
||||||
|
IoUtil.close(printWriter);
|
||||||
|
IoUtil.close(input);
|
||||||
|
if (channel != null) {
|
||||||
|
channel.disconnect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return returnCode;
|
return returnCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean executeShell(String command) {
|
public void close(){
|
||||||
int result = execute(command);
|
if (session != null) {
|
||||||
for (String str : stdout) {
|
session.disconnect();
|
||||||
log.info(str);
|
|
||||||
}
|
|
||||||
if (result == 0) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String executeForResult(String command) {
|
public String executeForResult(String command) {
|
||||||
execute(command);
|
execute(command);
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
for (String str : stdout) {
|
for (String str : stdout) {
|
||||||
sb.append(str);
|
sb.append(str);
|
||||||
log.info(str);
|
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 返回值有三条就代表成功了,2条是没启动,多余三条代表脚本有问题
|
|
||||||
* @param command
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public int checkAppStatus(String command) {
|
|
||||||
execute(command);
|
|
||||||
for (String str : stdout) {
|
|
||||||
log.info(str);
|
|
||||||
}
|
|
||||||
return stdout.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,7 +46,7 @@ CREATE TABLE `mnt_app` (
|
||||||
-- ----------------------------
|
-- ----------------------------
|
||||||
-- Records of mnt_app
|
-- Records of mnt_app
|
||||||
-- ----------------------------
|
-- ----------------------------
|
||||||
INSERT INTO `mnt_app` VALUES (1, 'eladmin-monitor-2.3.jar', '/opt/upload', '/opt/monitor', '/opt/backup', 8777, 'cd /opt/monitor/\nnohup java -jar eladmin-monitor-2.2.jar >nohup.out 2>&1 &\n', 'mv /opt/upload/eladmin-monitor-2.2.jar /opt/monitor/\ncd /opt/monitor\nnohup java -jar eladmin-monitor-2.2.jar >nohup.out 2>&1 &\n', '2019-11-24 20:52:59');
|
INSERT INTO `mnt_app` VALUES (1, 'eladmin-monitor-2.3.jar', '/opt/upload', '/opt/monitor', '/opt/backup', 8777, 'cd /opt/monitor\nnohup java -jar eladmin-monitor-2.3.jar >nohup.out 2>&1 &\n', 'mkdir -p /opt/monitor\nmv -f /opt/upload/eladmin-monitor-2.3.jar /opt/monitor\n', '2019-11-24 20:52:59');
|
||||||
|
|
||||||
-- ----------------------------
|
-- ----------------------------
|
||||||
-- Table structure for mnt_database
|
-- Table structure for mnt_database
|
||||||
|
|
|
@ -353,7 +353,7 @@ CREATE TABLE `mnt_app` (
|
||||||
-- ----------------------------
|
-- ----------------------------
|
||||||
-- Records of mnt_app
|
-- Records of mnt_app
|
||||||
-- ----------------------------
|
-- ----------------------------
|
||||||
INSERT INTO `mnt_app` VALUES (1, 'eladmin-monitor-2.3.jar', '/opt/upload', '/opt/monitor', '/opt/backup', 8777, 'cd /opt/monitor/\nnohup java -jar eladmin-monitor-2.2.jar >nohup.out 2>&1 &\n', 'mv /opt/upload/eladmin-monitor-2.2.jar /opt/monitor/\ncd /opt/monitor\nnohup java -jar eladmin-monitor-2.2.jar >nohup.out 2>&1 &\n', '2019-11-24 20:52:59');
|
INSERT INTO `mnt_app` VALUES (1, 'eladmin-monitor-2.3.jar', '/opt/upload', '/opt/monitor', '/opt/backup', 8777, 'cd /opt/monitor\nnohup java -jar eladmin-monitor-2.3.jar >nohup.out 2>&1 &\n', 'mkdir -p /opt/monitor\nmv -f /opt/upload/eladmin-monitor-2.3.jar /opt/monitor\n', '2019-11-24 20:52:59');
|
||||||
|
|
||||||
-- ----------------------------
|
-- ----------------------------
|
||||||
-- Table structure for mnt_database
|
-- Table structure for mnt_database
|
||||||
|
|
Loading…
Reference in New Issue