Prechádzať zdrojové kódy

数据模型调整完成

reghao 5 rokov pred
rodič
commit
518a90aef4

+ 83 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/caching/OrchestrationCaching.java

@@ -0,0 +1,83 @@
+package cn.reghao.autodop.dmaster.app.caching;
+
+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.AppDAO;
+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.AppOrchestrationRepository;
+import cn.reghao.autodop.dmaster.app.repository.orchestration.NotificationRepository;
+import cn.reghao.autodop.dmaster.app.repository.orchestration.ProjectOrchestrationRepository;
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.cache.annotation.CachePut;
+import org.springframework.cache.annotation.Cacheable;
+import org.springframework.stereotype.Service;
+
+/**
+ * @author reghao
+ * @date 2020-03-06 15:17:33
+ */
+@Service
+public class OrchestrationCaching {
+    private ProjectOrchestrationRepository projRepository;
+    private AppOrchestrationRepository appRepository;
+    private AppUpdateRepository appUpdateRepository;
+    private AppCompileRepository appCompileRepository;
+    private AppPackRepository appPackRepository;
+    private NotificationRepository notificationRepository;
+    private AppDAO appDAO;
+
+    public OrchestrationCaching(ProjectOrchestrationRepository projRepository,
+                              AppOrchestrationRepository appRepository,
+                              AppUpdateRepository appUpdateRepository,
+                              AppCompileRepository appCompileRepository,
+                              AppPackRepository appPackRepository,
+                              NotificationRepository notificationRepository,
+                              AppDAO appDAO) {
+        this.projRepository = projRepository;
+        this.appRepository = appRepository;
+        this.appUpdateRepository = appUpdateRepository;
+        this.appCompileRepository = appCompileRepository;
+        this.appPackRepository = appPackRepository;
+        this.notificationRepository = notificationRepository;
+        this.appDAO = appDAO;
+    }
+
+    @Cacheable(cacheNames = "proj", key = "#proj.projId")
+    public void addProj(ProjOrchestration proj) {
+    }
+
+    @Cacheable(cacheNames = "app", key = "#app.appId")
+    public void addApp(AppOrchestration app) {
+    }
+
+    @Cacheable(cacheNames = "proj", key = "#projId")
+    public ProjOrchestration findByProjId(String projId) {
+        // TODO 数据库中没有数据时不缓存
+        ProjOrchestration proj = projRepository.findByProjId(projId);
+        return proj;
+    }
+
+    @Cacheable(cacheNames = "app", key = "#appId")
+    public AppOrchestration findByAppId(String appId) {
+        AppOrchestration app = appRepository.findByAppId(appId);
+        return app;
+    }
+
+    @CachePut(cacheNames = "proj", key = "#proj.projId")
+    public void modifyProj(ProjOrchestration proj) {
+    }
+
+    @CachePut(cacheNames = "app", key = "#app.appId")
+    public void modifyApp(AppOrchestration app) {
+    }
+
+    @CacheEvict(cacheNames = "proj", key = "#projId")
+    public void deleteProj(String projId) {
+    }
+
+    @CacheEvict(cacheNames = "app", key = "#appId")
+    public void deleteApp(String appId) {
+    }
+}

+ 15 - 2
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/controller/OrchestrateController.java

@@ -43,9 +43,22 @@ public class OrchestrateController {
     public String getOrchestrationByPage(@PathVariable("type") int type,
                                          @RequestParam("page") int page,
                                          @RequestParam("size") int size) {
-
-        //Pagination pagination = new Pagination(page, size);
         PageRequest pageRequest = PageRequest.of(page-1, size);
         return orchestrateService.getByPage(type, pageRequest);
     }
+
+    @ApiOperation(value = "修改项目/应用编排")
+    @PutMapping("/{type}")
+    public String modifyOrchestration(@PathVariable("type") int type, @RequestBody String json) throws Exception {
+        orchestrateService.modify(type, json);
+        return new WebResult<String>(ResultCode.SUCCESS).jsonResult("ok");
+    }
+
+    @ApiOperation(value = "删除项目/应用编排")
+    @DeleteMapping("/{type}/{appId}")
+    public String deleteOrchestration(@PathVariable("type") int type,
+                                      @PathVariable("appId") String appId) {
+        orchestrateService.delete(type, appId);
+        return new WebResult<String>(ResultCode.SUCCESS).jsonResult("ok");
+    }
 }

+ 2 - 1
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/entity/BaseEntity.java

@@ -13,7 +13,8 @@ import java.util.Date;
  * @date 2019-10-18 14:42:48
  */
 @Entity
