Skip to content

Commit

Permalink
修复运维管理的一些bug
Browse files Browse the repository at this point in the history
  • Loading branch information
elunez committed Dec 21, 2019
1 parent d32216d commit fac8e2f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 25 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public ResponseEntity<Object> testConnect(@Validated @RequestBody Database resou
public ResponseEntity<Object> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Timestamp> deployDate;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -57,18 +58,34 @@ public AppDto findById(Long id) {
@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<Long> ids) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,15 @@ private void deployApp(String fileSavePath, Long id) {
//判断是否第一次部署
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<br>目录:%s下", ip, uploadPath);
msg = String.format("上传文件到服务器:%s<br>目录:%s下,请稍等...", ip, uploadPath);
sendMsg(msg, MsgType.INFO);
scpClientUtil.putFile(fileSavePath, uploadPath);
if (flag) {
Expand All @@ -151,13 +153,19 @@ private void deployApp(String fileSavePath, Long id) {
//部署文件,并启动应用
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("<br>应用:").append(app.getName());
sendResultMsg(result, sb);
executeShellUtil.close();
Expand Down Expand Up @@ -253,7 +261,7 @@ public String serverStatus(Deploy resources) {

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;
}

/**
Expand All @@ -273,9 +281,19 @@ public String startServer(Deploy resources) {
sb.append("服务器:").append(deploy.getName()).append("<br>应用:").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();
Expand Down Expand Up @@ -343,21 +361,24 @@ public String serverReduction(DeployHistory resources) {
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("<br>应用:").append(resources.getAppName());
sendResultMsg(result, sb);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public static boolean testConnection(String jdbcUrl, String userName, String pas
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();
Expand Down

0 comments on commit fac8e2f

Please sign in to comment.