|
|
@@ -9,18 +9,18 @@ import cn.reghao.autodop.common.utils.security.Md5Util;
|
|
|
import cn.reghao.autodop.common.utils.text.TextFile;
|
|
|
import com.google.gson.JsonObject;
|
|
|
import io.netty.handler.codec.http.FullHttpResponse;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* Docker 客户端
|
|
|
+ * TODO 执行完成后需要清理资源,关闭连接
|
|
|
*
|
|
|
* @author reghao
|
|
|
* @date 2020-01-13 14:16:38
|
|
|
@@ -126,16 +126,21 @@ public class DockerClient implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /* container 操作 */
|
|
|
private String create(String name, String image) throws DockerException {
|
|
|
String uri = DockerApi.createPost + "?name=" + name;
|
|
|
|
|
|
HostConfig hostConfig = HostConfig.builder().networkMode("host").build();
|
|
|
+ /*Map<String, String> volumes = new HashMap<>();
|
|
|
+ volumes.put("/home/reghao/tmp/autodop/opt/logs", "/app/Logs");*/
|
|
|
+ // TODO 挂载日志目录到宿主机
|
|
|
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");
|
|
|
+ //.replace("volumes", "Volumes");
|
|
|
|
|
|
FullHttpResponse response = client.postJson(uri, null, json);
|
|
|
if (response != null) {
|
|
|
@@ -152,11 +157,9 @@ public class DockerClient implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- /* container 操作 */
|
|
|
- // TODO 挂载宿主机目录
|
|
|
- // TODO 停止并删除当前正在运行的应用,然后运行新应用
|
|
|
@Override
|
|
|
public String run(String name, String image) throws DockerException {
|
|
|
+ stopAndDelete(name);
|
|
|
String containerId = create(name, image);
|
|
|
if (containerId != null) {
|
|
|
start(containerId);
|
|
|
@@ -206,6 +209,27 @@ public class DockerClient implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
return containers;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 停止并删除容器
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ * @date 2020-03-13 下午10:16
|
|
|
+ */
|
|
|
+ public void stopAndDelete(String containerName) throws DockerException {
|
|
|
+ List<Container> containers = ps(true);
|
|
|
+ String name = "/" + containerName;
|
|
|
+ for (Container container : containers) {
|
|
|
+ if (container.getNames()[0].equals(name)) {
|
|
|
+ String id = container.getId();
|
|
|
+ if ("running".equals(container.getState())) {
|
|
|
+ stop(id);
|
|
|
+ }
|
|
|
+ rmContainer(id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public List<String> logs(String containerId, int type) throws IOException {
|
|
|
String uri = DockerApi.logsGet.replace("{}", containerId);
|