|
|
@ -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<LinuxAccessMapper, LinuxAcce |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private ShellUtil shellUtil; |
|
|
|
@Autowired |
|
|
|
private QadAccessMapper qadAccessMapper; |
|
|
|
|
|
|
|
/** |
|
|
|
* 批量删除【请填写功能名称】 |
|
|
@ -39,7 +45,7 @@ public class LinuxAccessService extends ServiceImpl<LinuxAccessMapper, LinuxAcce |
|
|
|
* @return 删除的数量 |
|
|
|
*/ |
|
|
|
@Transactional |
|
|
|
public int deleteLinuxAccessByGuids(Long[] ids, Long userId) { |
|
|
|
public int deleteLinuxAccessByIds(Long[] ids, Long userId) { |
|
|
|
int result = 0; |
|
|
|
for(Long id : ids) { |
|
|
|
LinuxAccess linuxaccess = baseMapper.selectById(id); |
|
|
@ -50,4 +56,131 @@ public class LinuxAccessService extends ServiceImpl<LinuxAccessMapper, LinuxAcce |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 执行shell脚本 |
|
|
|
* |
|
|
|
* @param domain 域 |
|
|
|
* @param companyCode 公司编码 |
|
|
|
* @param jsonObject 传输对象 |
|
|
|
* @return 执行结果 |
|
|
|
* @throws JSchException shell异常 |
|
|
|
* @throws IOException io异常 |
|
|
|
*/ |
|
|
|
public AjaxResult executeShell(String domain, String companyCode, String uri, JSONObject jsonObject) throws JSchException, IOException { |
|
|
|
QueryWrapper<LinuxAccess> 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<QadAccess> 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<LinuxAccess> 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<QadAccess> 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); |
|
|
|
} |
|
|
|
|
|
|
|
} |