|
|
@@ -13,11 +13,13 @@ import cn.reghao.autodop.common.dockerc.pojo.Container;
|
|
|
import cn.reghao.autodop.common.dockerc.pojo.Config;
|
|
|
import cn.reghao.autodop.common.dockerc.pojo.ContainerInspect;
|
|
|
import cn.reghao.autodop.common.dockerc.pojo.HostConfig;
|
|
|
-import cn.reghao.autodop.common.utils.JsonUtil;
|
|
|
+import cn.reghao.autodop.common.utils.data.serializer.JsonArrayDeserializer;
|
|
|
+import cn.reghao.autodop.common.utils.data.serializer.JsonConverter;
|
|
|
import cn.reghao.autodop.common.utils.security.Base64Util;
|
|
|
-import cn.reghao.autodop.common.utils.compression.TarUtil;
|
|
|
+import cn.reghao.autodop.common.utils.compression.TarFiles;
|
|
|
import cn.reghao.autodop.common.utils.security.Md5Util;
|
|
|
import com.google.gson.JsonObject;
|
|
|
+import com.google.gson.JsonParser;
|
|
|
import io.netty.handler.codec.http.FullHttpResponse;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
@@ -85,7 +87,7 @@ public class DockerClient implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
@Override
|
|
|
public void build(String repoTag, String appdir) throws Exception {
|
|
|
String dstPath = "/tmp/" + Md5Util.md5(appdir) + ".tar";
|
|
|
- TarUtil.tar(appdir, dstPath);
|
|
|
+ TarFiles.tar(appdir, dstPath);
|
|
|
|
|
|
String uri = DockerApi.buildPost + "?t=" + repoTag;
|
|
|
File file = new File(dstPath);
|
|
|
@@ -101,7 +103,7 @@ public class DockerClient implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
@Override
|
|
|
public void build(String repoTag, String appdir, String dockerfile) throws Exception {
|
|
|
String dstPath = "/tmp/" + Md5Util.md5(appdir) + ".tar";
|
|
|
- TarUtil.tar(appdir, dstPath);
|
|
|
+ TarFiles.tar(appdir, dstPath);
|
|
|
|
|
|
//String uri = DockerApi.buildPost + "?t=" + repoTag + "&dockerfile=" + dockerfile;
|
|
|
String uri = DockerApi.buildPost + "?t=" + repoTag;
|
|
|
@@ -128,7 +130,7 @@ public class DockerClient implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
auth.setPassword("12345678");
|
|
|
auth.setEmail("reghaodev@gmail.com");
|
|
|
auth.setServeraddress("localhost");
|
|
|
- String encodeAuth = Base64Util.encode(JsonUtil.objectToJson(auth));
|
|
|
+ String encodeAuth = Base64Util.encode(JsonConverter.objectToJson(auth));
|
|
|
List<DockerHeader> headers = new ArrayList<>();
|
|
|
headers.add(new DockerHeader("X-Registry-Auth", encodeAuth));
|
|
|
|
|
|
@@ -177,7 +179,7 @@ public class DockerClient implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
// TODO 挂载日志目录到宿主机
|
|
|
Config config = Config.builder()
|
|
|
.image(image).hostConfig(hostConfig).build();
|
|
|
- String tmp = JsonUtil.objectToJson(config);
|
|
|
+ String tmp = JsonConverter.objectToJson(config);
|
|
|
String json = tmp.replace("image", "Image")
|
|
|
.replace("hostConfig", "HostConfig")
|
|
|
.replace("networkMode", "NetworkMode");
|
|
|
@@ -188,7 +190,7 @@ public class DockerClient implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
int statusCode = response.status().code();
|
|
|
String result = response.content().toString(StandardCharsets.UTF_8);
|
|
|
if (statusCode == 201) {
|
|
|
- JsonObject jsonObject = JsonUtil.jsonObject(result);
|
|
|
+ JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
|
|
|
return jsonObject.get("Id").getAsString();
|
|
|
} else {
|
|
|
throw new DockerException(result);
|
|
|
@@ -241,13 +243,9 @@ public class DockerClient implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
String uri = DockerApi.psGet + "?all=" + isAll;
|
|
|
FullHttpResponse response = client.get(uri, null);
|
|
|
String result = checkResponse(response);
|
|
|
- List<Object> objs = JsonUtil.jsonToObjectArray(result, Container.class);
|
|
|
- List<Container> containers = new ArrayList<>();
|
|
|
- objs.forEach(obj -> {
|
|
|
- containers.add((Container) obj);
|
|
|
- });
|
|
|
|
|
|
- return containers;
|
|
|
+ JsonArrayDeserializer<Container> jsonArrayDeserializer = new JsonArrayDeserializer<>();
|
|
|
+ return jsonArrayDeserializer.fromJsonArray(result, Container.class);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -347,7 +345,7 @@ public class DockerClient implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
FullHttpResponse response = client.get(uri, null);
|
|
|
if (response != null) {
|
|
|
String result = response.content().toString(StandardCharsets.UTF_8);
|
|
|
- return (ContainerInspect) JsonUtil.jsonToObject(result, ContainerInspect.class);
|
|
|
+ return (ContainerInspect) JsonConverter.jsonToObject(result, ContainerInspect.class);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
@@ -362,13 +360,14 @@ public class DockerClient implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
String appEntryDir = "/home/reghao/opt/data/autodop/compile-dir/dnkt-mgr/IQuizoo.BMS";
|
|
|
try (DockerClient docker = new DockerClient()) {
|
|
|
- String image = "hahahaha1";
|
|
|
+ List<Container> containers = docker.ps(true);
|
|
|
|
|
|
+ String image = "hahahaha1";
|
|
|
// Dockerfile 默认在 appEntryDir 中
|
|
|
// TODO Dockerfile 错误导致不能成功构建时,不会抛出异常
|
|
|
- docker.build(image, appEntryDir);
|
|
|
+ //docker.build(image, appEntryDir);
|
|
|
// TODO 有时不能成功 push,特别是 push 到远程主机上时
|
|
|
- docker.push(image);
|
|
|
+ //docker.push(image);
|
|
|
// TODO try-with-resource 中不需要显式调用 close
|
|
|
}
|
|
|
}
|