首页接口重构
This commit is contained in:
@@ -10,6 +10,7 @@ import com.jeesite.modules.biz.entity.MySftpAccounts;
|
|||||||
import io.micrometer.common.util.StringUtils;
|
import io.micrometer.common.util.StringUtils;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@@ -25,12 +26,10 @@ public class DockerUtil {
|
|||||||
ChannelExec channel = null;
|
ChannelExec channel = null;
|
||||||
InputStream in = null;
|
InputStream in = null;
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
int port = Optional.ofNullable(account.getHostPort()).orElse(22);
|
int port = Optional.ofNullable(account.getHostPort()).orElse(22);
|
||||||
session = jsch.getSession(account.getUsername(), account.getHostIp(), port);
|
session = jsch.getSession(account.getUsername(), account.getHostIp(), port);
|
||||||
session.setTimeout(SSH_TIMEOUT);
|
session.setTimeout(SSH_TIMEOUT);
|
||||||
|
|
||||||
// 认证
|
// 认证
|
||||||
if ("key".equalsIgnoreCase(account.getAuthType()) && StringUtils.isNotBlank(account.getPrivateKey())) {
|
if ("key".equalsIgnoreCase(account.getAuthType()) && StringUtils.isNotBlank(account.getPrivateKey())) {
|
||||||
jsch.addIdentity(
|
jsch.addIdentity(
|
||||||
@@ -41,52 +40,56 @@ public class DockerUtil {
|
|||||||
} else {
|
} else {
|
||||||
session.setPassword(account.getPassword());
|
session.setPassword(account.getPassword());
|
||||||
}
|
}
|
||||||
|
|
||||||
Properties config = new Properties();
|
Properties config = new Properties();
|
||||||
config.put("StrictHostKeyChecking", "no");
|
config.put("StrictHostKeyChecking", "no");
|
||||||
config.put("tcp.nodelay", "yes"); // 加速
|
config.put("tcp.nodelay", "yes"); // 加速
|
||||||
session.setConfig(config);
|
session.setConfig(config);
|
||||||
session.connect(SSH_TIMEOUT);
|
session.connect(SSH_TIMEOUT);
|
||||||
|
|
||||||
// 执行命令
|
// 执行命令
|
||||||
StringBuilder command = new StringBuilder();
|
StringBuilder command = new StringBuilder();
|
||||||
if (StringUtils.isNotBlank(account.getRootPath())) {
|
if (StringUtils.isNotBlank(account.getRootPath())) {
|
||||||
command.append("cd ").append(account.getRootPath()).append(" && ");
|
command.append("cd ").append(account.getRootPath()).append(" && ");
|
||||||
}
|
}
|
||||||
command.append(cmd);
|
command.append(cmd);
|
||||||
|
|
||||||
channel = (ChannelExec) session.openChannel("exec");
|
channel = (ChannelExec) session.openChannel("exec");
|
||||||
channel.setCommand(command.toString());
|
channel.setCommand(command.toString());
|
||||||
channel.setErrStream(System.err, true);
|
channel.setErrStream(System.err, true);
|
||||||
in = channel.getInputStream();
|
in = channel.getInputStream();
|
||||||
channel.connect();
|
channel.connect();
|
||||||
|
|
||||||
byte[] buf = new byte[8192];
|
byte[] buf = new byte[8192];
|
||||||
int len;
|
int len;
|
||||||
while ((len = in.read(buf)) != -1) {
|
while ((len = in.read(buf)) != -1) {
|
||||||
out.write(buf, 0, len);
|
out.write(buf, 0, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
return out.toString(StandardCharsets.UTF_8).trim();
|
return out.toString(StandardCharsets.UTF_8).trim();
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return null;
|
return e.getMessage();
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
allClose(in, channel, session);
|
||||||
if (in != null) in.close();
|
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
if (channel != null) channel.disconnect();
|
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
if (session != null) session.disconnect();
|
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void allClose(InputStream inputStream, ChannelExec channel, Session session) {
|
||||||
|
// 关闭流
|
||||||
|
Optional.ofNullable(inputStream).ifPresent(stream -> {
|
||||||
|
try {
|
||||||
|
stream.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println(e.getMessage());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 断开通道
|
||||||
|
Optional.ofNullable(channel)
|
||||||
|
.filter(ChannelExec::isConnected)
|
||||||
|
.ifPresent(ChannelExec::disconnect);
|
||||||
|
|
||||||
|
// 断开会话
|
||||||
|
Optional.ofNullable(session)
|
||||||
|
.filter(Session::isConnected)
|
||||||
|
.ifPresent(Session::disconnect);
|
||||||
|
}
|
||||||
|
|
||||||
public static List<ContainerInfo> listContainers(MySftpAccounts accounts, boolean all) {
|
public static List<ContainerInfo> listContainers(MySftpAccounts accounts, boolean all) {
|
||||||
String fmt = "{{.ID}}|{{.Image}}|{{.Command}}|{{.CreatedAt}}|{{.Status}}|{{.Ports}}|{{.Names}}";
|
String fmt = "{{.ID}}|{{.Image}}|{{.Command}}|{{.CreatedAt}}|{{.Status}}|{{.Ports}}|{{.Names}}";
|
||||||
String cmd = (all ? "docker ps -a" : "docker ps") + " --format \"" + fmt + "\"";
|
String cmd = (all ? "docker ps -a" : "docker ps") + " --format \"" + fmt + "\"";
|
||||||
|
|||||||
Reference in New Issue
Block a user