123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- package com.miniframe.solverjob;
- import com.github.dockerjava.api.DockerClient;
- import com.github.dockerjava.api.async.ResultCallback;
- import com.github.dockerjava.api.command.CreateContainerResponse;
- import com.github.dockerjava.api.command.InspectContainerResponse;
- import com.github.dockerjava.api.command.LogContainerCmd;
- import com.github.dockerjava.api.command.RemoveContainerCmd;
- import com.github.dockerjava.api.model.Frame;
- import com.github.dockerjava.api.model.HostConfig;
- import com.github.dockerjava.api.model.Mount;
- import com.github.dockerjava.api.model.MountType;
- import com.github.dockerjava.core.DefaultDockerClientConfig;
- import com.github.dockerjava.core.DockerClientBuilder;
- import com.github.dockerjava.core.DockerClientConfig;
- import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
- import com.github.dockerjava.transport.DockerHttpClient;
- import java.util.ArrayList;
- import java.util.List;
- public class DockerExe {
- // public static String DOCKERHOST="tcp://127.0.0.1:2375/";
- // public static String APIVERSION="26.1.0";
- public static final String DOCKERHOST="tcp://192.168.0.132:2375/";
- public static final String APIVERSION="1.13.1";
- // 获取容器信息
- public static String getDocker(Integer aid,String stype){
- DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
- .withDockerHost(DOCKERHOST) // 设置 Docker 主机地址
- .withDockerTlsVerify(false) // 启用 TLS 验证
- .withApiVersion(APIVERSION) // 设置 API 版本
- .build();
- DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
- .dockerHost(config.getDockerHost())
- .sslConfig(config.getSSLConfig())
- .build();
- DockerClient dockerClient = DockerClientBuilder.getInstance(config)
- .withDockerHttpClient(httpClient)
- .build();
- InspectContainerResponse containerInfo = dockerClient.inspectContainerCmd(stype+"_"+aid.toString()).exec();
- //exited 停止 running 运行
- try{
- return containerInfo.getState().getStatus();
- }catch (Exception e){
- return "No such container:"+aid;
- }
- }
- public static void stopDocker(Integer aid, String stype){
- DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
- .withDockerHost(DOCKERHOST) // 设置 Docker 主机地址
- .withDockerTlsVerify(false) // 启用 TLS 验证
- .withApiVersion(APIVERSION) // 设置 API 版本
- .build();
- DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
- .dockerHost(config.getDockerHost())
- .sslConfig(config.getSSLConfig())
- .build();
- DockerClient dockerClient = DockerClientBuilder.getInstance(config)
- .withDockerHttpClient(httpClient)
- .build();
- try {
- // InspectContainerResponse containerInfo = dockerClient.inspectContainerCmd(stype+"_"+aid.toString()).exec();
- // //exited 停止 running 运行
- // String status ="error";
- // try{
- // status= containerInfo.getState().getStatus();
- // }catch (Exception e){
- //
- // }
- // if("running".equals(status)){
- // dockerClient.stopContainerCmd(stype+"_"+aid.toString())
- // .withTimeout(1000)
- // .exec();
- // }
- // if("exited".equals(status)){
- RemoveContainerCmd removeContainerCmd = dockerClient.removeContainerCmd(stype+"_"+aid.toString())
- .withForce(true);
- removeContainerCmd.exec();
- // }
- System.out.println("容器已停止: " + aid.toString());
- } catch (Exception e) {
- System.err.println("停止容器失败: " + e.getMessage());
- e.printStackTrace();
- }
- }
- public static void getDockerLogs(Integer aid,String stype, ResultCallback.Adapter<Frame> logsexe) throws InterruptedException {
- DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
- .withDockerHost(DOCKERHOST) // 设置 Docker 主机地址
- .withDockerTlsVerify(false) // 启用 TLS 验证
- .withApiVersion(APIVERSION) // 设置 API 版本
- .build();
- DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
- .dockerHost(config.getDockerHost())
- .sslConfig(config.getSSLConfig())
- .build();
- DockerClient dockerClient = DockerClientBuilder.getInstance(config)
- .withDockerHttpClient(httpClient)
- .build();
- // 获取容器日志
- LogContainerCmd logContainerCmd = dockerClient.logContainerCmd(stype+"_"+aid.toString())
- .withStdOut(true)
- .withStdErr(true)
- .withFollowStream(true);
- // 使用 ResultCallback.Adapter 处理日志
- logContainerCmd.exec(logsexe).awaitCompletion();
- }
- public static void fireGass(Integer aid, Integer jid,String stype) {
- DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
- .withDockerHost(DOCKERHOST) // 设置 Docker 主机地址
- .withDockerTlsVerify(false) // 启用 TLS 验证
- .withApiVersion(APIVERSION) // 设置 API 版本
- .build();
- DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
- .dockerHost(config.getDockerHost())
- .sslConfig(config.getSSLConfig())
- .build();
- DockerClient dockerClient = DockerClientBuilder.getInstance(config)
- .withDockerHttpClient(httpClient)
- .build();
- Mount zhtyVMount = new Mount()
- .withType(MountType.BIND)
- .withSource("/home/disaster/zhty/")
- .withTarget("/home/disaster/zhty/")
- .withReadOnly(false);
- Mount cephfsVMount = new Mount()
- .withType(MountType.BIND)
- .withSource("/cephfs/disaster/")
- .withTarget("/cephfs/disaster/")
- .withReadOnly(false);
- Mount gasBinMount = new Mount()
- .withType(MountType.BIND)
- .withSource("/home/disaster/gas/bin/")
- .withTarget("/home/disaster/gas/bin/")
- .withReadOnly(false);
- Mount runGasMount = new Mount()
- .withType(MountType.BIND)
- .withSource("/cephfs/disaster/" +
- aid +
- "/" +
- jid +
- "/gas/run.sh")
- .withTarget("/home/disaster/zhty/Gas/runGas.sh")
- .withReadOnly(false);
- List<Mount> am =new ArrayList<>();
- am.add(zhtyVMount);
- am.add(cephfsVMount);
- am.add(gasBinMount);
- am.add(runGasMount);
- //创建容器
- CreateContainerResponse container = dockerClient
- .createContainerCmd("gass:1.0")//镜像名称
- .withName(stype+"_"+aid)//容器名称
- .withHostConfig(
- HostConfig.newHostConfig()
- .withMounts(am)
- ).exec();
- dockerClient.startContainerCmd(container.getId()).exec();
- System.out.println("容器启动成功,ID: " + container.getId());
- }
- public static void fireExec(Integer aid, Integer jid,String stype) {
- DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
- .withDockerHost(DOCKERHOST) // 设置 Docker 主机地址
- .withDockerTlsVerify(false) // 启用 TLS 验证
- .withApiVersion(APIVERSION) // 设置 API 版本
- .build();
- DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
- .dockerHost(config.getDockerHost())
- .sslConfig(config.getSSLConfig())
- .build();
- DockerClient dockerClient = DockerClientBuilder.getInstance(config)
- .withDockerHttpClient(httpClient)
- .build();
- Mount zhtyVMount = new Mount()
- .withType(MountType.BIND)
- .withSource("/home/disaster/zhty/")
- .withTarget("/home/disaster/zhty/")
- .withReadOnly(false);
- Mount cephfsVMount = new Mount()
- .withType(MountType.BIND)
- .withSource("/cephfs/disaster/")
- .withTarget("/cephfs/disaster/")
- .withReadOnly(false);
- Mount runWaterMount = new Mount()
- .withType(MountType.BIND)
- .withSource("/cephfs/disaster/" +
- aid +
- "/" +
- jid +
- "/fire/runFile.sh")
- .withTarget("/home/disaster/zhty/Fire/runFire.sh")
- .withReadOnly(false);
- List<Mount> am =new ArrayList<>();
- am.add(zhtyVMount);
- am.add(cephfsVMount);
- am.add(runWaterMount);
- //创建容器
- CreateContainerResponse container = dockerClient
- .createContainerCmd("fire:1.0")//镜像名称
- .withName(stype+"_"+aid)//容器名称
- .withHostConfig(
- HostConfig.newHostConfig()
- .withMounts(am)
- ).exec();
- dockerClient.startContainerCmd(container.getId()).exec();
- System.out.println("容器启动成功,ID: " + container.getId());
- }
- public static void waterExec(Integer aid, Integer jid,String stype) throws InterruptedException {
- DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
- .withDockerHost(DOCKERHOST) // 设置 Docker 主机地址
- .withDockerTlsVerify(false) // 启用 TLS 验证
- .withApiVersion(APIVERSION) // 设置 API 版本
- .build();
- DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
- .dockerHost(config.getDockerHost())
- .sslConfig(config.getSSLConfig())
- .build();
- DockerClient dockerClient = DockerClientBuilder.getInstance(config)
- .withDockerHttpClient(httpClient)
- .build();
- Mount zhtyVMount = new Mount()
- .withType(MountType.BIND)
- .withSource("/home/disaster/zhty/")
- .withTarget("/home/disaster/zhty/")
- .withReadOnly(false);
- Mount cephfsVMount = new Mount()
- .withType(MountType.BIND)
- .withSource("/cephfs/disaster/")
- .withTarget("/cephfs/disaster/")
- .withReadOnly(false);
- Mount runWaterMount = new Mount()
- .withType(MountType.BIND)
- .withSource("/cephfs/disaster/" +
- aid +
- "/" +
- jid +
- "/water/runWater.sh")
- .withTarget("/home/disaster/zhty/Water/runWater.sh")
- .withReadOnly(false);
- List<Mount> am =new ArrayList<>();
- am.add(zhtyVMount);
- am.add(cephfsVMount);
- am.add(runWaterMount);
- //创建容器
- CreateContainerResponse container = dockerClient
- .createContainerCmd("water:1.0")//镜像名称
- .withName(stype+"_"+aid)//容器名称
- .withHostConfig(
- HostConfig.newHostConfig()
- .withMounts(am)
- ).exec();
- dockerClient.startContainerCmd(container.getId()).exec();
- System.out.println("容器启动成功,ID: " + container.getId());
- }
- }
|