diff --git a/win-admin/src/main/java/com/win/web/controller/base/CommandController.java b/win-admin/src/main/java/com/win/web/controller/base/CommandController.java index ab68f27..30ac8ee 100644 --- a/win-admin/src/main/java/com/win/web/controller/base/CommandController.java +++ b/win-admin/src/main/java/com/win/web/controller/base/CommandController.java @@ -1,11 +1,14 @@ package com.win.web.controller.base; import com.alibaba.fastjson2.JSONObject; +import com.win.common.annotation.Log; import com.win.common.constant.HttpStatus; import com.win.common.core.domain.AjaxResult; +import com.win.common.enums.BusinessType; import com.win.common.utils.ProfileUtil; import com.win.common.utils.command.CommandUtil; import com.win.common.utils.shell.ShellVo; +import com.win.system.service.LinuxAccessService; import com.win.web.config.ShellConfig; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -32,7 +35,7 @@ public class CommandController { private static final Logger logger = LoggerFactory.getLogger("sys-user"); @Autowired - private ShellConfig shellConfig; + private LinuxAccessService linuxAccessService; /** * 对外提供一个接口,通过header中的interfaceName反射机制调用方法,方法必须写到这个controller中,并且不用加PostMapping注解。 @@ -42,6 +45,7 @@ public class CommandController { * @return 结果 */ @PostMapping("/api") + @Log(title = "命令行对接qad", businessType = BusinessType.INSERT, isManager = false) public AjaxResult api(HttpServletRequest request, @RequestBody String body) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { String interfaceName = request.getHeader("interface"); String sign = request.getHeader("sign"); @@ -55,7 +59,7 @@ public class CommandController { } catch (NumberFormatException e) { return AjaxResult.error(HttpStatus.TIMESTAMP_ERROR, "时间戳不正确"); } - String tmp = interfaceName + body + timestamp; + String tmp = interfaceName + body + "Wenyin@2024" + timestamp; String computeSign = DigestUtils.md5DigestAsHex(tmp.getBytes()); long tenTimestamp = timestamp + (10 * 60 * 1000); // 计算10分钟后的时间戳 long currentTimestamp = System.currentTimeMillis(); // 获取当前时间戳 @@ -72,61 +76,18 @@ public class CommandController { return (AjaxResult) method.invoke(this, body); } - public AjaxResult itemmastercreate(String body) throws IOException, InterruptedException { - String result = CommandUtil.execute("cmd /c ping www.baidu.com -n 5"); - return AjaxResult.success(result); - } - - public AjaxResult itemdetailcreate(String body) throws IOException, InterruptedException { - String result = CommandUtil.execute("cmd dir"); - return AjaxResult.success(result); - } - public AjaxResult discretepo(String body) throws IOException, InterruptedException { JSONObject jsonObject = JSONObject.parseObject(body); jsonObject = JSONObject.parseObject(jsonObject.getString("dsDescreteOrder")); - ShellVo shellVo; - if(jsonObject.getString("domain") != null && jsonObject.getString("domain").equals("JLHT2")) { - shellVo = shellConfig.getJlht2(); - } else { - shellVo = shellConfig.getJlht(); + String domain = jsonObject.getString("domain"); + if(domain == null || domain.isEmpty()) { + return AjaxResult.error(HttpStatus.DOMAIN_ERROR, "域不能为空"); } - //生成traceid - String traceid = jsonObject.getString("traceid"); - jsonObject.remove("traceid"); - jsonObject.remove("company"); - jsonObject.remove("domain"); - String inJson = "{\"dsDescreteOrder\":"+ jsonObject+"}"; - log.info("inJson: {}", inJson); - //写入json - String result = CommandUtil.execute("touch /home/mfg/work/cim/po/"+traceid+".json && echo '"+inJson+"'>/home/mfg/work/cim/po/"+traceid+".json"); - log.info("write json result : " + result); - //写入dat.json - String datJson = "\"/home/mfg/work/cim/po/"+traceid+".json\" \"xxdspoapi.p\" \"/home/mfg/work/out/po/"+traceid+".out.log\" \""+shellVo.getQadUser()+"\" \""+shellVo.getQadPassword()+"\"\r\n\""+shellVo.getQadDomain()+"\""; - log.info("datJson: {}", datJson); - result = CommandUtil.execute("touch /home/mfg/work/cim/po/"+traceid+".dat && echo '"+datJson+"'>/home/mfg/work/cim/po/"+traceid+".dat"); - log.info("write dat json result : " + result); - //写入执行文件 - String pJson = "output to /home/mfg/work/out/po/" + traceid + ".run.log.\r\n" + - "input from /home/mfg/work/cim/po/" + traceid + ".dat.\r\n" + - "run /home/mfg/work/scripts/xxcommon01.p.\r\n" + - "input close.\r\n" + - "output close."; - log.info("pJson: {}", pJson); - result = CommandUtil.execute("touch /home/mfg/work/cim/po/"+traceid+".p && echo '"+pJson+"'>/home/mfg/work/cim/po/"+traceid+".p"); - log.info("write p result : " + result); - //执行QAD脚本 - result = CommandUtil.execute("sh /home/mfg/work/scripts/client-ch-cimload /home/mfg/work/cim/po/"+traceid+".p"); - log.info("execute client-ch-loaddata : " + result); - result = CommandUtil.execute("cat /home/mfg/work/out/po/" + traceid + ".run.out.json"); - log.info("cat execute log : " + result); - if(result.indexOf("\"SUCCESS\"") > 0) { - //移动执行结果文件 - CommandUtil.execute("mv /home/mfg/work/out/po/" + traceid + ".run.out.json /home/mfg/work/success/po/" + traceid + ".run.out.json"); - return AjaxResult.success(traceid); + String companyCode = jsonObject.getString("company_code"); + if(companyCode == null || companyCode.isEmpty()) { + return AjaxResult.error(HttpStatus.COMPANY_CODE_ERROR, "公司编码不能为空"); } - CommandUtil.execute("mv /home/mfg/work/out/po/" + traceid + ".run.out.json /home/mfg/work/err/po/" + traceid + ".run.out.json"); - return AjaxResult.error(HttpStatus.EXECUTE_FAIL, result); + return linuxAccessService.executeCommand(domain, companyCode, "purchaseorder/discretepo/", jsonObject); } } diff --git a/win-admin/src/main/java/com/win/web/controller/base/LinuxAccessController.java b/win-admin/src/main/java/com/win/web/controller/base/LinuxAccessController.java index 84a870a..addca37 100644 --- a/win-admin/src/main/java/com/win/web/controller/base/LinuxAccessController.java +++ b/win-admin/src/main/java/com/win/web/controller/base/LinuxAccessController.java @@ -141,7 +141,7 @@ public class LinuxAccessController extends BaseController { @Log(title = "访问控制", businessType = BusinessType.DELETE) @DeleteMapping("/{ids}") public AjaxResult batchRemove(@PathVariable Long[] ids) { - return toAjax(linuxAccessService.deleteLinuxAccessByGuids(ids, getUserId())); + return toAjax(linuxAccessService.deleteLinuxAccessByIds(ids, getUserId())); } } diff --git a/win-admin/src/main/java/com/win/web/controller/base/ShellController.java b/win-admin/src/main/java/com/win/web/controller/base/ShellController.java index 1086ca2..9c6bd26 100644 --- a/win-admin/src/main/java/com/win/web/controller/base/ShellController.java +++ b/win-admin/src/main/java/com/win/web/controller/base/ShellController.java @@ -3,11 +3,14 @@ package com.win.web.controller.base; import com.alibaba.fastjson2.JSONObject; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; +import com.win.common.annotation.Log; import com.win.common.constant.HttpStatus; import com.win.common.core.domain.AjaxResult; +import com.win.common.enums.BusinessType; import com.win.common.utils.ProfileUtil; import com.win.common.utils.shell.ShellUtil; import com.win.common.utils.shell.ShellVo; +import com.win.system.service.LinuxAccessService; import com.win.web.config.ShellConfig; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -34,9 +37,7 @@ public class ShellController { private static final Logger logger = LoggerFactory.getLogger("sys-user"); @Autowired - private ShellUtil shellUtil; - @Autowired - private ShellConfig shellConfig; + private LinuxAccessService linuxAccessService; /** * 对外提供一个接口,通过header中的interfaceName反射机制调用方法,方法必须写到这个controller中,并且不用加PostMapping注解。 @@ -46,6 +47,7 @@ public class ShellController { * @return 结果 */ @PostMapping("/api") + @Log(title = "远程行对接qad", businessType = BusinessType.INSERT, isManager = false) public AjaxResult api(HttpServletRequest request, @RequestBody String body) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { String interfaceName = request.getHeader("interface"); String sign = request.getHeader("sign"); @@ -59,7 +61,7 @@ public class ShellController { } catch (NumberFormatException e) { return AjaxResult.error(HttpStatus.TIMESTAMP_ERROR, "时间戳不正确"); } - String tmp = interfaceName + body + timestamp; + String tmp = interfaceName + body + "Wenyin@2024" + timestamp; String computeSign = DigestUtils.md5DigestAsHex(tmp.getBytes()); long tenTimestamp = timestamp + (10 * 60 * 1000); // 计算10分钟后的时间戳 long currentTimestamp = System.currentTimeMillis(); // 获取当前时间戳 @@ -76,64 +78,18 @@ public class ShellController { return (AjaxResult) method.invoke(this, body); } - public AjaxResult itemmastercreate(String body) throws JSchException, IOException { - Session session = shellUtil.getShellSession(shellConfig.getJlht2()); - String result = shellUtil.execute(session, "cd /opt && ls -l"); - return AjaxResult.success(result); - } - - public AjaxResult itemdetailcreate(String body) throws JSchException, IOException { - Session session = shellUtil.getShellSession(shellConfig.getJlht2()); - String result = shellUtil.execute(session, "cd /opt && ls -l"); - return AjaxResult.success(result); - } - public AjaxResult discretepo(String body) throws JSchException, IOException { JSONObject jsonObject = JSONObject.parseObject(body); jsonObject = JSONObject.parseObject(jsonObject.getString("dsDescreteOrder")); - ShellVo shellVo; - if(jsonObject.getString("domain") != null && jsonObject.getString("domain").equals("JLHT2")) { - shellVo = shellConfig.getJlht2(); - } else { - shellVo = shellConfig.getJlht(); + String domain = jsonObject.getString("domain"); + if(domain == null || domain.isEmpty()) { + return AjaxResult.error(HttpStatus.DOMAIN_ERROR, "域不能为空"); } - Session session = shellUtil.getShellSession(shellVo); - //生成traceid - String traceid = jsonObject.getString("traceid"); - jsonObject.remove("traceid"); - jsonObject.remove("company"); - jsonObject.remove("domain"); - String inJson = "{\"dsDescreteOrder\":"+ jsonObject+"}"; - log.info("inJson: {}", inJson); - //写入json - String result = shellUtil.execute(session, "touch /home/mfg/work/cim/po/"+traceid+".json && echo '"+inJson+"'>/home/mfg/work/cim/po/"+traceid+".json"); - log.info("write json result : " + result); - //写入dat.json - String datJson = "\"/home/mfg/work/cim/po/"+traceid+".json\" \"xxdspoapi.p\" \"/home/mfg/work/out/po/"+traceid+".out.log\" \""+shellVo.getQadUser()+"\" \""+shellVo.getQadPassword()+"\"\r\n\""+shellVo.getQadDomain()+"\""; - log.info("datJson: {}", datJson); - result = shellUtil.execute(session, "touch /home/mfg/work/cim/po/"+traceid+".dat && echo '"+datJson+"'>/home/mfg/work/cim/po/"+traceid+".dat"); - log.info("write dat json result : " + result); - //写入执行文件 - String pJson = "output to /home/mfg/work/out/po/" + traceid + ".run.log.\r\n" + - "input from /home/mfg/work/cim/po/" + traceid + ".dat.\r\n" + - "run /home/mfg/work/scripts/xxcommon01.p.\r\n" + - "input close.\r\n" + - "output close."; - log.info("pJson: {}", pJson); - result = shellUtil.execute(session, "touch /home/mfg/work/cim/po/"+traceid+".p && echo '"+pJson+"'>/home/mfg/work/cim/po/"+traceid+".p"); - log.info("write p result : " + result); - //执行QAD脚本 - result = shellUtil.execute(session, "/home/mfg/work/scripts/client-ch-cimload /home/mfg/work/cim/po/"+traceid+".p"); - log.info("execute client-ch-loaddata : " + result); - result = shellUtil.execute(session, "cat /home/mfg/work/out/po/" + traceid + ".run.out.json"); - log.info("cat execute log : " + result); - if(result.indexOf("\"SUCCESS\"") > 0) { - //移动执行结果文件 - shellUtil.execute(session, "mv /home/mfg/work/out/po/" + traceid + ".run.out.json /home/mfg/work/success/po/" + traceid + ".run.out.json"); - return AjaxResult.success(traceid); + String companyCode = jsonObject.getString("company_code"); + if(companyCode == null || companyCode.isEmpty()) { + return AjaxResult.error(HttpStatus.COMPANY_CODE_ERROR, "公司编码不能为空"); } - shellUtil.execute(session, "mv /home/mfg/work/out/po/" + traceid + ".run.out.json /home/mfg/work/err/po/" + traceid + ".run.out.json"); - return AjaxResult.error(HttpStatus.EXECUTE_FAIL, result); + return linuxAccessService.executeShell(domain, companyCode, "purchaseorder/discretepo/", jsonObject); } } diff --git a/win-common/src/main/java/com/win/common/constant/HttpStatus.java b/win-common/src/main/java/com/win/common/constant/HttpStatus.java index 27097fc..383f5b0 100644 --- a/win-common/src/main/java/com/win/common/constant/HttpStatus.java +++ b/win-common/src/main/java/com/win/common/constant/HttpStatus.java @@ -57,6 +57,26 @@ public class HttpStatus */ public static final int EXPIRE_ERROR = 307; + /** + * 域不能为空 + */ + public static final int DOMAIN_ERROR = 308; + + /** + * 公司编码不能为空 + */ + public static final int COMPANY_CODE_ERROR = 309; + + /** + * linux访问控制不存在 + */ + public static final int LINUX_ACCESS_ERROR = 310; + + /** + * QAD访问控制不存在 + */ + public static final int QAD_ACCESS_ERROR = 311; + /** * 参数列表错误(缺少,格式不匹配) */ diff --git a/win-common/src/main/java/com/win/common/utils/shell/ShellUtil.java b/win-common/src/main/java/com/win/common/utils/shell/ShellUtil.java index 662b99e..de7fb5d 100644 --- a/win-common/src/main/java/com/win/common/utils/shell/ShellUtil.java +++ b/win-common/src/main/java/com/win/common/utils/shell/ShellUtil.java @@ -21,15 +21,18 @@ public class ShellUtil { /** * 获取linux连接 - * @param shellVo - * @return - * @throws JSchException + * @param userName 用户名 + * @param password 密码 + * @param ip ip + * @param port 端口 + * @return 连接 + * @throws JSchException 链接异常 */ - public Session getShellSession(ShellVo shellVo) throws JSchException { - Session session = this.sessionConcurrentHashMap.get(shellVo.getQadDomain()); + public Session getShellSession(String userName, String password, String ip, int port) throws JSchException { + Session session = this.sessionConcurrentHashMap.get(ip + "_" + userName); if(session == null || !session.isConnected()) { - session = this.createSession(shellVo); - this.sessionConcurrentHashMap.put(shellVo.getQadDomain(), session); + session = this.createSession(userName, password, ip, port); + this.sessionConcurrentHashMap.put(ip + "_" + userName, session); return session; } else { return session; @@ -38,11 +41,11 @@ public class ShellUtil { /** * 执行linux命令 - * @param session - * @param command - * @return - * @throws JSchException - * @throws IOException + * @param session 连接 + * @param command 命令 + * @return 执行结果 + * @throws JSchException 连接异常 + * @throws IOException io异常 */ public String execute(Session session, String command) throws JSchException, IOException { // 创建SSH Channel @@ -72,16 +75,19 @@ public class ShellUtil { /** * 创建linux连接 - * @param shellVo - * @return - * @throws JSchException + * @param userName 用户名 + * @param password 密码 + * @param ip ip + * @param port 端口 + * @return 连接session + * @throws JSchException 连接异常 */ - protected Session createSession(ShellVo shellVo) throws JSchException { + protected Session createSession(String userName, String password, String ip, int port) throws JSchException { // 创建JSch实例 JSch jsch = new JSch(); // 创建SSH Session - Session session = jsch.getSession(shellVo.getLinuxId(), shellVo.getIp(), shellVo.getPort()); - session.setPassword(shellVo.getLinuxPassword()); + Session session = jsch.getSession(userName, ip, port); + session.setPassword(password); // 禁用用户信息验证 java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); diff --git a/win-framework/src/main/java/com/win/framework/aspectj/LogAspect.java b/win-framework/src/main/java/com/win/framework/aspectj/LogAspect.java index 607626b..28cf46d 100644 --- a/win-framework/src/main/java/com/win/framework/aspectj/LogAspect.java +++ b/win-framework/src/main/java/com/win/framework/aspectj/LogAspect.java @@ -66,9 +66,6 @@ public class LogAspect { protected void handleLog(final JoinPoint joinPoint, Log controllerLog, final Exception e, Object jsonResult) { try { - // 获取当前的用户 - LoginUser loginUser = SecurityUtils.getLoginUser(); - // *========数据库日志=========*// SysOperLog operLog = new SysOperLog(); operLog.setStatus(BusinessStatus.SUCCESS.ordinal()); @@ -76,10 +73,14 @@ public class LogAspect { String ip = IpUtils.getIpAddr(ServletUtils.getRequest()); operLog.setOperIp(ip); operLog.setOperUrl(StringUtils.substring(ServletUtils.getRequest().getRequestURI(), 0, 255)); - if (loginUser != null) { - operLog.setOperName(loginUser.getUsername()); - } + if(controllerLog.isManager()) { + // 获取当前的用户 + LoginUser loginUser = SecurityUtils.getLoginUser(); + if (loginUser != null) { + operLog.setOperName(loginUser.getUsername()); + } + } if (e != null) { operLog.setStatus(BusinessStatus.FAIL.ordinal()); operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000)); diff --git a/win-system/src/main/java/com/win/system/domain/LinuxAccess.java b/win-system/src/main/java/com/win/system/domain/LinuxAccess.java index 09423b6..52cb530 100644 --- a/win-system/src/main/java/com/win/system/domain/LinuxAccess.java +++ b/win-system/src/main/java/com/win/system/domain/LinuxAccess.java @@ -35,7 +35,7 @@ public class LinuxAccess extends BaseEntity { private String ipAddress; @Excel(name = "端口") - private String port; + private Integer port; @Excel(name = "用户") private String userName; diff --git a/win-system/src/main/java/com/win/system/service/LinuxAccessService.java b/win-system/src/main/java/com/win/system/service/LinuxAccessService.java index 63e2725..a0507e7 100644 --- a/win-system/src/main/java/com/win/system/service/LinuxAccessService.java +++ b/win-system/src/main/java/com/win/system/service/LinuxAccessService.java @@ -1,14 +1,18 @@ package com.win.system.service; import com.alibaba.fastjson2.JSONObject; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import com.win.common.constant.HttpStatus; import com.win.common.core.domain.AjaxResult; +import com.win.common.utils.command.CommandUtil; import com.win.common.utils.shell.ShellUtil; import com.win.common.utils.shell.ShellVo; +import com.win.system.domain.QadAccess; +import com.win.system.mapper.QadAccessMapper; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -31,6 +35,8 @@ public class LinuxAccessService extends ServiceImpl linuxAccessQueryWrapper = new QueryWrapper<>(); + linuxAccessQueryWrapper.eq("company_code", companyCode); + linuxAccessQueryWrapper.eq("uri", uri); + LinuxAccess linuxAccess = baseMapper.selectOne(linuxAccessQueryWrapper); + if(linuxAccess == null) { + return AjaxResult.error(HttpStatus.LINUX_ACCESS_ERROR, "linux访问控制不存在"); + } + QueryWrapper qadAccessQueryWrapper = new QueryWrapper<>(); + qadAccessQueryWrapper.eq("domain", domain); + QadAccess qadAccess = qadAccessMapper.selectOne(qadAccessQueryWrapper); + if(qadAccess == null) { + return AjaxResult.error(HttpStatus.QAD_ACCESS_ERROR, "QAD_ACCESS_ERROR"); + } + Session session = shellUtil.getShellSession(linuxAccess.getUserName(), linuxAccess.getPassword(), linuxAccess.getIpAddress(), linuxAccess.getPort()); + //生成traceid + String traceid = jsonObject.getString("traceid"); + jsonObject.remove("traceid"); + jsonObject.remove("company_code"); + jsonObject.remove("domain"); + String inJson = "{\"dsDescreteOrder\":"+ jsonObject+"}"; + log.info("inJson: {}", inJson); + //写入json + String result = shellUtil.execute(session, "touch " + linuxAccess.getWorkPath() + traceid + ".json && echo '" + inJson + "'>" + linuxAccess.getWorkPath() + traceid + ".json"); + log.info("write json result : {}", result); + //写入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 = shellUtil.execute(session, "touch " + linuxAccess.getWorkPath() + traceid + ".dat && echo '" + datJson + "'>" + linuxAccess.getWorkPath() + traceid+".dat"); + log.info("write dat json result : {}", result); + //写入执行文件 + String pJson = "output to " + linuxAccess.getOutPath() + traceid + ".run.log.\r\n" + + "input from " + linuxAccess.getWorkPath() + traceid + ".dat.\r\n" + + "run " + linuxAccess.getScriptPath() + linuxAccess.getScriptProgram() + ".\r\n" + + "input close.\r\n" + + "output close."; + log.info("pJson: {}", pJson); + result = shellUtil.execute(session, "touch " + linuxAccess.getWorkPath() + traceid+".p && echo '"+pJson+"'>" + linuxAccess.getWorkPath() + traceid + ".p"); + log.info("write p result : {}", result); + //执行QAD脚本 + log.info("command : {}", linuxAccess.getScriptPath() + linuxAccess.getScriptName() + " " + linuxAccess.getWorkPath() + traceid + ".p"); + result = shellUtil.execute(session, linuxAccess.getScriptPath() + linuxAccess.getScriptName() + " " + linuxAccess.getWorkPath() + traceid + ".p"); + log.info("execute {} result : {}", linuxAccess.getScriptPath() + linuxAccess.getScriptName(), result); + result = shellUtil.execute(session, "cat " + linuxAccess.getOutPath() + traceid + ".run.out.json"); + log.info("cat execute log : {}", result); + if(result.indexOf("\"SUCCESS\"") > 0) { + //移动执行结果文件 + shellUtil.execute(session, "mv " + linuxAccess.getOutPath() + traceid + ".run.out.json " + linuxAccess.getSuccessPath() + traceid + ".run.out.json"); + return AjaxResult.success(traceid); + } + shellUtil.execute(session, "mv " + linuxAccess.getOutPath() + traceid + ".run.out.json " + linuxAccess.getErrorPath() + traceid + ".run.out.json"); + return AjaxResult.error(HttpStatus.EXECUTE_FAIL, result); + } + + /** + * 执行命令行 + * + * @param domain 域 + * @param companyCode 公司编码 + * @param jsonObject 传输对象 + * @return 执行结果 + * @throws InterruptedException 命令行异常 + * @throws IOException io异常 + */ + public AjaxResult executeCommand(String domain, String companyCode, String uri, JSONObject jsonObject) throws IOException, InterruptedException { + QueryWrapper linuxAccessQueryWrapper = new QueryWrapper<>(); + linuxAccessQueryWrapper.eq("company_code", companyCode); + linuxAccessQueryWrapper.eq("uri", uri); + LinuxAccess linuxAccess = baseMapper.selectOne(linuxAccessQueryWrapper); + if(linuxAccess == null) { + return AjaxResult.error(HttpStatus.LINUX_ACCESS_ERROR, "linux访问控制不存在"); + } + QueryWrapper qadAccessQueryWrapper = new QueryWrapper<>(); + qadAccessQueryWrapper.eq("domain", domain); + QadAccess qadAccess = qadAccessMapper.selectOne(qadAccessQueryWrapper); + if(qadAccess == null) { + return AjaxResult.error(HttpStatus.QAD_ACCESS_ERROR, "QAD_ACCESS_ERROR"); + } + //生成traceid + String traceid = jsonObject.getString("traceid"); + jsonObject.remove("traceid"); + jsonObject.remove("company_code"); + jsonObject.remove("domain"); + String inJson = "{\"dsDescreteOrder\":"+ 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); + //写入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); + //写入执行文件 + String pJson = "output to " + linuxAccess.getOutPath() + traceid + ".run.log.\r\n" + + "input from " + linuxAccess.getWorkPath() + traceid + ".dat.\r\n" + + "run " + linuxAccess.getScriptPath() + linuxAccess.getScriptProgram() + ".\r\n" + + "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); + //执行QAD脚本 + log.info("command : {}", linuxAccess.getScriptPath() + linuxAccess.getScriptName() + " " + linuxAccess.getWorkPath() + traceid + ".p"); + result = CommandUtil.execute(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"); + 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.execute("mv " + linuxAccess.getOutPath() + traceid + ".run.out.json " + linuxAccess.getErrorPath() + traceid + ".run.out.json"); + return AjaxResult.error(HttpStatus.EXECUTE_FAIL, result); + } + } \ No newline at end of file