Explorar o código

分离出 CRUD 接口和操作

reghao %!s(int64=5) %!d(string=hai) anos
pai
achega
94b83e572e

+ 3 - 3
dagent/src/main/resources/application-prod.yml

@@ -4,8 +4,8 @@ dmaster:
   api: /api/machine/status
 spring:
   rabbitmq:
-    host: mq.reghao.cn
+    host: mq.srv.iquizoo.com
     port: 5672
-    username: reghao
-    password: gL2dOiWYqXiKKsv5LOt9
+    username: iquizoo-mq1
+    password: z2pT1PXR
     virtual-host: /

+ 139 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/controller/crud/BuildCrudController.java

@@ -0,0 +1,139 @@
+package cn.reghao.autodop.dmaster.app.controller.crud;
+
+import cn.reghao.autodop.common.deploy.PackerType;
+import cn.reghao.autodop.common.result.WebResult;
+import cn.reghao.autodop.common.utils.data.db.PageList;
+import cn.reghao.autodop.dmaster.app.constant.*;
+import cn.reghao.autodop.dmaster.app.service.ConfigService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Sort;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author reghao
+ * @date 2019-08-30 18:49:15
+ */
+@Slf4j
+@Api(tags = "配置 CRUD 接口")
+@RestController
+@RequestMapping("/api/config1")
+public class BuildCrudController {
+    private ConfigService configService;
+
+    public BuildCrudController(ConfigService configService) {
+        this.configService = configService;
+    }
+
+    @ApiOperation(value = "添加配置")
+    @PostMapping(value = "/{configType}", consumes = "application/json")
+    public ResponseEntity<String> addConfig(@PathVariable("configType") String configType,
+                                            @RequestBody String json) throws Exception {
+        configService.addOrModify(configType, json);
+        return ResponseEntity.ok().body(WebResult.success("ok"));
+    }
+
+    @ApiOperation(value = "分页获取配置(更新时间倒序)")
+    @GetMapping("/{configType}")
+    public ResponseEntity<String> getConfigByPage(@PathVariable("configType") String configType,
+                                                  @RequestParam("page") int page,
+                                                  @RequestParam("size") int size) throws Exception {
+        PageRequest pageRequest =
+                PageRequest.of(page-1, size, Sort.by(Sort.Direction.DESC, "updateTime"));
+        PageList pageList = configService.getByPage(configType, pageRequest);
+        return ResponseEntity.ok().body(WebResult.success(pageList));
+    }
+
+    @ApiOperation(value = "修改配置")
+    @PutMapping("/{configType}")
+    public ResponseEntity<String> modifyConfig(@PathVariable("configType") String configType,
+                                               @RequestBody String json) throws Exception {
+        configService.addOrModify(configType, json);
+        return ResponseEntity.ok().body(WebResult.success("ok"));
+    }
+
+    @ApiOperation(value = "删除配置")
+    @DeleteMapping("/{configType}/{uniqueKey}")
+    public ResponseEntity<String> deleteConfig(@PathVariable("configType") String configType,
+                                               @PathVariable("uniqueKey") String uniqueKey) throws Exception {
+        configService.delete(configType, uniqueKey);
+        return ResponseEntity.ok().body(WebResult.success("ok"));
+    }
+
+    /**
+     * 各种配置类型
+     *
+     * @param
+     * @return
+     * @date 2020-09-16 下午2:12
+     */
+    @GetMapping("/types/{configType}")
+    public ResponseEntity<String> getConfigType(@PathVariable("configType") String configType) {
+        Map<String, String> map = new HashMap<>();
+        // TODO 配置类型可以动态修改
+        switch (ConfigType.valueOf(configType)) {
+            case localDir:
+                for (EnvType config : EnvType.values()) {
+                    map.put(config.name(), config.getValue());
+                }
+                break;
+            case notifier:
+                for (NotifierType config : NotifierType.values()) {
+                    map.put(config.name(), config.getValue());
+                }
+                break;
+            case repo:
+                for (RepoType config : RepoType.values()) {
+                    map.put(config.name(), config.getValue());
+                }
+                break;
+            case compiler:
+                for (CompilerType config : CompilerType.values()) {
+                    map.put(config.name(), config.getValue());
+                }
+                break;
+            case packer:
+                for (PackerType config : PackerType.values()) {
+                    map.put(config.name(), config.getValue());
+                }
+                break;
+            case repoAuth:
+                for (RepoAuthType config : RepoAuthType.values()) {
+                    map.put(config.name(), config.getValue());
+                }
+                break;
+            case app:
+                for (AppType config : AppType.values()) {
+                    map.put(config.name(), config.getValue());
+                }
+                break;
+            case env:
+                for (EnvType config : EnvType.values()) {
+                    map.put(config.name(), config.getValue());
+                }
+                break;
+            case proj:
+            case update:
+            case compile:
+            case pack:
+            case notify:
+                map.putAll(configService.buildConfigs(configType));
+                break;
+            default:
+        }
+
+        return ResponseEntity.ok().body(WebResult.success(map));
+    }
+
+    @GetMapping("/{configType}/list")
+    public ResponseEntity<String> getConfigList(@PathVariable("configType") String configType) throws Exception {
+        configService.buildConfigs("");
+        return ResponseEntity.ok().body(WebResult.success("pageList"));
+    }
+}

