Browse Source

继续优化构建部署流程

reghao 5 years ago
parent
commit
fc0454bbb1
32 changed files with 305 additions and 512 deletions
  1. 0 26
      common/pom.xml
  2. 0 1
      common/src/main/java/cn/reghao/autodop/common/deploy/DeployConfig.java
  3. 0 26
      dagent/pom.xml
  4. 1 1
      dagent/src/main/java/cn/reghao/autodop/dagent/app/deploy/AppDeployService.java
  5. 1 48
      dmaster/pom.xml
  6. 22 0
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/constant/AppStage.java
  7. 0 22
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/constant/BuildStage.java
  8. 6 46
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/controller/BuildController.java
  9. 1 1
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/controller/WebhookController.java
  10. 5 3
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/entity/log/BuildLog.java
  11. 1 0
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/entity/log/DeployLog.java
  12. 0 1
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/repository/log/BuildLogRepository.java
  13. 4 5
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/repository/log/LogRepository.java
  14. 1 3
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/BuildService.java
  15. 33 35
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/build/AppBuildPipeline.java
  16. 93 105
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/build/AppIntegrate.java
  17. 65 60
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/build/BuildDispatcher.java
  18. 4 2
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/deploy/AppDeployTask.java
  19. 31 37
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/deploy/AppDeployer.java
  20. 8 4
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/log/LogConsumer.java
  21. 0 25
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/tools/compiler/DotnetPreCompile.java
  22. 2 2
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/tools/compiler/NullCompiler.java
  23. 0 14
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/tools/compiler/PreCompile.java
  24. 0 2
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/tools/updater/CommitLog.java
  25. 0 3
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/tools/updater/GitImpl.java
  26. 2 8
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/tools/updater/SvnImpl.java
  27. 7 20
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/vo/log/BuildDeployLog.java
  28. 0 2
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/vo/log/BuildLogVO.java
  29. 0 2
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/vo/log/CommitLogVO.java
  30. 0 4
      dmaster/src/main/java/cn/reghao/autodop/dmaster/app/vo/log/DeployLogVO.java
  31. 16 3
      dmaster/src/main/java/cn/reghao/autodop/dmaster/common/exception/ExceptionUtil.java
  32. 2 1
      scripts/build.sh

+ 0 - 26
common/pom.xml

@@ -11,13 +11,6 @@
 
     <artifactId>common</artifactId>
 
-    <properties>
-        <projOrchestration.build.sourceEncoding>utf-8</projOrchestration.build.sourceEncoding>
-        <projOrchestration.reporting.outputEncoding>utf-8</projOrchestration.reporting.outputEncoding>
-        <java.version>1.8</java.version>
-        <grpc.version>1.23.1</grpc.version>
-    </properties>
-
     <dependencies>
         <dependency>
             <groupId>io.netty</groupId>
@@ -86,25 +79,6 @@
                     <target>1.8</target>
                 </configuration>
             </plugin>
-
-            <plugin>
-                <groupId>org.xolstice.maven.plugins</groupId>
-                <artifactId>protobuf-maven-plugin</artifactId>
-                <version>0.5.1</version>
-                <configuration>
-                    <protocArtifact>com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}</protocArtifact>
-                    <pluginId>grpc-java</pluginId>
-                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
-                </configuration>
-                <executions>
-                    <execution>
-                        <goals>
-                            <goal>compile</goal>
-                            <goal>compile-custom</goal>
-                        </goals>
-                    </execution>
-                </executions>
-            </plugin>
         </plugins>
     </build>
 </project>

+ 0 - 1
common/src/main/java/cn/reghao/autodop/common/deploy/DeployConfig.java

@@ -13,6 +13,5 @@ public class DeployConfig {
     private String appId;
     private String packerType;
     private String appPath;
-    private String host;
     private String runningDir;
 }

+ 0 - 26
dagent/pom.xml

@@ -11,14 +11,6 @@
 
     <artifactId>dagent</artifactId>
 
-    <properties>
-        <projOrchestration.build.sourceEncoding>utf-8</projOrchestration.build.sourceEncoding>
-        <projOrchestration.reporting.outputEncoding>utf-8</projOrchestration.reporting.outputEncoding>
-        <java.version>1.8</java.version>
-        <grpc.version>1.6.1</grpc.version>
-        <protobuf.version>3.3.0</protobuf.version>
-    </properties>
-
     <dependencies>
         <dependency>
             <groupId>cn.reghao.autodop</groupId>
@@ -90,24 +82,6 @@
                     <fork>true</fork>
                 </configuration>
             </plugin>
-            <plugin>
-                <groupId>org.xolstice.maven.plugins</groupId>
-                <artifactId>protobuf-maven-plugin</artifactId>
-                <version>0.5.0</version>
-                <configuration>
-                    <protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact>
-                    <pluginId>grpc-java</pluginId>
-                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
-                </configuration>
-                <executions>
-                    <execution>
-                        <goals>
-                            <goal>compile</goal>
-                            <goal>compile-custom</goal>
-                        </goals>
-                    </execution>
-                </executions>
-            </plugin>
         </plugins>
     </build>
 </project>

+ 1 - 1
dagent/src/main/java/cn/reghao/autodop/dagent/app/deploy/AppDeployService.java

