From fac8e2f51eb6664b2c975272336628a56a136c4d Mon Sep 17 00:00:00 2001 From: Elune <201507802@qq.com> Date: Sat, 21 Dec 2019 21:30:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=BF=90=E7=BB=B4=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E7=9A=84=E4=B8=80=E4=BA=9Bbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LICENSE | 2 +- .../modules/mnt/rest/DatabaseController.java | 2 +- .../dto/DeployHistoryQueryCriteria.java | 5 +- .../mnt/service/impl/AppServiceImpl.java | 17 +++++ .../mnt/service/impl/DeployServiceImpl.java | 63 ++++++++++++------- .../zhengjie/modules/mnt/util/SqlUtils.java | 2 +- 6 files changed, 66 insertions(+), 25 deletions(-) diff --git a/LICENSE b/LICENSE index 91e3c252..780fecac 100644 --- a/LICENSE +++ b/LICENSE @@ -176,7 +176,7 @@ recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright 2018 Elune + Copyright 2018 Zheng Jie Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/rest/DatabaseController.java b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/rest/DatabaseController.java index 2599b16b..72cf248b 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/rest/DatabaseController.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/rest/DatabaseController.java @@ -97,7 +97,7 @@ public class DatabaseController { public ResponseEntity upload(@RequestBody MultipartFile file, HttpServletRequest request)throws Exception{ String id = request.getParameter("id"); DatabaseDto database = databaseService.findById(id); - String fileName = ""; + String fileName; if(database != null){ fileName = file.getOriginalFilename(); File executeFile = new File(fileSavePath+fileName); diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/dto/DeployHistoryQueryCriteria.java b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/dto/DeployHistoryQueryCriteria.java index 1df6234b..68562c23 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/dto/DeployHistoryQueryCriteria.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/dto/DeployHistoryQueryCriteria.java @@ -15,9 +15,12 @@ public class DeployHistoryQueryCriteria{ /** * 精确 */ - @Query(blurry = "appName,ip,deployUser,deployId") + @Query(blurry = "appName,ip,deployUser") private String blurry; + @Query + private Long deployId; + @Query(type = Query.Type.BETWEEN) private List deployDate; } diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/impl/AppServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/impl/AppServiceImpl.java index b73ad34b..c62f53c2 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/impl/AppServiceImpl.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/impl/AppServiceImpl.java @@ -1,5 +1,6 @@ package me.zhengjie.modules.mnt.service.impl; +import me.zhengjie.exception.BadRequestException; import me.zhengjie.modules.mnt.domain.App; import me.zhengjie.modules.mnt.repository.AppRepository; import me.zhengjie.modules.mnt.service.AppService; @@ -57,18 +58,34 @@ public class AppServiceImpl implements AppService { @Override @Transactional(rollbackFor = Exception.class) public AppDto create(App resources) { + verification(resources); return appMapper.toDto(appRepository.save(resources)); } @Override @Transactional(rollbackFor = Exception.class) public void update(App resources) { + verification(resources); App app = appRepository.findById(resources.getId()).orElseGet(App::new); ValidationUtil.isNull(app.getId(),"App","id",resources.getId()); app.copy(resources); appRepository.save(app); } + private void verification(App resources){ + String opt = "/opt"; + String home = "/home"; + if (!(resources.getUploadPath().startsWith(opt) || resources.getUploadPath().startsWith(home))) { + throw new BadRequestException("文件只能上传在opt目录或者home目录 "); + } + if (!(resources.getDeployPath().startsWith(opt) || resources.getDeployPath().startsWith(home))) { + throw new BadRequestException("文件只能部署在opt目录或者home目录 "); + } + if (!(resources.getBackupPath().startsWith(opt) || resources.getBackupPath().startsWith(home))) { + throw new BadRequestException("文件只能备份在opt目录或者home目录 "); + } + } + @Override @Transactional(rollbackFor = Exception.class) public void delete(Set ids) { diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/impl/DeployServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/impl/DeployServiceImpl.java index 664214f7..636cae33 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/impl/DeployServiceImpl.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/service/impl/DeployServiceImpl.java @@ -130,13 +130,15 @@ public class DeployServiceImpl implements DeployService { //判断是否第一次部署 boolean flag = checkFile(executeShellUtil, app); //第一步要确认服务器上有这个目录 - executeShellUtil.execute("mkdir -p " + uploadPath); + executeShellUtil.execute("mkdir -p " + app.getUploadPath()); + executeShellUtil.execute("mkdir -p " + app.getBackupPath()); + executeShellUtil.execute("mkdir -p " + app.getDeployPath()); //上传文件 msg = String.format("登陆到服务器:%s", ip); ScpClientUtil scpClientUtil = getScpClientUtil(ip); log.info(msg); sendMsg(msg, MsgType.INFO); - msg = String.format("上传文件到服务器:%s
目录:%s下", ip, uploadPath); + msg = String.format("上传文件到服务器:%s
目录:%s下,请稍等...", ip, uploadPath); sendMsg(msg, MsgType.INFO); scpClientUtil.putFile(fileSavePath, uploadPath); if (flag) { @@ -151,13 +153,19 @@ public class DeployServiceImpl implements DeployService { //部署文件,并启动应用 String deployScript = app.getDeployScript(); executeShellUtil.execute(deployScript); - - sendMsg("启动应用", MsgType.INFO); - String startScript = app.getStartScript(); - executeShellUtil.execute(startScript); - //只有过5秒才能知道到底是不是启动成功了。 - sleep(5); - boolean result = checkIsRunningStatus(port, executeShellUtil); + sleep(3); + sendMsg("应用部署中,请耐心等待部署结果,或者稍后手动查看部署状态", MsgType.INFO); + int i = 0; + boolean result = false; + // 由于启动应用需要时间,所以需要循环获取状态,如果超过30次,则认为是启动失败 + while (i++ < 30){ + result = checkIsRunningStatus(port, executeShellUtil); + if(result){ + break; + } + // 休眠6秒 + sleep(6); + } sb.append("服务器:").append(deployDTO.getName()).append("
应用:").append(app.getName()); sendResultMsg(result, sb); executeShellUtil.close(); @@ -253,7 +261,7 @@ public class DeployServiceImpl implements DeployService { private boolean checkFile(ExecuteShellUtil executeShellUtil, AppDto appDTO) { String result = executeShellUtil.executeForResult("find " + appDTO.getDeployPath() + " -name " + appDTO.getName()); - return result.indexOf("/tcp:")>0; + return result.indexOf(appDTO.getName())>0; } /** @@ -273,9 +281,19 @@ public class DeployServiceImpl implements DeployService { sb.append("服务器:").append(deploy.getName()).append("
应用:").append(app.getName()); sendMsg("下发启动命令", MsgType.INFO); executeShellUtil.execute(app.getStartScript()); - //停止3秒,防止应用没有启动完成 sleep(3); - boolean result = checkIsRunningStatus(app.getPort(), executeShellUtil); + sendMsg("应用启动中,请耐心等待启动结果,或者稍后手动查看运行状态", MsgType.INFO); + int i = 0; + boolean result = false; + // 由于启动应用需要时间,所以需要循环获取状态,如果超过30次,则认为是启动失败 + while (i++ < 30){ + result = checkIsRunningStatus(app.getPort(), executeShellUtil); + if(result){ + break; + } + // 休眠6秒 + sleep(6); + } sendResultMsg(result, sb); log.info(sb.toString()); executeShellUtil.close(); @@ -343,21 +361,24 @@ public class DeployServiceImpl implements DeployService { stopApp(app.getPort(), executeShellUtil); //删除原来应用 sendMsg("删除应用", MsgType.INFO); - //考虑到系统安全性,必须限制下操作目录 - String path = "/opt"; - if (!deployPath.startsWith(path)) { - throw new BadRequestException("部署路径必须在opt目录下:" + deployPath); - } executeShellUtil.execute("rm -rf " + deployPath + FILE_SEPARATOR + resources.getAppName()); - //还原应用 sendMsg("还原应用", MsgType.INFO); executeShellUtil.execute("cp -r " + backupPath + "/. " + deployPath); sendMsg("启动应用", MsgType.INFO); executeShellUtil.execute(app.getStartScript()); - //只有过5秒才能知道到底是不是启动成功了。 - sleep(5); - boolean result = checkIsRunningStatus(app.getPort(), executeShellUtil); + sendMsg("应用启动中,请耐心等待启动结果,或者稍后手动查看启动状态", MsgType.INFO); + int i = 0; + boolean result = false; + // 由于启动应用需要时间,所以需要循环获取状态,如果超过30次,则认为是启动失败 + while (i++ < 30){ + result = checkIsRunningStatus(app.getPort(), executeShellUtil); + if(result){ + break; + } + // 休眠6秒 + sleep(6); + } StringBuilder sb = new StringBuilder(); sb.append("服务器:").append(ip).append("
应用:").append(resources.getAppName()); sendResultMsg(result, sb); diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/SqlUtils.java b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/SqlUtils.java index 74693cef..d30aa865 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/SqlUtils.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/mnt/util/SqlUtils.java @@ -155,7 +155,7 @@ public class SqlUtils { public static String executeFile(String jdbcUrl, String userName, String password, File sqlFile) { Connection connection = getConnection(jdbcUrl, userName, password); try { - batchExecute(connection, readSqlList( sqlFile)); + batchExecute(connection, readSqlList(sqlFile)); } catch (Exception e) { log.error("sql脚本执行发生异常:{}",e.getMessage()); return e.getMessage();