项目初始化
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.HostRuntime;
|
||||||
import com.mini.capi.utils.docker;
|
import com.mini.capi.utils.docker;
|
||||||
import com.mini.capi.utils.vDate;
|
import com.mini.capi.utils.vDate;
|
||||||
|
import com.mini.capi.utils.vToken;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@@ -126,45 +127,51 @@ public class sysController {
|
|||||||
|
|
||||||
|
|
||||||
@GetMapping("/getApiInfo")
|
@GetMapping("/getApiInfo")
|
||||||
public ApiResult<List<SnapshotDTO>> getApiInfo() {
|
public ApiResult<List<SnapshotDTO>> getApiInfo(String token) {
|
||||||
// 1. 新建一个一次性 List
|
if (vToken.isValidToken(token)) {
|
||||||
List<HostRuntime.Snapshot> snapshots =
|
// 1. 新建一个一次性 List
|
||||||
Collections.synchronizedList(new LinkedList<>());
|
List<HostRuntime.Snapshot> snapshots =
|
||||||
// 2. 采集并加入
|
Collections.synchronizedList(new LinkedList<>());
|
||||||
HostRuntime.Snapshot snap = HostRuntime.collect();
|
// 2. 采集并加入
|
||||||
snapshots.add(snap);
|
HostRuntime.Snapshot snap = HostRuntime.collect();
|
||||||
// 3. 如果只想保留一条,直接清空多余
|
snapshots.add(snap);
|
||||||
while (snapshots.size() > MAX_SIZE) {
|
// 3. 如果只想保留一条,直接清空多余
|
||||||
((LinkedList<HostRuntime.Snapshot>) snapshots).removeFirst();
|
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")
|
@GetMapping("/getApiDockerInfo")
|
||||||
public ApiResult<?> getDockerInfo(String dockerHostId) {
|
public ApiResult<?> getDockerInfo(String dockerHostId, String token) {
|
||||||
DockerHost host = dockerHostService.getById(dockerHostId);
|
if (vToken.isValidToken(token)) {
|
||||||
try {
|
DockerHost host = dockerHostService.getById(dockerHostId);
|
||||||
QueryWrapper<DockerContainerInfo> wrapper = new QueryWrapper<>();
|
try {
|
||||||
wrapper.eq("doker_host_id", dockerHostId);
|
QueryWrapper<DockerContainerInfo> wrapper = new QueryWrapper<>();
|
||||||
dockerInfoService.remove(wrapper);
|
wrapper.eq("doker_host_id", dockerHostId);
|
||||||
SshUser sshUser = sshUserService.getById(host.getUserId());
|
dockerInfoService.remove(wrapper);
|
||||||
SshInfo sshInfo = sshInfoService.getById(host.getHostId());
|
SshUser sshUser = sshUserService.getById(host.getUserId());
|
||||||
List<docker.DockerInfo> list = docker.getDockerInfo(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword());
|
SshInfo sshInfo = sshInfoService.getById(host.getHostId());
|
||||||
for (docker.DockerInfo dockerInfo : list) {
|
List<docker.DockerInfo> list = docker.getDockerInfo(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword());
|
||||||
DockerContainerInfo info = docker.getDockerContainerInfo(dockerHostId, dockerInfo, sshInfo);
|
for (docker.DockerInfo dockerInfo : list) {
|
||||||
dockerInfoService.save(info);
|
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")
|
@GetMapping("/getApiStartDockerInfo")
|
||||||
public ApiResult<?> startDockerInfo(String id) {
|
public ApiResult<?> startDockerInfo(String id, String token) {
|
||||||
DockerContainerInfo dockerContainerInfo = dockerInfoService.getById(id);
|
if (vToken.isValidToken(token)) {
|
||||||
DockerHost host = dockerHostService.getById(dockerContainerInfo.getDokerHostId());
|
DockerContainerInfo dockerContainerInfo = dockerInfoService.getById(id);
|
||||||
try {
|
DockerHost host = dockerHostService.getById(dockerContainerInfo.getDokerHostId());
|
||||||
QueryWrapper<DockerContainerInfo> wrapper = new QueryWrapper<>();
|
try {
|
||||||
wrapper.eq("doker_host_id", dockerContainerInfo.getDokerHostId());
|
QueryWrapper<DockerContainerInfo> wrapper = new QueryWrapper<>();
|
||||||
dockerInfoService.remove(wrapper);
|
wrapper.eq("doker_host_id", dockerContainerInfo.getDokerHostId());
|
||||||
SshUser sshUser = sshUserService.getById(host.getUserId());
|
dockerInfoService.remove(wrapper);
|
||||||
SshInfo sshInfo = sshInfoService.getById(host.getHostId());
|
SshUser sshUser = sshUserService.getById(host.getUserId());
|
||||||
docker.startDocker(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword(), dockerContainerInfo.getContainerId());
|
SshInfo sshInfo = sshInfoService.getById(host.getHostId());
|
||||||
List<docker.DockerInfo> list = docker.getDockerInfo(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword());
|
docker.startDocker(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword(), dockerContainerInfo.getContainerId());
|
||||||
for (docker.DockerInfo dockerInfo : list) {
|
List<docker.DockerInfo> list = docker.getDockerInfo(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword());
|
||||||
DockerContainerInfo info = docker.getDockerContainerInfo(dockerContainerInfo.getDokerHostId(), dockerInfo, sshInfo);
|
for (docker.DockerInfo dockerInfo : list) {
|
||||||
dockerInfoService.save(info);
|
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")
|
@GetMapping("/getApiStopDockerInfo")
|
||||||
public ApiResult<?> stopDockerInfo(String id) {
|
public ApiResult<?> stopDockerInfo(String id, String token) {
|
||||||
DockerContainerInfo dockerContainerInfo = dockerInfoService.getById(id);
|
if (vToken.isValidToken(token)) {
|
||||||
DockerHost host = dockerHostService.getById(dockerContainerInfo.getDokerHostId());
|
DockerContainerInfo dockerContainerInfo = dockerInfoService.getById(id);
|
||||||
try {
|
DockerHost host = dockerHostService.getById(dockerContainerInfo.getDokerHostId());
|
||||||
QueryWrapper<DockerContainerInfo> wrapper = new QueryWrapper<>();
|
try {
|
||||||
wrapper.eq("doker_host_id", dockerContainerInfo.getDokerHostId());
|
QueryWrapper<DockerContainerInfo> wrapper = new QueryWrapper<>();
|
||||||
dockerInfoService.remove(wrapper);
|
wrapper.eq("doker_host_id", dockerContainerInfo.getDokerHostId());
|
||||||
SshUser sshUser = sshUserService.getById(host.getUserId());
|
dockerInfoService.remove(wrapper);
|
||||||
SshInfo sshInfo = sshInfoService.getById(host.getHostId());
|
SshUser sshUser = sshUserService.getById(host.getUserId());
|
||||||
docker.stopDocker(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword(), dockerContainerInfo.getContainerId());
|
SshInfo sshInfo = sshInfoService.getById(host.getHostId());
|
||||||
List<docker.DockerInfo> list = docker.getDockerInfo(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword());
|
docker.stopDocker(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword(), dockerContainerInfo.getContainerId());
|
||||||
for (docker.DockerInfo dockerInfo : list) {
|
List<docker.DockerInfo> list = docker.getDockerInfo(sshInfo.getHostIp(), Long.valueOf(sshInfo.getHostPort()), sshUser.getCUsername(), sshUser.getCPassword());
|
||||||
DockerContainerInfo info = docker.getDockerContainerInfo(dockerContainerInfo.getDokerHostId(), dockerInfo, sshInfo);
|
for (docker.DockerInfo dockerInfo : list) {
|
||||||
dockerInfoService.save(info);
|
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