+ 86 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/controller/crud/OrchestrateCrudController.java

@@ -0,0 +1,86 @@
+package cn.reghao.autodop.dmaster.app.controller.crud;
+
+import cn.reghao.autodop.common.result.WebResult;
+import cn.reghao.autodop.dmaster.app.constant.EnvType;
+import cn.reghao.autodop.dmaster.app.service.OrchestrateService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Sort;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
+
+/**
+ * @author reghao
+ * @date 2019-11-27 11:29:43
+ */
+@Slf4j
+@Api(tags = "应用编排 CRUD 接口")
+@RestController
+@RequestMapping("/api/app/orchestrate1")
+public class OrchestrateCrudController {
+    private OrchestrateService orchestrateService;
+
+    public OrchestrateCrudController(OrchestrateService orchestrateService) {
+        this.orchestrateService = orchestrateService;
+    }
+
+    @ApiOperation(value = "添加项目/应用编排")
+    @PostMapping(value = "/{type}", consumes = "application/json")
+    public ResponseEntity<String> addOrchestration(@PathVariable("type") int type, @RequestBody String json)
+            throws Exception {
+        orchestrateService.addOrModify(type, json);
+        return ResponseEntity.ok().body(WebResult.success("ok"));
+    }
+
+    @ApiOperation(value = "拷贝项目/应用编排")
+    @PostMapping(value = "/copy/{type}")
+    public ResponseEntity<String> copyOrchestration(@PathVariable("type") int type,
+                                                    @RequestParam("from") String from,
+                                                    @RequestParam("to") String to) throws Exception {
+        boolean res = orchestrateService.copy(type, from, to);
+        if (res) {
+            return ResponseEntity.ok().body(WebResult.success("拷贝成功"));
+        } else {
+            return ResponseEntity.ok().body(WebResult.fail("拷贝失败"));
+        }
+    }
+
+    @ApiOperation(value = "获取项目/应用编排")
+    @GetMapping("/{type}/{id}")
+    public ResponseEntity<String> getOrchestration(@PathVariable("type") int type, @PathVariable("id") String id) {
+        Object res = orchestrateService.get(type, id);
+        return ResponseEntity.ok().body(WebResult.success(res));
+    }
+
+    @ApiOperation(value = "分页获取项目/应用编排(更新时间倒序)")
+    @GetMapping("/{type}")
+    public ResponseEntity<String> getOrchestrationByPage(@PathVariable("type") int type,
+                                                        @RequestParam("env") String env,
+                                                        @RequestParam("page") int page,
+                                                        @RequestParam("size") int size) {
+        String env1 = EnvType.valueOf(env).name();
+        PageRequest pageRequest =
+                PageRequest.of(page-1, size, Sort.by(Sort.Direction.DESC, "updateTime"));
+        Object res = orchestrateService.getByPage(type, env1, pageRequest);
+        return ResponseEntity.ok().body(WebResult.success(res));
+    }
+
+    @ApiOperation(value = "修改项目/应用编排")
+    @PutMapping("/{type}")
+    public ResponseEntity<String> modifyOrchestration(@PathVariable("type") int type, @RequestBody String json)
+            throws Exception {
+        // TODO 修改仓库相关的字段时应该删除本地仓库
+        orchestrateService.addOrModify(type, json);
+        return ResponseEntity.ok().body(WebResult.success("ok"));
+    }
+
+    @ApiOperation(value = "删除项目/应用编排")
+    @DeleteMapping("/{type}/{appId}")
+    public ResponseEntity<String> deleteOrchestration(@PathVariable("type") int type,
+                                                      @PathVariable("appId") String appId) throws Exception {
+        orchestrateService.delete(type, appId);
+        return ResponseEntity.ok().body(WebResult.success("ok"));
+    }
+}

