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. 48
      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>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>com.github.mwiede</groupId>
<artifactId>commons-exec</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId> <artifactId>jsch</artifactId>
<version>0.1.55</version> <version>0.2.16</version>
</dependency> </dependency>
<!-- 定时任务--> <!-- 定时任务-->

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

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

7
win-common/pom.xml

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

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

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

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

Loading…
Cancel
Save