|
|
@@ -8,19 +8,22 @@ import cn.reghao.devops.common.docker.model.Config;
|
|
|
import cn.reghao.devops.common.docker.model.Volumes;
|
|
|
import cn.reghao.jutil.jdk.converter.IpAddressConverter;
|
|
|
import cn.reghao.jutil.jdk.serializer.JsonConverter;
|
|
|
+import cn.reghao.jutil.jdk.shell.ShellExecutor;
|
|
|
+import cn.reghao.jutil.jdk.shell.ShellResult;
|
|
|
import com.github.dockerjava.api.command.InspectContainerResponse;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.Getter;
|
|
|
+import lombok.NoArgsConstructor;
|
|
|
import lombok.Setter;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.junit.Test;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
import oshi.SystemInfo;
|
|
|
import oshi.software.os.InternetProtocolStats;
|
|
|
import oshi.software.os.OperatingSystem;
|
|
|
|
|
|
import java.util.*;
|
|
|
-import java.util.regex.Pattern;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -83,8 +86,11 @@ public class DockerTest {
|
|
|
setLogLevel();
|
|
|
|
|
|
List<ListenProcess> listenProcessList = getListenProcesses();
|
|
|
+ Map<Integer, List<ListenProcess>> groupMap = listenProcessList.stream().collect(Collectors.groupingBy(ListenProcess::getPid));
|
|
|
+
|
|
|
DockerImpl docker = new DockerImpl();
|
|
|
List<InspectContainerResponse> list = docker.psAll();
|
|
|
+ List<DockerProcess> dockerProcessList = new ArrayList<>();
|
|
|
for (InspectContainerResponse response : list) {
|
|
|
String imageId = response.getImageId();
|
|
|
Boolean running = response.getState().getRunning();
|
|
|
@@ -97,8 +103,11 @@ public class DockerTest {
|
|
|
String containerId = response.getId();
|
|
|
String appId = response.getName().replace("/", "");
|
|
|
System.out.printf("%s: %s -> %s\n", pid, containerId, appId);
|
|
|
+ dockerProcessList.add(new DockerProcess(pid, containerId, appId));
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ System.out.println();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
@@ -175,6 +184,14 @@ public class DockerTest {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @AllArgsConstructor
|
|
|
+ @Getter
|
|
|
+ static class DockerProcess {
|
|
|
+ private int pid;
|
|
|
+ private String containerId;
|
|
|
+ private String appId;
|
|
|
+ }
|
|
|
+
|
|
|
@AllArgsConstructor
|
|
|
@Getter
|
|
|
static class SysProcess {
|
|
|
@@ -182,4 +199,118 @@ public class DockerTest {
|
|
|
private String name;
|
|
|
private int ppid;
|
|
|
}
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void dockerConfigTest() {
|
|
|
+ Config config = new Config();
|
|
|
+
|
|
|
+ Volumes volumes = new Volumes();
|
|
|
+ volumes.getMap().put("/app1", "/app1");
|
|
|
+ volumes.getMap().put("/app2", "/app2");
|
|
|
+ config.setVolumes(volumes);
|
|
|
+
|
|
|
+ System.out.println(JsonConverter.objectToJson(config));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void linuxPackageTest() {
|
|
|
+ ShellExecutor shell = new ShellExecutor();
|
|
|
+ String versionCmd = "/usr/bin/pacman -Q";
|
|
|
+
|
|
|
+ List<String> pkgList = new ArrayList<>();
|
|
|
+ ShellResult shellResult = shell.exec(versionCmd.split("\\s+"));
|
|
|
+ if (shellResult.getExitCode() == 0) {
|
|
|
+ for (String line : shellResult.getResult().split(System.lineSeparator())) {
|
|
|
+ String[] arr = line.split("\\s+");
|
|
|
+ pkgList.add(arr[0]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, LinuxPkg> linuxPkgMap = new HashMap<>();
|
|
|
+ for (int i = 0; i < pkgList.size(); i++) {
|
|
|
+ String pkg = pkgList.get(i);
|
|
|
+ versionCmd = "/usr/bin/pacman -Qi " + pkg;
|
|
|
+ shellResult = shell.exec(versionCmd.split("\\s+"));
|
|
|
+
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ if (shellResult.getExitCode() == 0) {
|
|
|
+ for (String line : shellResult.getResult().split(System.lineSeparator())) {
|
|
|
+ if (line.startsWith("Optional Deps")) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ String[] arr = line.split(": ");
|
|
|
+ try {
|
|
|
+ map.put(StringUtils.trimAllWhitespace(arr[0]), arr[1]);
|
|
|
+ } catch (Exception e) {
|
|
|
+ //e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ int id = linuxPkgMap.size()+1;
|
|
|
+ String name = map.get("Name");
|
|
|
+ LinuxPkg linuxPkg = linuxPkgMap.computeIfAbsent(name, v -> new LinuxPkg(id, name));
|
|
|
+ String dependsOn = map.get("DependsOn");
|
|
|
+ if (!dependsOn.equals("None")) {
|
|
|
+ for (String line : dependsOn.split(" ")) {
|
|
|
+ String pkgName = StringUtils.trimAllWhitespace(line);
|
|
|
+ if (!pkgName.isBlank()) {
|
|
|
+ for (String key : pkgList) {
|
|
|
+ if (pkgName.startsWith(key)) {
|
|
|
+ int id1 = linuxPkgMap.size()+1;
|
|
|
+ LinuxPkg linuxPkg1 = linuxPkgMap.computeIfAbsent(key, v -> new LinuxPkg(id1, key));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String requiredBy = map.get("RequiredBy");
|
|
|
+ if (!requiredBy.equals("None")) {
|
|
|
+ for (String line : requiredBy.split(" ")) {
|
|
|
+ String pkgName = StringUtils.trimAllWhitespace(line);
|
|
|
+ if (!pkgName.isBlank()) {
|
|
|
+ for (String key : pkgList) {
|
|
|
+ int id1 = linuxPkgMap.size()+1;
|
|
|
+ if (pkgName.equals(key)) {
|
|
|
+ LinuxPkg linuxPkg1 = linuxPkgMap.computeIfAbsent(key, v -> new LinuxPkg(id1, key));
|
|
|
+ linuxPkg.getChildren().add(key);
|
|
|
+ } else if (pkgName.startsWith(key)) {
|
|
|
+ LinuxPkg linuxPkg1 = linuxPkgMap.computeIfAbsent(key, v -> new LinuxPkg(id1, key));
|
|
|
+ linuxPkg.getChildren().add(key);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.printf("%s: %s\n", i, pkg);
|
|
|
+ }
|
|
|
+
|
|
|
+ linuxPkgMap.forEach((name, value) -> {
|
|
|
+ for (String child : value.getChildren()) {
|
|
|
+ linuxPkgMap.get(child).setPid(value.getId());
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ Map<Integer, List<LinuxPkg>> groupMap = linuxPkgMap.values().stream()
|
|
|
+ .collect(Collectors.groupingBy(LinuxPkg::getPid));
|
|
|
+ System.out.println();
|
|
|
+ }
|
|
|
+
|
|
|
+ @NoArgsConstructor
|
|
|
+ @Setter
|
|
|
+ @Getter
|
|
|
+ static class LinuxPkg {
|
|
|
+ private int id;
|
|
|
+ private int pid;
|
|
|
+ private String name;
|
|
|
+ private Set<String> children = new HashSet<>();
|
|
|
+
|
|
|
+ public LinuxPkg(int id, String name) {
|
|
|
+ this.id = id;
|
|
|
+ this.name = name;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|