+ 75 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/crud/LocalDirCrudService.java

@@ -0,0 +1,75 @@
+package cn.reghao.autodop.dmaster.app.service.crud;
+
+import cn.reghao.autodop.common.utils.data.db.CrudOps;
+import cn.reghao.autodop.common.utils.data.db.PageList;
+import cn.reghao.autodop.dmaster.app.entity.LocalDir;
+import cn.reghao.autodop.dmaster.app.repository.orchestration.LocalDirRepository;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Sort;
+import org.springframework.stereotype.Service;
+
+import java.io.File;
+import java.time.LocalDateTime;
+import java.util.stream.Collectors;
+
+/**
+ * @author reghao
+ * @date 2020-11-10 21:58:00
+ */
+@Service
+public class LocalDirCrudService implements CrudOps<LocalDir> {
+    private LocalDirRepository localDirRepository;
+
+    public LocalDirCrudService(LocalDirRepository localDirRepository) {
+        this.localDirRepository = localDirRepository;
+    }
+
+    @Override
+    public void addOrModify(LocalDir localDir) throws Exception {
+        // 检查并创建目录
+        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() + "不存在且创建失败...");
+        }
+
+        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);
+        }
+    }
+
+    @Override
+    public PageList<LocalDir> getByPage(int page, int size) {
+        // 默认按更新时间倒序
+        PageRequest pageRequest =
+                PageRequest.of(page-1, size, Sort.by(Sort.Direction.DESC, "updateTime"));
+        Page<LocalDir> page1 = localDirRepository.findAll(pageRequest);
+
+        PageList<LocalDir> pageList = new PageList<>();
+        pageList.setTotalSize(page1.getTotalElements());
+        pageList.setTotalPages(page1.getTotalPages());
+        pageList.setList(page1.getContent().stream().map(LocalDir::vo).collect(Collectors.toList()));
+        return pageList;
+    }
+
+    @Override
+    public void delete(String uniqueKey) throws Exception {
+        System.out.println("暂不实现 LocalDir 的删除...");
+    }
+}

+ 64 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/crud/NotificationCrudService.java