-@Inheritance(strategy = InheritanceType.JOINED)
+// 每个实体类一张表
+@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
 @Table
 @Data
 @NoArgsConstructor

+ 1 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/entity/orchestration/AppOrchestration.java

@@ -30,6 +30,7 @@ public class AppOrchestration extends BaseEntity {
     private Map<String, String> configFiles;
     private String appRepo;
     @Column(length = 1024)
+    // TODO 使用 List
     private String dependencyRepos;
 
     // projId 和 appBuild 二者只能存在一个

+ 36 - 5
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/OrchestrateService.java

@@ -128,23 +128,40 @@ public class OrchestrateService {
 
     public void batchAdd(int type, String json) throws Exception {
         // TODO 优化代码
+        AppBuild appBuild;
         switch (type) {
             case 1:
                 List<Object> objs = JsonUtil.jsonToObjectArray(json, ProjOrchestration.class);
+                List<ProjOrchestration> projs = new ArrayList<>();
                 for (Object obj : objs) {
                     ProjOrchestration proj = (ProjOrchestration) obj;
-                    //checkSharedEntity(proj);
-                    projRepository.save(proj);
+                    appBuild = proj.getAppBuild();
+                    checkSharedEntity(appBuild);
+                    projs.add(proj);
                 }
+                projRepository.saveAll(projs);
                 break;
             case 2:
                 List<Object> objs1 = JsonUtil.jsonToObjectArray(json, AppOrchestration.class);
+                List<AppOrchestration> apps = new ArrayList<>();
                 for (Object obj : objs1) {
                     AppOrchestration app = (AppOrchestration) obj;
-                    //checkSharedEntity(app);
-                    // TODO 使用 saveAll 时若多个编排中具有相同的 UNIQUE 约束值会引发 UNIQUE 约束异常
-                    appRepository.save(app);
+                    String projId = app.getProjId();
+                    if (projId == null) {
+                        appBuild = app.getAppBuild();
+                        checkSharedEntity(appBuild);
+                    }
+                    Notification notification = app.getNotification();
+                    Notification notifyEntity = notificationRepository.findByNotifyName(notification.getNotifyName());
+                    if (notifyEntity != null) {
+                        app.setNotification(notifyEntity);
+                    } else {
+                        notificationRepository.save(notification);
+                    }
+                    apps.add(app);
                 }
+                // TODO 使用 saveAll 时若多个编排中具有相同的 UNIQUE 约束值会引发 UNIQUE 约束异常
+                appRepository.saveAll(apps);
             default:
         }
     }
@@ -182,4 +199,18 @@ public class OrchestrateService {
                 return null;
         }
     }
+
+    public void modify(int type, String json) {
+    }
+
+    public void delete(int type, String appId) {
+        switch (type) {
+            case 1:
+                projRepository.deleteByProjId(appId);
+                break;
+            case 2:
+                appRepository.deleteByAppId(appId);
+            default:
+        }
+    }
 }

+ 34 - 43
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/build/BuildDispatcher.java

@@ -2,16 +2,21 @@ package cn.reghao.autodop.dmaster.app.service.build;
 
 import cn.reghao.autodop.common.config.BuildDeployResult;
 import cn.reghao.autodop.common.utils.JsonUtil;
+import cn.reghao.autodop.dmaster.app.caching.OrchestrationCaching;
 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.thread.ThreadPoolWrapper;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 
 import java.util.*;
 import java.util.concurrent.*;
+import java.util.stream.Collectors;
 
 /**
+ * 应用构建分发
+ *
  * @author reghao
  * @date 2019-11-12 17:20:27
  */
@@ -19,11 +24,11 @@ import java.util.concurrent.*;
 @Service
 public class BuildDispatcher {
     private final String STANDALONE_APP = "standaloneApp";
-    /*private OrchestrationCaching caching;
+    private OrchestrationCaching caching;
 
     public BuildDispatcher(OrchestrationCaching caching) {
         this.caching = caching;
-    }*/
+    }
 
     /**
      * 根据项目分发应用
@@ -38,15 +43,14 @@ public class BuildDispatcher {
         // 分离出存在的应用和不存在的应用
         if (appIds.remove("all")) {
             // 处理所有需编译的应用
-            // existApps.addAll(config.getApps().values());
         } else {
             appIds.forEach(appId -> {
-                /*AppOrchestration app = caching.findAppByIdentifier(appId);
+                AppOrchestration app = caching.findByAppId(appId);
                 if (app != null) {
                     existApps.add(app);
                 } else {
                     results.add(new BuildDeployResult(appId, "初始化", 0, "应用不存在"));
-                }*/
+                }
             });
         }
 
