首页接口重构
This commit is contained in:
@@ -17,4 +17,5 @@ public class ContainerInfo implements Serializable {
|
|||||||
private String created;
|
private String created;
|
||||||
private String names;
|
private String names;
|
||||||
private String ports;
|
private String ports;
|
||||||
|
private String accountId;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,15 +5,13 @@ import lombok.Data;
|
|||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class DockerInfo implements Serializable {
|
public class ServerInfo implements Serializable {
|
||||||
|
|
||||||
private String username; // 登录账号
|
|
||||||
private String password; // 登录密码
|
|
||||||
private String rootPath; // 初始目录
|
|
||||||
private String authType; // 认证方式
|
|
||||||
private String privateKey; // 密钥内容
|
|
||||||
private String hostIp; // 主机域名
|
private String hostIp; // 主机域名
|
||||||
private Integer hostPort; // 主机端口
|
private Integer hostPort; // 主机端口
|
||||||
|
private String username; // 登录账号
|
||||||
|
private String hostName; // 主机名称
|
||||||
|
private String accountId; // 账号标识
|
||||||
private String containerId;
|
private String containerId;
|
||||||
public DockerInfo(){}
|
public ServerInfo(){}
|
||||||
}
|
}
|
||||||
@@ -1,98 +0,0 @@
|
|||||||
package com.jeesite.modules.apps.web.docker;
|
|
||||||
|
|
||||||
import com.jeesite.modules.apps.Module.ContainerInfo;
|
|
||||||
import com.jeesite.modules.apps.Module.DockerInfo;
|
|
||||||
import com.jeesite.modules.apps.Module.DockerResult;
|
|
||||||
import com.jeesite.modules.biz.dao.MySftpAccountsDao;
|
|
||||||
import com.jeesite.modules.biz.entity.MySftpAccounts;
|
|
||||||
import com.jeesite.modules.utils.DockerUtil;
|
|
||||||
import jakarta.annotation.Resource;
|
|
||||||
import org.springframework.stereotype.Controller;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Controller
|
|
||||||
@RequestMapping(value = "${adminPath}/docker/analysis")
|
|
||||||
public class DockerController {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private MySftpAccountsDao mySftpAccountsDao;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 启动容器
|
|
||||||
*/
|
|
||||||
@RequestMapping(value = "start")
|
|
||||||
@ResponseBody
|
|
||||||
public DockerResult start(DockerInfo dockerInfo) {
|
|
||||||
return DockerUtil.start(dockerInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 停止容器
|
|
||||||
*/
|
|
||||||
@RequestMapping(value = "stop")
|
|
||||||
@ResponseBody
|
|
||||||
public DockerResult stop(DockerInfo dockerInfo) {
|
|
||||||
return DockerUtil.stop(dockerInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 重启容器
|
|
||||||
*/
|
|
||||||
@RequestMapping(value = "restart")
|
|
||||||
@ResponseBody
|
|
||||||
public DockerResult restart(DockerInfo dockerInfo) {
|
|
||||||
return DockerUtil.restart(dockerInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取容器详情
|
|
||||||
*/
|
|
||||||
@RequestMapping(value = "inspect")
|
|
||||||
@ResponseBody
|
|
||||||
public DockerResult inspect(DockerInfo dockerInfo) {
|
|
||||||
return DockerUtil.inspect(dockerInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取容器日志
|
|
||||||
*/
|
|
||||||
@RequestMapping(value = "logs")
|
|
||||||
@ResponseBody
|
|
||||||
public DockerResult logs(DockerInfo dockerInfo) {
|
|
||||||
return DockerUtil.getLogs(dockerInfo, 20000, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取容器列表
|
|
||||||
*/
|
|
||||||
@RequestMapping(value = "listAll")
|
|
||||||
@ResponseBody
|
|
||||||
public List<ContainerInfo> listAll(DockerInfo dockerInfo) {
|
|
||||||
return DockerUtil.listContainers(dockerInfo, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取服务列表
|
|
||||||
*/
|
|
||||||
@RequestMapping(value = "listInfo")
|
|
||||||
@ResponseBody
|
|
||||||
public List<DockerInfo> listInfo() {
|
|
||||||
List<MySftpAccounts> accountsList = mySftpAccountsDao.findList(new MySftpAccounts());
|
|
||||||
return accountsList.stream()
|
|
||||||
.map(account -> {
|
|
||||||
DockerInfo info = new DockerInfo();
|
|
||||||
info.setHostIp(account.getHostIp());
|
|
||||||
info.setHostPort(account.getHostPort());
|
|
||||||
info.setUsername(account.getUsername());
|
|
||||||
info.setPassword(account.getPassword());
|
|
||||||
info.setRootPath(account.getRootPath());
|
|
||||||
info.setAuthType(account.getAuthType());
|
|
||||||
info.setPrivateKey(account.getPrivateKey());
|
|
||||||
return info;
|
|
||||||
})
|
|
||||||
.toList();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
package com.jeesite.modules.apps.web.docker;
|
||||||
|
|
||||||
|
import com.jeesite.modules.apps.Module.ContainerInfo;
|
||||||
|
import com.jeesite.modules.apps.Module.DockerResult;
|
||||||
|
import com.jeesite.modules.biz.dao.MySftpAccountsDao;
|
||||||
|
import com.jeesite.modules.biz.entity.MySftpAccounts;
|
||||||
|
import com.jeesite.modules.utils.DockerUtil;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping(value = "${adminPath}/docker/myAccount")
|
||||||
|
public class myAccountController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private MySftpAccountsDao mySftpAccountsDao;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 启动容器
|
||||||
|
*/
|
||||||
|
@RequestMapping(value = "start")
|
||||||
|
@ResponseBody
|
||||||
|
public DockerResult start(ContainerInfo containerInfo) {
|
||||||
|
MySftpAccounts mySftpAccounts = new MySftpAccounts();
|
||||||
|
mySftpAccounts.setAccountId(containerInfo.getAccountId());
|
||||||
|
MySftpAccounts accounts = mySftpAccountsDao.get(mySftpAccounts);
|
||||||
|
return DockerUtil.start(accounts, containerInfo.getContainerId());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 停止容器
|
||||||
|
*/
|
||||||
|
@RequestMapping(value = "stop")
|
||||||
|
@ResponseBody
|
||||||
|
public DockerResult stop(ContainerInfo containerInfo) {
|
||||||
|
MySftpAccounts mySftpAccounts = new MySftpAccounts();
|
||||||
|
mySftpAccounts.setAccountId(containerInfo.getAccountId());
|
||||||
|
MySftpAccounts accounts = mySftpAccountsDao.get(mySftpAccounts);
|
||||||
|
return DockerUtil.stop(accounts, containerInfo.getContainerId());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重启容器
|
||||||
|
*/
|
||||||
|
@RequestMapping(value = "restart")
|
||||||
|
@ResponseBody
|
||||||
|
public DockerResult restart(ContainerInfo containerInfo) {
|
||||||
|
MySftpAccounts mySftpAccounts = new MySftpAccounts();
|
||||||
|
mySftpAccounts.setAccountId(containerInfo.getAccountId());
|
||||||
|
MySftpAccounts accounts = mySftpAccountsDao.get(mySftpAccounts);
|
||||||
|
return DockerUtil.restart(accounts, containerInfo.getContainerId());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取容器详情
|
||||||
|
*/
|
||||||
|
@RequestMapping(value = "inspect")
|
||||||
|
@ResponseBody
|
||||||
|
public DockerResult inspect(ContainerInfo containerInfo) {
|
||||||
|
MySftpAccounts mySftpAccounts = new MySftpAccounts();
|
||||||
|
mySftpAccounts.setAccountId(containerInfo.getAccountId());
|
||||||
|
MySftpAccounts accounts = mySftpAccountsDao.get(mySftpAccounts);
|
||||||
|
return DockerUtil.inspect(accounts, containerInfo.getContainerId());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取容器日志
|
||||||
|
*/
|
||||||
|
@RequestMapping(value = "logs")
|
||||||
|
@ResponseBody
|
||||||
|
public DockerResult logs(ContainerInfo containerInfo) {
|
||||||
|
MySftpAccounts mySftpAccounts = new MySftpAccounts();
|
||||||
|
mySftpAccounts.setAccountId(containerInfo.getAccountId());
|
||||||
|
MySftpAccounts accounts = mySftpAccountsDao.get(mySftpAccounts);
|
||||||
|
return DockerUtil.getLogs(accounts, containerInfo.getContainerId(), 2000, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取容器列表
|
||||||
|
*/
|
||||||
|
@RequestMapping(value = "listAll")
|
||||||
|
@ResponseBody
|
||||||
|
public List<ContainerInfo> listAll(ContainerInfo containerInfo) {
|
||||||
|
MySftpAccounts mySftpAccounts = new MySftpAccounts();
|
||||||
|
mySftpAccounts.setAccountId(containerInfo.getAccountId());
|
||||||
|
MySftpAccounts accounts = mySftpAccountsDao.get(mySftpAccounts);
|
||||||
|
return DockerUtil.listContainers(accounts, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package com.jeesite.modules.apps.web.docker;
|
||||||
|
|
||||||
|
import com.jeesite.modules.apps.Module.ServerInfo;
|
||||||
|
import com.jeesite.modules.biz.dao.MySftpAccountsDao;
|
||||||
|
import com.jeesite.modules.biz.entity.MySftpAccounts;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping(value = "${adminPath}/docker/myServer")
|
||||||
|
public class myServerController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private MySftpAccountsDao mySftpAccountsDao;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取服务列表
|
||||||
|
*/
|
||||||
|
@RequestMapping(value = "listAll")
|
||||||
|
@ResponseBody
|
||||||
|
public List<ServerInfo> listAll() {
|
||||||
|
List<MySftpAccounts> accountsList = mySftpAccountsDao.findList(new MySftpAccounts());
|
||||||
|
return accountsList.stream()
|
||||||
|
.map(account -> {
|
||||||
|
ServerInfo info = new ServerInfo();
|
||||||
|
info.setHostIp(account.getHostIp());
|
||||||
|
info.setUsername(account.getUsername());
|
||||||
|
info.setHostName(account.getHostName());
|
||||||
|
info.setAccountId(account.getAccountId());
|
||||||
|
return info;
|
||||||
|
})
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,8 +2,8 @@ package com.jeesite.modules.utils;
|
|||||||
|
|
||||||
import com.jcraft.jsch.*;
|
import com.jcraft.jsch.*;
|
||||||
import com.jeesite.modules.apps.Module.ContainerInfo;
|
import com.jeesite.modules.apps.Module.ContainerInfo;
|
||||||
import com.jeesite.modules.apps.Module.DockerInfo;
|
|
||||||
import com.jeesite.modules.apps.Module.DockerResult;
|
import com.jeesite.modules.apps.Module.DockerResult;
|
||||||
|
import com.jeesite.modules.biz.entity.MySftpAccounts;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
@@ -21,38 +21,38 @@ public class DockerUtil {
|
|||||||
|
|
||||||
private static final int SSH_TIMEOUT = 30000;
|
private static final int SSH_TIMEOUT = 30000;
|
||||||
|
|
||||||
public static DockerResult start(DockerInfo dockerInfo) {
|
public static DockerResult start(MySftpAccounts accounts, String containerId) {
|
||||||
return exec(dockerInfo, "docker start " + dockerInfo.getContainerId());
|
return exec(accounts, "docker start " + containerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DockerResult stop(DockerInfo dockerInfo) {
|
public static DockerResult stop(MySftpAccounts accounts, String containerId) {
|
||||||
return exec(dockerInfo, "docker stop " + dockerInfo.getContainerId());
|
return exec(accounts, "docker stop " + containerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DockerResult restart(DockerInfo dockerInfo) {
|
public static DockerResult restart(MySftpAccounts accounts, String containerId) {
|
||||||
return exec(dockerInfo, "docker restart " + dockerInfo.getContainerId());
|
return exec(accounts, "docker restart " + containerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DockerResult getLogs(DockerInfo dockerInfo,
|
public static DockerResult getLogs(MySftpAccounts accounts, String containerId,
|
||||||
int tail, boolean timestamps) {
|
int tail, boolean timestamps) {
|
||||||
String cmd = "docker logs" + (timestamps ? " -t" : "")
|
String cmd = "docker logs" + (timestamps ? " -t" : "")
|
||||||
+ " --tail " + tail + " " + dockerInfo.getContainerId();
|
+ " --tail " + tail + " " + containerId;
|
||||||
return exec(dockerInfo, cmd);
|
return exec(accounts, cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DockerResult list(DockerInfo dockerInfo, boolean all) {
|
public static DockerResult list(MySftpAccounts accounts, boolean all) {
|
||||||
return exec(dockerInfo, all ? "docker ps -a" : "docker ps");
|
return exec(accounts, all ? "docker ps -a" : "docker ps");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DockerResult inspect(DockerInfo dockerInfo) {
|
public static DockerResult inspect(MySftpAccounts accounts, String containerId) {
|
||||||
return exec(dockerInfo, "docker inspect " + dockerInfo.getContainerId());
|
return exec(accounts, "docker inspect " + containerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<ContainerInfo> listContainers(DockerInfo dockerInfo, boolean all) {
|
public static List<ContainerInfo> listContainers(MySftpAccounts accounts, boolean all) {
|
||||||
List<ContainerInfo> list = new ArrayList<>();
|
List<ContainerInfo> list = new ArrayList<>();
|
||||||
String cmd = (all ? "docker ps -a" : "docker ps")
|
String cmd = (all ? "docker ps -a" : "docker ps")
|
||||||
+ " --format \"{{.ID}}|{{.Image}}|{{.Command}}|{{.CreatedAt}}|{{.Status}}|{{.Ports}}|{{.Names}}\"";
|
+ " --format \"{{.ID}}|{{.Image}}|{{.Command}}|{{.CreatedAt}}|{{.Status}}|{{.Ports}}|{{.Names}}\"";
|
||||||
DockerResult r = exec(dockerInfo, cmd);
|
DockerResult r = exec(accounts, cmd);
|
||||||
if (!r.isSuccess()) {
|
if (!r.isSuccess()) {
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
@@ -68,27 +68,28 @@ public class DockerUtil {
|
|||||||
info.setStatus(p.length > 4 ? p[4].trim() : "");
|
info.setStatus(p.length > 4 ? p[4].trim() : "");
|
||||||
info.setPorts(p.length > 5 ? p[5].trim() : "");
|
info.setPorts(p.length > 5 ? p[5].trim() : "");
|
||||||
info.setNames(p.length > 6 ? p[6].trim() : "");
|
info.setNames(p.length > 6 ? p[6].trim() : "");
|
||||||
|
info.setAccountId(accounts.getAccountId());
|
||||||
list.add(info);
|
list.add(info);
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DockerResult exec(DockerInfo dockerInfo, String command) {
|
public static DockerResult exec(MySftpAccounts accounts, String command) {
|
||||||
JSch jsch = new JSch();
|
JSch jsch = new JSch();
|
||||||
Session session = null;
|
Session session = null;
|
||||||
ChannelExec channel = null;
|
ChannelExec channel = null;
|
||||||
try {
|
try {
|
||||||
int port = dockerInfo.getHostPort() != null ? dockerInfo.getHostPort() : 22;
|
int port = accounts.getHostPort() != null ? accounts.getHostPort() : 22;
|
||||||
session = jsch.getSession(dockerInfo.getUsername(), dockerInfo.getHostIp(), port);
|
session = jsch.getSession(accounts.getUsername(), accounts.getHostIp(), port);
|
||||||
session.setTimeout(SSH_TIMEOUT);
|
session.setTimeout(SSH_TIMEOUT);
|
||||||
|
|
||||||
if ("key".equalsIgnoreCase(dockerInfo.getAuthType())
|
if ("key".equalsIgnoreCase(accounts.getAuthType())
|
||||||
&& dockerInfo.getPrivateKey() != null && !dockerInfo.getPrivateKey().isEmpty()) {
|
&& accounts.getPrivateKey() != null && !accounts.getPrivateKey().isEmpty()) {
|
||||||
jsch.addIdentity("temp",
|
jsch.addIdentity("temp",
|
||||||
dockerInfo.getPrivateKey().getBytes(StandardCharsets.UTF_8),
|
accounts.getPrivateKey().getBytes(StandardCharsets.UTF_8),
|
||||||
null, null);
|
null, null);
|
||||||
} else if (dockerInfo.getPassword() != null && !dockerInfo.getPassword().isEmpty()) {
|
} else if (accounts.getPassword() != null && !accounts.getPassword().isEmpty()) {
|
||||||
session.setPassword(dockerInfo.getPassword());
|
session.setPassword(accounts.getPassword());
|
||||||
} else {
|
} else {
|
||||||
return DockerResult.fail("SSH 认证信息不完整:缺少密码或私钥");
|
return DockerResult.fail("SSH 认证信息不完整:缺少密码或私钥");
|
||||||
}
|
}
|
||||||
@@ -99,8 +100,8 @@ public class DockerUtil {
|
|||||||
session.connect(SSH_TIMEOUT);
|
session.connect(SSH_TIMEOUT);
|
||||||
|
|
||||||
channel = (ChannelExec) session.openChannel("exec");
|
channel = (ChannelExec) session.openChannel("exec");
|
||||||
String finalCmd = (dockerInfo.getRootPath() != null && !dockerInfo.getRootPath().isEmpty())
|
String finalCmd = (accounts.getRootPath() != null && !accounts.getRootPath().isEmpty())
|
||||||
? "cd " + dockerInfo.getRootPath() + " && " + command
|
? "cd " + accounts.getRootPath() + " && " + command
|
||||||
: command;
|
: command;
|
||||||
channel.setCommand(finalCmd);
|
channel.setCommand(finalCmd);
|
||||||
channel.setInputStream(null);
|
channel.setInputStream(null);
|
||||||
|
|||||||
Reference in New Issue
Block a user