@@ -0,0 +1,64 @@
+package cn.reghao.autodop.dmaster.app.service.crud;
+
+import cn.reghao.autodop.common.utils.data.db.CrudOps;
+import cn.reghao.autodop.common.utils.data.db.PageList;
+import cn.reghao.autodop.dmaster.app.entity.Notification;
+import cn.reghao.autodop.dmaster.app.repository.orchestration.NotificationRepository;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Sort;
+import org.springframework.stereotype.Service;
+
+import java.time.LocalDateTime;
+import java.util.stream.Collectors;
+
+/**
+ * @author reghao
+ * @date 2020-11-10 21:58:00
+ */
+@Service
+public class NotificationCrudService implements CrudOps<Notification> {
+    private NotificationRepository notifyRepository;
+
+    public NotificationCrudService(NotificationRepository notifyRepository) {
+        this.notifyRepository = notifyRepository;
+    }
+
+    @Override
+    public void addOrModify(Notification notification) throws Exception {
+        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);
+    }
+
+    @Override
+    public PageList<Notification> getByPage(int page, int size) {
+        // 默认按更新时间倒序
+        PageRequest pageRequest =
+                PageRequest.of(page-1, size, Sort.by(Sort.Direction.DESC, "updateTime"));
+        Page<Notification> notifiers = notifyRepository.findAll(pageRequest);
+
+        PageList<Notification> pageList = new PageList<>();
+        pageList.setTotalSize(notifiers.getTotalElements());
+        pageList.setTotalPages(notifiers.getTotalPages());
+        pageList.setList(notifiers.getContent().stream().map(Notification::vo).collect(Collectors.toList()));
+        return pageList;
+    }
+
+    @Override
+    public void delete(String uniqueKey) throws Exception {
+        Notification notifierEntity = notifyRepository.findByIsDeleteFalseAndNotifierName(uniqueKey);
+        if (notifierEntity == null) {
+            throw new Exception(uniqueKey + "不存在...");
+        }
+        // TODO 处理其他实体对共享实体的引用
+        notifierEntity.setIsDelete(true);
+        notifyRepository.save(notifierEntity);
+    }
+}

+ 85 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/crud/SharedEntityChecker.java

@@ -0,0 +1,85 @@
+package cn.reghao.autodop.dmaster.app.service.crud;
+
+import cn.reghao.autodop.dmaster.app.entity.Notification;
+import cn.reghao.autodop.dmaster.app.entity.build.AppBuild;
+import cn.reghao.autodop.dmaster.app.entity.build.AppCompile;
+import cn.reghao.autodop.dmaster.app.entity.build.AppPack;
+import cn.reghao.autodop.dmaster.app.entity.build.AppUpdate;
+import cn.reghao.autodop.dmaster.app.entity.orchestration.AppOrchestration;
+import cn.reghao.autodop.dmaster.app.repository.build.AppCompileRepository;
+import cn.reghao.autodop.dmaster.app.repository.build.AppPackRepository;
+import cn.reghao.autodop.dmaster.app.repository.build.AppUpdateRepository;
+import cn.reghao.autodop.dmaster.app.repository.orchestration.NotificationRepository;
+import cn.reghao.autodop.dmaster.app.vo.AppBuildVO;
+import org.springframework.stereotype.Service;
+
+/**
+ * @author reghao
+ * @date 2020-11-13 17:58:27
+ */
+@Service
+public class SharedEntityChecker {
+    private AppUpdateRepository appUpdateRepository;
+    private AppCompileRepository appCompileRepository;
+    private AppPackRepository appPackRepository;
+    private NotificationRepository notificationRepository;
+
+    public SharedEntityChecker(AppUpdateRepository appUpdateRepository,
+                               AppCompileRepository appCompileRepository,
+                               AppPackRepository appPackRepository,
+                               NotificationRepository notificationRepository) {
+        this.appUpdateRepository = appUpdateRepository;
+        this.appCompileRepository = appCompileRepository;
+        this.appPackRepository = appPackRepository;
+        this.notificationRepository = notificationRepository;
+    }
+
+    /**
+     * 检查构建共享配置
+     *
+     * @param
+     * @return
+     * @date 2020-09-30 下午3:17
+     */
+    public AppBuild checkBuildConfig(AppBuildVO buildVO) throws Exception {
+        AppBuild appBuild = new AppBuild();
+        String updater = buildVO.getUpdater();
+        AppUpdate appUpdate = appUpdateRepository.findByIsDeleteFalseAndRepoName(updater);
+        if (appUpdate == null) {
+            throw new Exception(updater + " 不存在...");
+        }
+        appBuild.setAppUpdate(appUpdate);
+
+        String compiler = buildVO.getCompiler();
+        AppCompile appCompile = appCompileRepository.findByIsDeleteFalseAndCompilerName(compiler);
+        if (appCompile == null) {
+            throw new Exception(compiler + " 不存在...");
+        }
+        appBuild.setAppCompile(appCompile);
+
+        String packer = buildVO.getPacker();
+        AppPack appPack = appPackRepository.findByIsDeleteFalseAndPackerName(packer);
+        if (appPack == null) {
+            throw new Exception(packer + " 不存在...");
+        }
+        appBuild.setAppPack(appPack);
+        return appBuild;
+    }
+
+    /**
+     * 检查通知共享配置
+     *
+     * @param
+     * @return
+     * @date 2020-09-30 下午3:18
+     */
+    public void checkNotification(AppOrchestration app, String notifier) throws Exception {
+        if (notifier != null) {
+            Notification notification = notificationRepository.findByIsDeleteFalseAndNotifierName(notifier);
+            if (notification == null) {
+                throw new Exception(notification + " 不存在...");
+            }
+            app.setNotification(notification);
+        }
+    }
+}

