|
|
@@ -1,297 +0,0 @@
|
|
|
-package cn.reghao.autodop.dmaster.app.service.build.tools.packer.docker;
|
|
|
-
|
|
|
-import cn.reghao.autodop.common.utils.JsonUtil;
|
|
|
-import cn.reghao.autodop.common.utils.compression.TarUtil;
|
|
|
-import cn.reghao.autodop.common.utils.security.Base64Util;
|
|
|
-import cn.reghao.autodop.common.utils.security.Md5Util;
|
|
|
-import cn.reghao.autodop.common.utils.text.TextFile;
|
|
|
-import cn.reghao.autodop.dmaster.app.service.build.tools.packer.docker.httpclient.UnixSocketClient;
|
|
|
-import cn.reghao.autodop.dmaster.app.service.build.tools.packer.docker.httpclient.WebClient;
|
|
|
-import com.google.gson.JsonObject;
|
|
|
-import io.netty.handler.codec.http.FullHttpResponse;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
-
|
|
|
-import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
-import java.security.NoSuchAlgorithmException;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
-/**
|
|
|
- * Docker 客户端
|
|
|
- *
|
|
|
- * @author reghao
|
|
|
- * @date 2020-01-13 14:16:38
|
|
|
- */
|
|
|
-public class DockerClient implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
- private static final Logger log = LoggerFactory.getLogger(DockerClient.class);
|
|
|
- private WebClient client;
|
|
|
-
|
|
|
- public DockerClient() {
|
|
|
- client = new WebClient(new UnixSocketClient());
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void close() {
|
|
|
- client.close();
|
|
|
- }
|
|
|
-
|
|
|
- private String checkResponse(FullHttpResponse response) throws DockerException {
|
|
|
- if (response != null) {
|
|
|
- int statusCode = response.status().code();
|
|
|
- String result = response.content().toString(StandardCharsets.UTF_8);
|
|
|
- if (statusCode >= 300) {
|
|
|
- throw new DockerException(result);
|
|
|
- }
|
|
|
-
|
|
|
- log.info("{}", result);
|
|
|
- return result;
|
|
|
- } else {
|
|
|
- throw new NullPointerException("docker response is null");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public String getIdByName(String name) throws DockerException {
|
|
|
- String wrappedName = "/" + name;
|
|
|
- List<Container> containers = ps(false);
|
|
|
- for (Container container : containers) {
|
|
|
- if (container.getNames()[0].equals(wrappedName)) {
|
|
|
- return container.getId();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- /* image 操作 */
|
|
|
-
|
|
|
- @Override
|
|
|
- public void build(String repoTag, String dockerfile) throws IOException, NoSuchAlgorithmException, DockerException {
|
|
|
- String dstPath = "/tmp/" + Md5Util.md5(dockerfile) + ".tar";
|
|
|
- TarUtil.tar(dockerfile, dstPath);
|
|
|
-
|
|
|
- String uri = DockerApi.buildPost + "?t=" + repoTag;
|
|
|
- File file = new File(dstPath);
|
|
|
- List<DockerHeader> headers = new ArrayList<>();
|
|
|
- headers.add(new DockerHeader("Content-Type", "application/tar"));
|
|
|
- headers.add(new DockerHeader("Expect", "100-continue"));
|
|
|
-
|
|
|
- FullHttpResponse response = client.postFile(uri, headers, file);
|
|
|
- file.delete();
|
|
|
- checkResponse(response);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void push(String image) throws DockerException {
|
|
|
- String uri = DockerApi.pushPost.replace("{}", image);
|
|
|
-
|
|
|
- DockerAuth auth = new DockerAuth();
|
|
|
- auth.setUsername("reghao");
|
|
|
- auth.setPassword("12345678");
|
|
|
- auth.setEmail("reghaodev@gmail.com");
|
|
|
- auth.setServeraddress("localhost");
|
|
|
- String encodeAuth = Base64Util.encode(JsonUtil.objectToJson(auth));
|
|
|
- List<DockerHeader> headers = new ArrayList<>();
|
|
|
- headers.add(new DockerHeader("X-Registry-Auth", encodeAuth));
|
|
|
-
|
|
|
- FullHttpResponse response = client.post(uri, headers);
|
|
|
- checkResponse(response);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void pull(String repoTag) throws DockerException {
|
|
|
- String params = "?fromImage=" + repoTag;
|
|
|
- String uri = DockerApi.pullPost + params;
|
|
|
-
|
|
|
- FullHttpResponse response = client.post(uri, null);
|
|
|
- checkResponse(response);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void rmImage(String repoTag) throws DockerException {
|
|
|
- String uri = DockerApi.rmImageDelete.replace("{}", repoTag);
|
|
|
- FullHttpResponse response = client.post(uri, null);
|
|
|
- checkResponse(response);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String inspectImage(String image) {
|
|
|
- String uri = DockerApi.inspectImageGet.replace("{}", image);
|
|
|
- FullHttpResponse response = client.get(uri, null);
|
|
|
- if (response != null) {
|
|
|
- return response.content().toString(StandardCharsets.UTF_8);
|
|
|
- } else {
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private String create(String name, String image) throws DockerException {
|
|
|
- String uri = DockerApi.createPost + "?name=" + name;
|
|
|
-
|
|
|
- HostConfig hostConfig = HostConfig.builder().networkMode("host").build();
|
|
|
- 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");
|
|
|
-
|
|
|
- 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 = JsonUtil.jsonObject(result);
|
|
|
- return jsonObject.get("Id").getAsString();
|
|
|
- } else {
|
|
|
- throw new DockerException(result);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- /* container 操作 */
|
|
|
-
|
|
|
- @Override
|
|
|
- public String run(String name, String image) throws DockerException {
|
|
|
- String containerId = create(name, image);
|
|
|
- if (containerId != null) {
|
|
|
- start(containerId);
|
|
|
- }
|
|
|
- return containerId;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void start(String containerId) throws DockerException {
|
|
|
- String uri = DockerApi.startPost.replace("{}", containerId);
|
|
|
- FullHttpResponse response = client.post(uri, null);
|
|
|
- checkResponse(response);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void stop(String containerId) throws DockerException {
|
|
|
- String uri = DockerApi.stopPost.replace("{}", containerId);
|
|
|
- FullHttpResponse response = client.post(uri, null);
|
|
|
- checkResponse(response);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void restart(String containerId) throws DockerException {
|
|
|
- String uri = DockerApi.restartPost.replace("{}", containerId);
|
|
|
- FullHttpResponse response = client.post(uri, null);
|
|
|
- checkResponse(response);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void rmContainer(String containerId) throws DockerException {
|
|
|
- String uri = DockerApi.rmContainerDelete.replace("{}", containerId);
|
|
|
- FullHttpResponse response = client.delete(uri, null);
|
|
|
- checkResponse(response);
|
|
|
- }
|
|
|
-
|
|
|
- @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);
|
|
|
- List<Object> objs = JsonUtil.jsonToObjectArray(result, Container.class);
|
|
|
- List<Container> containers = new ArrayList<>();
|
|
|
- objs.forEach(obj -> {
|
|
|
- containers.add((Container) obj);
|
|
|
- });
|
|
|
-
|
|
|
- return containers;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public List<String> logs(String containerId, int type) throws IOException {
|
|
|
- String uri = DockerApi.logsGet.replace("{}", containerId);
|
|
|
- FullHttpResponse response;
|
|
|
- switch (type) {
|
|
|
- case 0:
|
|
|
- // TODO stdout 和 stderr 输出的编码问题,详见 https://docs.docker.com/engine/api/v1.40/#operation/ContainerAttach
|
|
|
- // 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()));
|
|
|
- }
|
|
|
- break;
|
|
|
- case 1:
|
|
|
- // stderr
|
|
|
- params = "?stdout=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()));
|
|
|
- }
|
|
|
- break;
|
|
|
- case 2:
|
|
|
- // info 日志
|
|
|
- // TODO 通过 AppConfig 获取应用的日志路径
|
|
|
- String logDir = "/app/Logs/Info";
|
|
|
- String logFile = "/2020-01-19.log";
|
|
|
- String relativePath = logDir + logFile;
|
|
|
- return appLog(containerId, relativePath);
|
|
|
- case 3:
|
|
|
- // error 日志
|
|
|
- logDir = "/app/Logs/Info";
|
|
|
- logFile = "/2020-01-19.log";
|
|
|
- relativePath = logDir + logFile;
|
|
|
- return appLog(containerId, relativePath);
|
|
|
- }
|
|
|
-
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- List<String> appLog(String containerId, String filePath) throws IOException {
|
|
|
- String rootfs = rootfs(containerId);
|
|
|
- ///rootfs = "/home/reghao/Downloads";
|
|
|
- return new TextFile().tailRead(rootfs + filePath, 500);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String inspectContainer(String contianerId) {
|
|
|
- String uri = DockerApi.inspectContainerGet.replace("{}", contianerId);
|
|
|
- FullHttpResponse response = client.get(uri, null);
|
|
|
- if (response != null) {
|
|
|
- return response.content().toString(StandardCharsets.UTF_8);
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public String rootfs(String containerId) {
|
|
|
- String result = inspectContainer(containerId);
|
|
|
- ContainerInfo containerInfo = (ContainerInfo) JsonUtil.jsonToObject(result, ContainerInfo.class);
|
|
|
- // TODO 可能抛出 null 异常
|
|
|
- return containerInfo.getGraphDriver().getData().getMergedDir();
|
|
|
- }
|
|
|
-
|
|
|
- public static void main(String[] args) {
|
|
|
- try (DockerClient docker = new DockerClient()) {
|
|
|
- //String containerId = docker.run("user", "192.168.0.222:5000/iquizoo3x/user:12051_2020-01-15");
|
|
|
- //log.info("container id -> {}", containerId);
|
|
|
-
|
|
|
- /*List<String> logs = docker.logs(
|
|
|
- "f7e036d48a16e415c97a15a0b9693b8062d3d55abd67e75fb1fa44cde73a0d62", 2);
|
|
|
- log.info("-----log-----");
|
|
|
- logs.forEach(str -> {
|
|
|
- log.info("{}", str);
|
|
|
- });
|
|
|
- log.info("----------");*/
|
|
|
-
|
|
|
- //List<Container> containers = docker.ps(false);
|
|
|
- String containerId = docker.getIdByName("user");
|
|
|
- List<String> logs = docker.logs(containerId, 0);
|
|
|
- log.info("{}", containerId);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-}
|