新增获取磁盘主机的信息
This commit is contained in:
@@ -22,9 +22,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.Collections;
|
import java.util.*;
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@@ -126,6 +124,75 @@ public class sysController {
|
|||||||
private static final int MAX_SIZE = 100;
|
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")
|
@GetMapping("/getApiInfo")
|
||||||
public ApiResult<List<SnapshotDTO>> getApiInfo(String token) {
|
public ApiResult<List<SnapshotDTO>> getApiInfo(String token) {
|
||||||
if (vToken.isValidToken(token)) {
|
if (vToken.isValidToken(token)) {
|
||||||
@@ -145,31 +212,49 @@ public class sysController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取容器列表
|
||||||
|
*/
|
||||||
@GetMapping("/getApiDockerInfo")
|
@GetMapping("/getApiDockerInfo")
|
||||||
public ApiResult<?> getDockerInfo(String dockerHostId, String token) {
|
public ApiResult<?> getDockerInfo(String dockerHostId, String token) {
|
||||||
if (vToken.isValidToken(token)) {
|
if (vToken.isValidToken(token)) {
|
||||||
DockerHost host = dockerHostService.getById(dockerHostId);
|
DockerHost host = dockerHostService.getById(dockerHostId);
|
||||||
try {
|
/* 1. 现有数据一次性加载到内存 */
|
||||||
QueryWrapper<DockerContainerInfo> wrapper = new QueryWrapper<>();
|
List<DockerContainerInfo> oldList =
|
||||||
wrapper.eq("doker_host_id", dockerHostId);
|
dockerInfoService.lambdaQuery()
|
||||||
dockerInfoService.remove(wrapper);
|
.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());
|
SshUser sshUser = sshUserService.getById(host.getUserId());
|
||||||
SshInfo sshInfo = sshInfoService.getById(host.getHostId());
|
SshInfo sshInfo = sshInfoService.getById(host.getHostId());
|
||||||
List<docker.DockerInfo> list = docker.getDockerInfo(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword());
|
List<docker.DockerInfo> remoteList =
|
||||||
for (docker.DockerInfo dockerInfo : list) {
|
docker.getDockerInfo(sshInfo.getHostIp(),
|
||||||
DockerContainerInfo info = docker.getDockerContainerInfo(dockerHostId, dockerInfo, sshInfo);
|
Long.valueOf(sshInfo.getHostPort()),
|
||||||
dockerInfoService.save(info);
|
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);
|
||||||
}
|
}
|
||||||
long count = list.stream()
|
|
||||||
.filter(d -> "1".equals(d.getStatus()))
|
/* 5. 更新运行数 */
|
||||||
|
long runningCnt = remoteList.stream()
|
||||||
|
.filter(r -> "1".equals(r.getStatus()))
|
||||||
.count();
|
.count();
|
||||||
host.setRunNum(count);
|
host.setRunNum(runningCnt);
|
||||||
host.setUpdateTime(vDate.getNow());
|
host.setUpdateTime(vDate.getNow());
|
||||||
dockerHostService.updateById(host);
|
dockerHostService.updateById(host);
|
||||||
return ApiResult.success();
|
|
||||||
} catch (Exception e) {
|
|
||||||
return ApiResult.error(500, e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return ApiResult.error();
|
return ApiResult.error();
|
||||||
}
|
}
|
||||||
@@ -181,28 +266,25 @@ public class sysController {
|
|||||||
@GetMapping("/getApiStartDockerInfo")
|
@GetMapping("/getApiStartDockerInfo")
|
||||||
public ApiResult<?> startDockerInfo(String id, String token) {
|
public ApiResult<?> startDockerInfo(String id, String token) {
|
||||||
if (vToken.isValidToken(token)) {
|
if (vToken.isValidToken(token)) {
|
||||||
DockerContainerInfo dockerContainerInfo = dockerInfoService.getById(id);
|
DockerContainerInfo cur = dockerInfoService.getById(id);
|
||||||
DockerHost host = dockerHostService.getById(dockerContainerInfo.getDokerHostId());
|
DockerHost host = dockerHostService.getById(cur.getDokerHostId());
|
||||||
try {
|
try {
|
||||||
QueryWrapper<DockerContainerInfo> wrapper = new QueryWrapper<>();
|
|
||||||
wrapper.eq("doker_host_id", dockerContainerInfo.getDokerHostId());
|
|
||||||
dockerInfoService.remove(wrapper);
|
|
||||||
SshUser sshUser = sshUserService.getById(host.getUserId());
|
SshUser sshUser = sshUserService.getById(host.getUserId());
|
||||||
SshInfo sshInfo = sshInfoService.getById(host.getHostId());
|
SshInfo sshInfo = sshInfoService.getById(host.getHostId());
|
||||||
docker.startDocker(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword(), dockerContainerInfo.getContainerId());
|
docker.startDocker(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword(), cur.getContainerId());
|
||||||
List<docker.DockerInfo> list = docker.getDockerInfo(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword());
|
/* 2. 取回最新列表 */
|
||||||
for (docker.DockerInfo dockerInfo : list) {
|
List<docker.DockerInfo> remoteList = docker.getDockerInfo(
|
||||||
DockerContainerInfo info = docker.getDockerContainerInfo(dockerContainerInfo.getDokerHostId(), dockerInfo, sshInfo);
|
sshInfo.getHostIp(),
|
||||||
dockerInfoService.save(info);
|
Long.valueOf(sshInfo.getHostPort()),
|
||||||
}
|
sshUser.getCUsername(),
|
||||||
long count = list.stream()
|
sshUser.getCPassword());
|
||||||
.filter(d -> "1".equals(d.getStatus()))
|
/* 3. 有则更新、无则插入、失效删除 */
|
||||||
.count();
|
refreshContainerTable(cur.getDokerHostId(), remoteList, sshInfo);
|
||||||
host.setRunNum(count);
|
/* 4. 更新主机运行数 */
|
||||||
dockerHostService.updateById(host);
|
updateRunNum(host, remoteList);
|
||||||
return ApiResult.success();
|
return ApiResult.success();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return ApiResult.error(500, e.getMessage());
|
System.out.println(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ApiResult.error();
|
return ApiResult.error();
|
||||||
@@ -215,28 +297,25 @@ public class sysController {
|
|||||||
@GetMapping("/getApiStopDockerInfo")
|
@GetMapping("/getApiStopDockerInfo")
|
||||||
public ApiResult<?> stopDockerInfo(String id, String token) {
|
public ApiResult<?> stopDockerInfo(String id, String token) {
|
||||||
if (vToken.isValidToken(token)) {
|
if (vToken.isValidToken(token)) {
|
||||||
DockerContainerInfo dockerContainerInfo = dockerInfoService.getById(id);
|
DockerContainerInfo cur = dockerInfoService.getById(id);
|
||||||
DockerHost host = dockerHostService.getById(dockerContainerInfo.getDokerHostId());
|
DockerHost host = dockerHostService.getById(cur.getDokerHostId());
|
||||||
try {
|
try {
|
||||||
QueryWrapper<DockerContainerInfo> wrapper = new QueryWrapper<>();
|
|
||||||
wrapper.eq("doker_host_id", dockerContainerInfo.getDokerHostId());
|
|
||||||
dockerInfoService.remove(wrapper);
|
|
||||||
SshUser sshUser = sshUserService.getById(host.getUserId());
|
SshUser sshUser = sshUserService.getById(host.getUserId());
|
||||||
SshInfo sshInfo = sshInfoService.getById(host.getHostId());
|
SshInfo sshInfo = sshInfoService.getById(host.getHostId());
|
||||||
docker.stopDocker(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword(), dockerContainerInfo.getContainerId());
|
docker.stopDocker(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword(), cur.getContainerId());
|
||||||
List<docker.DockerInfo> list = docker.getDockerInfo(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword());
|
/* 2. 取回最新列表 */
|
||||||
for (docker.DockerInfo dockerInfo : list) {
|
List<docker.DockerInfo> remoteList = docker.getDockerInfo(
|
||||||
DockerContainerInfo info = docker.getDockerContainerInfo(dockerContainerInfo.getDokerHostId(), dockerInfo, sshInfo);
|
sshInfo.getHostIp(),
|
||||||
dockerInfoService.save(info);
|
Long.valueOf(sshInfo.getHostPort()),
|
||||||
}
|
sshUser.getCUsername(),
|
||||||
long count = list.stream()
|
sshUser.getCPassword());
|
||||||
.filter(d -> "1".equals(d.getStatus()))
|
/* 3. 有则更新、无则插入、失效删除 */
|
||||||
.count();
|
refreshContainerTable(cur.getDokerHostId(), remoteList, sshInfo);
|
||||||
host.setRunNum(count);
|
/* 4. 更新主机运行数 */
|
||||||
dockerHostService.updateById(host);
|
updateRunNum(host, remoteList);
|
||||||
return ApiResult.success();
|
return ApiResult.success();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return ApiResult.error(500, e.getMessage());
|
System.out.println(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ApiResult.error();
|
return ApiResult.error();
|
||||||
|
|||||||
Reference in New Issue
Block a user