|
|
@@ -0,0 +1,238 @@
|
|
|
+package cn.reghao.autodop.dmaster.app.service.config.impl;
|
|
|
+
|
|
|
+import cn.reghao.autodop.dmaster.app.db.crud.config.SharedEntityChecker;
|
|
|
+import cn.reghao.autodop.dmaster.app.db.repository.AppRunningRepository;
|
|
|
+import cn.reghao.autodop.dmaster.app.db.repository.config.AppConfigRepository;
|
|
|
+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.AppRunning;
|
|
|
+import cn.reghao.autodop.dmaster.app.model.po.config.AppConfig;
|
|
|
+import cn.reghao.autodop.dmaster.app.model.po.config.DeployConfig;
|
|
|
+import cn.reghao.autodop.dmaster.app.model.po.config.ProjConfig;
|
|
|
+import cn.reghao.autodop.dmaster.app.model.vo.NewApp;
|
|
|
+import cn.reghao.autodop.dmaster.app.service.AppBuildDeployService;
|
|
|
+import cn.reghao.autodop.dmaster.app.service.config.AppConfigService;
|
|
|
+import cn.reghao.autodop.dmaster.machine.db.query.MachineHostQuery;
|
|
|
+import cn.reghao.autodop.dmaster.machine.model.po.MachineHost;
|
|
|
+import cn.reghao.autodop.dmaster.notification.model.po.NotifyGroup;
|
|
|
+import cn.reghao.autodop.dmaster.notification.model.po.NotifyType;
|
|
|
+import cn.reghao.jdkutil.result.Result;
|
|
|
+import cn.reghao.jdkutil.result.ResultStatus;
|
|
|
+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 2021-09-16 18:28:51
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class AppConfigServiceImpl implements AppConfigService {
|
|
|
+ private AppConfigRepository appRepository;
|
|
|
+ private MachineHostQuery machineHostQuery;
|
|
|
+ private SharedEntityChecker sharedEntityChecker;
|
|
|
+ private AppBuildDeployService appBuildService;
|
|
|
+ private AppRunningRepository runningRepository;
|
|
|
+
|
|
|
+ public AppConfigServiceImpl(AppConfigRepository appRepository,
|
|
|
+ MachineHostQuery machineHostQuery,
|
|
|
+ SharedEntityChecker sharedEntityChecker,
|
|
|
+ AppBuildDeployService appBuildService,
|
|
|
+ AppRunningRepository runningRepository) {
|
|
|
+ this.appRepository = appRepository;
|
|
|
+ this.machineHostQuery = machineHostQuery;
|
|
|
+ this.sharedEntityChecker = sharedEntityChecker;
|
|
|
+ this.appBuildService = appBuildService;
|
|
|
+ this.runningRepository = runningRepository;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result setDeployConfig(int id, List<DeployConfig> deployConfigs) {
|
|
|
+ return Result.success("");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void save(AppConfig appConfig) {
|
|
|
+ appRepository.save(appConfig);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public Result add(AppConfig app) {
|
|
|
+ // 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 的所有属性填充完整
|
|
|
+ try {
|
|
|
+ checkSharedEntity(app);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return Result.fail(e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ checkNotifyGroups(app);
|
|
|
+ checkDeployConfigs(app);
|
|
|
+ appRepository.save(app);
|
|
|
+ appBuildService.refreshApp(app);
|
|
|
+
|
|
|
+ String msg = id != null ? "更新 " + app.getAppId() + " 成功"
|
|
|
+ : "添加 " + app.getAppId() + " 成功";
|
|
|
+ return Result.result(ResultStatus.SUCCESS, msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result update(AppConfig app) {
|
|
|
+ // 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 的所有属性填充完整
|
|
|
+ try {
|
|
|
+ checkSharedEntity(app);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return Result.fail(e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ checkNotifyGroups(app);
|
|
|
+ checkDeployConfigs(app);
|
|
|
+ appRepository.save(app);
|
|
|
+ appBuildService.refreshApp(app);
|
|
|
+
|
|
|
+ String msg = id != null ? "更新 " + app.getAppId() + " 成功"
|
|
|
+ : "添加 " + app.getAppId() + " 成功";
|
|
|
+ return Result.result(ResultStatus.SUCCESS, msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result setNotify(int id, List<NotifyGroup> notifyGroups) {
|
|
|
+ //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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result copy(AppConfig from, NewApp to) {
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ toApp = to.app((AppConfig) from.clone());
|
|
|
+ return add(toApp);
|
|
|
+ } catch (CloneNotSupportedException e) {
|
|
|
+ return Result.fail(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+}
|