项目初始化
This commit is contained in:
@@ -13,6 +13,7 @@ import com.mini.capi.model.ApiResult;
|
||||
import com.mini.capi.utils.HostRuntime;
|
||||
import com.mini.capi.utils.docker;
|
||||
import com.mini.capi.utils.vDate;
|
||||
import com.mini.capi.utils.vToken;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@@ -126,45 +127,51 @@ public class sysController {
|
||||
|
||||
|
||||
@GetMapping("/getApiInfo")
|
||||
public ApiResult<List<SnapshotDTO>> getApiInfo() {
|
||||
// 1. 新建一个一次性 List
|
||||
List<HostRuntime.Snapshot> snapshots =
|
||||
Collections.synchronizedList(new LinkedList<>());
|
||||
// 2. 采集并加入
|
||||
HostRuntime.Snapshot snap = HostRuntime.collect();
|
||||
snapshots.add(snap);
|
||||
// 3. 如果只想保留一条,直接清空多余
|
||||
while (snapshots.size() > MAX_SIZE) {
|
||||
((LinkedList<HostRuntime.Snapshot>) snapshots).removeFirst();
|
||||
public ApiResult<List<SnapshotDTO>> getApiInfo(String token) {
|
||||
if (vToken.isValidToken(token)) {
|
||||
// 1. 新建一个一次性 List
|
||||
List<HostRuntime.Snapshot> snapshots =
|
||||
Collections.synchronizedList(new LinkedList<>());
|
||||
// 2. 采集并加入
|
||||
HostRuntime.Snapshot snap = HostRuntime.collect();
|
||||
snapshots.add(snap);
|
||||
// 3. 如果只想保留一条,直接清空多余
|
||||
while (snapshots.size() > MAX_SIZE) {
|
||||
((LinkedList<HostRuntime.Snapshot>) snapshots).removeFirst();
|
||||
}
|
||||
return ApiResult.success(Collections.singletonList(SnapshotDTO.from(snap)));
|
||||
}
|
||||
return ApiResult.success(Collections.singletonList(SnapshotDTO.from(snap)));
|
||||
return ApiResult.error();
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/getApiDockerInfo")
|
||||
public ApiResult<?> getDockerInfo(String dockerHostId) {
|
||||
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);
|
||||
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());
|
||||
}
|
||||
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());
|
||||
}
|
||||
return ApiResult.error();
|
||||
}
|
||||
|
||||
|
||||
@@ -172,58 +179,67 @@ public class sysController {
|
||||
* 启动容器
|
||||
*/
|
||||
@GetMapping("/getApiStartDockerInfo")
|
||||
public ApiResult<?> startDockerInfo(String id) {
|
||||
DockerContainerInfo dockerContainerInfo = dockerInfoService.getById(id);
|
||||
DockerHost host = dockerHostService.getById(dockerContainerInfo.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);
|
||||
public ApiResult<?> startDockerInfo(String id, String token) {
|
||||
if (vToken.isValidToken(token)) {
|
||||
DockerContainerInfo dockerContainerInfo = dockerInfoService.getById(id);
|
||||
DockerHost host = dockerHostService.getById(dockerContainerInfo.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);
|
||||
return ApiResult.success();
|
||||
} catch (Exception e) {
|
||||
return ApiResult.error(500, e.getMessage());
|
||||
}
|
||||
long count = list.stream()
|
||||
.filter(d -> "1".equals(d.getStatus()))
|
||||
.count();
|
||||
host.setRunNum(count);
|
||||
dockerHostService.updateById(host);
|
||||
return ApiResult.success();
|
||||
} catch (Exception e) {
|
||||
return ApiResult.error(500, e.getMessage());
|
||||
}
|
||||
return ApiResult.error();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 停止容器
|
||||
*/
|
||||
@GetMapping("/getApiStopDockerInfo")
|
||||
public ApiResult<?> stopDockerInfo(String id) {
|
||||
DockerContainerInfo dockerContainerInfo = dockerInfoService.getById(id);
|
||||
DockerHost host = dockerHostService.getById(dockerContainerInfo.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);
|
||||
public ApiResult<?> stopDockerInfo(String id, String token) {
|
||||
if (vToken.isValidToken(token)) {
|
||||
DockerContainerInfo dockerContainerInfo = dockerInfoService.getById(id);
|
||||
DockerHost host = dockerHostService.getById(dockerContainerInfo.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);
|
||||
return ApiResult.success();
|
||||
} catch (Exception e) {
|
||||
return ApiResult.error(500, e.getMessage());
|
||||
}
|
||||
long count = list.stream()
|
||||
.filter(d -> "1".equals(d.getStatus()))
|
||||
.count();
|
||||
host.setRunNum(count);
|
||||
dockerHostService.updateById(host);
|
||||
return ApiResult.success();
|
||||
} catch (Exception e) {
|
||||
return ApiResult.error(500, e.getMessage());
|
||||
}
|
||||
return ApiResult.error();
|
||||
}
|
||||
|
||||
|
||||
|
||||
34
src/main/java/com/mini/capi/utils/vToken.java
Normal file
34
src/main/java/com/mini/capi/utils/vToken.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package com.mini.capi.utils;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
|
||||
public class vToken {
|
||||
|
||||
|
||||
private static final String DEFAULT_TOKEN = "3774e79ac55aff6d1afc0f94bfaf131d";
|
||||
|
||||
private static final SecureRandom RAND = new SecureRandom();
|
||||
private static final char[] HEX = "0123456789abcdef".toCharArray();
|
||||
|
||||
public static boolean isValidToken(String token) {
|
||||
|
||||
return DEFAULT_TOKEN.equals(token);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 随机32位
|
||||
*/
|
||||
public static String nextHex32() {
|
||||
byte[] bytes = new byte[16]; // 16 字节 = 128 bit
|
||||
RAND.nextBytes(bytes);
|
||||
char[] chars = new char[32];
|
||||
for (int i = 0, j = 0; i < 16; i++) {
|
||||
int v = bytes[i] & 0xFF;
|
||||
chars[j++] = HEX[v >>> 4];
|
||||
chars[j++] = HEX[v & 0x0F];
|
||||
}
|
||||
return new String(chars);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user