|
|
@ -1,5 +1,6 @@ |
|
|
|
package com.win.qad.controller; |
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.jcraft.jsch.JSchException; |
|
|
|
import com.jcraft.jsch.Session; |
|
|
|
import com.win.qad.common.CommonResult; |
|
|
@ -7,8 +8,11 @@ import com.win.qad.config.ShellConfig; |
|
|
|
import com.win.qad.exception.GlobalErrorCodeConstants; |
|
|
|
import com.win.qad.utils.ProfileUtil; |
|
|
|
import com.win.qad.utils.shell.ShellUtil; |
|
|
|
import com.win.qad.utils.shell.ShellVo; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.util.DigestUtils; |
|
|
|
import org.springframework.web.bind.annotation.PostMapping; |
|
|
@ -18,14 +22,19 @@ import org.springframework.web.bind.annotation.RestController; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import java.io.IOException; |
|
|
|
import java.lang.reflect.InvocationTargetException; |
|
|
|
import java.lang.reflect.Method; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
import static com.win.qad.exception.GlobalErrorCodeConstants.EXECUTE_FAIL; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
@RestController |
|
|
|
@RequestMapping("/shell") |
|
|
|
public class ShellController { |
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger("sys-user"); |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private ShellUtil shellUtil; |
|
|
|
@Autowired |
|
|
@ -40,7 +49,7 @@ public class ShellController { |
|
|
|
*/ |
|
|
|
@PostMapping("/api") |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
public CommonResult<String> api(HttpServletRequest request, @RequestBody String body) { |
|
|
|
public CommonResult<String> api(HttpServletRequest request, @RequestBody String body) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { |
|
|
|
String interfaceName = request.getHeader("interface"); |
|
|
|
String sign = request.getHeader("sign"); |
|
|
|
String timeStr = request.getHeader("timestamp"); |
|
|
@ -55,11 +64,6 @@ public class ShellController { |
|
|
|
} |
|
|
|
String tmp = interfaceName + body + timestamp; |
|
|
|
String computeSign = DigestUtils.md5DigestAsHex(tmp.getBytes()); |
|
|
|
log.info("{}, interfaceName: {}", "interfaceName", interfaceName); |
|
|
|
log.info("{}, sign: {}", "sign", sign); |
|
|
|
log.info("{}, timestamp: {}", "timestamp", timestamp); |
|
|
|
log.info("{}, tmp: {}", "tmp", tmp); |
|
|
|
log.info("{}, computeSign: {}", "computeSign", computeSign); |
|
|
|
long tenTimestamp = timestamp + (10 * 60 * 1000); // 计算10分钟后的时间戳
|
|
|
|
long currentTimestamp = System.currentTimeMillis(); // 获取当前时间戳
|
|
|
|
//过期
|
|
|
@ -71,26 +75,68 @@ public class ShellController { |
|
|
|
if(!activeProfile.contains("dev") && !StringUtils.equals(sign, computeSign.toUpperCase())) { |
|
|
|
return CommonResult.error(GlobalErrorCodeConstants.SIGN_ERROR); |
|
|
|
} |
|
|
|
try { |
|
|
|
Method method = this.getClass().getMethod(interfaceName, String.class); |
|
|
|
return (CommonResult<String>) method.invoke(this, body); |
|
|
|
} catch (NoSuchMethodException e) { |
|
|
|
return CommonResult.error(GlobalErrorCodeConstants.INTERFACE_ERROR); |
|
|
|
} catch (Exception e) { |
|
|
|
return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR); |
|
|
|
} |
|
|
|
Method method = this.getClass().getMethod(interfaceName, String.class); |
|
|
|
return (CommonResult<String>) method.invoke(this, body); |
|
|
|
} |
|
|
|
|
|
|
|
public CommonResult<String> jlht(String body) throws JSchException, IOException { |
|
|
|
Session session = shellUtil.getShellSession(shellConfig.getJlht()); |
|
|
|
public CommonResult<String> itemmastercreate(String body) throws JSchException, IOException { |
|
|
|
Session session = shellUtil.getShellSession(shellConfig.getJlht2()); |
|
|
|
String result = shellUtil.execute(session, "cd /opt && ls -l"); |
|
|
|
return CommonResult.success(result); |
|
|
|
} |
|
|
|
|
|
|
|
public CommonResult<String> jlht2(String body) throws JSchException, IOException { |
|
|
|
public CommonResult<String> itemdetailcreate(String body) throws JSchException, IOException { |
|
|
|
Session session = shellUtil.getShellSession(shellConfig.getJlht2()); |
|
|
|
String result = shellUtil.execute(session, "cd /opt && ls -l"); |
|
|
|
return CommonResult.success(result); |
|
|
|
} |
|
|
|
|
|
|
|
public CommonResult<String> 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(); |
|
|
|
} |
|
|
|
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 CommonResult.success(traceid); |
|
|
|
} |
|
|
|
shellUtil.execute(session, "mv /home/mfg/work/out/po/" + traceid + ".run.out.json /home/mfg/work/err/po/" + traceid + ".run.out.json"); |
|
|
|
return CommonResult.error(EXECUTE_FAIL.getCode(), result); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|