Selaa lähdekoodia

1.修复使用 DockerCompiler 时存在的 bug, bind 到容器 /app 路径下的源码目录是变化的, 若采用 /sourceCodeDir -> /app 映射, 会使得重复 bind /app 目录, 因此将映射顺序修改为 /app -> /sourceCodeDir, 这个顺序只在 DockerCompiler 及其使用的 Docker#runAndRm 中生效, 其他使用到 bind 的地方还是使用 hostDir -> containerDir 这个顺序
2.DockerCompiler#compile 方法中调用 Config 对象的 clone 方法, 使每次构建使用一个独立的 Config 对象, 是的, common.docker.model.Config 实现了 cloneable 接口

reghao 1 vuosi sitten
vanhempi
commit
2f9d7bb1a4

+ 6 - 8
common/src/main/java/cn/reghao/devops/common/docker/DockerImpl.java

@@ -237,10 +237,11 @@ public class DockerImpl implements Docker {
             Map<String, String> map = containerConfig.getVolumes().getMap();
             List<Bind> list = new ArrayList<>();
             for (Map.Entry<String, String> entry : map.entrySet()) {
-                String key = entry.getKey();
-                String value = entry.getValue();
-                Volume volume2 = new Volume(value);
-                Bind bind = new Bind(key, volume2);
+                String hostPath = entry.getValue();
+                String containerPath = entry.getKey();
+
+                Volume volume2 = new Volume(containerPath);
+                Bind bind = new Bind(hostPath, volume2);
                 list.add(bind);
             }
             hostConfig.withBinds(list);
@@ -266,9 +267,6 @@ public class DockerImpl implements Docker {
             @Override
             public void onNext(Frame object) {
                 if (object.getStreamType().equals(StreamType.STDERR)) {
-                    if (list.size() > 100) {
-                        list.clear();
-                    }
                     list.add(object.toString());
                 }
             }
@@ -296,7 +294,7 @@ public class DockerImpl implements Docker {
         if (Boolean.TRUE.equals(state.getRunning())) {
             dockerClient.stopContainerCmd(containerId).exec();
             dockerClient.removeContainerCmd(containerId).exec();
-            throw new Exception("docker build timeout");
+            throw new Exception("docker build timeout after 300 seconds");
         } else if (exitCode != null && exitCode == 0) {
             dockerClient.removeContainerCmd(containerId).exec();
         } else {

+ 6 - 1
common/src/main/java/cn/reghao/devops/common/docker/model/Config.java

@@ -12,7 +12,7 @@ import java.util.List;
  * @date 2020-01-14 23:11:21
  */
 @Data
-public class Config {
+public class Config implements Cloneable {
     @SerializedName("AttachStderr") private boolean attachStderr;
     @SerializedName("AttachStdin") private boolean attachStdin;
     @SerializedName("AttachStdout") private boolean attachStdout;
@@ -43,4 +43,9 @@ public class Config {
         this.image = image;
         this.hostConfig = new HostConfig();
     }
+
+    @Override
+    public Object clone() throws CloneNotSupportedException {
+        return super.clone();
+    }
 }

+ 2 - 1
mgr/src/main/java/cn/reghao/devops/mgr/mgr/build/chain/BuildTools.java

@@ -102,7 +102,8 @@ public class BuildTools {
                     config.setVolumes(new Volumes());
                     config.setCmd(List.of("sh", "-c", compileCmd));
                     compilerBinds.forEach(bind -> {
-                        config.getVolumes().getMap().put(bind.getHostPath(), bind.getContainerPath());
+                        //config.getVolumes().getMap().put(bind.getHostPath(), bind.getContainerPath());
+                        config.getVolumes().getMap().put(bind.getContainerPath(), bind.getHostPath());
                     });
 
                     codeCompiler = new DockerCompiler(config);

+ 7 - 2
mgr/src/main/java/cn/reghao/devops/mgr/mgr/build/tool/compiler/DockerCompiler.java

@@ -19,7 +19,12 @@ public class DockerCompiler implements CodeCompiler {
 
     @Override
     public void compile(String appId, String appCompileHome) throws Exception {
-        config.getVolumes().getMap().put(appCompileHome, "/app");
-        docker.runAndRm(config);
+        Config config1;
+        synchronized(this) {
+            config.getVolumes().getMap().put("/app", appCompileHome);
+            config1 = (Config) config.clone();
+        }
+
+        docker.runAndRm(config1);
     }
 }