新增获取磁盘主机的信息

This commit is contained in:
2025-08-24 20:28:37 +08:00
parent 79f0c62037
commit 234a03f2f1

View File

@@ -22,9 +22,7 @@ import org.springframework.web.bind.annotation.RestController;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
@RestController
@@ -126,6 +124,75 @@ public class sysController {
private static final int MAX_SIZE = 100;
private String buildKey(String dockerHostId, String image, String containerName) {
return dockerHostId + "|" + image + "|" + containerName;
}
private void addOrEdit(String dockerHostId, Map<String, DockerContainerInfo> oldMap, SshInfo sshInfo, List<docker.DockerInfo> remoteList) {
for (docker.DockerInfo d : remoteList) {
String key = buildKey(dockerHostId, d.getImageName(), d.getNames());
DockerContainerInfo entity = oldMap.get(key);
if (entity == null) {
entity = new DockerContainerInfo();
entity.setDokerHostId(dockerHostId);
entity.setContainerId(d.getContainerId());
entity.setImageName(d.getImageName());
entity.setUnames(d.getNames());
entity.setUstatus(d.getStatus());
entity.setHostIp(sshInfo.getHostIp());
entity.setPorts(d.getPorts());
entity.setGetTime(vDate.getNow());
dockerInfoService.save(entity);
} else {
entity.setContainerId(d.getContainerId());
entity.setImageName(d.getImageName());
entity.setUnames(d.getNames());
entity.setUstatus(d.getStatus());
entity.setHostIp(sshInfo.getHostIp());
entity.setPorts(d.getPorts());
entity.setGetTime(vDate.getNow());
dockerInfoService.updateById(entity);
oldMap.remove(key);
}
}
}
private void refreshContainerTable(String dockerHostId,
List<docker.DockerInfo> remoteList,
SshInfo sshInfo) {
/* 1. 旧数据 */
List<DockerContainerInfo> oldList = dockerInfoService.lambdaQuery()
.eq(DockerContainerInfo::getDokerHostId, dockerHostId)
.list();
Map<String, DockerContainerInfo> oldMap = oldList.stream()
.collect(Collectors.toMap(
c -> buildKey(dockerHostId, c.getImageName(), c.getUnames()),
c -> c));
/* 2. 遍历远端 */
addOrEdit(dockerHostId, oldMap, sshInfo, remoteList);
/* 3. 无效删除 */
if (!oldMap.isEmpty()) {
List<String> ids = new ArrayList<>();
for (DockerContainerInfo e : oldMap.values()) {
ids.add(e.getId());
}
dockerInfoService.removeByIds(ids);
}
}
private void updateRunNum(DockerHost host, List<docker.DockerInfo> list) {
long runningCnt = list.stream()
.filter(r -> "1".equals(r.getStatus()))
.count();
host.setRunNum(runningCnt);
host.setUpdateTime(vDate.getNow());
dockerHostService.updateById(host);
}
@GetMapping("/getApiInfo")
public ApiResult<List<SnapshotDTO>> getApiInfo(String token) {
if (vToken.isValidToken(token)) {
@@ -145,31 +212,49 @@ public class sysController {
}
/**
* 获取容器列表
*/
@GetMapping("/getApiDockerInfo")
public ApiResult<?> getDockerInfo(String dockerHostId, String token) {
if (vToken.isValidToken(token)) {
DockerHost host = dockerHostService.getById(dockerHostId);
try {
QueryWrapper<DockerContainerInfo> wrapper = new QueryWrapper<>();
wrapper.eq("doker_host_id", dockerHostId);
dockerInfoService.remove(wrapper);
SshUser sshUser = sshUserService.getById(host.getUserId());
SshInfo sshInfo = sshInfoService.getById(host.getHostId());
List<docker.DockerInfo> list = docker.getDockerInfo(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword());
for (docker.DockerInfo dockerInfo : list) {
DockerContainerInfo info = docker.getDockerContainerInfo(dockerHostId, dockerInfo, sshInfo);
dockerInfoService.save(info);
}
long count = list.stream()
.filter(d -> "1".equals(d.getStatus()))
.count();
host.setRunNum(count);
host.setUpdateTime(vDate.getNow());
dockerHostService.updateById(host);
return ApiResult.success();
} catch (Exception e) {
return ApiResult.error(500, e.getMessage());
/* 1. 现有数据一次性加载到内存 */
List<DockerContainerInfo> oldList =
dockerInfoService.lambdaQuery()
.eq(DockerContainerInfo::getDokerHostId, dockerHostId)
.list();
Map<String, DockerContainerInfo> oldMap = oldList.stream()
.collect(Collectors.toMap(
c -> buildKey(c.getDokerHostId(), c.getImageName(), c.getUnames()),
c -> c));
/* 2. 远程最新数据 */
SshUser sshUser = sshUserService.getById(host.getUserId());
SshInfo sshInfo = sshInfoService.getById(host.getHostId());
List<docker.DockerInfo> remoteList =
docker.getDockerInfo(sshInfo.getHostIp(),
Long.valueOf(sshInfo.getHostPort()),
sshUser.getCUsername(),
sshUser.getCPassword());
/* 3. 新增 or 更新 */
addOrEdit(dockerHostId, oldMap, sshInfo, remoteList);
/* 4. 本轮未命中的即为“已失效”,批量删除 */
if (!oldMap.isEmpty()) {
List<String> idsToDel = oldMap.values()
.stream()
.map(DockerContainerInfo::getId)
.collect(Collectors.toList());
dockerInfoService.removeByIds(idsToDel);
}
/* 5. 更新运行数 */
long runningCnt = remoteList.stream()
.filter(r -> "1".equals(r.getStatus()))
.count();
host.setRunNum(runningCnt);
host.setUpdateTime(vDate.getNow());
dockerHostService.updateById(host);
}
return ApiResult.error();
}
@@ -181,28 +266,25 @@ public class sysController {
@GetMapping("/getApiStartDockerInfo")
public ApiResult<?> startDockerInfo(String id, String token) {
if (vToken.isValidToken(token)) {
DockerContainerInfo dockerContainerInfo = dockerInfoService.getById(id);
DockerHost host = dockerHostService.getById(dockerContainerInfo.getDokerHostId());
DockerContainerInfo cur = dockerInfoService.getById(id);
DockerHost host = dockerHostService.getById(cur.getDokerHostId());
try {
QueryWrapper<DockerContainerInfo> wrapper = new QueryWrapper<>();
wrapper.eq("doker_host_id", dockerContainerInfo.getDokerHostId());
dockerInfoService.remove(wrapper);
SshUser sshUser = sshUserService.getById(host.getUserId());
SshInfo sshInfo = sshInfoService.getById(host.getHostId());
docker.startDocker(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword(), dockerContainerInfo.getContainerId());
List<docker.DockerInfo> list = docker.getDockerInfo(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword());
for (docker.DockerInfo dockerInfo : list) {
DockerContainerInfo info = docker.getDockerContainerInfo(dockerContainerInfo.getDokerHostId(), dockerInfo, sshInfo);
dockerInfoService.save(info);
}
long count = list.stream()
.filter(d -> "1".equals(d.getStatus()))
.count();
host.setRunNum(count);
dockerHostService.updateById(host);
docker.startDocker(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword(), cur.getContainerId());
/* 2. 取回最新列表 */
List<docker.DockerInfo> remoteList = docker.getDockerInfo(
sshInfo.getHostIp(),
Long.valueOf(sshInfo.getHostPort()),
sshUser.getCUsername(),
sshUser.getCPassword());
/* 3. 有则更新、无则插入、失效删除 */
refreshContainerTable(cur.getDokerHostId(), remoteList, sshInfo);
/* 4. 更新主机运行数 */
updateRunNum(host, remoteList);
return ApiResult.success();
} catch (Exception e) {
return ApiResult.error(500, e.getMessage());
System.out.println(e.getMessage());
}
}
return ApiResult.error();
@@ -215,28 +297,25 @@ public class sysController {
@GetMapping("/getApiStopDockerInfo")
public ApiResult<?> stopDockerInfo(String id, String token) {
if (vToken.isValidToken(token)) {
DockerContainerInfo dockerContainerInfo = dockerInfoService.getById(id);
DockerHost host = dockerHostService.getById(dockerContainerInfo.getDokerHostId());
DockerContainerInfo cur = dockerInfoService.getById(id);
DockerHost host = dockerHostService.getById(cur.getDokerHostId());
try {
QueryWrapper<DockerContainerInfo> wrapper = new QueryWrapper<>();
wrapper.eq("doker_host_id", dockerContainerInfo.getDokerHostId());
dockerInfoService.remove(wrapper);
SshUser sshUser = sshUserService.getById(host.getUserId());
SshInfo sshInfo = sshInfoService.getById(host.getHostId());
docker.stopDocker(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword(), dockerContainerInfo.getContainerId());
List<docker.DockerInfo> list = docker.getDockerInfo(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword());
for (docker.DockerInfo dockerInfo : list) {
DockerContainerInfo info = docker.getDockerContainerInfo(dockerContainerInfo.getDokerHostId(), dockerInfo, sshInfo);
dockerInfoService.save(info);
}
long count = list.stream()
.filter(d -> "1".equals(d.getStatus()))
.count();
host.setRunNum(count);
dockerHostService.updateById(host);
docker.stopDocker(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword(), cur.getContainerId());
/* 2. 取回最新列表 */
List<docker.DockerInfo> remoteList = docker.getDockerInfo(
sshInfo.getHostIp(),
Long.valueOf(sshInfo.getHostPort()),
sshUser.getCUsername(),
sshUser.getCPassword());
/* 3. 有则更新、无则插入、失效删除 */
refreshContainerTable(cur.getDokerHostId(), remoteList, sshInfo);
/* 4. 更新主机运行数 */
updateRunNum(host, remoteList);
return ApiResult.success();
} catch (Exception e) {
return ApiResult.error(500, e.getMessage());
System.out.println(e.getMessage());
}
}
return ApiResult.error();