+ 66 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/crud/build/AppCompileCrudService.java

@@ -0,0 +1,66 @@
+package cn.reghao.autodop.dmaster.app.service.crud.build;
+
+import cn.reghao.autodop.common.utils.data.db.CrudOps;
+import cn.reghao.autodop.common.utils.data.db.PageList;
+import cn.reghao.autodop.dmaster.app.entity.build.AppCompile;
+import cn.reghao.autodop.dmaster.app.repository.build.AppCompileRepository;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Sort;
+import org.springframework.stereotype.Service;
+
+import java.time.LocalDateTime;
+import java.util.stream.Collectors;
+
+/**
+ * @author reghao
+ * @date 2020-11-10 21:58:00
+ */
+@Service
+public class AppCompileCrudService implements CrudOps<AppCompile> {
+    private AppCompileRepository compileRepository;
+
+    public AppCompileCrudService(AppCompileRepository compileRepository) {
+        this.compileRepository = compileRepository;
+    }
+
+    @Override
+    public void addOrModify(AppCompile appCompile) throws Exception {
+        String script = appCompile.getCompilerHome() + appCompile.getVersionCmd();
+        // TODO 检测本机上是否存在编译器
+        //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);
+    }
+
+    @Override
+    public PageList<AppCompile> getByPage(int page, int size) {
+        // 默认按更新时间倒序
+        PageRequest pageRequest =
+                PageRequest.of(page-1, size, Sort.by(Sort.Direction.DESC, "updateTime"));
+        Page<AppCompile> appCompiles = compileRepository.findAll(pageRequest);
+        PageList<AppCompile> pageList = new PageList<>();
+        pageList.setTotalSize(appCompiles.getTotalElements());
+        pageList.setTotalPages(appCompiles.getTotalPages());
+        pageList.setList(appCompiles.getContent().stream().map(AppCompile::vo).collect(Collectors.toList()));
+        return pageList;
+    }
+
+    @Override
+    public void delete(String uniqueKey) throws Exception {
+        AppCompile compileEntity = compileRepository.findByIsDeleteFalseAndCompilerName(uniqueKey);
+        if (compileEntity == null) {
+            throw new Exception(uniqueKey + "不存在...");
+        }
+        compileEntity.setIsDelete(true);
+        compileRepository.save(compileEntity);
+    }
+}

+ 68 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/crud/build/AppPackCrudService.java

@@ -0,0 +1,68 @@
+package cn.reghao.autodop.dmaster.app.service.crud.build;
+
+import cn.reghao.autodop.common.deploy.PackerType;
+import cn.reghao.autodop.common.utils.data.db.CrudOps;
+import cn.reghao.autodop.common.utils.data.db.PageList;
+import cn.reghao.autodop.dmaster.app.entity.build.AppPack;
+import cn.reghao.autodop.dmaster.app.repository.build.AppPackRepository;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Sort;
+import org.springframework.stereotype.Service;
+
+import java.time.LocalDateTime;
+import java.util.stream.Collectors;
+
+/**
+ * @author reghao
+ * @date 2020-11-10 21:58:00
+ */
+@Service
+public class AppPackCrudService implements CrudOps<AppPack> {
+    private AppPackRepository packRepository;
+
+    public AppPackCrudService(AppPackRepository packRepository) {
+        this.packRepository = packRepository;
+    }
+
+    @Override
+    public void addOrModify(AppPack appPack) throws Exception {
+        if (PackerType.valueOf(appPack.getPackerType()) == PackerType.docker) {
+            // TODO 检测本机上是否存在 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);
+    }
+
+    @Override
+    public PageList<AppPack> getByPage(int page, int size) {
+        // 默认按更新时间倒序
+        PageRequest pageRequest =
+                PageRequest.of(page-1, size, Sort.by(Sort.Direction.DESC, "updateTime"));
+        Page<AppPack> appPacks = packRepository.findAll(pageRequest);
+
+        PageList<AppPack> pageList = new PageList<>();
+        pageList.setTotalSize(appPacks.getTotalElements());
+        pageList.setTotalPages(appPacks.getTotalPages());
+        pageList.setList(appPacks.getContent().stream().map(AppPack::vo).collect(Collectors.toList()));
+        return pageList;
+    }
+
+    @Override
+    public void delete(String uniqueKey) throws Exception {
+        AppPack packEntity = packRepository.findByIsDeleteFalseAndPackerName(uniqueKey);
+        if (packEntity == null) {
+            throw new Exception(uniqueKey + "不存在...");
+        }
+        packEntity.setIsDelete(true);
+        packRepository.save(packEntity);
+    }
+}

+ 62 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/crud/build/AppUpdateCrudService.java

@@ -0,0 +1,62 @@
+package cn.reghao.autodop.dmaster.app.service.crud.build;
+
+import cn.reghao.autodop.common.utils.data.db.CrudOps;
+import cn.reghao.autodop.common.utils.data.db.PageList;
+import cn.reghao.autodop.dmaster.app.entity.build.AppUpdate;
+import cn.reghao.autodop.dmaster.app.repository.build.AppUpdateRepository;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Sort;
+import org.springframework.stereotype.Service;
+
+import java.time.LocalDateTime;
+import java.util.stream.Collectors;
+
+/**
+ * @author reghao
+ * @date 2020-11-10 21:58:00
+ */
+@Service
+public class AppUpdateCrudService implements CrudOps<AppUpdate> {
+    private AppUpdateRepository updateRepository;
+
+    public AppUpdateCrudService(AppUpdateRepository updateRepository) {
+        this.updateRepository = updateRepository;
+    }
+
+    @Override
+    public void addOrModify(AppUpdate appUpdate) throws Exception {
+        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);
+    }
+
+    @Override
+    public PageList<AppUpdate> getByPage(int page, int size) {
+        // 默认按更新时间倒序
+        PageRequest pageRequest =
+                PageRequest.of(page-1, size, Sort.by(Sort.Direction.DESC, "updateTime"));
+        Page<AppUpdate> appUpdates = updateRepository.findAll(pageRequest);
+
+        PageList<AppUpdate> pageList = new PageList<>();
+        pageList.setTotalSize(appUpdates.getTotalElements());
+        pageList.setTotalPages(appUpdates.getTotalPages());
+        pageList.setList(appUpdates.getContent().stream().map(AppUpdate::vo).collect(Collectors.toList()));
+        return pageList;
+    }
+
+    @Override
+    public void delete(String uniqueKey) throws Exception {
+        AppUpdate updateEntity = updateRepository.findByIsDeleteFalseAndRepoName(uniqueKey);
+        if (updateEntity == null) {
+            throw new Exception(uniqueKey + "不存在...");
+        }
+        updateEntity.setIsDelete(true);
+        updateRepository.save(updateEntity);
+    }
+}

+ 89 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/crud/orchestarte/AppCrudService.java

@@ -0,0 +1,89 @@
+package cn.reghao.autodop.dmaster.app.service.crud.orchestarte;
+
+import cn.reghao.autodop.common.utils.data.db.CrudOps;
+import cn.reghao.autodop.common.utils.data.db.PageList;
+import cn.reghao.autodop.dmaster.app.entity.build.AppBuild;
+import cn.reghao.autodop.dmaster.app.entity.orchestration.AppOrchestration;
+import cn.reghao.autodop.dmaster.app.entity.orchestration.ProjOrchestration;
+import cn.reghao.autodop.dmaster.app.repository.orchestration.AppOrchestrationRepository;
+import cn.reghao.autodop.dmaster.app.repository.orchestration.ProjOrchestrationRepository;
+import cn.reghao.autodop.dmaster.app.service.crud.SharedEntityChecker;
+import cn.reghao.autodop.dmaster.app.vo.orchestration.AppVO;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Sort;
+import org.springframework.stereotype.Service;
+
+import java.time.LocalDateTime;
+
+/**
+ * @author reghao
+ * @date 2020-11-10 21:58:00
+ */
+@Service
+public class AppCrudService implements CrudOps<AppOrchestration> {
+    private AppOrchestrationRepository appRepository;
+    private ProjOrchestrationRepository projRepository;
+    private SharedEntityChecker sharedEntityChecker;
+
+    public AppCrudService(AppOrchestrationRepository appRepository,
+                          ProjOrchestrationRepository projRepository,
+                          SharedEntityChecker sharedEntityChecker) {
+        this.appRepository = appRepository;
+        this.projRepository = projRepository;
+        this.sharedEntityChecker = sharedEntityChecker;
+    }
+
+    @Override
+    public void addOrModify(AppOrchestration app) throws Exception {
+        AppVO appVO = AppVO.from(app);
+        if (appVO.getProjId() == null) {
+            AppBuild appBuild1 = sharedEntityChecker.checkBuildConfig(appVO.getAppBuild());
+            app.setAppBuild(appBuild1);
+
+            String notifier = appVO.getNotifier();
+            sharedEntityChecker.checkNotification(app, notifier);
+        } else {
+            // TODO
+            ProjOrchestration entity = projRepository.findByIsDeleteFalseAndProjId(appVO.getProjId());
+            if (entity == null) {
+                throw new Exception("项目 " + appVO.getProjId() + " 不存在...");
+            }
+            app.setProj(entity);
+        }
+
+        AppOrchestration appEntity = appRepository.findByIsDeleteFalseAndAppId(app.getAppId());
+        if (appEntity != null) {
+            app.setId(appEntity.getId());
+            app.setCreateTime(appEntity.getCreateTime());
+            app.setUpdateTime(LocalDateTime.now());
+        }
+        app.setIsDelete(false);
+        appRepository.save(app);
+    }
+
+    @Override
+    public PageList<AppOrchestration> getByPage(int page, int size) {
+        // 默认按更新时间倒序
+        PageRequest pageRequest =
+                PageRequest.of(page-1, size, Sort.by(Sort.Direction.DESC, "updateTime"));
+        // TODO 指定额外的参数
+        Page<AppOrchestration> page1 = appRepository.findByIsDeleteFalseAndEnv("test", pageRequest);
+
+        PageList<AppOrchestration> pageList = new PageList<>();
+        pageList.setTotalSize(page1.getTotalElements());
+        pageList.setTotalPages(page1.getTotalPages());
+        pageList.setList(page1.getContent());
+        return pageList;
+    }
+
+    @Override
+    public void delete(String uniqueKey) throws Exception {
+        AppOrchestration appEntity = appRepository.findByIsDeleteFalseAndAppId(uniqueKey);
+        if (appEntity == null) {
+            throw new Exception(uniqueKey + " 不存在");
+        }
+        appEntity.setIsDelete(true);
+        appRepository.save(appEntity);
+    }
+}

+ 75 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/crud/orchestarte/ProjCrudService.java

@@ -0,0 +1,75 @@
+package cn.reghao.autodop.dmaster.app.service.crud.orchestarte;
+
+import cn.reghao.autodop.common.utils.data.db.CrudOps;
+import cn.reghao.autodop.common.utils.data.db.PageList;
+import cn.reghao.autodop.dmaster.app.entity.build.AppBuild;
+import cn.reghao.autodop.dmaster.app.entity.orchestration.ProjOrchestration;
+import cn.reghao.autodop.dmaster.app.repository.orchestration.AppOrchestrationRepository;
+import cn.reghao.autodop.dmaster.app.repository.orchestration.ProjOrchestrationRepository;
+import cn.reghao.autodop.dmaster.app.service.crud.SharedEntityChecker;
+import cn.reghao.autodop.dmaster.app.vo.orchestration.ProjVO;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Sort;
+import org.springframework.stereotype.Service;
+
+import java.time.LocalDateTime;
+
+/**
+ * @author reghao
+ * @date 2020-11-10 21:58:00
+ */
+@Service
+public class ProjCrudService implements CrudOps<ProjOrchestration> {
+    private ProjOrchestrationRepository projRepository;
+    private AppOrchestrationRepository appRepository;
+    private SharedEntityChecker sharedEntityChecker;
+
+    public ProjCrudService(ProjOrchestrationRepository projRepository,
+                           AppOrchestrationRepository appRepository,
+                          SharedEntityChecker sharedEntityChecker) {
+        this.projRepository = projRepository;
+        this.appRepository = appRepository;
+        this.sharedEntityChecker = sharedEntityChecker;
+    }
+
+    @Override
+    public void addOrModify(ProjOrchestration proj) throws Exception {
+        ProjVO projVO = ProjVO.from(proj);
+        AppBuild appBuild = sharedEntityChecker.checkBuildConfig(projVO.getAppBuild());
+        proj.setAppBuild(appBuild);
+
+        ProjOrchestration projEntity = projRepository.findByIsDeleteFalseAndProjId(proj.getProjId());
+        if (projEntity != null) {
+            proj.setId(projEntity.getId());
+            proj.setCreateTime(projEntity.getCreateTime());
+            proj.setUpdateTime(LocalDateTime.now());
+        }
+        proj.setIsDelete(false);
+        projRepository.save(proj);
+    }
+
+    @Override
+    public PageList<ProjOrchestration> getByPage(int page, int size) {
+        // 默认按更新时间倒序
+        PageRequest pageRequest =
+                PageRequest.of(page-1, size, Sort.by(Sort.Direction.DESC, "updateTime"));
+        Page<ProjOrchestration> page1 = projRepository.findAll(pageRequest);
+
+        PageList<ProjOrchestration> pageList = new PageList<>();
+        pageList.setTotalSize(page1.getTotalElements());
+        pageList.setTotalPages(page1.getTotalPages());
+        pageList.setList(page1.getContent());
+        return pageList;
+    }
+
+    @Override
+    public void delete(String uniqueKey) throws Exception {
+        ProjOrchestration projEntity = projRepository.findByIsDeleteFalseAndProjId(uniqueKey);
+        if (projEntity == null) {
+            throw new Exception(uniqueKey + " 不存在");
+        }
+        projEntity.setIsDelete(true);
+        projRepository.save(projEntity);
+    }
+}

+ 0 - 1
dmaster/src/main/java/cn/reghao/autodop/dmaster/host/service/NodeService.java

@@ -8,5 +8,4 @@ import org.springframework.stereotype.Service;
  */
 @Service
 public class NodeService {
-
 }

+ 6 - 3
dmaster/src/main/resources/application-prod.yml

@@ -3,9 +3,12 @@ spring:
     url: jdbc:mysql://192.168.0.220:3306/autodop_tdb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
     username: azy_test
     password: Test@123456
+  data:
+    mongodb:
+      uri: mongodb://192.168.0.220/log
   rabbitmq:
-    host: mq.reghao.cn
+    host: mq.srv.iquizoo.com
     port: 5672
-    username: reghao
-    password: gL2dOiWYqXiKKsv5LOt9
+    username: iquizoo-mq1
+    password: z2pT1PXR
     virtual-host: /

+ 1 - 1
dmaster/src/main/resources/application-test.yml

@@ -5,7 +5,7 @@ spring:
     password: Test@123456
   data:
     mongodb:
-      uri: mongodb://192.168.0.222/log
+      uri: mongodb://192.168.0.220/log
   rabbitmq:
     host: mq.srv.iquizoo.com
     port: 5672