Browse Source

联调

master
liuchen864 8 months ago
parent
commit
0d71a46745
  1. 10
      pom.xml
  2. 22
      win-admin/src/main/java/com/win/web/controller/base/LinuxAccessController.java
  3. 7
      win-common/pom.xml
  4. 50
      win-common/src/main/java/com/win/common/utils/command/CommandUtil.java
  5. 39
      win-system/src/main/java/com/win/system/service/LinuxAccessService.java

10
pom.xml

@ -180,15 +180,9 @@
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<groupId>com.github.mwiede</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
<version>0.2.16</version>
</dependency>
<!-- 定时任务-->

22
win-admin/src/main/java/com/win/web/controller/base/LinuxAccessController.java

@ -1,8 +1,5 @@
package com.win.web.controller.base;
import java.time.LocalDateTime;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.win.common.annotation.Log;
import com.win.common.core.controller.BaseController;
@ -15,18 +12,13 @@ import com.win.system.domain.LinuxAccess;
import com.win.system.service.LinuxAccessService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.Validator;
import java.time.LocalDateTime;
import java.util.List;
/**
* 访问控制Controller
@ -53,13 +45,13 @@ public class LinuxAccessController extends BaseController {
QueryWrapper<LinuxAccess> queryWrapper = new QueryWrapper<>();
queryWrapper.isNull("delete_time");
if(linuxaccess.getCompanyCode() != null && !linuxaccess.getCompanyCode().isEmpty()) {
queryWrapper.eq("company_code", linuxaccess.getCompanyCode());
queryWrapper.like("company_code", linuxaccess.getCompanyCode());
}
if(linuxaccess.getCompanyName() != null && !linuxaccess.getCompanyName().isEmpty()) {
queryWrapper.like("company_name", linuxaccess.getCompanyName());
}
if(linuxaccess.getUri() != null && !linuxaccess.getUri().isEmpty()) {
queryWrapper.eq("uri", linuxaccess.getUri());
queryWrapper.like("uri", linuxaccess.getUri());
}
List<LinuxAccess> list = linuxAccessService.list(queryWrapper);
return getDataTable(list);
@ -74,13 +66,13 @@ public class LinuxAccessController extends BaseController {
QueryWrapper<LinuxAccess> queryWrapper = new QueryWrapper<>();
queryWrapper.isNull("delete_time");
if(linuxAccess.getCompanyCode() != null && !linuxAccess.getCompanyCode().isEmpty()) {
queryWrapper.eq("company_code", linuxAccess.getCompanyCode());
queryWrapper.like("company_code", linuxAccess.getCompanyCode());
}
if(linuxAccess.getCompanyName() != null && !linuxAccess.getCompanyName().isEmpty()) {
queryWrapper.like("company_name", linuxAccess.getCompanyName());
}
if(linuxAccess.getUri() != null && !linuxAccess.getUri().isEmpty()) {
queryWrapper.eq("uri", linuxAccess.getUri());
queryWrapper.like("uri", linuxAccess.getUri());
}
List<LinuxAccess> list = linuxAccessService.list(queryWrapper);
ExcelUtil<LinuxAccess> util = new ExcelUtil<>(LinuxAccess.class);

7
win-common/pom.xml

@ -142,12 +142,7 @@
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<groupId>com.github.mwiede</groupId>
<artifactId>jsch</artifactId>
</dependency>

50
win-common/src/main/java/com/win/common/utils/command/CommandUtil.java

@ -1,34 +1,40 @@
package com.win.common.utils.command;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.PumpStreamHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 执行系统命令工具类
*/
public class CommandUtil {
private static Logger logger = LoggerFactory.getLogger(CommandUtil.class.getSimpleName());
/**
* 执行系统命令
* 执行指定命令
*
* @param command 命令
* @throws IOException
* @throws InterruptedException
* @return 命令执行完成返回结果
* @throws RuntimeException 失败时抛出异常由调用者捕获处理
*/
public static String execute(String command) throws IOException, InterruptedException {
// 构建CommandLine对象
CommandLine commandLine = CommandLine.parse(command);
// 构建DefaultExecutor对象
DefaultExecutor defaultExecutor = new DefaultExecutor();
// 构建ByteArrayOutputStream对象
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// 构建PumpStreamHandler对象
PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(byteArrayOutputStream);
defaultExecutor.setStreamHandler(pumpStreamHandler);
// 开始执行命令
defaultExecutor.execute(commandLine);
// 返回Linux命令执行的结果
return byteArrayOutputStream.toString();
public synchronized static String exeCommand(String command) throws RuntimeException {
logger.info("开始执行命令: " + command);
try {
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
StringBuilder result = new StringBuilder();
while ((line = reader.readLine()) != null) {
result.append(line).append("\n");
}
return result.toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}

39
win-system/src/main/java/com/win/system/service/LinuxAccessService.java

@ -19,6 +19,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
@ -84,6 +85,7 @@ public class LinuxAccessService extends ServiceImpl<LinuxAccessMapper, LinuxAcce
return AjaxResult.error(HttpStatus.LINUX_ACCESS_ERROR, "linux访问控制不存在");
}
QueryWrapper<QadAccess> qadAccessQueryWrapper = new QueryWrapper<>();
qadAccessQueryWrapper.eq("company_code", companyCode);
qadAccessQueryWrapper.eq("domain", domain);
QadAccess qadAccess = qadAccessMapper.selectOne(qadAccessQueryWrapper);
if(qadAccess == null) {
@ -93,9 +95,10 @@ public class LinuxAccessService extends ServiceImpl<LinuxAccessMapper, LinuxAcce
//生成traceid
String traceid = jsonObject.getString("traceid");
jsonObject.remove("traceid");
jsonObject.remove("dataid");
jsonObject.remove("company_code");
jsonObject.remove("domain");
String inJson = "{\"dsDescreteOrder\":"+ jsonObject+"}";
String inJson = "{\"" + key + "\":"+ jsonObject+"}";
log.info("inJson: {}", inJson);
//写入json
String result = shellUtil.execute(session, "touch " + linuxAccess.getWorkPath() + traceid + ".json && echo '" + inJson + "'>" + linuxAccess.getWorkPath() + traceid + ".json");
@ -141,7 +144,7 @@ public class LinuxAccessService extends ServiceImpl<LinuxAccessMapper, LinuxAcce
*/
public AjaxResult executeCommand(String body, String uri, String key) throws IOException, InterruptedException {
JSONObject jsonObject = JSONObject.parseObject(body);
jsonObject = JSONObject.parseObject(jsonObject.getString("dsDescreteOrder"));
jsonObject = JSONObject.parseObject(jsonObject.getString(key));
String domain = jsonObject.getString("domain");
if(domain == null || domain.isEmpty()) {
return AjaxResult.error(HttpStatus.DOMAIN_ERROR, "域不能为空");
@ -158,6 +161,7 @@ public class LinuxAccessService extends ServiceImpl<LinuxAccessMapper, LinuxAcce
return AjaxResult.error(HttpStatus.LINUX_ACCESS_ERROR, "linux访问控制不存在");
}
QueryWrapper<QadAccess> qadAccessQueryWrapper = new QueryWrapper<>();
qadAccessQueryWrapper.eq("company_code", companyCode);
qadAccessQueryWrapper.eq("domain", domain);
QadAccess qadAccess = qadAccessMapper.selectOne(qadAccessQueryWrapper);
if(qadAccess == null) {
@ -166,18 +170,19 @@ public class LinuxAccessService extends ServiceImpl<LinuxAccessMapper, LinuxAcce
//生成traceid
String traceid = jsonObject.getString("traceid");
jsonObject.remove("traceid");
jsonObject.remove("dataid");
jsonObject.remove("company_code");
jsonObject.remove("domain");
String inJson = "{\"dsDescreteOrder\":"+ jsonObject+"}";
String inJson = "{\"" + key + "\":"+ jsonObject+"}";
log.info("inJson: {}", inJson);
//写入json
String result = CommandUtil.execute("touch " + linuxAccess.getWorkPath() + traceid + ".json && echo '" + inJson + "'>" + linuxAccess.getWorkPath() + traceid + ".json");
log.info("write json result : {}", result);
FileOutputStream fileOutputStream = new FileOutputStream(linuxAccess.getWorkPath() + traceid + ".json");
fileOutputStream.write(inJson.getBytes());
fileOutputStream.close();
//写入dat.json
String datJson = "\"" + linuxAccess.getWorkPath() + traceid + ".json\" \"" + linuxAccess.getQadProgramCode() + "\" \"" + linuxAccess.getOutPath() + traceid+".out.log\" \"" + qadAccess.getUserName() + "\" \"" + qadAccess.getPassword() + "\"\r\n\"" + domain + "\"";
log.info("datJson: {}", datJson);
result = CommandUtil.execute("touch " + linuxAccess.getWorkPath() + traceid + ".dat && echo '" + datJson + "'>" + linuxAccess.getWorkPath() + traceid+".dat");
log.info("write dat json result : {}", result);
fileOutputStream = new FileOutputStream(linuxAccess.getWorkPath() + traceid + ".dat");
fileOutputStream.write(datJson.getBytes());
fileOutputStream.close();
//写入执行文件
String pJson = "output to " + linuxAccess.getOutPath() + traceid + ".run.log.\r\n" +
"input from " + linuxAccess.getWorkPath() + traceid + ".dat.\r\n" +
@ -185,20 +190,22 @@ public class LinuxAccessService extends ServiceImpl<LinuxAccessMapper, LinuxAcce
"input close.\r\n" +
"output close.";
log.info("pJson: {}", pJson);
result = CommandUtil.execute("touch " + linuxAccess.getWorkPath() + traceid+".p && echo '"+pJson+"'>" + linuxAccess.getWorkPath() + traceid + ".p");
log.info("write p result : {}", result);
fileOutputStream = new FileOutputStream(linuxAccess.getWorkPath() + traceid + ".p");
fileOutputStream.write(pJson.getBytes());
fileOutputStream.close();
log.info("pJson: {}", pJson);
//执行QAD脚本
log.info("command : {}", linuxAccess.getScriptPath() + linuxAccess.getScriptName() + " " + linuxAccess.getWorkPath() + traceid + ".p");
result = CommandUtil.execute(linuxAccess.getScriptPath() + linuxAccess.getScriptName() + " " + linuxAccess.getWorkPath() + traceid + ".p");
String result = CommandUtil.exeCommand(linuxAccess.getScriptPath() + linuxAccess.getScriptName() + " " + linuxAccess.getWorkPath() + traceid + ".p");
log.info("execute {} result : {}", linuxAccess.getScriptPath() + linuxAccess.getScriptName(), result);
result = CommandUtil.execute("cat " + linuxAccess.getOutPath() + traceid + ".run.out.json");
result = CommandUtil.exeCommand("cat " + linuxAccess.getOutPath() + traceid + ".run.out.json");
log.info("cat execute log : {}", result);
if(result.indexOf("\"SUCCESS\"") > 0) {
//移动执行结果文件
CommandUtil.execute("mv " + linuxAccess.getOutPath() + traceid + ".run.out.json " + linuxAccess.getSuccessPath() + traceid + ".run.out.json");
return AjaxResult.success(traceid);
CommandUtil.exeCommand("mv " + linuxAccess.getOutPath() + traceid + ".run.out.json " + linuxAccess.getSuccessPath() + traceid + ".run.out.json");
return AjaxResult.success(result);
}
CommandUtil.execute("mv " + linuxAccess.getOutPath() + traceid + ".run.out.json " + linuxAccess.getErrorPath() + traceid + ".run.out.json");
CommandUtil.exeCommand("mv " + linuxAccess.getOutPath() + traceid + ".run.out.json " + linuxAccess.getErrorPath() + traceid + ".run.out.json");
return AjaxResult.error(HttpStatus.EXECUTE_FAIL, result);
}

Loading…
Cancel
Save