feat(ssw-dos): 阶段5.2 - 迁移ssw.common.sandbox (7个类)

## 迁移 5.2 ssw.common.sandbox → 6.2 ssw.dos.sandbox
- SandBoxUtil (沙盒主工具)
- DockerUtil (调 Docker API, 适配 3.4 API)
- K8sUtil (调 K8s API)
- ContainerManager (容器管理)
- ContainerCache (容器缓存)
- TlsConfig (TLS 配置)
- TarUtil (5.2 ssw.common.util.TarUtil, 一起迁)

## 适配 6.2 docker-java 3.4 API
- 3.x 删除 DockerClientBuilder
- 用 DockerClientImpl.getInstance(config, httpClient) 替代
- ApacheDockerHttpClient 在 com.github.dockerjava.httpclient5 包

## pom.xml 新增依赖
- docker-java-core 3.4.0
- docker-java-api 3.4.0
- docker-java-transport-httpclient5 3.4.0
- commons-compress 1.26.0
- io.kubernetes:client-java 19.0.1

## 编译
- mvn compile -pl ssw-dos/ssw-dos-biz -am
- BUILD SUCCESS (5.412s)
This commit is contained in:
ssw committed 2026-07-06 17:04:34 +08:00
1 parent 2a2591bf45
commit eae1e2fcba
8 files changed
+1308

No files matched your search

