Selaa lähdekoodia

修复 docker 运行应用时的 bug

dmaster 和 dagent 已部署到测试环境
reghao 4 vuotta sitten
vanhempi
commit
c2855fb07e

+ 4 - 17
common/src/main/java/cn/reghao/autodop/common/docker/Docker.java

@@ -179,9 +179,10 @@ public class Docker implements ImageOps, ContainerOps, AutoCloseable {
 
     /* container 操作 */
     @Override
-    public String run(String containerName, String repoTag, Config config) throws DockerException {
+    public String run(String containerName, Config config) throws DockerException {
         stopAndDelete(containerName);
-        String containerId = create(containerName, repoTag, config);
+        //config.setImage(repoTag);
+        String containerId = create(containerName, config);
         start(containerId);
         return containerId;
     }
@@ -209,10 +210,8 @@ public class Docker implements ImageOps, ContainerOps, AutoCloseable {
         }
     }
 
-    private String create(String containerName, String repoTag, Config config) throws DockerException {
+    private String create(String containerName, Config config) throws DockerException {
         String uri = DockerApi.createPost + "?name=" + containerName;
-
-        config.setImage(repoTag);
         String json = JsonConverter.objectToJson(config);
         try {
             FullHttpResponse response = client.postJson(uri, null, json);
@@ -420,16 +419,4 @@ public class Docker implements ImageOps, ContainerOps, AutoCloseable {
 
         }
     }
-
-    public static void main(String[] args) {
-        try (Docker docker = new Docker()) {
-            String image = "test:123456";
-            docker.build(image, "appCompileHome", "dockerfile");
-            log.info("{} 构建完成...", image);
-            docker.push(image);
-            log.info("{} 已 push 到仓库...", image);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
 }

+ 1 - 2
common/src/main/java/cn/reghao/autodop/common/docker/api/ContainerOps.java

@@ -18,12 +18,11 @@ public interface ContainerOps {
      * 运行镜像
      *
      * @param containerName 容器运行时的名字
-     * @param repoTag 运行的 docker image
      * @param config 容器运行时配置
      * @return 容器 ID
      * @date 2020-01-11 下午9:37
      */
-    String run(String containerName, String repoTag, Config config) throws DockerException;
+    String run(String containerName, Config config) throws DockerException;
 
     void start(String containerId) throws DockerException;
     void restart(String containerId) throws DockerException;

+ 2 - 2
common/src/main/java/cn/reghao/autodop/common/docker/pojo/Config.java

@@ -39,8 +39,8 @@ public class Config {
     @SerializedName("HostConfig") private HostConfig hostConfig;
     @SerializedName("NetworkingConfig") private NetworkingConfig networkingConfig;
 
-    public Config(String image, HostConfig hostConfig) {
+    public Config(String image) {
         this.image = image;
-        this.hostConfig = hostConfig;
+        this.hostConfig = new HostConfig();
     }
 }

+ 5 - 0
common/src/main/java/cn/reghao/autodop/common/docker/pojo/HostConfig.java

@@ -15,4 +15,9 @@ import lombok.Data;
 public class HostConfig {
     @SerializedName("NetworkMode") private String networkMode;
     @SerializedName("RestartPolicy") private RestartPolicy restartPolicy;
+
+    public HostConfig() {
+        this.networkMode = "host";
+        this.restartPolicy = new RestartPolicy();
+    }
 }

+ 5 - 0
common/src/main/java/cn/reghao/autodop/common/docker/pojo/RestartPolicy.java

@@ -13,4 +13,9 @@ import lombok.Data;
 public class RestartPolicy {
     @SerializedName("Name") private String name;
     @SerializedName("MaximumRetryCount") private int maximumRetryCount;
+
+    public RestartPolicy() {
+        this.name = "on-failure";
+        this.maximumRetryCount = 3;
+    }
 }

+ 4 - 4
dagent/src/main/java/cn/reghao/autodop/dagent/app/AppService.java

@@ -14,6 +14,10 @@ import java.util.List;
  */
 public interface AppService {
     AppStatus deploy(AppDeployArgs appDeployArgs) throws DockerException;
+    AppStatus status(String appId) throws DockerException;
+    AppStatus restart(String appId) throws DockerException;
+    AppStatus stop(String appId) throws DockerException;
+    AppStatus start(String appId) throws DockerException;
     /**
      * 日志文件列表
      *
@@ -31,8 +35,4 @@ public interface AppService {
      * @date 2021-02-26 下午3:26
      */
     List<String> logContent(AppLogArgs appLogArgs) throws DockerException;
-    AppStatus status(String appId) throws DockerException;
-    AppStatus restart(String appId) throws DockerException;
-    AppStatus stop(String appId) throws DockerException;
-    AppStatus start(String appId) throws DockerException;
 }

+ 61 - 52
dagent/src/main/java/cn/reghao/autodop/dagent/app/DockerAppServiceImpl.java

@@ -9,6 +9,7 @@ import cn.reghao.autodop.common.dagent.app.api.data.log.LogType;
 import cn.reghao.autodop.common.docker.Docker;
 import cn.reghao.autodop.common.docker.exception.DockerException;
 import cn.reghao.autodop.common.docker.pojo.Config;
+import cn.reghao.autodop.common.docker.pojo.HostConfig;
 import cn.reghao.autodop.common.docker.pojo.State;
 import cn.reghao.autodop.common.docker.pojo.result.ContainerInfo;
 import cn.reghao.autodop.common.utils.DateTimeConverter;
@@ -37,10 +38,18 @@ public class DockerAppServiceImpl implements AppService {
         String appId = appDeployArgs.getAppId();
         String packagePath = appDeployArgs.getPackagePath();
         Config dockerConfig = JsonConverter.jsonToObject(appDeployArgs.getStartScript(), Config.class);
+        if (dockerConfig == null) {
+            dockerConfig = new Config(packagePath);
+        } else {
+            dockerConfig.setImage(packagePath);
+            if (dockerConfig.getHostConfig() == null) {
+                dockerConfig.setHostConfig(new HostConfig());
+            }
+        }
 
         try (Docker docker = new Docker()) {
             docker.pull(packagePath);
-            String containerId = docker.run(appId, packagePath, dockerConfig);
+            String containerId = docker.run(appId, dockerConfig);
             Thread.sleep(sleep);
 
             ContainerInfo containerInfo = docker.inspectContainer(containerId);
@@ -69,6 +78,57 @@ public class DockerAppServiceImpl implements AppService {
         return appStatus;
     }
 
+    @Override
+    public AppStatus status(String appId) throws DockerException {
+        try (Docker docker = new Docker()) {
+            String containerId = docker.getIdByName(appId);
+            if (containerId == null) {
+                throw new DockerException("没有和 " + appId + " 关联的容器");
+            }
+            ContainerInfo containerInfo = docker.inspectContainer(containerId);
+            return getAppStatus(appId, containerInfo);
+        }
+    }
+
+    @Override
+    public AppStatus restart(String appId) throws DockerException {
+        try (Docker docker = new Docker()) {
+            String containerId = docker.getIdByName(appId);
+            docker.restart(containerId);
+            Thread.sleep(sleep);
+
+            ContainerInfo containerInfo = docker.inspectContainer(containerId);
+            return getAppStatus(appId, containerInfo);
+        } catch (InterruptedException e) {
+            throw new DockerException(ExceptionUtil.errorMsg(e));
+        }
+    }
+
+    @Override
+    public AppStatus stop(String appId) throws DockerException {
+        try (Docker docker = new Docker()) {
+            String containerId = docker.getIdByName(appId);
+            docker.stop(containerId);
+
+            ContainerInfo containerInfo = docker.inspectContainer(containerId);
+            return getAppStatus(appId, containerInfo);
+        }
+    }
+
+    @Override
+    public AppStatus start(String appId) throws DockerException {
+        try (Docker docker = new Docker()) {
+            String containerId = docker.getIdByName(appId);
+            docker.start(containerId);
+            Thread.sleep(sleep);
+
+            ContainerInfo containerInfo = docker.inspectContainer(containerId);
+            return getAppStatus(appId, containerInfo);
+        } catch (InterruptedException e) {
+            throw new DockerException(ExceptionUtil.errorMsg(e));
+        }
+    }
+
     @Override
     public List<LogFile> logFiles(AppLogArgs appLogArgs) throws DockerException {
         String appId = appLogArgs.getAppId();
@@ -120,55 +180,4 @@ public class DockerAppServiceImpl implements AppService {
             }
         }
     }
-
-    @Override
-    public AppStatus status(String appId) throws DockerException {
-        try (Docker docker = new Docker()) {
-            String containerId = docker.getIdByName(appId);
-            if (containerId == null) {
-                throw new DockerException("没有和 " + appId + " 关联的容器");
-            }
-            ContainerInfo containerInfo = docker.inspectContainer(containerId);
-            return getAppStatus(appId, containerInfo);
-        }
-    }
-
-    @Override
-    public AppStatus restart(String appId) throws DockerException {
-        try (Docker docker = new Docker()) {
-            String containerId = docker.getIdByName(appId);
-            docker.restart(containerId);
-            Thread.sleep(sleep);
-
-            ContainerInfo containerInfo = docker.inspectContainer(containerId);
-            return getAppStatus(appId, containerInfo);
-        } catch (InterruptedException e) {
-            throw new DockerException(ExceptionUtil.errorMsg(e));
-        }
-    }
-
-    @Override
-    public AppStatus stop(String appId) throws DockerException {
-        try (Docker docker = new Docker()) {
-            String containerId = docker.getIdByName(appId);
-            docker.stop(containerId);
-
-            ContainerInfo containerInfo = docker.inspectContainer(containerId);
-            return getAppStatus(appId, containerInfo);
-        }
-    }
-
-    @Override
-    public AppStatus start(String appId) throws DockerException {
-        try (Docker docker = new Docker()) {
-            String containerId = docker.getIdByName(appId);
-            docker.start(containerId);
-            Thread.sleep(sleep);
-
-            ContainerInfo containerInfo = docker.inspectContainer(containerId);
-            return getAppStatus(appId, containerInfo);
-        } catch (InterruptedException e) {
-            throw new DockerException(ExceptionUtil.errorMsg(e));
-        }
-    }
 }

+ 4 - 0
scripts/shutdown.sh

@@ -1 +1,5 @@
 #!/bin/bash
+
+app='autodop-dagent.jar'
+pid=`jps | grep ${app} | awk '{print $1}'`
+kill -15 ${pid}

+ 3 - 0
scripts/start.sh

@@ -1 +1,4 @@
 #!/bin/bash
+
+app_path='/opt/apps/dagent/autodop-dagent.jar'
+nohup java -jar ${app_path} > console.log 2>&1 &