|
|
@@ -1,190 +1,11 @@
|
|
|
package cn.reghao.autodop.dmaster.app.db.crud.config;
|
|
|
|
|
|
-import cn.reghao.autodop.dmaster.machine.db.query.MachineHostQuery;
|
|
|
-import cn.reghao.autodop.dmaster.machine.model.po.MachineHost;
|
|
|
-import cn.reghao.jdkutil.result.Result;
|
|
|
-import cn.reghao.jdkutil.result.ResultStatus;
|
|
|
-import cn.reghao.autodop.dmaster.app.model.constant.AppType;
|
|
|
-import cn.reghao.autodop.dmaster.app.model.constant.EnvList;
|
|
|
-import cn.reghao.autodop.dmaster.app.model.po.config.ProjConfig;
|
|
|
-import cn.reghao.autodop.dmaster.notification.model.po.NotifyType;
|
|
|
-import cn.reghao.autodop.dmaster.app.model.po.AppRunning;
|
|
|
-import cn.reghao.autodop.dmaster.app.model.po.config.DeployConfig;
|
|
|
-import cn.reghao.autodop.dmaster.app.db.repository.AppRunningRepository;
|
|
|
-import cn.reghao.autodop.dmaster.app.service.impl.BuildDeployConfigServiceImpl;
|
|
|
-import cn.reghao.autodop.dmaster.app.model.vo.NewApp;
|
|
|
+import cn.reghao.jdkutil.db.BaseCrud;
|
|
|
import cn.reghao.autodop.dmaster.app.model.po.config.AppConfig;
|
|
|
-import cn.reghao.autodop.dmaster.app.db.repository.config.AppConfigRepository;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
-
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Objects;
|
|
|
-import java.util.Set;
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author reghao
|
|
|
* @date 2020-11-10 21:58:00
|
|
|
*/
|
|
|
-//@CacheConfig(cacheNames = {"caffeineCacheManager"})
|
|
|
-@Service
|
|
|
-public class AppConfigCrud {
|
|
|
- private AppConfigRepository appRepository;
|
|
|
- private MachineHostQuery machineHostQuery;
|
|
|
- private SharedEntityChecker sharedEntityChecker;
|
|
|
- private BuildDeployConfigServiceImpl appBuildService;
|
|
|
- private AppRunningRepository runningRepository;
|
|
|
-
|
|
|
- public AppConfigCrud(AppConfigRepository appRepository,
|
|
|
- MachineHostQuery machineHostQuery,
|
|
|
- SharedEntityChecker sharedEntityChecker,
|
|
|
- BuildDeployConfigServiceImpl appBuildService,
|
|
|
- AppRunningRepository runningRepository) {
|
|
|
- this.appRepository = appRepository;
|
|
|
- this.machineHostQuery = machineHostQuery;
|
|
|
- this.sharedEntityChecker = sharedEntityChecker;
|
|
|
- this.appBuildService = appBuildService;
|
|
|
- this.runningRepository = runningRepository;
|
|
|
- }
|
|
|
-
|
|
|
- public void save(AppConfig appConfig) {
|
|
|
- appRepository.save(appConfig);
|
|
|
- }
|
|
|
-
|
|
|
- // TODO 数据修改后要在引用它的对象中立即体现
|
|
|
- //@CachePut(cacheNames = {"app"}, key = "#app.appId")
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
- public Result insertOrUpdate(AppConfig app) throws Exception {
|
|
|
- // TODO 处理所有与 app 关联的数据
|
|
|
- // TODO 不能有应用处于构建中
|
|
|
- Integer id = app.getId();
|
|
|
- /*if (id != null) {
|
|
|
- app = appRepository.findById(id).orElse(null);
|
|
|
- }*/
|
|
|
-
|
|
|
- Result result = checkEnum(app);
|
|
|
- if (result.getCode() != ResultStatus.SUCCESS.getCode()) {
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- // 首先处理共享实体,将 app 的所有属性填充完整
|
|
|
- checkSharedEntity(app);
|
|
|
- checkNotifyGroups(app);
|
|
|
- checkDeployConfigs(app);
|
|
|
- appRepository.save(app);
|
|
|
- appBuildService.refreshApp(app);
|
|
|
-
|
|
|
- String msg = id != null ? "更新 " + app.getAppId() + " 成功"
|
|
|
- : "添加 " + app.getAppId() + " 成功";
|
|
|
- return Result.result(ResultStatus.SUCCESS, msg);
|
|
|
- }
|
|
|
-
|
|
|
- public Result updateNotify(AppConfig app) {
|
|
|
- appRepository.save(app);
|
|
|
- return Result.result(ResultStatus.SUCCESS);
|
|
|
- }
|
|
|
-
|
|
|
- private Result checkEnum(AppConfig app) {
|
|
|
- Set<String> envs = Arrays.stream(EnvList.values())
|
|
|
- .map(Enum::name)
|
|
|
- .collect(Collectors.toSet());
|
|
|
- String env = app.getEnv();
|
|
|
- if (!envs.contains(env)) {
|
|
|
- String msg = env + " 环境不存在";
|
|
|
- return Result.result(ResultStatus.FAIL, msg);
|
|
|
- }
|
|
|
-
|
|
|
- String appType = app.getAppType();
|
|
|
- Set<String> appTypes = Arrays.stream(AppType.values())
|
|
|
- .map(Enum::name)
|
|
|
- .collect(Collectors.toSet());
|
|
|
- if (!appTypes.contains(appType)) {
|
|
|
- String msg = appType + " 类型不存在";
|
|
|
- return Result.result(ResultStatus.FAIL, msg);
|
|
|
- }
|
|
|
-
|
|
|
- return Result.result(ResultStatus.SUCCESS);
|
|
|
- }
|
|
|
-
|
|
|
- private void checkNotifyGroups(AppConfig app) {
|
|
|
- // TODO 根据通知类型创建相应的通知实例
|
|
|
-
|
|
|
- Set<String> notifyTypes = Arrays.stream(NotifyType.values())
|
|
|
- .map(Enum::name)
|
|
|
- .collect(Collectors.toSet());
|
|
|
- // TODO notifyReceivers 为 null 时会发生异常,好像不能正常返回,后面需要测试一下
|
|
|
- /*List<NotifyReceiver> notifyReceivers = app.getNotifyReceivers().stream()
|
|
|
- .filter(notifyReceiver -> notifyTypes.contains(notifyReceiver.getNotifyType()))
|
|
|
- .collect(Collectors.toList());
|
|
|
- app.setNotifyReceivers(notifyReceivers);*/
|
|
|
- }
|
|
|
-
|
|
|
- private void checkDeployConfigs(AppConfig app) {
|
|
|
- // TODO null 异常不会被 ControllerExceptionHandler 捕获
|
|
|
- ProjConfig proj = app.getProj();
|
|
|
- String packType;
|
|
|
- if (proj != null) {
|
|
|
- packType = proj.getPackerConfig().getType();
|
|
|
- } else {
|
|
|
- packType = app.getPackerConfig().getType();
|
|
|
- }
|
|
|
-
|
|
|
- List<DeployConfig> deployConfigs = app.getDeployConfigs().stream()
|
|
|
- .map(deployConfig -> {
|
|
|
- String machineId = deployConfig.getMachineId();
|
|
|
- MachineHost machineHost = machineHostQuery.findByMachineId(machineId);
|
|
|
- if (machineHost != null) {
|
|
|
- deployConfig.setMachineIpv4(machineHost.getMachineIpv4());
|
|
|
- deployConfig.setPackType(packType);
|
|
|
- return deployConfig;
|
|
|
- } else {
|
|
|
- return null;
|
|
|
- }
|
|
|
- })
|
|
|
- .filter(Objects::nonNull)
|
|
|
- .collect(Collectors.toList());
|
|
|
- app.setDeployConfigs(deployConfigs);
|
|
|
- }
|
|
|
-
|
|
|
- private void checkSharedEntity(AppConfig app) throws Exception {
|
|
|
- if (app.getProj() == null) {
|
|
|
- sharedEntityChecker.checkAndSetBuildConfig(app);
|
|
|
- } else {
|
|
|
- sharedEntityChecker.checkAndSetProj(app);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public Result copy(AppConfig from, NewApp to) throws Exception {
|
|
|
- if (from == null) {
|
|
|
- String msg = "应用配置不存在";
|
|
|
- return Result.result(ResultStatus.FAIL, msg);
|
|
|
- }
|
|
|
-
|
|
|
- AppConfig toApp = appRepository.findByIsDeleteFalseAndAppId(to.getNewId());
|
|
|
- if (toApp != null) {
|
|
|
- String msg = to.getNewId() + " 已存在";
|
|
|
- return Result.result(ResultStatus.FAIL, msg);
|
|
|
- }
|
|
|
-
|
|
|
- toApp = to.app((AppConfig) from.clone());
|
|
|
- return insertOrUpdate(toApp);
|
|
|
- }
|
|
|
-
|
|
|
- //@CacheEvict(cacheNames = {"app"}, key = "#app.appId")
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
- public Result delete(AppConfig app) {
|
|
|
- String appId = app.getAppId();
|
|
|
- List<AppRunning> appRunnings = runningRepository.findByAppId(appId).stream()
|
|
|
- .filter(AppRunning::getIsRunning).collect(Collectors.toList());
|
|
|
- if (!appRunnings.isEmpty()) {
|
|
|
- return Result.result(ResultStatus.FAIL, appId + " 正在运行中");
|
|
|
- }
|
|
|
-
|
|
|
- // TODO 处理所有与 app 关联的数据
|
|
|
- appBuildService.delete(app.getAppId());
|
|
|
- appRepository.delete(app);
|
|
|
- return Result.result(ResultStatus.SUCCESS);
|
|
|
- }
|
|
|
+public interface AppConfigCrud extends BaseCrud<AppConfig> {
|
|
|
}
|