+26
View File
@@ -33,5 +33,31 @@
<artifactId>client</artifactId>
<version>5.1.3</version>
</dependency>
<!-- 5.2 ssw-common.sandbox Docker/K8s 集成 -->
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-core</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-api</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-transport-httpclient5</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.26.0</version>
</dependency>
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
<version>19.0.1</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,35 @@
package ssw.dos.sandbox;
import java.util.List;
import java.util.Map;
public class ContainerCache {
public String id;
public int port;
public String host;
public String url;
public String test_url;
public Map JsonDate;
public int getExposedPort(String cPort) {
// 获取 NetworkSettings 对象
Map networkSettings = (Map)JsonDate.get("NetworkSettings");
// 获取 Ports 对象
Map ports = (Map)networkSettings.get("Ports");
if (ports == null || !ports.containsKey(cPort))
return 0;
// 获取端口对应的数组
List portArray = (List)ports.get(cPort);
// 获取第一个元素
if(portArray == null || portArray.isEmpty())
return 0;
Map portInfo = (Map)portArray.get(0);
return Integer.parseInt(portInfo.get("HostPort").toString());
}
}
@@ -0,0 +1,175 @@
package ssw.dos.sandbox;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.Socket;
import java.net.URL;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
@Slf4j
public class ContainerManager {
private static ConcurrentHashMap<String, ContainerCache> containerMap = new ConcurrentHashMap<>();
/**
* 检查指定端口是否开放
*
* @param port 待检查的端口号
* @return 如果端口开放,返回true;否则返回false
*/
public static boolean PortChecker(String host,int port) {
try (Socket socket = new Socket(host, port)) {
return true;
} catch (IOException e) {
return false;
}
}
/**
* 创建一个配置信息。
*
* @param containerName
* @param host 主机名或IP地址
* @return 返回可用的端口号
*/
private static ContainerCache newContainer(String containerName, String host){
// 寻找一个空闲端口并存储到缓存
ContainerCache c = new ContainerCache();
containerMap.put(containerName, c);
c.port = findAvailablePort(host);
c.host = host;
return c;
}
/**
* 获取容器缓存(如果没有创建一个)
* @param containerName
* @param du
* @param mHost
* @param mAddress
* @param cPort
* @return
* @throws Exception
*/
public static ContainerCache getContainer(String containerName, SandBoxUtil du, String mHost, String mAddress,String cPort) throws Exception {
ContainerCache container = containerMap.get(containerName);
if(container == null){
try {
container = du.getContainer(containerName);
}catch (Exception e){
log.info("宿主机不存在该名称的容器:" +containerName );
}
if(container != null){
container.host = mHost;
container.port = container.getExposedPort(cPort);
}
}
if(container == null)
container = newContainer(containerName,mHost);
container.url = String.format(mAddress,container.port);
return container;
}
/**
* 仅获得容器配置缓存
* @param containerName
* @return
*/
public static ContainerCache getContainer(String containerName){
return containerMap.get(containerName);
}
/**
* 判断指定的HTTP服务器是否可用
*
* @param urlString HTTP服务器的URL字符串
* @return 如果HTTP服务器可用,则返回true;否则返回false
*/
public static boolean isHttpServerAvailable(String urlString) {
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("HEAD");
int responseCode = connection.getResponseCode();
// 200-299之间的状态码通常表示成功
return (responseCode >= 200 && responseCode <= 299);
} catch (IOException e) {
// 如果有异常,则服务器可能不可用
log.error(e.toString());
return false;
}
}
/**
* 检查端口或URL是否可用
*
* @param host 主机名或IP地址
* @param port 端口号
* @param urlString URL字符串
* @return 如果端口可用或URL指向的HTTP服务器可用则返回true,否则返回false
*/
public static Boolean checkPortOrUrl(String host,int port,String urlString){
return PortChecker(host,port) || isHttpServerAvailable(urlString);
}
public static Boolean checkAndReUrl(ContainerCache c){
return PortChecker(c.host,c.port) || isHttpServerAvailable(c.url);
}
/**
* 重定向到指定URL地址
*
* @param url 要重定向到的URL地址
* @return 返回包含JavaScript代码的字符串,用于在浏览器中执行重定向操作
*/
public static String redir(String url) {
return url;
//return String.format("<script>window.location.href = \"%s\"</script>",url);
}
/**
* 查找可用的端口号
*
* @param host 主机地址
* @return 返回一个可用的端口号,如果未找到则返回0
* @throws IllegalArgumentException 如果传入的host为空或非法
*/
public static int findAvailablePort(String host) {
// 这里为了简化,我们直接随机生成端口并检查(实际中可能需要更复杂的逻辑)
int maxAttempts = 100; // 尝试次数
int portRangeStart = 10000;
int portRangeEnd = 29999;
for (int i = 0; i < maxAttempts; i++) {
int port = getRandomPort(portRangeStart, portRangeEnd);
if (!PortChecker(host,port)) {
// 检查端口是否真正被占用的逻辑(如使用Socket尝试连接)
return port;
}
}
return 0; // 如果没有找到空闲端口
}
/**
* 生成指定范围内的随机端口号
*
* @param start 端口号范围的起始值(包含)
* @param end 端口号范围的结束值(包含)
* @return 返回生成的随机端口号
* @throws IllegalArgumentException 如果起始值大于结束值,则抛出异常
*/
private static int getRandomPort(int start, int end) {
Random random = new Random();
return start + random.nextInt(end - start + 1);
}
}
@@ -0,0 +1,292 @@
package ssw.dos.sandbox;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.*;
import com.github.dockerjava.api.model.*;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientImpl;
import com.github.dockerjava.transport.DockerHttpClient;
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
import com.github.dockerjava.core.command.ExecStartResultCallback;
import com.github.dockerjava.core.command.LogContainerResultCallback;
import com.github.dockerjava.core.command.PullImageResultCallback;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import ssw.dos.sandbox.TarUtil;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
@Slf4j
public class DockerUtil {
private final DockerClient dockerClient;
/**
* 创建一个DockerUtil对象示例 (适配 docker-java 3.4 API)
*
* @throws InterruptedException 如果在等待获取Docker客户端实例时线程被中断,则抛出此异常
*/
public DockerUtil(String host,int port,String certPath) throws InterruptedException {
DefaultDockerClientConfig config = DefaultDockerClientConfig
.createDefaultConfigBuilder()
.withDockerHost("tcp://" + host + ":" + port)
.withDockerTlsVerify(true)
.withDockerCertPath(certPath)
.build();
DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
.dockerHost(config.getDockerHost())
.sslConfig(config.getSSLConfig())
.build();
dockerClient = DockerClientImpl.getInstance(config, httpClient);
}
public DockerUtil() {
DefaultDockerClientConfig config = DefaultDockerClientConfig
.createDefaultConfigBuilder()
.withDockerHost("unix:///var/run/docker.sock").build();
DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
.dockerHost(config.getDockerHost())
.build();
dockerClient = DockerClientImpl.getInstance(config, httpClient);
}
public DockerClient getClient( )
{
return dockerClient;
}
/**
* 获取GPU设备配置信息(这里只用gup1,多卡服务器要分开来)
*
* @return 包含GPU设备请求的列表
*/
public List<DeviceRequest> setGpuDevices() {
List<DeviceRequest> list = new ArrayList<>();
DeviceRequest deviceRequest = new DeviceRequest();
deviceRequest.withCount(1);
deviceRequest.withDriver("nvidia");
List<List<String>> capabilities = new ArrayList<>();
List<String> capability = new ArrayList<>();
capability.add("gpu");
capabilities.add (capability);
deviceRequest.withCapabilities(capabilities);
list.add(deviceRequest);
return list;
}
/**
* 根据名称查询容器
*
* @param containerName 容器名称
* @return 返回查询到的容器对象,若未找到则返回null
*/
public Container getContainerByName(String containerName) throws InterruptedException {
List<Container> containerList = dockerClient.listContainersCmd()
.withShowAll(Boolean.TRUE)
.exec();
for (int j = 0; j < containerList.size(); j++) {
Container container = containerList.get(j);
String[] labels = container.getNames();
for (int i = 0; i < labels.length; i++)
if(Objects.equals(labels[i],"/" + containerName)){
log.info("通过名称查询到容器, containerName:" + containerName);
return container;
}
}
log.error("未找到指定镜像容器:"+ containerName);
return null;
}
/**
* 启动容器
*
* @param imageName 镜像名称
* @param containerName 容器名称
* @param hostConfig 主机配置
* @return 返回启动的容器对象
* @throws InterruptedException 如果等待容器启动的过程中被中断,则抛出此异常
*/
public Container createContainer(String imageName, String containerName, HostConfig hostConfig, String[] envs, Boolean gpu, String...cmd) throws InterruptedException {
//查找容器是否存在,如果存在则先清除,以确保配置更新
dropContainer(containerName);
//下载镜像(如果镜像未下载)
pullImage(imageName);
if (gpu) //是否gpu环境
hostConfig.withDeviceRequests(setGpuDevices());
CreateContainerCmd ccc = dockerClient.createContainerCmd(imageName)
.withHostConfig(hostConfig)
.withName(containerName);
if (envs != null)
ccc = ccc.withEnv(envs);
if (cmd != null) //是否需要执行命令,则绑定启动项
ccc = ccc.withCmd(cmd);
ccc.exec();
Container container = getContainerByName(containerName);
//启动它
dockerClient.startContainerCmd(container.getId()).exec();
// 设置日志输出
LogContainerCmd logContainerCmd = dockerClient.logContainerCmd(container.getId())
.withStdOut(true)
.withStdErr(true);
logContainerCmd.exec(logContainerResultCallback);
return container;
}
public Container createContainer(String imageName, String containerName, HostConfig hostConfig, Boolean gpu, String...cmd) throws InterruptedException {
return createContainer(imageName, containerName,hostConfig,null,gpu, cmd);
}
/**
* 从Docker仓库中拉取指定名称的镜像
*
* @param imageName 镜像名称
* @throws InterruptedException 如果在拉取镜像过程中发生中断,则抛出此异常
*/
public void pullImage(String imageName) throws InterruptedException {
//判断镜像是否存在,如果存在就不再下载
List<Image> images = dockerClient.listImagesCmd().exec();
for(Image i : images){
if(Arrays.asList(i.getRepoTags()).contains(imageName)){
System.out.println("镜像已下载:" + imageName);
return;
}
}
PullImageCmd pullImageCmd = dockerClient.pullImageCmd(imageName);
PullImageResultCallback pullImageResultCallback = new PullImageResultCallback(){
@Override
public void onNext(PullResponseItem item) {
log.info("下载镜像状态:" + item.getStatus());
super.onNext(item);
}
};
pullImageCmd.exec(pullImageResultCallback)
.awaitCompletion(); // 等待下载完成,不然一直阻塞
log.info("镜像下载完成" + imageName);
}
/**
* 启动指定名称的Docker容器(已经存在)
*
* @param containerName 要启动的Docker容器的名称
* @throws Exception 如果在启动容器过程中发生任何异常,将抛出此异常
*/
public void execStartContainer(String containerName){
dockerClient.startContainerCmd(containerName).exec();
}
/**
* 销毁指定的 Docker 容器
*
* @param containerName 要销毁的容器的名称
* @return 返回销毁结果,成功返回"销毁环境成功!",失败返回"环境不存在!"
* @throws InterruptedException 如果等待线程中断,则抛出 InterruptedException 异常
*/
public String dropContainer(String containerName) throws InterruptedException {
Container container = getContainerByName(containerName);
if(container == null) {
log.error("环境不存在!", containerName);
return "环境不存在!";
}
if (Objects.equals(container.getState(), "running")) {
dockerClient.stopContainerCmd(containerName).exec();
log.info("停止环境成功!docker name:{}", containerName);
}
dockerClient.removeContainerCmd(containerName).exec();
log.info("删除环境成功!docker name:{}", containerName);
return "销毁环境成功!";
}
/**
* 停止指定名称的Docker容器
*
* @param containerName 要停止的Docker容器的名称
* @throws Exception 如果在停止容器过程中发生任何异常,将抛出此异常
*/
public void execStopContainerCmd(String containerName) {
dockerClient.stopContainerCmd(containerName).exec();
}
/**
* 在指定的Docker容器中执行命令
*
* @param id 容器ID
* @param cmd 要在容器中执行的命令
* @throws InterruptedException 如果线程在等待时被中断,则抛出此异常
*/
public void exec(String id, String cmd) throws InterruptedException {
// 要在容器中执行的命令
ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(id)
.withAttachStdout(true).withCmd("bash", "-c", cmd).exec();
dockerClient.execStartCmd(execCreateCmdResponse.getId()).withDetach(false).withTty(true)
.exec(new ExecStartResultCallback(System.out, System.err)).awaitCompletion();
}
/**
* 日志信息回调
*/
static LogContainerResultCallback logContainerResultCallback = new LogContainerResultCallback() {
/**
* 当接收到下一个Frame对象时触发该方法
*
* @param item 接收到的Frame对象
*/
@Override
public void onNext(Frame item) {
log.info(new String(item.getPayload()));
super.onNext(item);
}
};
/**
* 压缩文件从输入流复制到Docker容器中指定路径下
*
* @param id Docker容器的ID
* @param from 输入,需要复制的文件名(tar格式)
* @param to Docker容器中的目标路径
*/
public void copy(String id, String from, String to) throws IOException {
FileInputStream f = new FileInputStream(from);
CopyArchiveToContainerCmd cmd = dockerClient.copyArchiveToContainerCmd(id)
.withTarInputStream(f)
.withRemotePath(to);
cmd.exec();
}
/**
* 将多个文件复制到Docker容器的指定目录下
*
* @param id Docker容器的ID或名称
* @param to 目标目录
* @param files 要复制的文件列表,可变参数
* @throws IOException 如果复制文件时发生IO异常
*/
public void copyfs(String id, String to,String ...files) throws IOException {
InputStream f = new ByteArrayInputStream(TarUtil.createTarStream(files));
CopyArchiveToContainerCmd cmd = dockerClient.copyArchiveToContainerCmd(id)
.withTarInputStream(f)
.withRemotePath(to);
cmd.exec();
}
}
@@ -0,0 +1,169 @@
package ssw.dos.sandbox;
import java.io.*;
import java.net.URL;
import java.util.Base64;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.HttpsURLConnection;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class K8sUtil {
private static final Logger logger = Logger.getLogger(K8sUtil.class.getName());
private final String apiServerUrl;
private final String token;
private final String namespace;
public K8sUtil(String apiServerUrl, String token, String namespace) {
this.apiServerUrl = apiServerUrl;
this.token = token;
this.namespace = namespace;
}
private HttpsURLConnection createConnection(String endpoint) throws IOException {
URL url = new URL(apiServerUrl + "/api/v1/namespaces/" + namespace + endpoint);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "Bearer " + token);
conn.setRequestProperty("Content-Type", "application/json");
return conn;
}
public JSONObject createPod(String podName, String imageName, String[] command) throws IOException {
JSONObject podSpec = new JSONObject();
podSpec.put("name", podName);
podSpec.put("image", imageName);
if (command != null) {
podSpec.put("command", command);
}
JSONObject podBody = new JSONObject();
podBody.put("apiVersion", "v1");
podBody.put("kind", "Pod");
podBody.put("metadata", new JSONObject().put("name", podName));
podBody.put("spec", new JSONObject().put("containers", new JSONArray().add(podSpec)));
HttpsURLConnection conn = (HttpsURLConnection) createConnection("/pods");
conn.setRequestMethod("POST");
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(podBody.toJSONString().getBytes("UTF-8"));
os.flush();
os.close();
if (conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
InputStream is = conn.getInputStream();
String response = getResponseString(is);
return JSON.parseObject(response);
} else {
InputStream errorStream = conn.getErrorStream();
String error = getResponseString(errorStream != null ? errorStream : conn.getInputStream());
logger.log(Level.SEVERE, "创建 Pod 失败: " + error);
return null;
}
}
public JSONObject getPod(String podName) throws IOException {
HttpsURLConnection conn = (HttpsURLConnection) createConnection("/pods/" + podName);
conn.setRequestMethod("GET");
if (conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
InputStream is = conn.getInputStream();
String response = getResponseString(is);
return JSON.parseObject(response);
} else {
InputStream errorStream = conn.getErrorStream();
String error = getResponseString(errorStream != null ? errorStream : conn.getInputStream());
logger.log(Level.SEVERE, "获取 Pod 失败: " + error);
return null;
}
}
public boolean deletePod(String podName) throws IOException {
HttpsURLConnection conn = (HttpsURLConnection) createConnection("/pods/" + podName);
conn.setRequestMethod("DELETE");
if (conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
return true;
} else {
InputStream errorStream = conn.getErrorStream();
String error = getResponseString(errorStream != null ? errorStream : conn.getInputStream());
logger.log(Level.SEVERE, "删除 Pod 失败: " + error);
return false;
}
}
public JSONObject execInPod(String podName, String command) throws IOException {
String encodedCommand = Base64.getEncoder().encodeToString(command.getBytes("UTF-8"));
HttpsURLConnection conn = (HttpsURLConnection) createConnection("/pods/" + podName + "/exec");
conn.setRequestMethod("POST");
conn.setDoOutput(true);
JSONObject requestBody = new JSONObject();
requestBody.put("container", podName);
requestBody.put("command", new String[]{"/bin/sh", "-c", "echo " + encodedCommand + " | base64 -d | sh"});
requestBody.put("stdin", true);
requestBody.put("stdout", true);
requestBody.put("stderr", true);
OutputStream os = conn.getOutputStream();
os.write(requestBody.toJSONString().getBytes("UTF-8"));
os.flush();
os.close();
if (conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
InputStream is = conn.getInputStream();
String response = getResponseString(is);
return JSON.parseObject(response);
} else {
InputStream errorStream = conn.getErrorStream();
String error = getResponseString(errorStream != null ? errorStream : conn.getInputStream());
logger.log(Level.SEVERE, "在 Pod 中执行命令失败: " + error);
return null;
}
}
private String getResponseString(InputStream is) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder response = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
response.append(line);
}
br.close();
return response.toString();
}
public static void test() {
// 示例用法
String apiServerUrl = "https://your-kubernetes-api-server:443";
String token = "your-service-account-token";
String namespace = "default";
K8sUtil kubernetesUtil = new K8sUtil(apiServerUrl, token, namespace);
try {
// 创建一个 Pod
JSONObject pod = kubernetesUtil.createPod("test-pod", "nginx:latest", new String[]{"/bin/sh", "-c", "nginx -g 'daemon off;'"});
System.out.println("创建的 Pod: " + pod.toJSONString());
// 获取 Pod 信息
JSONObject podInfo = kubernetesUtil.getPod("test-pod");
System.out.println("Pod 信息: " + podInfo.toJSONString());
// 在 Pod 中执行命令
JSONObject execResult = kubernetesUtil.execInPod("test-pod", "echo 'Hello from exec!'");
System.out.println("执行结果: " + execResult.toJSONString());
// 删除 Pod
boolean deleted = kubernetesUtil.deletePod("test-pod");
System.out.println("Pod 已删除: " + deleted);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@@ -0,0 +1,416 @@
package ssw.dos.sandbox;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import ssw.dos.sandbox.TarUtil;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
@Slf4j
public class SandBoxUtil {
private static final String DOCKER_API_VERSION = "/v1.41"; // 根据你的Docker版本调整API版本
private static final String DEFAULT_DOCKER_HOST = "http://localhost:2375";
private final String dockerHost;
private final String dockerCertPath;
public SandBoxUtil(String host, int port, String certPath) {
this.dockerHost = "https://" + host + ":" + port;
this.dockerCertPath = certPath;
}
public SandBoxUtil() {
this.dockerHost = DEFAULT_DOCKER_HOST;
this.dockerCertPath = null;
}
private HttpsURLConnection createConnection(String endpoint) throws Exception {
URL url = new URL(dockerHost + DOCKER_API_VERSION + endpoint);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/json");
SSLContext sslContext = TlsConfig.configureSSLContext(dockerCertPath, "abcvefdf");
conn.setSSLSocketFactory(sslContext.getSocketFactory());
return conn;
}
Map<String,Object> hostConfig = new HashMap<>();
List<String> Env = new ArrayList<>();
List<String> Cmd = new ArrayList<>();
/**
* 挂载目录
* @param hostPath 宿主机目录
* @param cpath 容器目录.如需要请在后面增加模式,如:ro 只读
*/
public void addBinds(String hostPath,String cpath){
if(!hostConfig.containsKey("Binds"))
hostConfig.put("Binds",new ArrayList<String>());
((ArrayList<String>)hostConfig.get("Binds")).add(hostPath + ":" + cpath);
}
public void withShmSize(long l) {
hostConfig.put("ShmSize",l);
}
public void addEnv(String env) {
Env.add(env);
}
public void addCmd(String cmd) {
Cmd.add(cmd);
}
public void addPortBindings(int export, int hostPort) {
if (!hostConfig.containsKey("PortBindings"))
hostConfig.put("PortBindings",new HashMap());
List list = new ArrayList<>();
Map<String,String> host = new HashMap<String,String>();
host.put("HostIp","0.0.0.0");
host.put("HostPort", String.valueOf(export));
list.add(host);
((Map)hostConfig.get("PortBindings")).put(hostPort + "/tcp",list);
}
public String createContainer(String imageName, String Name) throws Exception {
// 下载镜像(如果镜像未下载)
pullImage(imageName);
// 构建容器配置
Map<String, Object> containerConfig = new HashMap<>();
containerConfig.put("Image", imageName);
containerConfig.put("Cmd", Cmd);
containerConfig.put("Env", Env);
containerConfig.put("HostConfig", hostConfig);
ObjectMapper objectMapper = new ObjectMapper();
// 发送创建容器请求
HttpURLConnection conn = createConnection("/containers/create?name=" + Name);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(objectMapper.writeValueAsBytes(containerConfig));
os.flush();
os.close();
InputStream is = conn.getInputStream();
// 处理响应
if (conn.getResponseCode() == HttpURLConnection.HTTP_CREATED) {
Map<String, Object> ro = objectMapper.readValue(is, Map.class);
return ro.get("Id").toString();
} else {
throw new Exception("创建容器失败: " + is);
}
}
public void withGpu() {
// 设置GPU设备
List<Map<String,Object>> listDeviceRequest = new ArrayList<>();
Map<String,Object> deviceRequest = new HashMap<>();
deviceRequest.put("Count",1);
deviceRequest.put("Driver","nvidia");
List<List<String>> capabilities = new ArrayList<>();
List<String> capability = new ArrayList<>();
capability.add("gpu");
capabilities.add(capability);
deviceRequest.put("Capabilities",capabilities);
listDeviceRequest.add(deviceRequest);
hostConfig.put("DeviceRequests",listDeviceRequest);
}
public void pullImage(String imageName) throws Exception {
// 判断镜像是否存在,如果存在就不再下载
List images = listImages();
for (Object image : images) {
Map imageJson = (Map) image;
// 获取repoTags数组
List repoTags = (List) imageJson.get("RepoTags");
if(repoTags == null)continue;
// 遍历repoTags数组
for (Object tag : repoTags) {
String repoTag = tag.toString();
if (repoTag.contains(imageName)) {
System.out.println("镜像已存在: " + imageName);
return;
}
}
}
// 发送拉取镜像请求
HttpsURLConnection conn = createConnection(String.format("/images/create?fromImage=%s",imageName));
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setConnectTimeout(15000);
conn.setReadTimeout(60000);
int responseCode = conn.getResponseCode();
log.info("Docker API Response Code: {}",responseCode);
// 等待镜像拉取完成
InputStream is = conn.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 解析响应
log.info("Docker API Response: {}", response);
// 检查是否成功
if (responseCode == 200 || responseCode == 201) {
log.info("Image {}pulled successfully!", imageName);
} else {
log.info("Failed to pull image.");
}
}
public List listImages() throws Exception {
HttpURLConnection conn = createConnection("/images/json");
conn.setRequestMethod("GET");
InputStream is = conn.getInputStream();
return new ObjectMapper().readValue(is,List.class);
}
public String buildImage(String Image_name,String DOCKERFILE_CONTENT) throws Exception {
// 构建Docker镜像
HttpURLConnection conn = createConnection("/build?t=" + Image_name);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setConnectTimeout(3600000); // 设置连接超时时间(毫秒)
conn.setReadTimeout(3600000); // 设置读取超时时间(毫秒)
File dockerfile = new File("rts", "Dockerfile");
try (BufferedWriter writer = new BufferedWriter(new FileWriter(dockerfile))) {
writer.write(DOCKERFILE_CONTENT);
}
// 设置构建上下文
try (OutputStream os = conn.getOutputStream();
TarArchiveOutputStream taos = new TarArchiveOutputStream(os)) {
// 创建tar流
taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
// 添加Dockerfile到tar流
TarArchiveEntry entry = new TarArchiveEntry(dockerfile.getName());
entry.setSize(dockerfile.length());
taos.putArchiveEntry(entry);
try (FileInputStream dockerfileInputStream = new FileInputStream(dockerfile)) {
byte[] buffer = new byte[4096];
int length;
while ((length = dockerfileInputStream.read(buffer)) != -1)
taos.write(buffer, 0, length);
}
taos.closeArchiveEntry();
taos.finish();
}
// 发送请求
int responseCode = conn.getResponseCode();
log.info("Docker API Response Code: {}", responseCode);
// 读取响应
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null)
response.append(inputLine);
in.close();
// 解析响应
log.info("Docker API Response: {}", response);
return response.toString();
}
public ContainerCache getContainer(String Id) throws Exception {
HttpsURLConnection conn = createConnection("/containers/"+Id+"/json");
conn.setRequestMethod("GET");
InputStream is = conn.getInputStream();
ContainerCache container = new ContainerCache();
container.JsonDate = new ObjectMapper().readValue(is,Map.class);
return container;
}
public String getContainerList() throws Exception {
// 调用Docker Remote API获取容器列表
HttpsURLConnection connection = createConnection("/containers/json?all=1");
connection.setRequestMethod("GET");
return readStream(connection.getInputStream());
}
public void startContainer(String containerId) throws Exception {
HttpsURLConnection conn = createConnection("/containers/" + containerId + "/start");
conn.setRequestMethod("POST");
conn.getResponseCode();
log.info("容器启动成功: {}", containerId);
}
public String logContainer(String containerId) throws Exception {
HttpsURLConnection conn = createConnection("/containers/" + containerId + "/logs?stdout=1&stderr=1");
conn.setRequestMethod("GET");
return readStream(conn.getInputStream());
}
public String dropContainer(String containerId) throws Exception {
stopContainer(containerId);
deleteContainer(containerId);
log.info("容器删除成功: " + containerId);
return "容器删除成功";
}
public void stopContainer(String containerId) throws Exception {
HttpsURLConnection conn = createConnection("/containers/" + containerId + "/stop");
conn.setRequestMethod("POST");
conn.getResponseCode();
log.info("容器停止成功: " + containerId);
}
public String restartContainer(String containerId) throws Exception {
HttpsURLConnection conn = createConnection("/containers/" + containerId + "/restart");
conn.setRequestMethod("POST");
conn.getResponseCode();
log.info("容器重启成功: {}", containerId);
return "容器重启成功";
}
public void deleteContainer(String containerId) throws Exception {
HttpsURLConnection conn = createConnection("/containers/" + containerId);
conn.setRequestMethod("DELETE");
conn.getResponseCode();
log.info("容器删除成功: {}", containerId);
}
public String execContainer(String id, String... cmd) throws Exception {
// 创建执行实例的请求体
Map<String,Object> createExecBody = new HashMap<>();
createExecBody.put("Cmd", cmd); // 必须是字符串数组,如 ["ls","-l"]
createExecBody.put("AttachStdin", false);
createExecBody.put("AttachStdout", true);
createExecBody.put("AttachStderr", true);
// 第一步:创建exec实例
HttpsURLConnection execCreateConn = createConnection("/containers/" + id + "/exec");
execCreateConn.setRequestMethod("POST");
execCreateConn.setDoOutput(true);
execCreateConn.setRequestProperty("Content-Type", "application/json");
try (OutputStream os = execCreateConn.getOutputStream()) {
ObjectMapper mapper = new ObjectMapper();
os.write(mapper.writeValueAsBytes(createExecBody));
}
// 获取exec实例的ID(响应体包含)
int responseCode = execCreateConn.getResponseCode();
if (responseCode != 201) {
throw new RuntimeException("Failed to create exec instance: " + readStream(execCreateConn.getErrorStream()));
}
String execInstanceId = "";
try (InputStream is = execCreateConn.getInputStream()) {
ObjectMapper mapper = new ObjectMapper();
Map responseMap = mapper.readValue(is, Map.class);
execInstanceId = (String) responseMap.get("Id");
}
// 第二步:启动exec实例
HttpsURLConnection execStartConn = createConnection("/exec/" + execInstanceId + "/start");
execStartConn.setRequestMethod("POST");
execStartConn.setDoOutput(true);
Map<String,Object> startExecBody = new HashMap<>();
startExecBody.put("Detach", false); // 阻塞直到命令执行完成
startExecBody.put("Tty", false); // 根据需求设置伪终端
try (OutputStream os = execStartConn.getOutputStream()) {
ObjectMapper mapper = new ObjectMapper();
os.write(mapper.writeValueAsBytes(startExecBody));
}
// 处理执行结果和输出(需要读取响应流)
int startResponseCode = execStartConn.getResponseCode();
if (startResponseCode != 200) {
throw new RuntimeException("Execution failed: " + readStream(execStartConn.getErrorStream()));
}
return readStream(execStartConn.getInputStream());
}
/**
* 从流读取字符串
* @param is
* @return
* @throws IOException
*/
private String readStream(InputStream is) throws IOException {
if (is == null) return "";
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
return sb.toString();
}
}
/**
* 发送文件到容器
*
* @param containerId 容器 ID
* @param containerDirectory 容器中的目标目录
* @param dataPath Tar 流原始路径
* @throws IOException 如果发送请求时发生 IO 异常
*/
public void sendFileToContainer( String containerId, String containerDirectory, String... dataPath) throws Exception {
HttpsURLConnection conn = createConnection("/containers/"+containerId+"/archive?path="+containerDirectory);
conn.setRequestMethod("PUT");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
conn.setRequestProperty("Content-Type", "application/x-tar");
try (OutputStream os = conn.getOutputStream();) {
os.write(TarUtil.createTarStream(dataPath));
os.flush();
}
// 检查响应
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
log.info("文件发送成功!");
} else {
log.error("文件发送失败,响应码: " + conn.getResponseCode());
}
conn.disconnect();
}
}
@@ -0,0 +1,118 @@
package ssw.dos.sandbox;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import java.io.*;
import java.nio.file.Paths;
/**
* 提供创建tar压缩包的工具方法
*/
public class TarUtil {
/**
* 创建tar压缩包
*
* @param files 要压缩的文件路径数组
* @return 返回tar压缩包的字节数组
* @throws IOException 如果创建tar压缩包时发生IO异常
*/
public static byte[] createTarStream(String ...files) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (TarArchiveOutputStream tarOutput = new TarArchiveOutputStream(byteArrayOutputStream)) {
for (String filePath : files) {
File file = new File(filePath);
if (file.isDirectory()) {
addDirectoryToTar(file, file.getName(), tarOutput);
} else {
addFileToTar(file, file.getName(), tarOutput);
}
}
}
return byteArrayOutputStream.toByteArray();
}
/**
* 将文件添加到tar压缩包中
*
* @param file 要添加的文件
* @param entryName tar压缩包中的文件名称
* @param tarOutput tar压缩包的输出流
* @throws IOException 如果添加文件时发生IO异常
*/
private static void addFileToTar(File file, String entryName, TarArchiveOutputStream tarOutput) throws IOException {
TarArchiveEntry tarEntry = new TarArchiveEntry(file, entryName);
tarOutput.putArchiveEntry(tarEntry);
try (FileInputStream fis = new FileInputStream(file)) {
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
tarOutput.write(buffer, 0, length);
}
}
tarOutput.closeArchiveEntry();
}
/**
* 将目录递归添加到tar压缩包中
*
* @param directory 要添加的目录
* @param base tar压缩包中的基础路径
* @param tarOutput tar压缩包的输出流
* @throws IOException 如果添加目录时发生IO异常
*/
private static void addDirectoryToTar(File directory, String base, TarArchiveOutputStream tarOutput) throws IOException {
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
String entryName = base + File.separator + file.getName();
if (file.isDirectory()) {
addDirectoryToTar(file, entryName, tarOutput);
} else {
addFileToTar(file, entryName, tarOutput);
}
}
}
}
/**
* 使用系统tar命令压缩文件
*
* @param tarFilePath 压缩后的tar文件路径
*/
private void compressFilesToTar(String tarFilePath, String[] filePaths) throws Exception {
// 构建tar命令
StringBuilder tarCommand = new StringBuilder("tar -cvf ");
tarCommand.append(tarFilePath).append(" ");
for (String filePath : filePaths)
tarCommand.append(filePath).append(" ");
// 执行tar命令
Process process = Runtime.getRuntime().exec(tarCommand.toString(),null, Paths.get("rts/").toFile());
int exitCode = process.waitFor();
if (exitCode != 0) {
throw new Exception("压缩文件失败,tar命令执行错误");
}
}
//
// File tempTarFile = File.createTempFile("temp", ".tar");
// tempTarFile.deleteOnExit();
//
// // 使用系统tar命令压缩文件
// compressFilesToTar(tempTarFile.getAbsolutePath(), dataPath);
//
// try (OutputStream os = conn.getOutputStream();
// FileInputStream fis = new FileInputStream(tempTarFile)) {
//
// byte[] buffer = new byte[4096];
// int bytesRead;
// while ((bytesRead = fis.read(buffer)) != -1) {
// os.write(buffer, 0, bytesRead);
// }
// os.flush();
// }
// // 删除临时tar文件
// tempTarFile.delete();
}
@@ -0,0 +1,77 @@
package ssw.dos.sandbox;
import javax.net.ssl.*;
import java.io.*;
import java.net.URL;
import java.security.KeyStore;
import java.security.cert.CertificateFactory;
public class TlsConfig {
/**
* 配置 SSLContext 使用客户端证书和私钥
*
* @param certPath 客户端证书路径
* @param certPassword 客户端证书密码
* @return 配置好的 SSLContext
* @throws Exception 如果配置过程中发生任何异常
*/
public static SSLContext configureSSLContext(String certPath, String certPassword) throws Exception {
// 加载客户端证书和私钥
KeyStore keyStore = KeyStore.getInstance("PKCS12");
try (FileInputStream fis = new FileInputStream(certPath + "/client.p12")) {
keyStore.load(fis, certPassword.toCharArray());
}
// 创建 KeyManagerFactory
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, certPassword.toCharArray());
// 加载 CA 证书
KeyStore trustStore = KeyStore.getInstance("JKS");
try (FileInputStream fis = new FileInputStream(certPath + "/ca.pem")) {
// 将 CA 证书转换为 Java 可识别的格式
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
java.security.cert.Certificate cert = certificateFactory.generateCertificate(fis);
trustStore.load(null); // 初始化一个空的 KeyStore
trustStore.setCertificateEntry("ca", cert);
}
// 创建 TrustManagerFactory
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
// 创建并初始化 SSLContext
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(
keyManagerFactory.getKeyManagers(),
trustManagerFactory.getTrustManagers(),
null
);
return sslContext;
}
/**
* 创建一个使用 TLS 的安全 HTTP 连接
*
* @param dockerHost Docker Remote API 的地址
* @param certPath 客户端证书路径
* @param certPassword 客户端证书密码
* @return 配置好的 HttpURLConnection
* @throws Exception 如果创建连接过程中发生任何异常
*/
public static HttpsURLConnection createTLSConnection(String dockerHost, String certPath, String certPassword) throws Exception {
// 配置 SSLContext
SSLContext sslContext = configureSSLContext(certPath, certPassword);
// 创建 HTTPS URL
URL url = new URL(dockerHost);
// 创建 HTTPS 连接
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setSSLSocketFactory(sslContext.getSocketFactory());
return conn;
}
}