@@ -60,8 +64,8 @@ public class BuildDispatcher {
             List<AppOrchestration> apps = entry.getValue();
             if (!STANDALONE_APP.equalsIgnoreCase(key)) {
                 // 首先更新整个项目,然后再分别更新,编译,打包应用
-                //ProjOrchestration proj = caching.findProjByIdentifier(key);
-                //projFutures.add(threadPool.submit(new ProjectUpdateTask(proj, apps)));
+                ProjOrchestration proj = caching.findByProjId(key);
+                projFutures.add(threadPool.submit(new ProjectUpdateTask(proj, apps)));
             } else {
                 // 更新,编译,打包独立的应用
                 apps.forEach(app -> {
@@ -84,11 +88,11 @@ public class BuildDispatcher {
                 List<ProjectUpdateTask.AppVersion> appVersions = future.get();
                 appVersions.forEach(appVersion -> {
                     if (appVersion.isUpdated()) {
-                        /*appFutures.add(threadPool.submit(
-                                new AppBuildPipeline(appVersion.getApp(), appVersion.getVersion(), isDeploy)));*/
+                        appFutures.add(threadPool.submit(
+                                new AppBuildPipeline(appVersion.getApp(), appVersion.getVersion(), isDeploy)));
                     } else {
                         BuildDeployResult buildDeployResult = new BuildDeployResult();
-                        //buildDeployResult.setAppId(appVersion.getApp());
+                        buildDeployResult.setAppId(appVersion.getApp().getAppId());
                         buildDeployResult.setVersion(appVersion.getVersion());
                         buildDeployResult.setMsg("应用不需要更新");
                         results.add(buildDeployResult);
@@ -125,16 +129,14 @@ public class BuildDispatcher {
     private Map<String, List<AppOrchestration>> filterByProj(List<AppOrchestration> apps) {
         Map<String, List<AppOrchestration>> appMap = new HashMap<>();
         for (AppOrchestration app : apps) {
-            String mapkey = null;
-            /*if (app.getProjBuild() != null) {
-                mapkey = app.getProjBuild().getProjId();
-                ProjOrchestration proj = caching.findProjByIdentifier(mapkey);
-                AppBuild projBuild = proj.getBuild();
-                fillAppBuild(projBuild, app);
+            String mapkey = app.getProjId();
+            if (mapkey != null) {
+                ProjOrchestration proj = caching.findByProjId(mapkey);
+                fillAppBuild(proj, app);
             } else {
                 // 单独的应用,不隶属于任何项目
                 mapkey = STANDALONE_APP;
-            }*/
+            }
 
             if (appMap.containsKey(mapkey)) {
                 appMap.get(mapkey).add(app);
@@ -157,31 +159,20 @@ public class BuildDispatcher {
      * @return
      * @date 2020-03-09 下午4:39
      */
-    private void fillAppBuild(AppBuild build, AppOrchestration app) {
-        //ProjBuild projBuild = app.getProjBuild();
-
-        AppBuild appBuild = new AppBuild();
-        /*appBuild.setNotifier(build.getNotifier());
-        appBuild.setCodeCompiler(build.getCodeCompiler());
-        appBuild.setAppPacker(build.getAppPacker());*/
-
-        /*String projReposUrl = build.getCodeRepos().getReposUrl();
-        String appReposUrl = projReposUrl + projBuild.getAppReposUrl();
-
-        CodeRepos codeRepos = new CodeRepos();
-        codeRepos.setReposUrl(appReposUrl);
-        if (projBuild.getAppDependencyUrls() != null) {
-            String[] urls = projBuild.getAppDependencyUrls().split(",");
-            StringBuilder sb = new StringBuilder();
-            for (String str : urls) {
-                sb.append(projReposUrl).append(str).append(",");
-            }
-            String depUrl = sb.toString();
-            int index = depUrl.lastIndexOf(",");
-            codeRepos.setDependencyUrls(depUrl.substring(0, index));
-        }
-
-        appBuild.setCodeRepos(codeRepos);
-        app.setBuild(appBuild);*/
+    private void fillAppBuild(ProjOrchestration proj, AppOrchestration app) {
+        String projRepo = proj.getProjRepo();
+        AppBuild appBuild = proj.getAppBuild();
+
+        String appRepo = app.getAppRepo();
+        app.setAppRepo(projRepo + appRepo);
+        StringBuilder sb = new StringBuilder();
+        List<String> repos = Arrays.stream(app.getDependencyRepos().split(","))
+                .map(repo -> projRepo + repo)
+                .collect(Collectors.toList());
+        repos.forEach(repo -> {
+            sb.append(repo).append(",");
+        });
+        app.setDependencyRepos(sb.toString());
+        app.setAppBuild(appBuild);
     }
 }

+ 14 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/common/config/SysConfig.java

@@ -0,0 +1,14 @@
+package cn.reghao.autodop.dmaster.common.config;
+
+/**
+ * @author reghao
+ * @date 2020-03-09 14:36:41
+ */
+public class SysConfig {
+    // 本地仓库
+    public static String localRepos;
+    // 编译目录
+    public static String compileDir;
+    // 应用打包后存放的目录
+    public static String packDir;
+}

+ 0 - 21
scripts/app-in-proj.json

@@ -1,21 +0,0 @@
-{
-  "appId":"content",
-  "dirname":"ContentService",
-  "description":"内容服务",
-  "appRepo":"/ContentService",
-  "dependencyRepos":"/DataCenter,/ContentService,/UserService,/DotNetCore",
-  "env":"dev",
-  "entryDir":"/ContentService/4-ContentPresentation/Com.IQuizoo.ContentService",
-  "projId":"branch-20200509",
-  "appDeploy":{
-    "hosts":"localhost",
-    "httpPort":8002,
-    "healthCheck":"/health",
-    "runningDir":""
-  },
-  "notification":{
-    "notifyName":"ding 通知",
-    "notifyType":"ding",
-    "destination":"https://oapi.dingtalk.com/robot/send?access_token=ba9cf0d846cff8c471168e0d2f91ec0c44645a086cf5e4e421697c9b0c606bd2"
-  }
-}

+ 0 - 39
scripts/app.json

@@ -1,39 +0,0 @@
-{
-  "appId":"content",
-  "dirname":"ContentService",
-  "description":"内容服务",
-  "env":"dev",
-  "entryDir":"/ContentService/4-ContentPresentation/Com.IQuizoo.ContentService",
-  "appRepo":"svn://192.168.0.214/azy/CodeRepositories/IQuizoo.Service/branches/20200509/ContentService",
-  "dependencyRepos":"svn://192.168.0.214/azy/CodeRepositories/IQuizoo.Service/branches/20200509/DataCenter,svn://192.168.0.214/azy/CodeRepositories/IQuizoo.Service/branches/20200509/ContentService,svn://192.168.0.214/azy/CodeRepositories/IQuizoo.Service/branches/20200509/UserService,svn://192.168.0.214/azy/CodeRepositories/IQuizoo.Service/branches/20200509/DotNetCore",
-  "appBuild":{
-    "appUpdate":{
-      "repoName":"svn-214",
-      "repoType":"svn",
-      "username":"gjs",
-      "password":"gjs1234"
-    },
-    "appCompile":{
-      "compilerName":"dotnet2.2",
-      "compilerType":"dotnet",
-      "compilerHome":"/home/reghao/opt/env/dotnet/dotnet-sdk-2.2.101",
-      "script":"dotnet publish"
-    },
-    "appPack":{
-      "packerName":"docker",
-      "packerType":"docker",
-      "packerPath":"192.168.0.222:5000/iq3x"
-    }
-  },
-  "appDeploy":{
-    "hosts":"localhost",
-    "httpPort":8002,
-    "healthCheck":"/health",
-    "runningDir":""
-  },
-  "notification":{
-    "notifyName":"ding 通知",
-    "notifyType":"ding",
-    "destination":"https://oapi.dingtalk.com/robot/send?access_token=ba9cf0d846cff8c471168e0d2f91ec0c44645a086cf5e4e421697c9b0c606bd2"
-  }
-}

+ 0 - 24
scripts/proj.json

@@ -1,24 +0,0 @@
-{
-  "projId":"branch-20200509",
-  "description":"20200509 分支",
-  "projRepo":"svn://192.168.0.214/azy/CodeRepositories/IQuizoo.Service/branches/20200509",
-  "appBuild":{
-    "appUpdate":{
-      "repoName":"svn-214",
-      "repoType":"svn",
-      "username":"gjs",
-      "password":"gjs1234"
-    },
-    "appCompile":{
-      "compilerName":"dotnet2.2",
-      "compilerType":"dotnet",
-      "compilerHome":"/home/reghao/opt/env/dotnet/dotnet-sdk-2.2.101",
-      "script":"dotnet publish"
-    },
-    "appPack":{
-      "packerName":"docker",
-      "packerType":"docker",
-      "packerPath":"192.168.0.222:5000/iq3x"
-    }
-  }
-}