|
|
@@ -1,12 +1,12 @@
|
|
|
package cn.reghao.autodop.common.dockerc;
|
|
|
|
|
|
+import cn.reghao.autodop.common.deploy.LogLevel;
|
|
|
import cn.reghao.autodop.common.dockerc.httpclient.UnixSocketClient;
|
|
|
import cn.reghao.autodop.common.dockerc.httpclient.HttpClient;
|
|
|
import cn.reghao.autodop.common.utils.JsonUtil;
|
|
|
import cn.reghao.autodop.common.utils.security.Base64Util;
|
|
|
import cn.reghao.autodop.common.utils.compression.TarUtil;
|
|
|
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 org.slf4j.Logger;
|
|
|
@@ -15,10 +15,10 @@ import org.slf4j.LoggerFactory;
|
|
|
import java.io.*;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* Docker 客户端
|
|
|
- * TODO 执行完成后需要清理资源,关闭连接
|
|
|
*
|
|
|
* @author reghao
|
|
|
* @date 2020-01-13 14:16:38
|
|
|
@@ -145,7 +145,11 @@ public class DockerClient implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
private String create(String name, String image) throws DockerException {
|
|
|
String uri = DockerApi.createPost + "?name=" + name;
|
|
|
|
|
|
- HostConfig hostConfig = HostConfig.builder().networkMode("host").build();
|
|
|
+ HostConfig hostConfig = HostConfig.builder()
|
|
|
+ .networkMode("host")
|
|
|
+ .restartPolicy()
|
|
|
+ .build();
|
|
|
+
|
|
|
/*Map<String, String> volumes = new HashMap<>();
|
|
|
volumes.put("/home/reghao/tmp/autodop/opt/logs", "/app/Logs");*/
|
|
|
// TODO 挂载日志目录到宿主机
|
|
|
@@ -246,13 +250,20 @@ public class DockerClient implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 控制台日志
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ * @date 2020-05-19 上午10:26
|
|
|
+ */
|
|
|
@Override
|
|
|
- public List<String> logs(String containerId, int type) throws IOException {
|
|
|
+ public List<String> consoleLog(String containerId, String logLevel) {
|
|
|
+ LogLevel level = LogLevel.valueOf(logLevel);
|
|
|
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
|
|
|
+ switch (level) {
|
|
|
+ case info:
|
|
|
// stdout
|
|
|
String params = "?stdout=true&tail=500";
|
|
|
response = client.get(uri + params, null);
|
|
|
@@ -260,38 +271,51 @@ public class DockerClient implements ImageOps, ContainerOps, AutoCloseable {
|
|
|
String stdout = response.content().toString(StandardCharsets.UTF_8);
|
|
|
return Arrays.asList(stdout.split(System.lineSeparator()));
|
|
|
}
|
|
|
- break;
|
|
|
- case 1:
|
|
|
+ case error:
|
|
|
// stderr
|
|
|
- params = "?stdout=true&tail=500";
|
|
|
+ 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()));
|
|
|
}
|
|
|
- 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);
|
|
|
+ default:
|
|
|
+ return null;
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- return null;
|
|
|
+ /**
|
|
|
+ * 文件日志
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ * @date 2020-05-19 上午10:26
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<String> fileLog(String containerId, String logLevel, String logPath) throws IOException {
|
|
|
+ LogLevel level = LogLevel.valueOf(logLevel);
|
|
|
+ switch (level) {
|
|
|
+ case info:
|
|
|
+ // info 日志目录
|
|
|
+ return appLog(containerId, logPath);
|
|
|
+ case error:
|
|
|
+ // error 日志目录
|
|
|
+ return appLog(containerId, logPath);
|
|
|
+ default:
|
|
|
+ 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);
|
|
|
+ String logDir = rootfs(containerId) + filePath;
|
|
|
+ File dir = new File(logDir);
|
|
|
+ if (!dir.exists()) {
|
|
|
+ return null;
|
|
|
+ } else {
|
|
|
+ return Arrays.stream(Objects.requireNonNull(dir.listFiles()))
|
|
|
+ .map(File::getName)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|