| | |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.baomidou.mybatisplus.extension.toolkit.SqlHelper; |
| | | import com.vci.ubcs.deploy.entity.CmdConfig; |
| | | import com.vci.ubcs.deploy.entity.DeployApps; |
| | | import com.vci.ubcs.deploy.enumpack.CmdConfigEnum; |
| | | import com.vci.ubcs.deploy.mapper.DeployAppsMapper; |
| | |
| | | import com.vci.ubcs.starter.util.HttpUtils; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springblade.core.secure.utils.AuthUtil; |
| | | import org.springblade.core.tool.api.R; |
| | | import org.springblade.core.tool.utils.Func; |
| | | import org.springblade.core.tool.utils.WebUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.cloud.client.ServiceInstance; |
| | |
| | | import org.springframework.context.EnvironmentAware; |
| | | import org.springframework.core.env.Environment; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.LinkedMultiValueMap; |
| | | import org.springframework.util.MultiValueMap; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.ServletRequest; |
| | |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | import java.util.stream.Stream; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipFile; |
| | | import java.util.zip.ZipInputStream; |
| | | |
| | | /** |
| | | * @author ludc |
| | |
| | | } |
| | | // 未从库中查询到,需要生成服务信息保存默认信息到库中 |
| | | DeployApps defaultDeployApps = new DeployApps(); |
| | | defaultDeployApps.setLogPath(PARENTPATH + deployAppsVO.getName().replace("-","_") + LOGPATH); |
| | | defaultDeployApps.setLogPath(PARENTPATH + SEPARATOR + deployAppsVO.getName().replace("-","_") + LOGPATH); |
| | | defaultDeployApps.setServerName(deployAppsVO.getName()); |
| | | defaultDeployApps.setStartCmd(CmdConfigEnum.START_CMD.getValue() + deployAppsVO.getName()); |
| | | defaultDeployApps.setStopCmd(CmdConfigEnum.STOP_CMD.getValue() + deployAppsVO.getName()); |
| | | defaultDeployApps.setRestartCmd(CmdConfigEnum.RESTART_CMD.getValue() + deployAppsVO.getName()); |
| | | defaultDeployApps.setServerPath(PARENTPATH + deployAppsVO.getName().replace("-","_")); |
| | | defaultDeployApps.setServerPath(PARENTPATH + SEPARATOR + deployAppsVO.getName().replace("-","_")); |
| | | int eft = deployAppsMapper.insert(defaultDeployApps); |
| | | if (!SqlHelper.retBool(eft)) { |
| | | throw new ServiceException("生成默认服务信息到库中时失败!"); |
| | |
| | | * @throws ServiceException |
| | | */ |
| | | @Override |
| | | public boolean saveOrUpdateServiceInfo(DeployAppsVO deployAppsVO) throws ServiceException { |
| | | return this.saveOrUpdate(deployAppsVO); |
| | | public R saveOrUpdateServiceInfo(DeployAppsVO deployAppsVO) throws ServiceException { |
| | | boolean checkBoolean = checkCmdVer(deployAppsVO.getStartCmd()) && checkCmdVer(deployAppsVO.getStopCmd()) && checkCmdVer(deployAppsVO.getRestartCmd()); |
| | | if(checkBoolean){ |
| | | return R.fail("配置的命令中包含危险命令,如:rm、mv、rm -rf、chmod等命令和关键字! "); |
| | | } |
| | | return R.status(this.saveOrUpdate(deployAppsVO)); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return |
| | | */ |
| | | @Override |
| | | public R importClassify(MultipartFile[] files, String serverName) throws ServiceException { |
| | | public R importJarUpdate(MultipartFile[] files, String serverName) throws ServiceException { |
| | | // 根据服务名查看到服务相关信息 |
| | | List<DeployApps> deployAppsDB = deployAppsMapper.selectList(Wrappers.<DeployApps>query().lambda().eq(DeployApps::getServerName, serverName)); |
| | | if(deployAppsDB.isEmpty()){ |
| | |
| | | file1.delete(); |
| | | } |
| | | } |
| | | String output = excute(deployAppsDB.get(0),"UP"); |
| | | String output = execute(deployAppsDB.get(0),"UP"); |
| | | return R.success(output.toString()); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | |
| | | if(deployAppsDB.isEmpty()){ |
| | | return R.fail("命令执行出错,库中未找到"+ deployAppsVO.getName() +"服务相关配置:" ); |
| | | } |
| | | excuteRes = excute(deployAppsDB.get(0),deployAppsVO.getStatus()); |
| | | excuteRes = execute(deployAppsDB.get(0),deployAppsVO.getStatus()); |
| | | return R.success("命令执行结束:"+excuteRes); |
| | | }catch (Exception e){ |
| | | throw new ServiceException(e.getMessage()); |
| | |
| | | * @return |
| | | * @throws ServiceException |
| | | */ |
| | | private String excute(DeployApps deployApps,String type) throws ServiceException { |
| | | public String execute(DeployApps deployApps,String type) throws ServiceException { |
| | | // 处理上传文件的逻辑 |
| | | StringBuilder output = new StringBuilder(); |
| | | try { |
| | |
| | | if(Func.isEmpty(cmd)){ |
| | | return "The executed command is empty"; |
| | | } |
| | | if(checkCmdVer(cmd)){ |
| | | return "配置的命令中包含危险命令,如:rm、mv、rm -rf、chmod等命令和关键词! "; |
| | | } |
| | | // 执行Linux命令 |
| | | log.info("开始执行命令:"+cmd); |
| | | Process process = Runtime.getRuntime().exec(cmd); |
| | | // Process process = Runtime.getRuntime().exec(cmd); |
| | | ProcessBuilder processBuilder = new ProcessBuilder(cmd.split("\\s")); |
| | | // processBuilder.command(cmd); |
| | | Process process = processBuilder.start(); |
| | | // 等待命令执行完成 |
| | | int exitCode = process.waitFor(); |
| | | |
| | | InputStream inputStream = process.getInputStream(); |
| | | OutputStream outputStream = process.getOutputStream(); |
| | | InputStream errorStream = process.getErrorStream(); |
| | | // 读取命令执行结果 |
| | | BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); |
| | | BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); |
| | | String line; |
| | | while ((line = reader.readLine()) != null) { |
| | | output.append(line).append("\n"); |
| | | } |
| | | // 等待命令执行完成 |
| | | int exitCode = process.waitFor(); |
| | | log.info("命令执行结果:" + output.toString()+":"+exitCode); |
| | | reader.close(); |
| | | |
| | | // 读取命令执行的错误输出流 |
| | | BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorStream)); |
| | | StringBuilder errorOutput = new StringBuilder(); |
| | | String errorLine; |
| | | while ((errorLine = errorReader.readLine()) != null) { |
| | | errorOutput.append(errorLine).append("\n"); |
| | | } |
| | | String errorOutputString = errorOutput.toString(); |
| | | errorReader.close(); |
| | | |
| | | log.info("错误输出:" + errorOutputString); |
| | | int exitValue = process.exitValue(); |
| | | log.info("命令执行结果:" + output.toString()+":"+exitCode+","+exitValue); |
| | | return output.toString(); |
| | | }catch (IOException | InterruptedException e){ |
| | | e.printStackTrace(); |
| | |
| | | return "8080"; |
| | | } |
| | | |
| | | /** |
| | | * 启动、停止、重启命令校验 |
| | | * @param cmd |
| | | * @return true: 包含高风险命令, false:不包含 |
| | | */ |
| | | private boolean checkCmdVer(String cmd){ |
| | | if(Func.isEmpty(cmd)){ |
| | | return false; |
| | | } |
| | | String[] highRiskCommands = {"rm", "rmdir", "mv", "unlink", "rm -rf", "mv -rf", "dd", "chmod", "chown", "mkfs", "shutdown", "reboot", "kill"}; |
| | | for(String highRiskCmd : highRiskCommands){ |
| | | if(cmd.contains(highRiskCmd)){ |
| | | return true; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | } |