Преглед изворни кода

调整需要构建部署的应用列表接口

reghao пре 5 година
родитељ
комит
8a72535911

+ 0 - 3
common/src/main/java/cn/reghao/autodop/common/dagent/machine/api/data/MachineRegistry.java

@@ -10,14 +10,11 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import org.hibernate.annotations.LazyCollection;
 import org.hibernate.annotations.LazyCollectionOption;
-import sun.nio.ch.Net;
 
 import javax.persistence.Column;
 import javax.persistence.ElementCollection;
 import javax.persistence.Entity;
-import javax.persistence.FetchType;
 import java.util.List;
-import java.util.Set;
 
 /**
  * @author reghao

+ 3 - 2
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/controller/crud/OrchestrateCrudController.java

@@ -54,8 +54,9 @@ public class OrchestrateCrudController {
     @ApiOperation(value = "分页获取应用编排")
     @GetMapping("/app")
     public ResponseEntity<String> getAppOrchestrationByPage(@RequestParam("page") int page,
-                                                            @RequestParam("size") int size) {
-        PageList<AppOrchestration> pageList = appCrudService.getByPage(page, size);
+                                                            @RequestParam("size") int size,
+                                                            @RequestParam("env") String env) {
+        PageList<AppOrchestration> pageList = appCrudService.getByPage(page, size, env);
         PageList<AppVO> vos = new PageList<>();
         vos.setTotalPages(pageList.getTotalPages());
         vos.setTotalSize(pageList.getTotalSize());

+ 19 - 18
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/entity/log/BuildDeployApp.java

@@ -4,7 +4,6 @@ import cn.reghao.autodop.common.orm.BaseEntity;
 import cn.reghao.autodop.dmaster.app.entity.config.orchestration.AppOrchestration;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
-import lombok.NoArgsConstructor;
 
 import javax.persistence.*;
 import java.time.LocalDateTime;
@@ -12,19 +11,21 @@ import java.util.List;
 import java.util.stream.Collectors;
 
 /**
- * 需要构建部署的应用
+ * 需要构建部署的应用,是 AppOrchestration 的一个镜像,跟随 AppOrchestration 的变化而改变
  *
  * @author reghao
  * @date 2020-05-20 22:09:53
  */
-@NoArgsConstructor
 @Data
 @EqualsAndHashCode(callSuper = false)
 @Entity
 public class BuildDeployApp extends BaseEntity {
-    @OneToOne(cascade = CascadeType.DETACH)
-    @JoinColumn(name = "app_orchestration_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
-    private AppOrchestration app;
+    @Column(nullable = false, unique = true)
+    private String appId;
+    @Column(nullable = false)
+    private String env;
+    @Column(nullable = false)
+    private boolean enable;
     private String commitId;
     private String bdType;
     private LocalDateTime bdTime;
@@ -32,29 +33,27 @@ public class BuildDeployApp extends BaseEntity {
     private int statusCode;
     private String result;
 
-    public BuildDeployApp(AppOrchestration app) {
-        this.app = app;
-    }
-
-    /*public static BuildDeployApp of(AppOrchestration app) {
+    public static BuildDeployApp of(AppOrchestration app) {
         BuildDeployApp buildDeployApp = new BuildDeployApp();
         buildDeployApp.setAppId(app.getAppId());
         buildDeployApp.setEnv(app.getEnv());
         buildDeployApp.setEnable(app.isEnable());
         return buildDeployApp;
-    }*/
+    }
 
-    public static BuildDeployApp of(AppOrchestration app, BuildLog buildLog) {
+    public static BuildDeployApp of(BuildLog buildLog) {
         BuildDeployApp buildDeployApp = new BuildDeployApp();
-        buildDeployApp.setApp(app);
+        buildDeployApp.setAppId(buildLog.getAppId());
+        buildDeployApp.setEnv(buildLog.getEnv());
+        buildDeployApp.setEnable(true);
         buildDeployApp.setCommitId(buildLog.getCommitId());
         buildDeployApp.setBdType("构建");
         buildDeployApp.setBdTime(buildLog.getBuildTime());
         buildDeployApp.setStatusCode(buildLog.getStatusCode());
         if (buildLog.getStatusCode() != 0) {
             String errDetail = buildLog.getErrDetail();
-            if (errDetail.length() > 500) {
-                buildDeployApp.setResult(errDetail.substring(0, 500));
+            if (errDetail.length() > 1000) {
+                buildDeployApp.setResult(errDetail.substring(0, 1000));
             } else {
                 buildDeployApp.setResult(errDetail);
             }
@@ -65,9 +64,11 @@ public class BuildDeployApp extends BaseEntity {
         return buildDeployApp;
     }
 
-    public static BuildDeployApp of(AppOrchestration app, DeployResult deployResult) {
+    public static BuildDeployApp of(DeployResult deployResult) {
         BuildDeployApp buildDeployApp = new BuildDeployApp();
-        buildDeployApp.setApp(app);
+        buildDeployApp.setAppId(deployResult.getAppId());
+        buildDeployApp.setEnv(deployResult.getEnv());
+        buildDeployApp.setEnable(true);
         buildDeployApp.setCommitId(deployResult.getCommitId());
         buildDeployApp.setBdType("部署");
         buildDeployApp.setBdTime(deployResult.getDeployTime());

+ 5 - 2
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/repository/log/BuildDeployAppRepository.java

@@ -1,7 +1,8 @@
 package cn.reghao.autodop.dmaster.app.repository.log;
 
-import cn.reghao.autodop.dmaster.app.entity.config.orchestration.AppOrchestration;
 import cn.reghao.autodop.dmaster.app.entity.log.BuildDeployApp;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
 import org.springframework.data.jpa.repository.JpaRepository;
 
 /**
@@ -9,5 +10,7 @@ import org.springframework.data.jpa.repository.JpaRepository;
  * @date 2020-01-21 14:53:03
  */
 public interface BuildDeployAppRepository extends JpaRepository<BuildDeployApp, Long> {
-    BuildDeployApp findByApp(AppOrchestration app);
+    BuildDeployApp findByAppId(String appId);
+    BuildDeployApp findByIsDeleteFalseAndAppId(String appId);
+    Page<BuildDeployApp> findByIsDeleteFalseAndEnableIsTrueAndEnv(String env, Pageable pageable);
 }

+ 3 - 9
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/BuildDeployDispatcher.java

@@ -79,13 +79,9 @@ public class BuildDeployDispatcher {
         if (buildLog.getStatusCode() == 0 && isDeploy) {
             DeployResult deployResult = deployApp(buildLog);
             assert deployResult != null;
-            BuildDeployApp buildDeployApp = BuildDeployApp.of(appIntegrate.app(), deployResult);
-            buildDeployLogConsumer.addBuildDeployApp(buildDeployApp);
-            return buildDeployApp;
+            return BuildDeployApp.of(deployResult);
         } else {
-            BuildDeployApp buildDeployApp = BuildDeployApp.of(appIntegrate.app(), buildLog);
-            buildDeployLogConsumer.addBuildDeployApp(buildDeployApp);
-            return buildDeployApp;
+            return BuildDeployApp.of(buildLog);
         }
     }
 
@@ -173,9 +169,7 @@ public class BuildDeployDispatcher {
         BuildLog buildLog = cache.findByAppIdAndCommitId(appId, commitId);
         DeployResult deployResult = deployApp(buildLog);
         assert deployResult != null;
-        BuildDeployApp buildDeployApp = BuildDeployApp.of(cache.findByAppId(appId), deployResult);
-        buildDeployLogConsumer.addBuildDeployApp(buildDeployApp);
-        return buildDeployApp;
+        return BuildDeployApp.of(deployResult);
     }
 
     public void deployNotify(NotifierConfig notifierConfig, DeployLog deployLog) {

+ 17 - 41
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/crud/BuildDeployAppCrudService.java

@@ -2,20 +2,14 @@ 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.config.orchestration.AppOrchestration;
 import cn.reghao.autodop.dmaster.app.entity.log.BuildDeployApp;
 import cn.reghao.autodop.dmaster.app.repository.log.BuildDeployAppRepository;
-import cn.reghao.autodop.dmaster.app.repository.orchestration.AppOrchestrationRepository;
 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.HashSet;
-import java.util.List;
-import java.util.Set;
-import java.util.stream.Collectors;
 
 /**
  * @author reghao
@@ -23,18 +17,15 @@ import java.util.stream.Collectors;
  */
 @Service
 public class BuildDeployAppCrudService implements CrudOps<BuildDeployApp> {
-    private AppOrchestrationRepository appRepository;
     private BuildDeployAppRepository buildDeployAppRepository;
 
-    public BuildDeployAppCrudService(AppOrchestrationRepository appRepository,
-                                     BuildDeployAppRepository buildDeployAppRepository) {
-        this.appRepository = appRepository;
+    public BuildDeployAppCrudService(BuildDeployAppRepository buildDeployAppRepository) {
         this.buildDeployAppRepository = buildDeployAppRepository;
     }
 
     @Override
     public void addOrUpdate(BuildDeployApp buildDeployApp) throws Exception {
-        BuildDeployApp entity = buildDeployAppRepository.findByApp(buildDeployApp.getApp());
+        BuildDeployApp entity = buildDeployAppRepository.findByAppId(buildDeployApp.getAppId());
         if (entity != null) {
             buildDeployApp.setId(entity.getId());
             buildDeployApp.setCreateTime(entity.getCreateTime());
@@ -46,41 +37,26 @@ public class BuildDeployAppCrudService implements CrudOps<BuildDeployApp> {
 
     @Override
     public PageList<BuildDeployApp> getByPage(int page, int size, String env) {
-        PageRequest pageRequest = PageRequest.of(page-1, size, Sort.by(Sort.Direction.DESC, "bdTime"));
-        Page<BuildDeployApp> buildDeployApps = buildDeployAppRepository.findAll(pageRequest);
-
+        // 默认按更新时间倒序
+        PageRequest pageRequest =
+                PageRequest.of(page-1, size, Sort.by(Sort.Direction.DESC, "updateTime"));
+        Page<BuildDeployApp> page1 =
+                buildDeployAppRepository.findByIsDeleteFalseAndEnableIsTrueAndEnv(env, pageRequest);
         PageList<BuildDeployApp> pageList = new PageList<>();
-        int minus = size - buildDeployApps.getContent().size();
-        if (minus > 0) {
-            PageRequest pageRequest1 =
-                    PageRequest.of(page-1, size, Sort.by(Sort.Direction.DESC, "updateTime"));
-            Page<AppOrchestration> apps = appRepository.findByIsDeleteFalseAndEnableIsTrueAndEnv(env, pageRequest1);
-            if (minus == size) {
-                pageList.setTotalSize(apps.getTotalElements());
-                pageList.setTotalPages(apps.getTotalPages());
-                pageList.setList(apps.getContent().stream().map(BuildDeployApp::new).collect(Collectors.toList()));
-            } else {
-                Set<BuildDeployApp> set = new HashSet<>(buildDeployApps.getContent());
-                List<BuildDeployApp> list = apps.getContent().stream()
-                        .map(BuildDeployApp::new).collect(Collectors.toList());
-
-                for (BuildDeployApp buildDeployApp : list) {
-                    if (!set.contains(buildDeployApp) && minus != 0) {
-
-                        minus--;
-                    }
-                }
-            }
-        } else {
-            pageList.setTotalSize(buildDeployApps.getTotalElements());
-            pageList.setTotalPages(buildDeployApps.getTotalPages());
-            pageList.setList(buildDeployApps.getContent());
-        }
-
+        pageList.setTotalSize(page1.getTotalElements());
+        pageList.setTotalPages(page1.getTotalPages());
+        pageList.setList(page1.getContent());
         return pageList;
     }
 
     @Override
     public void delete(String uniqueKey) throws Exception {
+        BuildDeployApp entity = buildDeployAppRepository.findByIsDeleteFalseAndAppId(uniqueKey);
+        if (entity == null) {
+            throw new Exception(uniqueKey + " 不存在");
+        } else {
+            entity.setIsDelete(true);
+            buildDeployAppRepository.save(entity);
+        }
     }
 }

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

@@ -50,7 +50,7 @@ public class AppCrudService implements CrudOps<AppOrchestration> {
         }
         app.setIsDelete(false);
         appRepository.save(app);
-        //buildDeployAppCrudService.addOrUpdate(BuildDeployApp.of(app));
+        buildDeployAppCrudService.addOrUpdate(BuildDeployApp.of(app));
     }
 
     public void copy(String from, String to) throws Exception {
@@ -64,7 +64,7 @@ public class AppCrudService implements CrudOps<AppOrchestration> {
             toApp = (AppOrchestration) fromApp.clone();
             copyToDifferentApp(toApp, to);
             appRepository.save(toApp);
-            //buildDeployAppCrudService.addOrUpdate(BuildDeployApp.of(toApp));
+            buildDeployAppCrudService.addOrUpdate(BuildDeployApp.of(toApp));
         }
     }
 
@@ -91,12 +91,12 @@ public class AppCrudService implements CrudOps<AppOrchestration> {
     }
 
     @Override
-    public PageList<AppOrchestration> getByPage(int page, int size) {
+    public PageList<AppOrchestration> getByPage(int page, int size, String env) {
         // 默认按更新时间倒序
         PageRequest pageRequest =
                 PageRequest.of(page-1, size, Sort.by(Sort.Direction.DESC, "updateTime"));
         // TODO 指定额外的参数
-        Page<AppOrchestration> page1 = appRepository.findByIsDeleteFalseAndEnv("dev", pageRequest);
+        Page<AppOrchestration> page1 = appRepository.findByIsDeleteFalseAndEnv(env, pageRequest);
         PageList<AppOrchestration> pageList = new PageList<>();
         pageList.setTotalSize(page1.getTotalElements());
         pageList.setTotalPages(page1.getTotalPages());

+ 4 - 9
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/log/BuildDeployLogConsumer.java

@@ -1,6 +1,9 @@
 package cn.reghao.autodop.dmaster.app.service.log;
 
-import cn.reghao.autodop.dmaster.app.entity.log.*;
+import cn.reghao.autodop.dmaster.app.entity.log.BuildLog;
+import cn.reghao.autodop.dmaster.app.entity.log.DeployLog;
+import cn.reghao.autodop.dmaster.app.entity.log.CommitLog;
+import cn.reghao.autodop.dmaster.app.entity.log.DeployResult;
 import lombok.extern.slf4j.Slf4j;
 
 import java.util.concurrent.BlockingQueue;
@@ -33,10 +36,6 @@ public class BuildDeployLogConsumer implements Runnable {
         logQueue.add(deployResult);
     }
 
-    public void addBuildDeployApp(BuildDeployApp buildDeployApp) {
-        logQueue.add(buildDeployApp);
-    }
-
     @Override
     public void run() {
         log.info("日志持久化线程已启动...");
@@ -55,10 +54,6 @@ public class BuildDeployLogConsumer implements Runnable {
                     DeployResult deployResult = (DeployResult) object;
                     logService.saveDeployLog(deployResult);
                     log.info("持久化 {} 部署日志完成...", deployResult.getAppId());
-                } else if (object instanceof BuildDeployApp) {
-                    BuildDeployApp buildDeployApp = (BuildDeployApp) object;
-                    logService.saveBuildDeployApp(buildDeployApp);
-                    log.info("持久化 {} 的构建部署记录完成...", buildDeployApp.getApp().getAppId());
                 }
             } catch (InterruptedException e) {
                 // 中断线程

+ 2 - 5
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/log/BuildDeployLogService.java

@@ -41,15 +41,12 @@ public class BuildDeployLogService {
     @Transactional(rollbackFor = {Exception.class})
     public void saveBuildLog(BuildLog buildLog) throws Exception {
         buildLogRepository.save(buildLog);
+        buildDeployAppCrudService.addOrUpdate(BuildDeployApp.of(buildLog));
     }
 
     @Transactional(rollbackFor = {Exception.class})
     public void saveDeployLog(DeployResult deployResult) throws Exception {
         deployResult.getDeployLogs().forEach(deployLog -> deployLogRepository.save(deployLog));
-    }
-
-    @Transactional(rollbackFor = {Exception.class})
-    public void saveBuildDeployApp(BuildDeployApp buildDeployApp) throws Exception {
-        buildDeployAppCrudService.addOrUpdate(buildDeployApp);
+        buildDeployAppCrudService.addOrUpdate(BuildDeployApp.of(deployResult));
     }
 }

+ 2 - 3
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/vo/log/BuildDeployAppVO.java

@@ -21,9 +21,8 @@ public class BuildDeployAppVO {
 
     public static BuildDeployAppVO of(BuildDeployApp buildDeployApp) {
         BuildDeployAppVO vo = new BuildDeployAppVO();
-        vo.setAppId(buildDeployApp.getApp().getAppId());
-        vo.setEnv(buildDeployApp.getApp().getEnv());
-
+        vo.setAppId(buildDeployApp.getAppId());
+        vo.setEnv(buildDeployApp.getEnv());
         vo.setCommitId(buildDeployApp.getCommitId());
         vo.setBdType(buildDeployApp.getBdType());
         if (buildDeployApp.getBdTime() != null) {