|
|
@@ -10,6 +10,7 @@ import cn.reghao.autodop.common.dockerc.unixdomain.DockerAuth;
|
|
|
import cn.reghao.autodop.common.dockerc.unixdomain.DockerHeader;
|
|
|
import cn.reghao.autodop.common.dockerc.unixdomain.UnixSocketClient;
|
|
|
import cn.reghao.autodop.common.dockerc.unixdomain.HttpClient;
|
|
|
+import cn.reghao.autodop.common.utils.ExceptionUtil;
|
|
|
import cn.reghao.autodop.common.utils.serializer.JsonArrayDeserializer;
|
|
|
import cn.reghao.autodop.common.utils.serializer.JsonConverter;
|
|
|
import cn.reghao.autodop.common.utils.security.Base64Util;
|
|
|
@@ -68,7 +69,7 @@ public class Docker implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
}
|
|
|
return result;
|
|
|
} else {
|
|
|
- throw new NullPointerException("Docker Response is NULL");
|
|
|
+ throw new DockerException("Docker 响应是 NULL...");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -128,7 +129,7 @@ public class Docker implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void push(String image) throws DockerException, InterruptedException {
|
|
|
+ public void push(String image) throws DockerException {
|
|
|
String uri = DockerApi.pushPost.replace("{}", image);
|
|
|
|
|
|
DockerAuth auth = new DockerAuth();
|
|
|
@@ -140,8 +141,12 @@ public class Docker implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
List<DockerHeader> headers = new ArrayList<>();
|
|
|
headers.add(new DockerHeader("X-Registry-Auth", encodeAuth));
|
|
|
|
|
|
- FullHttpResponse response = client.post(uri, headers);
|
|
|
- checkResponse(response);
|
|
|
+ try {
|
|
|
+ FullHttpResponse response = client.post(uri, headers);
|
|
|
+ checkResponse(response);
|
|
|
+ } catch (IOException | InterruptedException e) {
|
|
|
+ throw new DockerException(ExceptionUtil.errorMsg(e));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -149,25 +154,37 @@ public class Docker implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
String params = "?fromImage=" + repoTag;
|
|
|
String uri = DockerApi.pullPost + params;
|
|
|
|
|
|
- FullHttpResponse response = client.post(uri, null);
|
|
|
- checkResponse(response);
|
|
|
+ try {
|
|
|
+ FullHttpResponse response = client.post(uri, null);
|
|
|
+ checkResponse(response);
|
|
|
+ } catch (IOException | InterruptedException e) {
|
|
|
+ throw new DockerException(ExceptionUtil.errorMsg(e));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void rmImage(String repoTag) throws DockerException {
|
|
|
String uri = DockerApi.rmImageDelete.replace("{}", repoTag);
|
|
|
- FullHttpResponse response = client.post(uri, null);
|
|
|
- checkResponse(response);
|
|
|
+ try {
|
|
|
+ FullHttpResponse response = client.post(uri, null);
|
|
|
+ checkResponse(response);
|
|
|
+ } catch (IOException | InterruptedException e) {
|
|
|
+ throw new DockerException(ExceptionUtil.errorMsg(e));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String inspectImage(String image) throws DockerException {
|
|
|
String uri = DockerApi.inspectImageGet.replace("{}", image);
|
|
|
- FullHttpResponse response = client.get(uri, null);
|
|
|
- if (response != null) {
|
|
|
- return response.content().toString(StandardCharsets.UTF_8);
|
|
|
- } else {
|
|
|
- throw new DockerException("Docker Response is NULL");
|
|
|
+ try {
|
|
|
+ FullHttpResponse response = client.get(uri, null);
|
|
|
+ if (response != null) {
|
|
|
+ return response.content().toString(StandardCharsets.UTF_8);
|
|
|
+ } else {
|
|
|
+ throw new DockerException("请求 " + uri + " 的响应是 NULL...");
|
|
|
+ }
|
|
|
+ } catch (IOException | InterruptedException e) {
|
|
|
+ throw new DockerException(ExceptionUtil.errorMsg(e));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -185,19 +202,22 @@ public class Docker implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
.replace("networkMode", "NetworkMode");
|
|
|
//.replace("volumes", "Volumes");
|
|
|
|
|
|
- FullHttpResponse response = client.postJson(uri, null, json);
|
|
|
- if (response != null) {
|
|
|
- int statusCode = response.status().code();
|
|
|
- String result = response.content().toString(StandardCharsets.UTF_8);
|
|
|
- if (statusCode == 201) {
|
|
|
- JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
|
|
|
- return jsonObject.get("Id").getAsString();
|
|
|
- } else {
|
|
|
- throw new DockerException(result);
|
|
|
+ try {
|
|
|
+ FullHttpResponse response = client.postJson(uri, null, json);
|
|
|
+ if (response != null) {
|
|
|
+ int statusCode = response.status().code();
|
|
|
+ String result = response.content().toString(StandardCharsets.UTF_8);
|
|
|
+ if (statusCode == 201) {
|
|
|
+ JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
|
|
|
+ return jsonObject.get("Id").getAsString();
|
|
|
+ } else {
|
|
|
+ throw new DockerException(result);
|
|
|
+ }
|
|
|
}
|
|
|
+ return null;
|
|
|
+ } catch (IOException | InterruptedException e) {
|
|
|
+ throw new DockerException(ExceptionUtil.errorMsg(e));
|
|
|
}
|
|
|
-
|
|
|
- return null;
|
|
|
}
|
|
|
|
|
|
private String create(String containerName, String repoTag, Config config) throws DockerException {
|
|
|
@@ -210,19 +230,22 @@ public class Docker implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
.replace("networkMode", "NetworkMode");
|
|
|
//.replace("volumes", "Volumes");
|
|
|
|
|
|
- FullHttpResponse response = client.postJson(uri, null, json);
|
|
|
- if (response != null) {
|
|
|
- int statusCode = response.status().code();
|
|
|
- String result = response.content().toString(StandardCharsets.UTF_8);
|
|
|
- if (statusCode == 201) {
|
|
|
- JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
|
|
|
- return jsonObject.get("Id").getAsString();
|
|
|
- } else {
|
|
|
- throw new DockerException(result);
|
|
|
+ try {
|
|
|
+ FullHttpResponse response = client.postJson(uri, null, json);
|
|
|
+ if (response != null) {
|
|
|
+ int statusCode = response.status().code();
|
|
|
+ String result = response.content().toString(StandardCharsets.UTF_8);
|
|
|
+ if (statusCode == 201) {
|
|
|
+ JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
|
|
|
+ return jsonObject.get("Id").getAsString();
|
|
|
+ } else {
|
|
|
+ throw new DockerException(result);
|
|
|
+ }
|
|
|
}
|
|
|
+ return null;
|
|
|
+ } catch (IOException | InterruptedException e) {
|
|
|
+ throw new DockerException(ExceptionUtil.errorMsg(e));
|
|
|
}
|
|
|
-
|
|
|
- return null;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -247,39 +270,58 @@ public class Docker implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
@Override
|
|
|
public void start(String containerId) throws DockerException {
|
|
|
String uri = DockerApi.startPost.replace("{}", containerId);
|
|
|
- FullHttpResponse response = client.post(uri, null);
|
|
|
- checkResponse(response);
|
|
|
+ try {
|
|
|
+ FullHttpResponse response = client.post(uri, null);
|
|
|
+ checkResponse(response);
|
|
|
+ } catch (IOException | InterruptedException e) {
|
|
|
+ throw new DockerException(ExceptionUtil.errorMsg(e));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void stop(String containerId) throws DockerException {
|
|
|
String uri = DockerApi.stopPost.replace("{}", containerId);
|
|
|
- FullHttpResponse response = client.post(uri, null);
|
|
|
- checkResponse(response);
|
|
|
+ try {
|
|
|
+ FullHttpResponse response = client.post(uri, null);
|
|
|
+ checkResponse(response);
|
|
|
+ } catch (IOException | InterruptedException e) {
|
|
|
+ throw new DockerException(ExceptionUtil.errorMsg(e));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void restart(String containerId) throws DockerException {
|
|
|
String uri = DockerApi.restartPost.replace("{}", containerId);
|
|
|
- FullHttpResponse response = client.post(uri, null);
|
|
|
- checkResponse(response);
|
|
|
+ try {
|
|
|
+ FullHttpResponse response = client.post(uri, null);
|
|
|
+ checkResponse(response);
|
|
|
+ } catch (IOException | InterruptedException e) {
|
|
|
+ throw new DockerException(ExceptionUtil.errorMsg(e));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void rmContainer(String containerId) throws DockerException {
|
|
|
String uri = DockerApi.rmContainerDelete.replace("{}", containerId);
|
|
|
- FullHttpResponse response = client.delete(uri, null);
|
|
|
- checkResponse(response);
|
|
|
+ try {
|
|
|
+ FullHttpResponse response = client.delete(uri, null);
|
|
|
+ checkResponse(response);
|
|
|
+ } catch (IOException | InterruptedException e) {
|
|
|
+ throw new DockerException(ExceptionUtil.errorMsg(e));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<Container> ps(boolean isAll) throws DockerException {
|
|
|
String uri = DockerApi.psGet + "?all=" + isAll;
|
|
|
- FullHttpResponse response = client.get(uri, null);
|
|
|
- String result = checkResponse(response);
|
|
|
-
|
|
|
- JsonArrayDeserializer<Container> jsonArrayDeserializer = new JsonArrayDeserializer<>();
|
|
|
- return jsonArrayDeserializer.fromJsonArray(result, Container.class);
|
|
|
+ try {
|
|
|
+ FullHttpResponse response = client.get(uri, null);
|
|
|
+ String result = checkResponse(response);
|
|
|
+ JsonArrayDeserializer<Container> jsonArrayDeserializer = new JsonArrayDeserializer<>();
|
|
|
+ return jsonArrayDeserializer.fromJsonArray(result, Container.class);
|
|
|
+ } catch (IOException | InterruptedException e) {
|
|
|
+ throw new DockerException(ExceptionUtil.errorMsg(e));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -312,29 +354,33 @@ public class Docker implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
* @date 2020-05-19 上午10:26
|
|
|
*/
|
|
|
@Override
|
|
|
- public List<String> logs(String containerId, String logLevel) {
|
|
|
+ public List<String> logs(String containerId, String logLevel) throws DockerException {
|
|
|
LogLevel level = LogLevel.valueOf(logLevel);
|
|
|
String uri = DockerApi.logsGet.replace("{}", containerId);
|
|
|
- FullHttpResponse response;
|
|
|
- switch (level) {
|
|
|
- case info:
|
|
|
- // stdout
|
|
|
- String params = "?stdout=true&tail=500";
|
|
|
- response = client.get(uri + params, null);
|
|
|
- if (response != null) {
|
|
|
- String stdout = response.content().toString(StandardCharsets.UTF_8);
|
|
|
- return Arrays.asList(stdout.split(System.lineSeparator()));
|
|
|
- }
|
|
|
- case error:
|
|
|
- // stderr
|
|
|
- params = "?stderr=true&tail=500";
|
|
|
- response = client.get(uri + params, null);
|
|
|
- if (response != null) {
|
|
|
- String stderr = response.content().toString(StandardCharsets.UTF_8);
|
|
|
- return Arrays.asList(stderr.split(System.lineSeparator()));
|
|
|
- }
|
|
|
- default:
|
|
|
- return null;
|
|
|
+ try {
|
|
|
+ FullHttpResponse response;
|
|
|
+ switch (level) {
|
|
|
+ case info:
|
|
|
+ // stdout
|
|
|
+ String params = "?stdout=true&tail=500";
|
|
|
+ response = client.get(uri + params, null);
|
|
|
+ if (response != null) {
|
|
|
+ String stdout = response.content().toString(StandardCharsets.UTF_8);
|
|
|
+ return Arrays.asList(stdout.split(System.lineSeparator()));
|
|
|
+ }
|
|
|
+ case error:
|
|
|
+ // stderr
|
|
|
+ params = "?stderr=true&tail=500";
|
|
|
+ response = client.get(uri + params, null);
|
|
|
+ if (response != null) {
|
|
|
+ String stderr = response.content().toString(StandardCharsets.UTF_8);
|
|
|
+ return Arrays.asList(stderr.split(System.lineSeparator()));
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ throw new DockerException("日志级别不存在...");
|
|
|
+ }
|
|
|
+ } catch (IOException | InterruptedException e) {
|
|
|
+ throw new DockerException(ExceptionUtil.errorMsg(e));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -373,18 +419,23 @@ public class Docker implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public ContainerInspect inspectContainer(String contianerId) {
|
|
|
+ public ContainerInspect inspectContainer(String contianerId) throws DockerException {
|
|
|
String uri = DockerApi.inspectContainerGet.replace("{}", contianerId);
|
|
|
- FullHttpResponse response = client.get(uri, null);
|
|
|
- if (response != null) {
|
|
|
- String result = response.content().toString(StandardCharsets.UTF_8);
|
|
|
- return (ContainerInspect) JsonConverter.jsonToObject(result, ContainerInspect.class);
|
|
|
+ try {
|
|
|
+ FullHttpResponse response = client.get(uri, null);
|
|
|
+ if (response != null) {
|
|
|
+ String result = response.content().toString(StandardCharsets.UTF_8);
|
|
|
+ return (ContainerInspect) JsonConverter.jsonToObject(result, ContainerInspect.class);
|
|
|
+ } else {
|
|
|
+ throw new DockerException("请求 " + uri + " 的响应是 NULL...");
|
|
|
+ }
|
|
|
+ } catch (IOException | InterruptedException e) {
|
|
|
+ throw new DockerException(ExceptionUtil.errorMsg(e));
|
|
|
}
|
|
|
- return null;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public String rootfs(String containerId) {
|
|
|
+ public String rootfs(String containerId) throws DockerException {
|
|
|
ContainerInspect containerInspect = inspectContainer(containerId);;
|
|
|
// TODO 可能抛出 null 异常
|
|
|
return containerInspect.getGraphDriver().getData().getMergedDir();
|
|
|
@@ -393,7 +444,7 @@ public class Docker implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
public static void main(String[] args) {
|
|
|
try (Docker docker = new Docker()) {
|
|
|
docker.pull("localhost:5000/iq3x/dnkt-mgr:dfc43519");
|
|
|
- } catch (Exception e) {
|
|
|
+ } catch (DockerException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|