Bläddra i källkod

调整 docker 客户端实现

reghao 6 år sedan
förälder
incheckning
f3932f5995

+ 8 - 0
common/src/main/java/cn/reghao/autodop/common/docker/ContainerConfig.java

@@ -7,10 +7,12 @@ package cn.reghao.autodop.common.docker;
 public class ContainerConfig {
     private String image;
     private HostConfig hostConfig;
+    //private Map<String, String> volumes;
 
     public ContainerConfig(Builder builder) {
         this.image = builder.image;
         this.hostConfig = builder.hostConfig;
+        //this.volumes = builder.volumes;
     }
 
     public static Builder builder() {
@@ -20,6 +22,7 @@ public class ContainerConfig {
     public static class Builder {
         private String image;
         private HostConfig hostConfig;
+        //private Map<String, String> volumes;
 
         public Builder() {
         }
@@ -34,6 +37,11 @@ public class ContainerConfig {
             return this;
         }
 
+        /*public Builder volumes(final Map<String, String> volumes) {
+            this.volumes = volumes;
+            return this;
+        }*/
+
         public ContainerConfig build() {
             return new ContainerConfig(this);
         }

+ 11 - 0
common/src/main/java/cn/reghao/autodop/common/docker/ContainerOps.java

@@ -21,6 +21,7 @@ public interface ContainerOps {
     void stop(String containerId) throws DockerException;
     void restart(String containerId) throws DockerException;
     void rmContainer(String containerId) throws DockerException;
+
     /**
      * 查看容器状态
      *
@@ -29,6 +30,7 @@ public interface ContainerOps {
      * @date 2020-01-19 下午1:38
      */
     List<Container> ps(boolean isAll) throws DockerException;
+
     /**
      * 容器日志
      *
@@ -38,6 +40,7 @@ public interface ContainerOps {
      * @date 2020-01-19 下午1:39
      */
     List<String> logs(String containerId, int type) throws IOException;
+
     /**
      * 查看容器的详细信息
      *
@@ -46,5 +49,13 @@ public interface ContainerOps {
      * @date 2020-01-19 下午1:39
      */
     String inspectContainer(String contianerId);
+
+    /**
+     * 容器映射到宿主机上的根目录
+     *
+     * @param
+     * @return
+     * @date 2020-03-13 下午10:36
+     */
     String rootfs(String containerId);
 }

+ 30 - 6
common/src/main/java/cn/reghao/autodop/common/docker/DockerClient.java

@@ -9,18 +9,18 @@ import cn.reghao.autodop.common.utils.security.Md5Util;
 import cn.reghao.autodop.common.utils.text.TextFile;
 import com.google.gson.JsonObject;
 import io.netty.handler.codec.http.FullHttpResponse;
+import lombok.extern.slf4j.Slf4j;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.*;
 import java.nio.charset.StandardCharsets;
 import java.security.NoSuchAlgorithmException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
+import java.util.*;
 
 /**
  * Docker 客户端
+ * TODO 执行完成后需要清理资源,关闭连接
  *
  * @author reghao
  * @date 2020-01-13 14:16:38
@@ -126,16 +126,21 @@ public class DockerClient implements ImageOps, ContainerOps, AutoCloseable {
         }
     }
 
+    /* container 操作 */
     private String create(String name, String image) throws DockerException {
         String uri = DockerApi.createPost + "?name=" + name;
 
         HostConfig hostConfig = HostConfig.builder().networkMode("host").build();
+        /*Map<String, String> volumes = new HashMap<>();
+        volumes.put("/home/reghao/tmp/autodop/opt/logs", "/app/Logs");*/
+        // TODO 挂载日志目录到宿主机
         ContainerConfig containerConfig = ContainerConfig.builder()
                 .image(image).hostConfig(hostConfig).build();
         String tmp = JsonUtil.objectToJson(containerConfig);
         String json = tmp.replace("image", "Image")
                 .replace("hostConfig", "HostConfig")
                 .replace("networkMode", "NetworkMode");
+                //.replace("volumes", "Volumes");
 
         FullHttpResponse response = client.postJson(uri, null, json);
         if (response != null) {
@@ -152,11 +157,9 @@ public class DockerClient implements ImageOps, ContainerOps, AutoCloseable {
         return null;
     }
 
-    /* container 操作 */
-    // TODO 挂载宿主机目录
-    // TODO 停止并删除当前正在运行的应用,然后运行新应用
     @Override
     public String run(String name, String image) throws DockerException {
+        stopAndDelete(name);
         String containerId = create(name, image);
         if (containerId != null) {
             start(containerId);
@@ -206,6 +209,27 @@ public class DockerClient implements ImageOps, ContainerOps, AutoCloseable {
         return containers;
     }
 
+    /**
+     * 停止并删除容器
+     *
+     * @param
+     * @return
+     * @date 2020-03-13 下午10:16
+     */
+    public void stopAndDelete(String containerName) throws DockerException {
+        List<Container> containers = ps(true);
+        String name = "/" + containerName;
+        for (Container container : containers) {
+            if (container.getNames()[0].equals(name)) {
+                String id = container.getId();
+                if ("running".equals(container.getState())) {
+                    stop(id);
+                }
+                rmContainer(id);
+            }
+        }
+    }
+
     @Override
     public List<String> logs(String containerId, int type) throws IOException {
         String uri = DockerApi.logsGet.replace("{}", containerId);

+ 1 - 0
dagent/src/main/java/cn/reghao/autodop/dagent/service/DockerAppServiceImpl.java

@@ -22,6 +22,7 @@ public class DockerAppServiceImpl implements AppService {
 
     @Override
     public void run(String appId, String version) throws DockerException {
+
         log.info("运行 {} 应用的 {} 版本", appId, version);
     }
 

+ 10 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/controller/BuildController.java

@@ -93,7 +93,17 @@ public class BuildController {
     @PostMapping("/deploy")
     public String deployApp(@RequestParam("appId") String appId, @RequestParam("appPath") String appPath)
             throws Exception {
+        return null;
+    }
 
+    @ApiOperation(value = "运行应用")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name="appId", value="应用标识", paramType="query", dataType = "String"),
+            @ApiImplicitParam(name="appPath", value="应用版本", paramType="query", dataType = "String")
+    })
+    @PostMapping("/run")
+    public String runApp(@RequestParam("appId") String appId, @RequestParam("appVersion") String appVersion)
+            throws Exception {
         return null;
     }
 

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

@@ -153,7 +153,7 @@ public class BuildDispatcher {
     }
 
     /**
-     * TODO 系统在初始化完成后自动填充属于项目的应用,并在项目有改动后自动更新
+     * TODO 是否需要系统在初始化完成后自动填充属于项目的应用,并在项目有改动后自动更新
      * 填充项目中的应用在构建阶段的一些配置
      * 属于同一项目的应用具有很多共性,在构建阶段,只有仓库地址和依赖地址不同
      *

+ 9 - 0
dmaster/src/main/java/cn/reghao/autodop/dmaster/app/service/build/Deployer.java

@@ -45,6 +45,15 @@ public class Deployer {
             Object object = deployService.deploy(deployConfig);
             JSONObject jsonObject = (JSONObject) object;
             BuildDeployResult result = jsonObject.toJavaObject(BuildDeployResult.class);
+
+            // TODO 若部署成功,则检查应用是否成功运行,若启动失败,则回退到上一个版本。若上一个版本仍失败,则返回错误并发出紧急通知
+            if (isRun && "成功".equals(result.getMsg())) {
+                String healthCheck = app.getRunning().getHealthCheck();
+                int port = app.getRunning().getPort();
+                // 健康检查 URL
+                String url = "http://" + host + ":" + port + healthCheck;
+            }
+
             results.add(result);
         }