@@ -17,12 +17,12 @@ public class AppDeployService {
         String packerType = deployConfig.getPackerType();
 
         Deploy deployApp;
-        log.info("部署 {}...", appId);
         switch (PackerType.valueOf(packerType)) {
             case docker:
                 deployApp = new DockerDeploy();
                 // TODO 对变量的使用,变量返回后应避免再设置其内部字段的值
                 deployApp.deploy(appId, appPath, null);
+                log.info("{} 部署完成...", appId);
                 break;
             case zip:
                 deployApp = new ZipDeploy();

+ 1 - 48
dmaster/pom.xml

@@ -11,11 +11,6 @@
 
     <artifactId>dmaster</artifactId>
 
-    <properties>
-        <grpc.version>1.6.1</grpc.version>
-        <protobuf.version>3.3.0</protobuf.version>
-    </properties>
-
     <dependencies>
         <dependency>
             <groupId>cn.reghao.autodop</groupId>
@@ -146,31 +141,7 @@
     <build>
         <finalName>autodop-dmaster</finalName>
 
-        <extensions>
-            <extension>
-                <groupId>kr.motd.maven</groupId>
-                <artifactId>os-maven-plugin</artifactId>
-                <version>1.5.0.Final</version>
-            </extension>
-        </extensions>
-
         <resources>
-            <!--<resource>
-                <directory>src/main/resources</directory>
-                <filtering>true</filtering>
-                <excludes>
-                    <exclude>static/layuix/font/**</exclude>
-                    <exclude>static/layui/font/**</exclude>
-                </excludes>
-            </resource>
-            <resource>
-                <directory>src/main/resources</directory>
-                <filtering>false</filtering>
-                <includes>
-                    <include>static/layuix/font/**</include>
-                    <include>static/layui/font/**</include>
-                </includes>
-            </resource>-->
             <resource>
                 <directory>src/main/resources</directory>
                 <filtering>true</filtering>
@@ -200,24 +171,6 @@
                     <fork>true</fork>
                 </configuration>
             </plugin>
-            <plugin>
-                <groupId>org.xolstice.maven.plugins</groupId>
-                <artifactId>protobuf-maven-plugin</artifactId>
-                <version>0.5.0</version>
-                <configuration>
-                    <protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact>
-                    <pluginId>grpc-java</pluginId>
-                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
-                </configuration>
-                <executions>
-                    <execution>
-                        <goals>
-                            <goal>compile</goal>
-                            <goal>compile-custom</goal>
-                        </goals>
-                    </execution>
-                </executions>
-            </plugin>
         </plugins>
     </build>
-</project>
+</project>

+ 22 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/constant/AppStage.java

@@ -0,0 +1,22 @@
+package cn.reghao.autodop.dmaster.app.constant;
+
+/**
+ * 应用构建部署时所处的阶段
+ * TODO 后期使用状态机
+ *
+ * @author reghao
+ * @date 2019-10-18 14:31:29
+ */
+public enum AppStage {
+    update("更新"), compile("编译"), pack("打包"), build("构建"), deploy("部署");
+
+    private String value;
+
+    AppStage(String value) {
+        this.value = value;
+    }
+
+    public String getValue() {
+        return value;
+    }
+}

+ 0 - 22
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/constant/BuildStage.java

@@ -1,22 +0,0 @@
-package cn.reghao.autodop.dmaster.app.constant;
-
-/**
- * 应用构建时所处的阶段
- * TODO 后期使用状态机
- *
- * @author reghao
- * @date 2019-10-18 14:31:29
- */
-public enum BuildStage {
-    update("更新阶段"), compile("编译阶段"), pack("打包阶段"), build("构建完成");
-
-    private String name;
-
-    BuildStage(String name) {
-        this.name = name;
-    }
-
-    public String getName() {
-        return name;
-    }
-}

+ 6 - 46
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/controller/BuildController.java

@@ -22,12 +22,6 @@ import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 import org.tmatesoft.svn.core.SVNException;
 
-import java.util.Arrays;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-import java.util.stream.Collectors;
-
 /**
  * @author reghao
  * @date 2019-08-30 18:49:15
@@ -49,7 +43,6 @@ public class BuildController {
 
     // TODO docker 打包时 repository:tag 为空
     // TODO docker push 没有成功
-    // TODO 添加一个 from 参数来表示构建部署请求来自何处,是主动构建还是自动构建
     @ApiOperation(value = "构建部署应用")
     @ApiImplicitParams(
             @ApiImplicitParam(name="appId", value="应用标识(多个应用间使用 , 分隔)",
@@ -57,19 +50,8 @@ public class BuildController {
     )
     @PostMapping("/pipeline")
     public String buildAndDeploy(@RequestParam("appId") String appId) throws Exception {
-        Set<String> apps = checkArgs(appId);
-        List<BuildDeployLog> buildDeployLogs;
-        if (apps.size() == 0) {
-            return WebResult.success("ok");
-        } else {
-            buildDeployLogs = buildDispatcher.dispatch(apps, true);
-            List<DeployLogVO> deployResultVOS = buildDeployLogs.stream()
-                    .filter(buildDeployLog -> buildDeployLog.getDeployLog() != null)
-                    .map(BuildDeployLog::deployLog)
-                    .collect(Collectors.toList());
-
-            return WebResult.success(deployResultVOS);
-        }
+        BuildDeployLog buildDeployLog = buildDispatcher.buildAndDeploy(appId, true);
+        return WebResult.success(buildDeployLog);
     }
 
     @ApiOperation(value = "构建应用")
@@ -79,27 +61,18 @@ public class BuildController {
     )
     @PostMapping("/build")
     public String build(@RequestParam("appId") String appId) throws Exception {
-        Set<String> apps = checkArgs(appId);
-        List<BuildDeployLog> buildDeployLogs;
-        if (apps.size() == 0) {
-            return WebResult.success("ok");
-        } else {
-            buildDeployLogs = buildDispatcher.dispatch(apps, false);
-            List<BuildLogVO> buildResults = buildDeployLogs.stream()
-                    .map(BuildDeployLog::buildLog)
-                    .collect(Collectors.toList());
-
-            return WebResult.success(buildResults);
-        }
+        BuildDeployLog buildDeployLog = buildDispatcher.buildAndDeploy(appId, false);
+        return WebResult.success(buildDeployLog);
     }
 
     @ApiOperation(value = "部署应用")
     @ApiImplicitParams({
             @ApiImplicitParam(name="appId", value="应用标识", paramType="query", dataType = "String"),
-            @ApiImplicitParam(name="commitId", value="应用地址", paramType="query", dataType = "String")
+            @ApiImplicitParam(name="commitId", value="代码版本", paramType="query", dataType = "String")
     })
     @PostMapping("/deploy")
     public String deploy(@RequestParam("appId") String appId, @RequestParam("commitId") String commitId) {
+        // TODO 待实现
         buildDispatcher.deploy(appId, commitId);
         return WebResult.success("待实现");
     }
@@ -114,19 +87,6 @@ public class BuildController {
         return WebResult.success("ok");
     }
 
-    /**
-     * 检查参数
-     *
-     * @param
-     * @return
-     * @date 2019-12-02 下午2:36
-     */
-    private Set<String> checkArgs(String appId) {
-        String whiteSpace = "\\s+";
-        String[] array = appId.replaceAll(whiteSpace, "").split(",");
-        return new HashSet<>(Arrays.asList(array));
-    }
-
     @ApiOperation(value = "需要构建的应用")
     @GetMapping("/list")
     public ResponseEntity<String> buildList(@RequestParam("env") String env,

+ 1 - 1
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/controller/WebhookController.java

@@ -30,7 +30,7 @@ public class WebhookController {
         String repo = jsonObject.get("repository").getAsJsonObject().get("url").getAsString();
         String ref = jsonObject.get("ref").getAsString();
         String branch = ref.substring(ref.lastIndexOf("/")+1);
-        BuildDeployLog buildDeployLog = buildDispatcher.dispatch(repo, branch, true);
+        BuildDeployLog buildDeployLog = buildDispatcher.buildAndDeploy(repo, branch, true);
         return WebResult.success(buildDeployLog);
     }
 }

+ 5 - 3
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/entity/log/BuildLog.java

@@ -21,17 +21,19 @@ import java.time.LocalDateTime;
 @EqualsAndHashCode(callSuper = false)
 @Entity
 public class BuildLog extends BaseEntity {
-    private String stage;
     // 0 - 成功 1 - 失败
     private int code;
-    @Column(length = 1024)
     private String msg;
+    @Column(length = 1024)
+    private String errDetail;
 
     @ManyToOne(cascade = CascadeType.DETACH)
     @JoinColumn(name = "app_orchestration_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
     private AppOrchestration app;
     private CommitLog commitLog;
     private String appPath;
-    // TODO 对实体中的字段添加适当的约束
+    private long updateTotalTime;
+    private long compileTotalTime;
+    private long packTotalTime;
     private LocalDateTime buildTime;
 }

+ 1 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/entity/log/DeployLog.java

@@ -22,6 +22,7 @@ public class DeployLog extends BaseEntity {
     @ManyToOne(cascade = CascadeType.DETACH)
     @JoinColumn(name = "build_log_id", foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
     private BuildLog buildLog;
+    private long deployTotalTime;
     @ElementCollection(fetch = FetchType.EAGER)
     private Set<DeployResult> deployResults;
 }

+ 0 - 1
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/repository/log/BuildLogRepository.java

@@ -10,6 +10,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
  * @date 2020-01-21 14:53:03
  */
 public interface BuildLogRepository extends JpaRepository<BuildLog, Long> {
-    BuildLog findByCommitLogRemoteRepoUrlAndCommitLogCommitId(String repoUrl, String commitId);
     Page<BuildLog> findByAppEnv(String env, Pageable pageable);
 }

+ 4 - 5
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/repository/log/LogRepository.java

@@ -40,7 +40,7 @@ public class LogRepository {
 
     @Transactional
     public void saveBuildLog(BuildLog buildLog) {
-        CommitLog commitLog = buildLog.getCommitLog();
+        /*CommitLog commitLog = buildLog.getCommitLog();
         if (commitLog == null) {
             return;
         }
@@ -56,7 +56,7 @@ public class LogRepository {
             saveOrUpdateBuildLog(buildLog);
         } else {
             saveOrUpdateBuildLog(buildLog);
-        }
+        }*/
     }
 
     private void saveOrUpdateBuildLog(BuildLog buildLog) {
@@ -72,8 +72,7 @@ public class LogRepository {
 
     @Transactional
     public void saveOrUpdateDeployLog(DeployLog deployLog) {
-        BuildLog buildLog = deployLog.getBuildLog();
-        String repo = buildLog.getCommitLog().getRemoteRepoUrl();
+        /*BuildLog buildLog = deployLog.getBuildLog();
         String commitId = buildLog.getCommitLog().getCommitId();
         BuildLog entity = buildLogRepository.findByCommitLogRemoteRepoUrlAndCommitLogCommitId(repo, commitId);
         if (entity == null) {
@@ -89,7 +88,7 @@ public class LogRepository {
             deployLogEntity.setDeployResults(deployLog.getDeployResults());
             deployLogEntity.setUpdateTime(LocalDateTime.now());
             deployLogRepository.save(deployLogEntity);
-        }
+        }*/
     }
 
     private void saveOrUpdateDeployedApp(BuildLog buildLog) {

+ 1 - 3
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/BuildService.java

@@ -103,14 +103,12 @@ public class BuildService {
         for (DeployLog deployLog : deployLogs) {
             BuildLog buildLog = deployLog.getBuildLog();
             String appId = buildLog.getApp().getAppId();
-            String remoteRepoUrl = buildLog.getCommitLog().getRemoteRepoUrl();
-            String branch = buildLog.getCommitLog().getBranch();
             String commitId = buildLog.getCommitLog().getCommitId();
 
             Set<DeployResultVO> deployResultVOS = deployLog.getDeployResults().stream()
                     .map(DeployResultVO::of)
                     .collect(Collectors.toSet());
-            list.add(new DeployLogVO(appId, env, remoteRepoUrl, branch, commitId, deployResultVOS));
+            list.add(new DeployLogVO(appId, env, commitId, deployResultVOS));
         }
         pageList.setList(list);
         return pageList;

+ 33 - 35
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/build/AppBuildPipeline.java

@@ -1,6 +1,8 @@
 package cn.reghao.autodop.dmaster.app.service.build;
 
-import cn.reghao.autodop.dmaster.app.constant.BuildStage;
+import cn.reghao.autodop.dmaster.app.entity.log.BuildLog;
+import cn.reghao.autodop.dmaster.app.entity.log.DeployLog;
+import cn.reghao.autodop.dmaster.app.service.log.LogConsumer;
 import cn.reghao.autodop.dmaster.app.vo.log.BuildDeployLog;
 import lombok.extern.slf4j.Slf4j;
 
@@ -17,10 +19,12 @@ import java.util.concurrent.Callable;
 public class AppBuildPipeline implements Callable<BuildDeployLog> {
     private AppIntegrate appIntegrate;
     private boolean isDeploy;
+    private LogConsumer logConsumer;
 
-    public AppBuildPipeline(AppIntegrate appIntegrate, boolean isDeploy) {
+    public AppBuildPipeline(AppIntegrate appIntegrate, boolean isDeploy, LogConsumer logConsumer) {
         this.appIntegrate = appIntegrate;
         this.isDeploy = isDeploy;
+        this.logConsumer = logConsumer;
     }
 
     /**
@@ -32,43 +36,37 @@ public class AppBuildPipeline implements Callable<BuildDeployLog> {
      */
     @Override
     public BuildDeployLog call() {
-        String appId = appIntegrate.getApp().getAppId();
-        long start = System.currentTimeMillis();
-        if (!appIntegrate.update()) {
-            return appIntegrate.log();
+        BuildLog buildLog = appIntegrate.build();
+        if (buildLog.getCode() == 0 && isDeploy) {
+            DeployLog deployLog = appIntegrate.deploy(buildLog);
+            logConsumer.addDeployLog(deployLog);
+            return fromDeployLog(deployLog);
+        } else {
+            logConsumer.addBuildLog(buildLog);
+            return fromBuildLog(buildLog);
         }
-        long updateTime = System.currentTimeMillis() - start;
-        log.info("{} 更新代码完成...", appId);
-
-        start = System.currentTimeMillis();
-        if (!appIntegrate.compile()) {
-            return appIntegrate.log();
-        }
-        long compileTime = System.currentTimeMillis() - start;
-        log.info("{} 编译代码完成...", appId);
-
-        start = System.currentTimeMillis();
-        if (!appIntegrate.pack()) {
-            return appIntegrate.log();
-        }
-        long packTime = System.currentTimeMillis() - start;
-        log.info("{} 打包代码完成...", appId);
-        buildSuccessfully();
+    }
 
-        if (isDeploy) {
-            start = System.currentTimeMillis();
-            appIntegrate.deploy();
-            long deployTime = System.currentTimeMillis() - start;
-            log.info("{} 部署代码完成...", appId);
+    private BuildDeployLog fromBuildLog(BuildLog buildLog) {
+        BuildDeployLog buildDeployLog = new BuildDeployLog();
+        buildDeployLog.setAppId(buildLog.getApp().getAppId());
+        buildDeployLog.setDeploy(false);
+        buildDeployLog.setCode(buildLog.getCode());
+        buildDeployLog.setMsg(buildLog.getMsg());
+        if (buildLog.getCode() == 0) {
+            buildDeployLog.setCommitId(buildLog.getCommitLog().getCommitId());
+            return buildDeployLog;
+        } else {
+            buildDeployLog.setErrDetail(buildLog.getErrDetail());
+            if (buildLog.getCommitLog() != null) {
+                buildDeployLog.setCommitId(buildLog.getCommitLog().getCommitId());
+            }
+            return buildDeployLog;
         }
-
-        return appIntegrate.log();
     }
 
-    private void buildSuccessfully() {
-        BuildDeployLog buildDeployLog = appIntegrate.log();
-        buildDeployLog.getBuildLog().setStage(BuildStage.build.name());
-        buildDeployLog.getBuildLog().setCode(0);
-        buildDeployLog.getBuildLog().setMsg("构建成功");
+    private BuildDeployLog fromDeployLog(DeployLog deployLog) {
+        BuildDeployLog buildDeployLog = new BuildDeployLog();
+        return buildDeployLog;
     }
 }

+ 93 - 105
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/build/AppIntegrate.java

@@ -1,16 +1,14 @@
 package cn.reghao.autodop.dmaster.app.service.build;
 
 import cn.reghao.autodop.common.utils.text.TextFile;
-import cn.reghao.autodop.dmaster.app.constant.AppType;
 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.constant.BuildStage;
+import cn.reghao.autodop.dmaster.app.constant.AppStage;
 import cn.reghao.autodop.dmaster.app.constant.CompilerType;
 import cn.reghao.autodop.common.deploy.PackerType;
 import cn.reghao.autodop.dmaster.app.constant.RepoType;
-import cn.reghao.autodop.dmaster.app.vo.log.BuildDeployLog;
 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.orchestration.AppOrchestration;
@@ -34,9 +32,7 @@ import java.time.LocalDateTime;
 import java.util.Set;
 
 /**
- * TODO 统计构建各阶段耗时
- *
- * 应用构建部署,根据 AppOrchestration 的值完成应用构建和部署的配置
+ * 应用构建部署
  *
  * @author reghao
  * @date 2019-10-09 10:54:43
@@ -50,11 +46,8 @@ public class AppIntegrate {
     private AppOrchestration app;
     // 远程仓库最近一次提交的信息
     private CommitLog lastCommitLog;
-    private BuildLog buildLog;
-    private DeployLog deployLog;
 
     private CodeUpdater codeUpdater;
-    private PreCompile preCompile;
     private CodeCompiler codeCompiler;
     private CodePacker codePacker;
     private AppDeployer appDeployer;
@@ -82,7 +75,6 @@ public class AppIntegrate {
 
     private void init() {
         initLocalDir();
-        initBuildLog();
         initBuildDeployConfig();
     }
 
@@ -92,11 +84,6 @@ public class AppIntegrate {
         appPackDir = SysConfig.packDir + "/" + app.getAppId();
     }
 
-    private void initBuildLog() {
-        buildLog = new BuildLog();
-        buildLog.setApp(app);
-    }
-
     private void initBuildDeployConfig() {
         AppBuild appBuild;
         ProjOrchestration proj = app.getProj();
@@ -129,16 +116,11 @@ public class AppIntegrate {
         AppCompile appCompile = appBuild.getAppCompile();
         if (appCompile.getCompilerType().equals(CompilerType.shell.name())) {
             codeCompiler = new ShellCompiler(appCompile);
-            if (app.getAppType().equals(AppType.dotnetCore.name())) {
-                preCompile = new DotnetPreCompile();
-            } else {
-                preCompile = null;
-            }
         } else if (appCompile.getCompilerType().equals(CompilerType.maven.name())) {
             codeCompiler = new MavenCompiler("");
         } else if (appCompile.getCompilerType().equals(CompilerType.none.name())) {
             // 静态应用,不需要编译器
-            codeCompiler = new DefaultCompiler();
+            codeCompiler = new NullCompiler();
         }
 
         // 初始化打包配置
@@ -150,53 +132,89 @@ public class AppIntegrate {
         }
     }
 
-    /**
-     * 编译源码前的操作(一般是调整配置)
-     *
-     * @date 2019-11-07 下午2:03
-     */
-    public void preCompile(String appEntryDir) throws Exception {
-        preCompile.preCompile(app, appEntryDir);
-    }
+    /* 构建:更新 -> 编译 -> 打包 */
+    public BuildLog build() {
+        BuildLog buildLog = new BuildLog();
+        buildLog.setApp(app);
 
-    /* 更新 -> 编译 -> 打包 -> 部署 */
-    public boolean update() {
+        boolean isUpdate;
         try {
-            String remote;
-            String branch;
-            String local = SysConfig.localRepo + "/" + app.getAppId();
-            ProjOrchestration proj = app.getProj();
-            if (proj != null) {
-                remote = proj.getProjRepo();
-                branch = proj.getBranch();
-            } else {
-                remote = app.getAppRepo();
-                branch = app.getBranch();
-            }
+            long start = System.currentTimeMillis();
+            isUpdate = update(buildLog);
+            buildLog.setUpdateTotalTime(System.currentTimeMillis() - start);
+            log.info("{} 更新完成...", app.getAppId());
+        } catch (Exception e) {
+            buildException(buildLog, e);
+            return buildLog;
+        }
 
-            if (lastCommitLog == null) {
-                lastCommitLog = codeUpdater.lastCommitInfo(local + "/" + app.getDirname(), branch);
+        if (isUpdate) {
+            try {
+                long start = System.currentTimeMillis();
+                compile();
+                buildLog.setCompileTotalTime(System.currentTimeMillis() - start);
+                log.info("{} 编译完成...", app.getAppId());
+            } catch (Exception e) {
+                buildException(buildLog, e);
+                return buildLog;
             }
 
-            UpdateStatus updateStatus = update0(remote, branch, local);
-            if (updateStatus.isUpdated) {
-                lastCommitLog = updateStatus.getLastCommitLog();
-                buildLog.setCommitLog(lastCommitLog);
-                BuilderUtil.copyToCompileDir(local, app);
-                return true;
-            } else {
-                buildLog.setStage(BuildStage.update.name());
-                buildLog.setCode(0);
-                buildLog.setMsg("应用代码未更新");
-                buildLog.setBuildTime(LocalDateTime.now());
-                return false;
+            try {
+                long start = System.currentTimeMillis();
+                pack(buildLog);
+                buildLog.setPackTotalTime(System.currentTimeMillis() - start);
+                log.info("{} 打包完成...", app.getAppId());
+            } catch (Exception e) {
+                buildException(buildLog, e);
+                return buildLog;
             }
-        } catch (Exception e) {
-            e.printStackTrace();
-            buildLog.setStage(BuildStage.update.name());
-            buildLog.setCode(1);
-            buildLog.setMsg(ExceptionUtil.errorMsg(e));
+        } else {
+            buildLog.setCompileTotalTime(0);
+            buildLog.setPackTotalTime(0);
+            buildLog.setMsg("代码未变,无需构建");
+            buildLog.setCode(2);
             buildLog.setBuildTime(LocalDateTime.now());
+            return buildLog;
+        }
+
+        buildLog.setMsg("构建成功");
+        buildLog.setCode(0);
+        buildLog.setBuildTime(LocalDateTime.now());
+        return buildLog;
+    }
+
+    private void buildException(BuildLog buildLog, Exception e) {
+        buildLog.setCode(1);
+        buildLog.setMsg("构建失败");
+        buildLog.setErrDetail(ExceptionUtil.errorMsg(e));
+        buildLog.setBuildTime(LocalDateTime.now());
+    }
+
+    private boolean update(BuildLog buildLog) throws Exception {
+        String remote;
+        String branch;
+        String local = SysConfig.localRepo + "/" + app.getAppId();
+        ProjOrchestration proj = app.getProj();
+        if (proj != null) {
+            remote = proj.getProjRepo();
+            branch = proj.getBranch();
+        } else {
+            remote = app.getAppRepo();
+            branch = app.getBranch();
+        }
+
+        if (lastCommitLog == null) {
+            lastCommitLog = codeUpdater.lastCommitInfo(local + "/" + app.getDirname(), branch);
+        }
+
+        UpdateStatus updateStatus = update0(remote, branch, local);
+        // TODO 代码版本未更改的情况下,是否应该构建
+        if (updateStatus.isUpdated || app.isAlwaysBuild()) {
+            lastCommitLog = updateStatus.getLastCommitLog();
+            buildLog.setCommitLog(lastCommitLog);
+            BuilderUtil.copyToCompileDir(local, app);
+            return true;
+        } else {
             return false;
         }
     }
@@ -224,54 +242,24 @@ public class AppIntegrate {
         return new UpdateStatus(current, true);
     }
 
-    public boolean compile() {
-        try {
-            if (preCompile != null) {
-                preCompile(appCompileDir + app.getCompileDir());
-            }
-            // TODO 通过脚本调用编译器时会卡住
-            codeCompiler.compile(app.getDirname(), appCompileDir + app.getCompileDir(), app.getEnv());
-            return true;
-        } catch (Exception e) {
-            e.printStackTrace();
-            buildLog.setStage(BuildStage.compile.name());
-            buildLog.setCode(1);
-            buildLog.setMsg(ExceptionUtil.errorMsg(e));
-            buildLog.setBuildTime(LocalDateTime.now());
-            return false;
-        }
+    private void compile() throws Exception {
+        // TODO 通过脚本调用编译器时会卡住
+        codeCompiler.compile(app.getDirname(), appCompileDir + app.getCompileDir(), app.getEnv());
     }
 
-    public boolean pack() {
-        try {
-            String appPath = codePacker.pack(app.getAppId(), lastCommitLog.getCommitId(),
-                    appCompileDir + app.getCompileDir());
-            buildLog.setAppPath(appPath);
-            buildLog.setBuildTime(LocalDateTime.now());
-            return true;
-        } catch (Exception e) {
-            e.printStackTrace();
-            buildLog.setStage(BuildStage.pack.name());
-            buildLog.setCode(1);
-            buildLog.setMsg(ExceptionUtil.errorMsg(e));
-            buildLog.setBuildTime(LocalDateTime.now());
-            return false;
-        }
-    }
-
-    public void deploy() {
-        deployLog = appDeployer.deploy(app, buildLog);
+    private void pack(BuildLog buildLog) throws Exception {
+        String appEntryDir = appCompileDir + app.getCompileDir();
+        String appPath = codePacker.pack(app.getAppId(), lastCommitLog.getCommitId(), appEntryDir);
+        buildLog.setAppPath(appPath);
     }
 
-    /**
-     * 构建部署日志
-     *
-     * @param
-     * @return
-     * @date 2020-05-15 上午11:31
-     */
-    public BuildDeployLog log() {
-        return new BuildDeployLog(buildLog, deployLog);
+    /* 部署 */
+    public DeployLog deploy(BuildLog buildLog) {
+        long totalTime = System.currentTimeMillis();
+        DeployLog deployLog = appDeployer.deploy(buildLog);
+        deployLog.setDeployTotalTime(System.currentTimeMillis() - totalTime);
+        log.info("{} 部署完成...", app.getAppId());
+        return deployLog;
     }
 
     @Data

+ 65 - 60
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/build/BuildDispatcher.java

@@ -1,18 +1,23 @@
 package cn.reghao.autodop.dmaster.app.service.build;
 
+import cn.reghao.autodop.common.notification.DingNotify;
+import cn.reghao.autodop.common.notification.Notify;
 import cn.reghao.autodop.dmaster.app.cache.OrchestrationCache;
+import cn.reghao.autodop.dmaster.app.constant.NotifierType;
+import cn.reghao.autodop.dmaster.app.entity.Notification;
 import cn.reghao.autodop.dmaster.app.service.deploy.AppDeployer;
+import cn.reghao.autodop.dmaster.app.service.deploy.NotifyTask;
 import cn.reghao.autodop.dmaster.app.vo.log.BuildDeployLog;
 import cn.reghao.autodop.dmaster.app.entity.orchestration.AppOrchestration;
 import cn.reghao.autodop.dmaster.app.repository.log.LogRepository;
 import cn.reghao.autodop.dmaster.app.service.log.LogConsumer;
 import cn.reghao.autodop.dmaster.common.thread.ThreadPoolWrapper;
+import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 
 import java.util.*;
 import java.util.concurrent.*;
-import java.util.stream.Collectors;
 
 /**
  * 应用构建分发
@@ -41,8 +46,17 @@ public class BuildDispatcher {
         threadPool.submit(logConsumer);
     }
 
-    public BuildDeployLog dispatch(String repo, String branch, boolean isDeploy) throws Exception {
+    public BuildDeployLog buildAndDeploy(String appId, boolean isDeploy) {
+        AppOrchestration app = caching.findByAppId(appId);
+        return dispatch(app, isDeploy);
+    }
+
+    public BuildDeployLog buildAndDeploy(String repo, String branch, boolean isDeploy) {
         AppOrchestration app = caching.findByRepoAndBranch(repo, branch);
+        return dispatch(app, isDeploy);
+    }
+
+    public BuildDeployLog dispatch(AppOrchestration app, boolean isDeploy) {
         String appId = app.getAppId();
         if (!onBuilding.contains(appId)) {
             onBuilding.add(appId);
@@ -57,74 +71,65 @@ public class BuildDispatcher {
             map.put(appId, appIntegrate);
         }
 
-        Future<BuildDeployLog> future = threadPool.submit(new AppBuildPipeline(appIntegrate, isDeploy));
+        Future<BuildDeployLog> future = threadPool.submit(new AppBuildPipeline(appIntegrate, isDeploy, logConsumer));
         while (!future.isDone() && !future.isCancelled()) {
-            Thread.sleep(1_000);
+            try {
+                Thread.sleep(1_000);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
         }
 
-        BuildDeployLog buildDeployLog = future.get();
+        BuildDeployLog buildDeployLog = null;
+        try {
+            buildDeployLog = future.get();
+            if (buildDeployLog.isDeploy()) {
+                // TODO 异步发送通知
+            }
+        } catch (InterruptedException | ExecutionException e) {
+            e.printStackTrace();
+        }
         return buildDeployLog;
     }
 
-    /**
-     * 根据项目分发应用
-     *
-     * @param
-     * @return
-     * @date 2019-11-12 上午9:48
-     */
-    public List<BuildDeployLog> dispatch(Set<String> appIds, boolean isDeploy) throws Exception {
-        List<BuildDeployLog> results = new ArrayList<>();
-        List<AppOrchestration> existApps = new ArrayList<>();
-        // 分离出存在的应用和不存在的应用
-        appIds.forEach(appId -> {
-            AppOrchestration app = caching.findByAppId(appId);
-            if (app != null) {
-                if (!onBuilding.contains(appId)) {
-                    onBuilding.add(appId);
-                    existApps.add(app);
-                } else {
-                    log.info("{} 正在构建中...", appId);
-                }
-            } else {
-                log.info("{} 不存在...", appId);
-            }
-        });
-
-        // TODO 缓存 appIntegrates,AppOrchestration 没有变化时 AppIntegrate 也应该不变
-        List<AppIntegrate> appIntegrates = existApps.stream()
-                .map(app -> new AppIntegrate(app, appDeployer))
-                .collect(Collectors.toList());
-
-        List<Future<BuildDeployLog>> appFutures = new ArrayList<>();
-        appIntegrates.forEach(appIntegrate -> {
-            appFutures.add(threadPool.submit(new AppBuildPipeline(appIntegrate, isDeploy)));
-        });
-
-        // 等待所有正在构建的应用
-        Iterator<Future<BuildDeployLog>> iterator1 = appFutures.iterator();
-        while (iterator1.hasNext()) {
-            Future<BuildDeployLog> future = iterator1.next();
-            while (!future.isDone() && !future.isCancelled()) {
-                Thread.sleep(1_000);
-            }
+    public void deploy(String appId, String commitId) {
+        AppOrchestration app = caching.findByAppId(appId);
+    }
 
-            BuildDeployLog buildDeployLog = future.get();
-            results.add(buildDeployLog);
-            // TODO 抛出异常时无法删除集合中的 appId
-            onBuilding.remove(buildDeployLog.getBuildLog().getApp().getAppId());
-            iterator1.remove();
+    public void notifyAsync(AppOrchestration app) {
+        Notification notification = app.getNotification();
+        String notifierType = notification.getNotifierType();
+        String dest = notification.getUrl();
+        Notify notify = null;
+        if (notifierType.equals(NotifierType.webhook.name())) {
+            notify = new DingNotify();
         }
 
-        // 使用一个单独的线程来持久化构建过程产生的日志,有一定的延时
-        results.forEach(buildDeployLog -> {
-            logConsumer.addLog(buildDeployLog);
-        });
-
-        return results;
+        // TODO 检查应用是否成功运行,若启动失败,则回退到上一个版本。若上一个版本仍失败,则返回错误并发出紧急通知
+        /*NotifyMsg notifyMsg = new NotifyMsg(app.getAppId(), app.getEnv(), buildLog.getCommitLog().getCommitId());
+        try {
+            future.get();
+            notifyMsg.setMsg("部署成功");
+            deployResults.add(DeployResult.success(host));
+        } catch (Exception e) {
+            e.printStackTrace();
+            notifyMsg.setMsg("部署失败");
+            deployResults.add(DeployResult.fail(host, e.getMessage()));
+        }
+        threadPool.submit(new NotifyTask(notify, dest, notifyMsg));*/
     }
 
-    public void deploy(String appId, String commitId) {
-        AppOrchestration app = caching.findByAppId(appId);
+    @Data
+    static class NotifyMsg {
+        private String appId;
+        private String env;
+        private String commitId;
+        private String msg;
+
+        public NotifyMsg(String appId, String env, String commitId) {
+            this.appId = appId;
+            this.env = env;
+            this.commitId = commitId;
+        }
     }
 }

+ 4 - 2
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/deploy/AppDeployTask.java

@@ -16,16 +16,18 @@ import java.util.concurrent.Callable;
 public class AppDeployTask implements Callable<RpcResult> {
     private MessageSender sender;
     private DeployConfig deployConfig;
+    private String host;
 
-    public AppDeployTask(MessageSender sender, DeployConfig deployConfig) {
+    public AppDeployTask(MessageSender sender, DeployConfig deployConfig, String host) {
         this.sender = sender;
         this.deployConfig = deployConfig;
+        this.host = host;
     }
 
     // 异常在 Future 中抛出
     @Override
     public RpcResult call() {
-        String routeKey = "dagent.rpc@" + deployConfig.getHost();
+        String routeKey = "dagent.rpc@" + host;
         MqMessage<DeployConfig> mqMessage = new MqMessage<>();
         mqMessage.setMsgType(MsgType.deploy.name());
         mqMessage.setMsg(deployConfig);

+ 31 - 37
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/deploy/AppDeployer.java

@@ -1,6 +1,7 @@
 package cn.reghao.autodop.dmaster.app.service.deploy;
 
 import cn.reghao.autodop.common.deploy.DeployConfig;
+import cn.reghao.autodop.common.deploy.PackerType;
 import cn.reghao.autodop.common.mq.MessageSender;
 import cn.reghao.autodop.common.result.RpcResult;
 import cn.reghao.autodop.common.notification.DingNotify;
@@ -18,6 +19,7 @@ import lombok.Data;
 import org.springframework.stereotype.Component;
 
 import java.util.*;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Future;
 
@@ -41,27 +43,20 @@ public class AppDeployer {
      * @return
      * @date 2020-03-13 下午1:00
      */
-    public DeployLog deploy(AppOrchestration app, BuildLog buildLog) {
-        Notification notification = app.getNotification();
-        String notifierType = notification.getNotifierType();
-        String dest = notification.getUrl();
-        Notify notify = null;
-        if (notifierType.equals(NotifierType.webhook.name())) {
-            notify = new DingNotify();
-        }
-
+    public DeployLog deploy(BuildLog buildLog) {
+        AppOrchestration app = buildLog.getApp();
         AppPack appPack = app.getAppBuild().getAppPack();
         AppDeploy appDeploy = app.getAppDeploy();
+
+        DeployConfig deployConfig = new DeployConfig();
+        deployConfig.setAppId(app.getAppId());
+        deployConfig.setAppPath(buildLog.getAppPath());
+        deployConfig.setPackerType(appPack.getPackerType());
+        deployConfig.setRunningDir(appDeploy.getRunningDir());
+
         Map<String, Future<RpcResult>> futureMap = new HashMap<>();
         for (String host : appDeploy.getHosts()) {
-            DeployConfig deployConfig = new DeployConfig();
-            deployConfig.setAppId(app.getAppId());
-            deployConfig.setAppPath(buildLog.getAppPath());
-            deployConfig.setPackerType(appPack.getPackerType());
-            deployConfig.setHost(host);
-            deployConfig.setRunningDir(appDeploy.getRunningDir());
-
-            futureMap.put(host, threadPool.submit(new AppDeployTask(sender, deployConfig)));
+            futureMap.put(host, threadPool.submit(new AppDeployTask(sender, deployConfig, host)));
         }
 
         DeployLog deployLog = new DeployLog();
@@ -80,33 +75,32 @@ public class AppDeployer {
                 }
             }
 
-            // TODO 检查应用是否成功运行,若启动失败,则回退到上一个版本。若上一个版本仍失败,则返回错误并发出紧急通知
-            NotifyMsg notifyMsg = new NotifyMsg(app.getAppId(), app.getEnv(), buildLog.getCommitLog().getCommitId());
+            DeployResult deployResult = new DeployResult();
+            deployResult.setHost(host);
             try {
-                future.get();
-                notifyMsg.setMsg("部署成功");
-                deployResults.add(DeployResult.success(host));
-            } catch (Exception e) {
+                RpcResult rpcResult = future.get();
+                if (rpcResult == null) {
+
+                }
+            } catch (InterruptedException | ExecutionException e) {
                 e.printStackTrace();
-                notifyMsg.setMsg("部署失败");
-                deployResults.add(DeployResult.fail(host, e.getMessage()));
             }
-            threadPool.submit(new NotifyTask(notify, dest, notifyMsg));
         }
+
         return deployLog;
     }
 
-    @Data
-    static class NotifyMsg {
-        private String appId;
-        private String env;
-        private String commitId;
-        private String msg;
-
-        public NotifyMsg(String appId, String env, String commitId) {
-            this.appId = appId;
-            this.env = env;
-            this.commitId = commitId;
+    private String appPath(AppOrchestration app, String commitId) {
+        String appId = app.getAppId();
+        AppPack appPack = app.getAppBuild().getAppPack();
+        String packerPath = appPack.getPackerPath();
+        switch (PackerType.valueOf(appPack.getPackerType())) {
+            case docker:
+                return packerPath + appId + ":" + commitId;
+            case zip:
+                return null;
+            default:
+                return null;
         }
     }
 }

+ 8 - 4
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/log/LogConsumer.java

@@ -24,15 +24,19 @@ public class LogConsumer implements Runnable {
         this.logDAO = logDAO;
     }
 
-    public void addLog(BuildDeployLog log) {
-        queue.add(log);
+    public void addBuildLog(BuildLog buildLog) {
+        log.info("持久化 {} 构建日志...", buildLog.getApp().getAppId());
+    }
+
+    public void addDeployLog(DeployLog deployLog) {
+        log.info("持久化部署日志...");
     }
 
     @Override
     public void run() {
         log.info("日志持久化线程已启动...");
         while (!Thread.interrupted()) {
-            try {
+            /*try {
                 BuildDeployLog buildDeployLog = queue.take();
                 BuildLog buildLog = buildDeployLog.getBuildLog();
                 if (buildLog != null) {
@@ -52,7 +56,7 @@ public class LogConsumer implements Runnable {
             } catch (Exception e) {
                 log.info("持久化异常...");
                 e.printStackTrace();
-            }
+            }*/
         }
     }
 }

+ 0 - 25
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/tools/compiler/DotnetPreCompile.java

@@ -1,25 +0,0 @@
-package cn.reghao.autodop.dmaster.app.service.tools.compiler;
-
-import cn.reghao.autodop.common.utils.FileUtil;
-import cn.reghao.autodop.dmaster.app.entity.deploy.ConfigFile;
-import cn.reghao.autodop.dmaster.app.entity.orchestration.AppOrchestration;
-import lombok.extern.slf4j.Slf4j;
-
-import java.io.IOException;
-
-/**
- * @author reghao
- * @date 2020-01-21 17:01:32
- */
-@Slf4j
-public class DotnetPreCompile implements PreCompile {
-    @Override
-    public void preCompile(AppOrchestration app, String appEntryDir) throws IOException {
-        for (ConfigFile configFile : app.getConfigFiles()) {
-            String filepath = appEntryDir + "/" + configFile.getName();
-            String filecontent = configFile.getContent();
-            FileUtil.newFile(filepath, filecontent);
-            log.info("成功向 {}  文件写入内容", filepath);
-        }
-    }
-}

+ 2 - 2
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/tools/compiler/DefaultCompiler.java → dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/tools/compiler/NullCompiler.java

@@ -1,12 +1,12 @@
 package cn.reghao.autodop.dmaster.app.service.tools.compiler;
 
 /**
- * 默认实现,什么也不做
+ * 空编译器
  *
  * @author reghao
  * @date 2019-10-12 23:55:01
  */
-public class DefaultCompiler implements CodeCompiler {
+public class NullCompiler implements CodeCompiler {
     @Override
     public void compile(String appName, String appDir, String env) throws Exception {
     }

+ 0 - 14
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/tools/compiler/PreCompile.java

@@ -1,14 +0,0 @@
-package cn.reghao.autodop.dmaster.app.service.tools.compiler;
-
-import cn.reghao.autodop.dmaster.app.entity.orchestration.AppOrchestration;
-
-import java.io.IOException;
-
-/**
- * @author reghao
- * @date 2020-01-21 17:00:41
- */
-@Deprecated
-public interface PreCompile {
-    void preCompile(AppOrchestration app, String appEntryDir) throws IOException;
-}

+ 0 - 2
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/tools/updater/CommitLog.java

@@ -14,8 +14,6 @@ import java.util.List;
 @Data
 @Embeddable
 public class CommitLog {
-    private String remoteRepoUrl;
-    private String branch;
     private String commitId;
     private String commitAuthor;
     private String commitMsg;

+ 0 - 3
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/tools/updater/GitImpl.java

@@ -81,7 +81,6 @@ public class GitImpl implements CodeUpdater {
         CommitLog commitLog;
         if (!localRepo.exists()) {
             commitLog = clone(remote, branch, localRepoDir);
-            commitLog.setRemoteRepoUrl(remote);
         } else {
             try (Repository repo = new FileRepository(localRepo.getAbsolutePath())) {
                 PullCommand gitPull = new Git(repo).pull().setTransportConfigCallback(new TransportConfigCallback() {
@@ -94,7 +93,6 @@ public class GitImpl implements CodeUpdater {
 
                 gitPull.call();
                 commitLog = commitInfo(repo, branch);
-                commitLog.setRemoteRepoUrl(remote);
                 commitLog.setChangedFiles(changedFileList(repo, branch));
             }
         }
@@ -137,7 +135,6 @@ public class GitImpl implements CodeUpdater {
             commitLog.setCommitTime(commitTime*1000);
             commitLog.setCommitAuthor(commitAuthor);
             commitLog.setCommitMsg(commitMsg);
-            commitLog.setBranch(repo.getBranch());
             return commitLog;
         }
     }

+ 2 - 8
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/tools/updater/SvnImpl.java

@@ -45,9 +45,7 @@ public class SvnImpl implements CodeUpdater {
                     true, false);
         }
 
-        CommitLog commitLog = lastCommitInfo(local, branch);
-        commitLog.setBranch("");
-        return commitLog;
+        return lastCommitInfo(local, branch);
     }
 
     private List<ChangedFile> changedFileList(Map<String, SVNLogEntryPath> changedPaths) {
@@ -89,10 +87,7 @@ public class SvnImpl implements CodeUpdater {
 
         SVNStatus status = statusClient.doStatus(localRepo, false);
         long version = status.getCommittedRevision().getNumber();
-        CommitLog commitLog = commitInfo(localRepo, version);
-        commitLog.setRemoteRepoUrl(status.getRemoteURL().toString());
-
-        return commitLog;
+        return commitInfo(localRepo, version);
     }
 
     private CommitLog commitInfo(File localRepo, long version) throws SVNException {
@@ -108,7 +103,6 @@ public class SvnImpl implements CodeUpdater {
             String commitId = String.valueOf(logEntry.getRevision());
             Map<String, SVNLogEntryPath> changedPaths = logEntry.getChangedPaths();
 
-            //commitInfo.setRepoUrl(remote);
             commitLog.setCommitId(commitId);
             commitLog.setCommitMsg(commitMsg);
             commitLog.setCommitAuthor(commitAuthor);

+ 7 - 20
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/vo/log/BuildDeployLog.java

@@ -1,9 +1,5 @@
 package cn.reghao.autodop.dmaster.app.vo.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.vo.log.BuildLogVO;
-import cn.reghao.autodop.dmaster.app.vo.log.DeployLogVO;
 import lombok.Data;
 
 /**
@@ -14,20 +10,11 @@ import lombok.Data;
  */
 @Data
 public class BuildDeployLog {
-    private BuildLog buildLog;
-    // 一个应用可能会部署到多台主机
-    private DeployLog deployLog;
-
-    public BuildDeployLog(BuildLog buildLog, DeployLog deployLog) {
-        this.buildLog = buildLog;
-        this.deployLog = deployLog;
-    }
-
-    public BuildLogVO buildLog() {
-        return BuildLogVO.of(buildLog);
-    }
-
-    public DeployLogVO deployLog() {
-        return DeployLogVO.of(deployLog);
-    }
+    private String appId;
+    private String commitId;
+    private boolean isDeploy;
+    // 0 - 成功 1 - 失败
+    private int code;
+    private String msg;
+    private String errDetail;
 }

+ 0 - 2
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/vo/log/BuildLogVO.java

@@ -28,8 +28,6 @@ public class BuildLogVO {
 
         CommitLog commitLog = buildLog.getCommitLog();
         if (commitLog != null) {
-            buildLogVO.setRemoteRepoUrl(commitLog.getRemoteRepoUrl());
-            buildLogVO.setBranch(commitLog.getBranch());
             buildLogVO.setCommitId(commitLog.getCommitId());
         }
 

+ 0 - 2
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/vo/log/CommitLogVO.java

@@ -26,8 +26,6 @@ public class CommitLogVO {
     public static CommitLogVO of(String appId, CommitLog commitLog) {
         CommitLogVO commitLogVO = new CommitLogVO();
         commitLogVO.setAppId(appId);
-        commitLogVO.setRemoteRepoUrl(commitLog.getRemoteRepoUrl());
-        commitLogVO.setBranch(commitLog.getBranch());
         commitLogVO.setCommitId(commitLog.getCommitId());
         commitLogVO.setCommitAuthor(commitLog.getCommitAuthor());
         commitLogVO.setCommitMsg(commitLog.getCommitMsg());

+ 0 - 4
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/vo/log/DeployLogVO.java

@@ -18,8 +18,6 @@ import java.util.stream.Collectors;
 public class DeployLogVO {
     private String appId;
     private String env;
-    private String remoteRepoUrl;
-    private String branch;
     private String commitId;
     private Set<DeployResultVO> deployResults;
 
@@ -27,8 +25,6 @@ public class DeployLogVO {
         DeployLogVO deployLogVO = new DeployLogVO();
         deployLogVO.appId = deployLog.getBuildLog().getApp().getAppId();
         deployLogVO.env = deployLog.getBuildLog().getApp().getEnv();
-        deployLogVO.remoteRepoUrl = deployLog.getBuildLog().getCommitLog().getRemoteRepoUrl();
-        deployLogVO.branch = deployLog.getBuildLog().getCommitLog().getBranch();
         deployLogVO.commitId = deployLog.getBuildLog().getCommitLog().getCommitId();
         deployLogVO.deployResults = deployLog.getDeployResults().stream()
                 .map(DeployResultVO::of)

+ 16 - 3
dmaster/src/main/java/cn/reghao/autodop/dmaster/common/exception/ExceptionUtil.java

@@ -1,13 +1,26 @@
 package cn.reghao.autodop.dmaster.common.exception;
 
+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * @author reghao
  * @date 2020-08-10 13:52:13
  */
 public class ExceptionUtil {
     public static String errorMsg(Exception e) {
-        return e.getMessage() +
-                System.lineSeparator() +
-                e.getCause().getMessage();
+        List<String> stack = stackTrace(e);
+        return e.getMessage();
+    }
+
+    public static List<String> stackTrace(Exception e) {
+        List<String> lines = new ArrayList<>();
+        for (StackTraceElement element : e.getStackTrace()) {
+            String line = element.getClassName() + "." + element.getMethodName()
+                    + "(" + element.getFileName() + ":" + element.getLineNumber() + ")";
+            lines.add(line);
+        }
+
+        return lines;
     }
 }

+ 2 - 1
scripts/build.sh

@@ -15,7 +15,8 @@ proj_home="/home/reghao/code/aha/autodop"
 proj=`basename ${proj_home}`
 app_name=${proj}-${app}
 cd ${proj_home}
-mvn clean package -pl ${app} -am -Dmaven.test.skip=true -P${env}
+mvn clean package -am -pl ${app} -Dmaven.test.skip=true -P${env}
+#mvn clean package -am -pl ${app} -DskipTest=true -P${env}
 
 # 存放应用的根目录
 jar_home="/home/reghao/tmp/autodop/app"