|
|
@@ -1,5 +1,6 @@
|
|
|
package cn.reghao.autodop.dmaster.app.service;
|
|
|
|
|
|
+import cn.reghao.autodop.common.deploy.PackerType;
|
|
|
import cn.reghao.autodop.common.shell.ShellExecutor;
|
|
|
import cn.reghao.autodop.common.shell.ShellResult;
|
|
|
import cn.reghao.autodop.common.utils.JsonUtil;
|
|
|
@@ -21,6 +22,7 @@ import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.io.File;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
@@ -32,81 +34,135 @@ import java.util.stream.Collectors;
|
|
|
*/
|
|
|
@Service
|
|
|
public class ConfigService {
|
|
|
- private LocalDirRepository configRepository;
|
|
|
- private ProjOrchestrationRepository projRepository;
|
|
|
+ private LocalDirRepository localDirRepository;
|
|
|
+ private NotificationRepository notifyRepository;
|
|
|
private AppUpdateRepository updateRepository;
|
|
|
private AppCompileRepository compileRepository;
|
|
|
private AppPackRepository packRepository;
|
|
|
- private NotificationRepository notifyRepository;
|
|
|
+ private ProjOrchestrationRepository projRepository;
|
|
|
+ private ShellExecutor shellExecutor;
|
|
|
|
|
|
- public ConfigService(LocalDirRepository configRepository,
|
|
|
- ProjOrchestrationRepository projRepository,
|
|
|
+ public ConfigService(LocalDirRepository localDirRepository,
|
|
|
+ NotificationRepository notifyRepository,
|
|
|
AppUpdateRepository updateRepository,
|
|
|
AppCompileRepository compileRepository,
|
|
|
AppPackRepository packRepository,
|
|
|
- NotificationRepository notifyRepository) {
|
|
|
- this.configRepository = configRepository;
|
|
|
- this.projRepository = projRepository;
|
|
|
+ ProjOrchestrationRepository projRepository) {
|
|
|
+ this.localDirRepository = localDirRepository;
|
|
|
+ this.notifyRepository = notifyRepository;
|
|
|
this.updateRepository = updateRepository;
|
|
|
this.compileRepository = compileRepository;
|
|
|
this.packRepository = packRepository;
|
|
|
- this.notifyRepository = notifyRepository;
|
|
|
+ this.projRepository = projRepository;
|
|
|
+ this.shellExecutor = new ShellExecutor();
|
|
|
}
|
|
|
|
|
|
- public void add(String configType, String json) throws Exception {
|
|
|
+ public void addOrModify(String configType, String json) throws Exception {
|
|
|
switch (ConfigType.valueOf(configType)) {
|
|
|
// 本地文件系统目录
|
|
|
case localDir:
|
|
|
LocalDir localDir = (LocalDir) JsonUtil.jsonToObject(json, LocalDir.class);
|
|
|
- configRepository.save(localDir);
|
|
|
+ // 检查并创建目录
|
|
|
+ File localRepo = new File(localDir.getLocalRepo());
|
|
|
+ if (!localRepo.exists() && !localRepo.mkdirs()) {
|
|
|
+ throw new Exception(localDir.getLocalRepo() + "不存在且创建失败...");
|
|
|
+ }
|
|
|
+
|
|
|
+ File compileDir = new File(localDir.getCompileDir());
|
|
|
+ if (!compileDir.exists() && !compileDir.mkdirs()) {
|
|
|
+ throw new Exception(localDir.getCompileDir() + "不存在且创建失败...");
|
|
|
+ }
|
|
|
|
|
|
- /*AppEnv configEntity = configRepository.findByEnv(appEnv.getEnv());
|
|
|
- if (configEntity == null) {
|
|
|
- configRepository.save(appEnv);
|
|
|
- }*/
|
|
|
+ File packDir = new File(localDir.getPackDir());
|
|
|
+ if (!packDir.exists() && !packDir.mkdirs()) {
|
|
|
+ throw new Exception(localDir.getPackDir() + "不存在且创建失败...");
|
|
|
+ }
|
|
|
+
|
|
|
+ LocalDir localDirEntity = localDirRepository.findByHostId(localDir.getHostId());
|
|
|
+ if (localDirEntity != null) {
|
|
|
+ localDir.setId(localDirEntity.getId());
|
|
|
+ localDir.setCreateTime(localDirEntity.getCreateTime());
|
|
|
+ localDir.setUpdateTime(LocalDateTime.now());
|
|
|
+ localDir.setIsDelete(localDirEntity.getIsDelete());
|
|
|
+
|
|
|
+ localDirRepository.save(localDir);
|
|
|
+ }
|
|
|
break;
|
|
|
// 通知配置
|
|
|
case notifier:
|
|
|
Notification notification = (Notification) JsonUtil.jsonToObject(json, Notification.class);
|
|
|
- // TODO 考虑 isDelete 为 true 的情况
|
|
|
- Notification notifyEntity = notifyRepository.findByNotifierName(notification.getNotifierName());
|
|
|
- if (notifyEntity == null) {
|
|
|
- notifyRepository.save(notification);
|
|
|
+ Notification notifyEntity =
|
|
|
+ notifyRepository.findByIsDeleteFalseAndNotifierName(notification.getNotifierName());
|
|
|
+ if (notifyEntity != null) {
|
|
|
+ notification.setId(notifyEntity.getId());
|
|
|
+ notification.setCreateTime(notifyEntity.getCreateTime());
|
|
|
+ notification.setUpdateTime(LocalDateTime.now());
|
|
|
}
|
|
|
+ notification.setIsDelete(false);
|
|
|
+ notifyRepository.save(notification);
|
|
|
break;
|
|
|
// 仓库配置
|
|
|
case repo:
|
|
|
AppUpdate appUpdate = (AppUpdate) JsonUtil.jsonToObject(json, AppUpdate.class);
|
|
|
- AppUpdate updateEntity = updateRepository.findByRepoName(appUpdate.getRepoName());
|
|
|
- if (updateEntity == null) {
|
|
|
- updateRepository.save(appUpdate);
|
|
|
+ AppUpdate updateEntity = updateRepository.findByIsDeleteFalseAndRepoName(appUpdate.getRepoName());
|
|
|
+ if (updateEntity != null) {
|
|
|
+ appUpdate.setId(updateEntity.getId());
|
|
|
+ appUpdate.setCreateTime(updateEntity.getCreateTime());
|
|
|
+ appUpdate.setUpdateTime(LocalDateTime.now());
|
|
|
}
|
|
|
+ appUpdate.setIsDelete(false);
|
|
|
+ updateRepository.save(appUpdate);
|
|
|
break;
|
|
|
// 编译器配置
|
|
|
case compiler:
|
|
|
AppCompile appCompile = (AppCompile) JsonUtil.jsonToObject(json, AppCompile.class);
|
|
|
- AppCompile compileEntity = compileRepository.findByCompilerName(appCompile.getCompilerName());
|
|
|
- if (compileEntity == null) {
|
|
|
- compileRepository.save(appCompile);
|
|
|
+ // 检测本机上是否存在编译器
|
|
|
+ String script = appCompile.getCompilerHome() + appCompile.getVersionCmd();
|
|
|
+ checkScript(script);
|
|
|
+
|
|
|
+ AppCompile compileEntity =
|
|
|
+ compileRepository.findByIsDeleteFalseAndCompilerName(appCompile.getCompilerName());
|
|
|
+ if (compileEntity != null) {
|
|
|
+ appCompile.setId(compileEntity.getId());
|
|
|
+ appCompile.setCreateTime(compileEntity.getCreateTime());
|
|
|
+ appCompile.setUpdateTime(LocalDateTime.now());
|
|
|
}
|
|
|
+ appCompile.setIsDelete(false);
|
|
|
+ compileRepository.save(appCompile);
|
|
|
break;
|
|
|
// 打包工具配置
|
|
|
case packer:
|
|
|
AppPack appPack = (AppPack) JsonUtil.jsonToObject(json, AppPack.class);
|
|
|
- AppPack packEntity = packRepository.findByPackerName(appPack.getPackerName());
|
|
|
- if (packEntity == null) {
|
|
|
- packRepository.save(appPack);
|
|
|
+ // 检测本机上是否存在 docker
|
|
|
+ if (PackerType.valueOf(appPack.getPackerType()) == PackerType.docker) {
|
|
|
+ checkScript("docker -v");
|
|
|
+ }
|
|
|
+
|
|
|
+ AppPack packEntity = packRepository.findByIsDeleteFalseAndPackerName(appPack.getPackerName());
|
|
|
+ if (packEntity != null) {
|
|
|
+ appPack.setId(packEntity.getId());
|
|
|
+ appPack.setCreateTime(packEntity.getCreateTime());
|
|
|
+ appPack.setUpdateTime(LocalDateTime.now());
|
|
|
}
|
|
|
+ appPack.setIsDelete(false);
|
|
|
+ packRepository.save(appPack);
|
|
|
break;
|
|
|
default:
|
|
|
throw new Exception(configType + " 类型不存在");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void checkScript(String script) throws Exception {
|
|
|
+ ShellResult shellResult = shellExecutor.execute(script);
|
|
|
+ if (shellResult.hasError()) {
|
|
|
+ throw new Exception(shellResult.getStdout());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public PageList getByPage(String configType, PageRequest pageRequest) throws Exception {
|
|
|
switch (ConfigType.valueOf(configType)) {
|
|
|
case localDir:
|
|
|
- Page<LocalDir> configs = configRepository.findAll(pageRequest);
|
|
|
+ Page<LocalDir> configs = localDirRepository.findAll(pageRequest);
|
|
|
PageList<LocalDir> pageList = new PageList<>();
|
|
|
pageList.setTotalSize(configs.getTotalElements());
|
|
|
pageList.setTotalPages(configs.getTotalPages());
|
|
|
@@ -145,107 +201,49 @@ public class ConfigService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void update(String configType, String json) throws Exception {
|
|
|
+ /**
|
|
|
+ * 只做逻辑删除
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ * @date 2020-09-30 下午2:53
|
|
|
+ */
|
|
|
+ public void delete(String configType, String name) throws Exception {
|
|
|
switch (ConfigType.valueOf(configType)) {
|
|
|
- // 本地文件系统目录
|
|
|
case localDir:
|
|
|
- LocalDir localDir = (LocalDir) JsonUtil.jsonToObject(json, LocalDir.class);
|
|
|
- configRepository.save(localDir);
|
|
|
- /*AppEnv configEntity = configRepository.findByEnv(appEnv.getEnv());
|
|
|
- if (configEntity != null && !configEntity.equals(appEnv)) {
|
|
|
- appEnv.setId(configEntity.getId());
|
|
|
- appEnv.setIsDelete(configEntity.getIsDelete());
|
|
|
- appEnv.setCreateTime(configEntity.getCreateTime());
|
|
|
- appEnv.setUpdateTime(LocalDateTime.now());
|
|
|
- configRepository.save(appEnv);
|
|
|
- } else if (configEntity == null) {
|
|
|
- configRepository.save(appEnv);
|
|
|
- }*/
|
|
|
break;
|
|
|
- // 通知配置
|
|
|
case notifier:
|
|
|
- Notification notification = (Notification) JsonUtil.jsonToObject(json, Notification.class);
|
|
|
- // TODO 考虑 isDelete 为 true 的情况
|
|
|
- Notification notifyEntity = notifyRepository.findByNotifierName(notification.getNotifierName());
|
|
|
- if (notifyEntity != null && !notifyEntity.equals(notification)) {
|
|
|
- notification.setId(notifyEntity.getId());
|
|
|
- notification.setIsDelete(notifyEntity.getIsDelete());
|
|
|
- notification.setCreateTime(notifyEntity.getCreateTime());
|
|
|
- notification.setUpdateTime(LocalDateTime.now());
|
|
|
- notifyRepository.save(notification);
|
|
|
- } else if (notifyEntity == null) {
|
|
|
- notifyRepository.save(notification);
|
|
|
+ Notification notifierEntity = notifyRepository.findByIsDeleteFalseAndNotifierName(name);
|
|
|
+ if (notifierEntity == null) {
|
|
|
+ throw new Exception(name + "不存在...");
|
|
|
}
|
|
|
+ // TODO 处理其他实体对共享实体的引用
|
|
|
+ notifierEntity.setIsDelete(true);
|
|
|
+ notifyRepository.save(notifierEntity);
|
|
|
break;
|
|
|
- // 仓库配置
|
|
|
case repo:
|
|
|
- AppUpdate appUpdate = (AppUpdate) JsonUtil.jsonToObject(json, AppUpdate.class);
|
|
|
- AppUpdate updateEntity = updateRepository.findByRepoName(appUpdate.getRepoName());
|
|
|
- if (updateEntity != null && !updateEntity.equals(appUpdate)) {
|
|
|
- appUpdate.setId(updateEntity.getId());
|
|
|
- appUpdate.setIsDelete(false);
|
|
|
- appUpdate.setCreateTime(updateEntity.getCreateTime());
|
|
|
- appUpdate.setUpdateTime(LocalDateTime.now());
|
|
|
- updateRepository.save(appUpdate);
|
|
|
- } else if (updateEntity == null) {
|
|
|
- updateRepository.save(appUpdate);
|
|
|
+ AppUpdate updateEntity = updateRepository.findByIsDeleteFalseAndRepoName(name);
|
|
|
+ if (updateEntity == null) {
|
|
|
+ throw new Exception(name + "不存在...");
|
|
|
}
|
|
|
+ updateEntity.setIsDelete(true);
|
|
|
+ updateRepository.save(updateEntity);
|
|
|
break;
|
|
|
- // 编译器配置
|
|
|
case compiler:
|
|
|
- AppCompile appCompile = (AppCompile) JsonUtil.jsonToObject(json, AppCompile.class);
|
|
|
- String cmd = appCompile.getCompilerHome() + "/" + appCompile.getVersionCmd();
|
|
|
- ShellExecutor executor = new ShellExecutor();
|
|
|
- ShellResult result = executor.execute(null, cmd);
|
|
|
- if (result.getExitCode() != 0) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- AppCompile compileEntity = compileRepository.findByCompilerName(appCompile.getCompilerName());
|
|
|
- if (compileEntity != null && !compileEntity.equals(appCompile)) {
|
|
|
- appCompile.setId(compileEntity.getId());
|
|
|
- appCompile.setIsDelete(false);
|
|
|
- appCompile.setCreateTime(compileEntity.getCreateTime());
|
|
|
- appCompile.setUpdateTime(LocalDateTime.now());
|
|
|
- compileRepository.save(appCompile);
|
|
|
- } else if (compileEntity == null) {
|
|
|
- compileRepository.save(appCompile);
|
|
|
+ AppCompile compileEntity = compileRepository.findByIsDeleteFalseAndCompilerName(name);
|
|
|
+ if (compileEntity == null) {
|
|
|
+ throw new Exception(name + "不存在...");
|
|
|
}
|
|
|
+ compileEntity.setIsDelete(true);
|
|
|
+ compileRepository.save(compileEntity);
|
|
|
break;
|
|
|
- // 打包工具配置
|
|
|
case packer:
|
|
|
- AppPack appPack = (AppPack) JsonUtil.jsonToObject(json, AppPack.class);
|
|
|
- AppPack packEntity = packRepository.findByPackerName(appPack.getPackerName());
|
|
|
- if (packEntity != null && !packEntity.equals(appPack)) {
|
|
|
- appPack.setId(packEntity.getId());
|
|
|
- appPack.setIsDelete(false);
|
|
|
- appPack.setCreateTime(packEntity.getCreateTime());
|
|
|
- appPack.setUpdateTime(LocalDateTime.now());
|
|
|
- packRepository.save(appPack);
|
|
|
- } else if (packEntity == null) {
|
|
|
- packRepository.save(appPack);
|
|
|
+ AppPack packEntity = packRepository.findByIsDeleteFalseAndPackerName(name);
|
|
|
+ if (packEntity == null) {
|
|
|
+ throw new Exception(name + "不存在...");
|
|
|
}
|
|
|
- break;
|
|
|
- default:
|
|
|
- throw new Exception(configType + " 类型不存在");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void delete(String configType, String name) throws Exception {
|
|
|
- switch (ConfigType.valueOf(configType)) {
|
|
|
- case localDir:
|
|
|
- break;
|
|
|
- case notifier:
|
|
|
- notifyRepository.deleteByNotifierName(name);
|
|
|
- break;
|
|
|
- case repo:
|
|
|
- updateRepository.deleteByRepoName(name);
|
|
|
- break;
|
|
|
- case compiler:
|
|
|
- compileRepository.deleteByCompilerName(name);
|
|
|
- break;
|
|
|
- case packer:
|
|
|
- packRepository.deleteByPackerName(name);
|
|
|
+ packEntity.setIsDelete(true);
|
|
|
+ packRepository.save(packEntity);
|
|
|
break;
|
|
|
default:
|
|
|
throw new Exception(configType + " 类型不存在");
|