浏览代码

继续优化构建部署流程

reghao 5 年之前
父节点
当前提交
e536d6940c

+ 0 - 6
dmaster/pom.xml

@@ -88,12 +88,6 @@
             <artifactId>spring-boot-starter-security</artifactId>
         </dependency>
 
-        <dependency>
-            <groupId>cglib</groupId>
-            <artifactId>cglib</artifactId>
-            <version>3.3.0</version>
-        </dependency>
-
         <dependency>
             <groupId>io.springfox</groupId>
             <artifactId>springfox-swagger2</artifactId>

+ 0 - 3
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/build/AppBuildPipeline.java

@@ -21,9 +21,6 @@ public class AppBuildPipeline implements Callable<BuildDeployLog> {
     public AppBuildPipeline(AppIntegrate appIntegrate, boolean isDeploy) {
         this.appIntegrate = appIntegrate;
         this.isDeploy = isDeploy;
-        // TODO 暂时不使用动态代理
-        /*this.appIntegrate = ProxyUtils.createProxyObject(AppIntegrate.class);
-        this.appIntegrate.setApp(app);*/
     }
 
     /**

+ 23 - 45
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/build/AppIntegrate.java

@@ -10,7 +10,6 @@ import cn.reghao.autodop.dmaster.app.constant.BuildStage;
 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.entity.deploy.AppDeploy;
 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;
@@ -58,17 +57,8 @@ public class AppIntegrate {
     private PreCompile preCompile;
     private CodeCompiler codeCompiler;
     private CodePacker codePacker;
-
     private AppDeployer appDeployer;
 
-    /*public AppIntegrate() {
-    }
-
-    public void setApp(AppOrchestration app) {
-        this.app = app;
-        init();
-    }*/
-
     public AppIntegrate(AppOrchestration app, AppDeployer appDeployer) {
         this.app = app;
         this.appDeployer = appDeployer;
@@ -80,7 +70,7 @@ public class AppIntegrate {
     }
 
     /**
-     * 若 AppOrchestration 发生变化则重新初始化
+     * TODO 若 AppOrchestration 发生变化则重新初始化
      *
      * @param
      * @return
@@ -158,14 +148,6 @@ public class AppIntegrate {
         } else if (appPack.getPackerType().equals(PackerType.zip.name())) {
             codePacker = new ZipPack(appPackDir);
         }
-
-        // TODO 特定于 dnkt-mgr 项目的处理
-        /*if (app.getAppId().startsWith("dnkt-mgr")) {
-            codePacker = new DntkMgrDockerPack(appPack.getPackerPath(), app.getEnv());
-        }*/
-
-        // TODO 初始化部署配置
-        AppDeploy appDeploy = app.getAppDeploy();
     }
 
     /**
@@ -193,15 +175,13 @@ public class AppIntegrate {
             }
 
             if (lastCommitLog == null) {
-                lastCommitLog = codeUpdater.lastCommitInfo(local, branch);
+                lastCommitLog = codeUpdater.lastCommitInfo(local + "/" + app.getDirname(), branch);
             }
 
-            AppUpdateStatus updateStatus = update0(remote, branch, local);
-            lastCommitLog = updateStatus.getLastCommitLog();
-            lastCommitLog.setRemoteRepoUrl(app.getAppRepo());
-            buildLog.setCommitLog(lastCommitLog);
-
-            if (updateStatus.isUpdated()) {
+            UpdateStatus updateStatus = update0(remote, branch, local);
+            if (updateStatus.isUpdated) {
+                lastCommitLog = updateStatus.getLastCommitLog();
+                buildLog.setCommitLog(lastCommitLog);
                 BuilderUtil.copyToCompileDir(local, app);
                 return true;
             } else {
@@ -221,28 +201,27 @@ public class AppIntegrate {
         }
     }
 
-    private AppUpdateStatus update0(String remote, String branch, String local) throws Exception {
+    private UpdateStatus update0(String remote, String branch, String local) throws Exception {
         CommitLog current = codeUpdater.update(remote, branch, local);
-        if (lastCommitLog == null
-                || current.getCommitTime() > lastCommitLog.getCommitTime()
-                || app.isAlwaysBuild()) {
+        if (lastCommitLog != null) {
+            if (lastCommitLog.getCommitId().equals(current.getCommitId())) {
+                return new UpdateStatus(current, false);
+            } else if (lastCommitLog.getCommitTime() > current.getCommitTime()) {
+                throw new Exception("本地仓库代码比远程仓库更新...");
+            }
+        }
 
-            if (app.getProj() == null) {
-                // TODO 拉取应用依赖的其他仓库(若存在)
-                Set<String> deps = app.getDependencyRepos();
-                if (deps != null && !deps.isEmpty()) {
-                    for (String repo : deps) {
-                        if (!repo.isEmpty()) {
-                            codeUpdater.update(repo, app.getBranch(), appLocalRepo + app.getDirname());
-                        }
-                    }
+        // TODO 拉取应用依赖的其他仓库(若存在)
+        Set<String> deps = app.getDependencyRepos();
+        if (app.getProj() == null && deps != null) {
+            for (String repo : deps) {
+                if (!repo.isEmpty()) {
+                    codeUpdater.update(repo, app.getBranch(), appLocalRepo + app.getDirname());
                 }
             }
-
-            return new AppUpdateStatus(app, current, true);
-        } else {
-            return new AppUpdateStatus(app, current, false);
         }
+
+        return new UpdateStatus(current, true);
     }
 
     public boolean compile() {
@@ -297,8 +276,7 @@ public class AppIntegrate {
 
     @Data
     @AllArgsConstructor
-    static class AppUpdateStatus {
-        private AppOrchestration app;
+    static class UpdateStatus {
         private CommitLog lastCommitLog;
         private boolean isUpdated;
     }

+ 8 - 1
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/build/BuildDispatcher.java

@@ -26,6 +26,7 @@ public class BuildDispatcher {
     private AppDeployer appDeployer;
     // TODO 多线程访问的集合
     private Set<String> onBuilding;
+    private Map<String, AppIntegrate> map;
     private OrchestrationCache caching;
     private LogConsumer logConsumer;
     private ExecutorService threadPool;
@@ -33,6 +34,7 @@ public class BuildDispatcher {
     public BuildDispatcher(AppDeployer appDeployer, OrchestrationCache caching, LogRepository logDAO) {
         this.appDeployer = appDeployer;
         this.onBuilding = new HashSet<>();
+        this.map = new HashMap<>();
         this.caching = caching;
         this.logConsumer = new LogConsumer(logDAO);
         this.threadPool = ThreadPoolWrapper.threadPool("app-build-deploy");
@@ -49,7 +51,12 @@ public class BuildDispatcher {
         }
 
         // TODO AppIntegrate 可缓存,不需要每次请求都创建,但要注意缓存失效时机
-        AppIntegrate appIntegrate = new AppIntegrate(app, appDeployer);
+        AppIntegrate appIntegrate = map.get(appId);
+        if (appIntegrate == null) {
+            appIntegrate = new AppIntegrate(app, appDeployer);
+            map.put(appId, appIntegrate);
+        }
+
         Future<BuildDeployLog> future = threadPool.submit(new AppBuildPipeline(appIntegrate, isDeploy));
         while (!future.isDone() && !future.isCancelled()) {
             Thread.sleep(1_000);

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

@@ -8,6 +8,7 @@ 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 - 66
dmaster/src/main/java/cn/reghao/autodop/dmaster/common/utils/ClassProxy.java

@@ -1,66 +0,0 @@
-package cn.reghao.autodop.dmaster.common.utils;
-
-import lombok.extern.slf4j.Slf4j;
-import net.sf.cglib.proxy.MethodInterceptor;
-import net.sf.cglib.proxy.MethodProxy;
-
-import java.lang.reflect.Method;
-
-/**
- * @author reghao
- * @date 2020-03-10 15:10:23
- */
-@Slf4j
-public class ClassProxy implements MethodInterceptor {
-    private final static int MAX_LEVEL = 3;
-    private final static String DOT = ".";
-
-    public static String methodName(Method method) {
-        if (method == null) {
-            return null;
-        }
-
-        String[] arr = method.toString().split(" ");
-        String methodName = arr[2].split("\\(")[0] + "()";
-        String[] arr2 = methodName.split("\\.");
-        if (arr2.length > MAX_LEVEL) {
-            StringBuilder sb = new StringBuilder();
-            for (int i = 0; i < arr2.length; i++) {
-                if (i <= MAX_LEVEL) {
-                    sb.append(arr2[i].substring(0, 1)).append(DOT);
-                } else {
-                    sb.append(arr2[i]).append(DOT);
-                }
-            }
-
-            String temp = sb.toString();
-            if (temp.endsWith(DOT)) {
-                temp = temp.substring(0, temp.length() - 1);
-            }
-
-            return temp;
-        }
-
-        return methodName;
-    }
-
-    @Override
-    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
-            throws Throwable {
-        StringBuilder sb = new StringBuilder();
-        Object result = null;
-        long start = System.currentTimeMillis();
-        try {
-            sb.append(methodName(method));
-            result = proxy.invokeSuper(obj, args);
-        } catch (Exception e) {
-            e.printStackTrace();
-        } finally {
-            long execTime = System.currentTimeMillis() - start;
-            sb.append(" cost -> ").append(execTime).append("ms");
-        }
-
-        log.info(sb.toString());
-        return result;
-    }
-}

+ 0 - 20
dmaster/src/main/java/cn/reghao/autodop/dmaster/common/utils/ProxyUtils.java

@@ -1,20 +0,0 @@
-package cn.reghao.autodop.dmaster.common.utils;
-
-import net.sf.cglib.proxy.Enhancer;
-
-/**
- * @author reghao
- * @date 2020-03-10 15:17:32
- */
-public class ProxyUtils {
-    public static <T> T createProxyObject(Class<T> type) {
-        ClassProxy factory = new ClassProxy();
-        Enhancer enhancer = new Enhancer();
-        enhancer.setSuperclass(type);
-        enhancer.setCallback(factory);
-
-        // 被代理的类必须有无参构造函数
-        T instance = (T) enhancer.create();
-        return instance;
-    }
-}