reghao 5 лет назад
Родитель
Сommit
320c951d98

+ 3 - 3
common/src/main/java/cn/reghao/autodop/common/dockerc/Docker.java

@@ -128,7 +128,7 @@ public class Docker implements ImageOps, ContainerOps, AutoCloseable {
     }
     }
 
 
     @Override
     @Override
-    public void push(String image) throws DockerException {
+    public void push(String image) throws DockerException, InterruptedException {
         String uri = DockerApi.pushPost.replace("{}", image);
         String uri = DockerApi.pushPost.replace("{}", image);
 
 
         DockerAuth auth = new DockerAuth();
         DockerAuth auth = new DockerAuth();
@@ -161,13 +161,13 @@ public class Docker implements ImageOps, ContainerOps, AutoCloseable {
     }
     }
 
 
     @Override
     @Override
-    public String inspectImage(String image) {
+    public String inspectImage(String image) throws DockerException {
         String uri = DockerApi.inspectImageGet.replace("{}", image);
         String uri = DockerApi.inspectImageGet.replace("{}", image);
         FullHttpResponse response = client.get(uri, null);
         FullHttpResponse response = client.get(uri, null);
         if (response != null) {
         if (response != null) {
             return response.content().toString(StandardCharsets.UTF_8);
             return response.content().toString(StandardCharsets.UTF_8);
         } else {
         } else {
-            return null;
+            throw new DockerException("Docker Response is NULL");
         }
         }
     }
     }
 
 

+ 2 - 2
common/src/main/java/cn/reghao/autodop/common/dockerc/api/ImageOps.java

@@ -29,7 +29,7 @@ public interface ImageOps {
      * @date 2019-12-10 下午6:08
      * @date 2019-12-10 下午6:08
      */
      */
     void build(String image, String compileHome, String dockerfile) throws Exception;
     void build(String image, String compileHome, String dockerfile) throws Exception;
-    void push(String image) throws DockerException;
+    void push(String image) throws DockerException, InterruptedException;
     void pull(String image) throws DockerException;
     void pull(String image) throws DockerException;
     void rmImage(String image) throws DockerException;
     void rmImage(String image) throws DockerException;
     /**
     /**
@@ -39,5 +39,5 @@ public interface ImageOps {
      * @return 镜像详细信息
      * @return 镜像详细信息
      * @date 2020-01-15 下午9:24
      * @date 2020-01-15 下午9:24
      */
      */
-    String inspectImage(String image);
+    String inspectImage(String image) throws DockerException;
 }
 }

+ 21 - 6
common/src/main/java/cn/reghao/autodop/common/dockerc/unixdomain/HttpClient.java

@@ -29,7 +29,11 @@ public class HttpClient {
             });
             });
         }
         }
 
 
-        return client.syncSend(request);
+        try {
+            return client.syncSend(request);
+        } catch (InterruptedException e) {
+            return null;
+        }
     }
     }
 
 
     public FullHttpResponse post(String uri, List<DockerHeader> headers) {
     public FullHttpResponse post(String uri, List<DockerHeader> headers) {
@@ -42,7 +46,11 @@ public class HttpClient {
             });
             });
         }
         }
 
 
-        return client.syncSend(request);
+        try {
+            return client.syncSend(request);
+        } catch (InterruptedException e) {
+            return null;
+        }
     }
     }
 
 
     public FullHttpResponse postJson(String uri, List<DockerHeader> headers, String json) {
     public FullHttpResponse postJson(String uri, List<DockerHeader> headers, String json) {
@@ -60,7 +68,11 @@ public class HttpClient {
         }
         }
         request.content().clear().writeBytes(byteBuf);
         request.content().clear().writeBytes(byteBuf);
 
 
-        return client.syncSend(request);
+        try {
+            return client.syncSend(request);
+        } catch (InterruptedException e) {
+            return null;
+        }
     }
     }
 
 
     public FullHttpResponse postFile(String uri, List<DockerHeader> headers, File file) {
     public FullHttpResponse postFile(String uri, List<DockerHeader> headers, File file) {
@@ -82,9 +94,8 @@ public class HttpClient {
             return client.syncSend(request);
             return client.syncSend(request);
         } catch (Exception e) {
         } catch (Exception e) {
             e.printStackTrace();
             e.printStackTrace();
+            return null;
         }
         }
-
-        return null;
     }
     }
 
 
     private byte[] transfer(File file) throws IOException {
     private byte[] transfer(File file) throws IOException {
@@ -110,7 +121,11 @@ public class HttpClient {
             });
             });
         }
         }
 
 
-        return client.syncSend(request);
+        try {
+            return client.syncSend(request);
+        } catch (InterruptedException e) {
+            return null;
+        }
     }
     }
 
 
     public void close() {
     public void close() {

+ 4 - 19
common/src/main/java/cn/reghao/autodop/common/dockerc/unixdomain/UnixSocketClient.java

@@ -10,6 +10,7 @@ import io.netty.handler.codec.http.*;
 import org.slf4j.Logger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
 
 
+import java.io.IOException;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeUnit;
 
 
 /**
 /**
@@ -55,7 +56,7 @@ public class UnixSocketClient {
                log.info("已连接到 Unix Socket");
                log.info("已连接到 Unix Socket");
                channel = channelFuture.channel();
                channel = channelFuture.channel();
            } else {
            } else {
-               log.info("Unix Socket 连接失败...");
+               throw new IOException("Unix Socket 连接失败...");
            }
            }
         });
         });
 
 
@@ -73,25 +74,9 @@ public class UnixSocketClient {
         }
         }
     }
     }
 
 
-    public FullHttpResponse syncSend(FullHttpRequest request) {
+    public FullHttpResponse syncSend(FullHttpRequest request) throws InterruptedException {
         ChannelPromise promise = clientHandler.sendRequest(request);
         ChannelPromise promise = clientHandler.sendRequest(request);
-        /*promise.addListener(new ChannelFutureListener() {
-            @Override
-            public void operationComplete(ChannelFuture future) throws Exception {
-                if (future.isSuccess()) {
-                    future.get();
-                } else {
-                    log.info("request failed...");
-                }
-            }
-        });*/
-
-        int second = 30;
-        try {
-            promise.await(second, TimeUnit.SECONDS);
-        } catch (InterruptedException e) {
-            e.printStackTrace();
-        }
+        promise.await(30, TimeUnit.SECONDS);
         return clientHandler.response();
         return clientHandler.response();
     }
     }
 }
 }