DockerExe.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package com.miniframe.solverjob;
  2. import com.github.dockerjava.api.DockerClient;
  3. import com.github.dockerjava.api.async.ResultCallback;
  4. import com.github.dockerjava.api.command.CreateContainerResponse;
  5. import com.github.dockerjava.api.command.InspectContainerResponse;
  6. import com.github.dockerjava.api.command.LogContainerCmd;
  7. import com.github.dockerjava.api.command.RemoveContainerCmd;
  8. import com.github.dockerjava.api.model.Frame;
  9. import com.github.dockerjava.api.model.HostConfig;
  10. import com.github.dockerjava.api.model.Mount;
  11. import com.github.dockerjava.api.model.MountType;
  12. import com.github.dockerjava.core.DefaultDockerClientConfig;
  13. import com.github.dockerjava.core.DockerClientBuilder;
  14. import com.github.dockerjava.core.DockerClientConfig;
  15. import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
  16. import com.github.dockerjava.transport.DockerHttpClient;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. public class DockerExe {
  20. public static String DOCKERHOST="tcp://127.0.0.1:2375/";
  21. public static String APIVERSION="26.1.0";
  22. // public static final String DOCKERHOST="tcp://192.168.0.132:2375/";
  23. // public static final String APIVERSION="1.13.1";
  24. // 获取容器信息
  25. public static String getDocker(Integer aid,String stype){
  26. DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
  27. .withDockerHost(DOCKERHOST) // 设置 Docker 主机地址
  28. .withDockerTlsVerify(false) // 启用 TLS 验证
  29. .withApiVersion(APIVERSION) // 设置 API 版本
  30. .build();
  31. DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
  32. .dockerHost(config.getDockerHost())
  33. .sslConfig(config.getSSLConfig())
  34. .build();
  35. DockerClient dockerClient = DockerClientBuilder.getInstance(config)
  36. .withDockerHttpClient(httpClient)
  37. .build();
  38. InspectContainerResponse containerInfo = dockerClient.inspectContainerCmd(stype+"_"+aid.toString()).exec();
  39. //exited 停止 running 运行
  40. try{
  41. return containerInfo.getState().getStatus();
  42. }catch (Exception e){
  43. return "No such container:"+aid;
  44. }
  45. }
  46. public static void stopDocker(Integer aid, String stype){
  47. DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
  48. .withDockerHost(DOCKERHOST) // 设置 Docker 主机地址
  49. .withDockerTlsVerify(false) // 启用 TLS 验证
  50. .withApiVersion(APIVERSION) // 设置 API 版本
  51. .build();
  52. DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
  53. .dockerHost(config.getDockerHost())
  54. .sslConfig(config.getSSLConfig())
  55. .build();
  56. DockerClient dockerClient = DockerClientBuilder.getInstance(config)
  57. .withDockerHttpClient(httpClient)
  58. .build();
  59. try {
  60. // InspectContainerResponse containerInfo = dockerClient.inspectContainerCmd(stype+"_"+aid.toString()).exec();
  61. // //exited 停止 running 运行
  62. // String status ="error";
  63. // try{
  64. // status= containerInfo.getState().getStatus();
  65. // }catch (Exception e){
  66. //
  67. // }
  68. // if("running".equals(status)){
  69. // dockerClient.stopContainerCmd(stype+"_"+aid.toString())
  70. // .withTimeout(1000)
  71. // .exec();
  72. // }
  73. // if("exited".equals(status)){
  74. RemoveContainerCmd removeContainerCmd = dockerClient.removeContainerCmd(stype+"_"+aid.toString())
  75. .withForce(true);
  76. removeContainerCmd.exec();
  77. // }
  78. System.out.println("容器已停止: " + aid.toString());
  79. } catch (Exception e) {
  80. System.err.println("停止容器失败: " + e.getMessage());
  81. e.printStackTrace();
  82. }
  83. }
  84. public static void getDockerLogs(Integer aid,String stype, ResultCallback.Adapter<Frame> logsexe) throws InterruptedException {
  85. DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
  86. .withDockerHost(DOCKERHOST) // 设置 Docker 主机地址
  87. .withDockerTlsVerify(false) // 启用 TLS 验证
  88. .withApiVersion(APIVERSION) // 设置 API 版本
  89. .build();
  90. DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
  91. .dockerHost(config.getDockerHost())
  92. .sslConfig(config.getSSLConfig())
  93. .build();
  94. DockerClient dockerClient = DockerClientBuilder.getInstance(config)
  95. .withDockerHttpClient(httpClient)
  96. .build();
  97. // 获取容器日志
  98. LogContainerCmd logContainerCmd = dockerClient.logContainerCmd(stype+"_"+aid.toString())
  99. .withStdOut(true)
  100. .withStdErr(true)
  101. .withFollowStream(true);
  102. // 使用 ResultCallback.Adapter 处理日志
  103. logContainerCmd.exec(logsexe).awaitCompletion();
  104. }
  105. public static void fireGass(Integer aid, Integer jid,String stype) {
  106. DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
  107. .withDockerHost(DOCKERHOST) // 设置 Docker 主机地址
  108. .withDockerTlsVerify(false) // 启用 TLS 验证
  109. .withApiVersion(APIVERSION) // 设置 API 版本
  110. .build();
  111. DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
  112. .dockerHost(config.getDockerHost())
  113. .sslConfig(config.getSSLConfig())
  114. .build();
  115. DockerClient dockerClient = DockerClientBuilder.getInstance(config)
  116. .withDockerHttpClient(httpClient)
  117. .build();
  118. Mount zhtyVMount = new Mount()
  119. .withType(MountType.BIND)
  120. .withSource("/home/disaster/zhty/")
  121. .withTarget("/home/disaster/zhty/")
  122. .withReadOnly(false);
  123. Mount cephfsVMount = new Mount()
  124. .withType(MountType.BIND)
  125. .withSource("/cephfs/disaster/")
  126. .withTarget("/cephfs/disaster/")
  127. .withReadOnly(false);
  128. Mount gasBinMount = new Mount()
  129. .withType(MountType.BIND)
  130. .withSource("/home/disaster/gas/bin/")
  131. .withTarget("/home/disaster/gas/bin/")
  132. .withReadOnly(false);
  133. Mount runGasMount = new Mount()
  134. .withType(MountType.BIND)
  135. .withSource("/cephfs/disaster/" +
  136. aid +
  137. "/" +
  138. jid +
  139. "/gas/run.sh")
  140. .withTarget("/home/disaster/zhty/Gas/runGas.sh")
  141. .withReadOnly(false);
  142. List<Mount> am =new ArrayList<>();
  143. am.add(zhtyVMount);
  144. am.add(cephfsVMount);
  145. am.add(gasBinMount);
  146. am.add(runGasMount);
  147. //创建容器
  148. CreateContainerResponse container = dockerClient
  149. .createContainerCmd("gass:1.0")//镜像名称
  150. .withName(stype+"_"+aid)//容器名称
  151. .withHostConfig(
  152. HostConfig.newHostConfig()
  153. .withMounts(am)
  154. ).exec();
  155. dockerClient.startContainerCmd(container.getId()).exec();
  156. System.out.println("容器启动成功,ID: " + container.getId());
  157. }
  158. public static void fireExec(Integer aid, Integer jid,String stype) {
  159. DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
  160. .withDockerHost(DOCKERHOST) // 设置 Docker 主机地址
  161. .withDockerTlsVerify(false) // 启用 TLS 验证
  162. .withApiVersion(APIVERSION) // 设置 API 版本
  163. .build();
  164. DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
  165. .dockerHost(config.getDockerHost())
  166. .sslConfig(config.getSSLConfig())
  167. .build();
  168. DockerClient dockerClient = DockerClientBuilder.getInstance(config)
  169. .withDockerHttpClient(httpClient)
  170. .build();
  171. Mount zhtyVMount = new Mount()
  172. .withType(MountType.BIND)
  173. .withSource("/home/disaster/zhty/")
  174. .withTarget("/home/disaster/zhty/")
  175. .withReadOnly(false);
  176. Mount cephfsVMount = new Mount()
  177. .withType(MountType.BIND)
  178. .withSource("/cephfs/disaster/")
  179. .withTarget("/cephfs/disaster/")
  180. .withReadOnly(false);
  181. Mount runWaterMount = new Mount()
  182. .withType(MountType.BIND)
  183. .withSource("/cephfs/disaster/" +
  184. aid +
  185. "/" +
  186. jid +
  187. "/fire/runFile.sh")
  188. .withTarget("/home/disaster/zhty/Fire/runFire.sh")
  189. .withReadOnly(false);
  190. List<Mount> am =new ArrayList<>();
  191. am.add(zhtyVMount);
  192. am.add(cephfsVMount);
  193. am.add(runWaterMount);
  194. //创建容器
  195. CreateContainerResponse container = dockerClient
  196. .createContainerCmd("fire:1.0")//镜像名称
  197. .withName(stype+"_"+aid)//容器名称
  198. .withHostConfig(
  199. HostConfig.newHostConfig()
  200. .withMounts(am)
  201. ).exec();
  202. dockerClient.startContainerCmd(container.getId()).exec();
  203. System.out.println("容器启动成功,ID: " + container.getId());
  204. }
  205. public static void waterExec(Integer aid, Integer jid,String stype) throws InterruptedException {
  206. DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
  207. .withDockerHost(DOCKERHOST) // 设置 Docker 主机地址
  208. .withDockerTlsVerify(false) // 启用 TLS 验证
  209. .withApiVersion(APIVERSION) // 设置 API 版本
  210. .build();
  211. DockerHttpClient httpClient = new ApacheDockerHttpClient.Builder()
  212. .dockerHost(config.getDockerHost())
  213. .sslConfig(config.getSSLConfig())
  214. .build();
  215. DockerClient dockerClient = DockerClientBuilder.getInstance(config)
  216. .withDockerHttpClient(httpClient)
  217. .build();
  218. Mount zhtyVMount = new Mount()
  219. .withType(MountType.BIND)
  220. .withSource("/home/disaster/zhty/")
  221. .withTarget("/home/disaster/zhty/")
  222. .withReadOnly(false);
  223. Mount cephfsVMount = new Mount()
  224. .withType(MountType.BIND)
  225. .withSource("/cephfs/disaster/")
  226. .withTarget("/cephfs/disaster/")
  227. .withReadOnly(false);
  228. Mount runWaterMount = new Mount()
  229. .withType(MountType.BIND)
  230. .withSource("/cephfs/disaster/" +
  231. aid +
  232. "/" +
  233. jid +
  234. "/water/runWater.sh")
  235. .withTarget("/home/disaster/zhty/Water/runWater.sh")
  236. .withReadOnly(false);
  237. List<Mount> am =new ArrayList<>();
  238. am.add(zhtyVMount);
  239. am.add(cephfsVMount);
  240. am.add(runWaterMount);
  241. //创建容器
  242. CreateContainerResponse container = dockerClient
  243. .createContainerCmd("water:1.0")//镜像名称
  244. .withName(stype+"_"+aid)//容器名称
  245. .withHostConfig(
  246. HostConfig.newHostConfig()
  247. .withMounts(am)
  248. ).exec();
  249. dockerClient.startContainerCmd(container.getId()).exec();
  250. System.out.println("容器启动成功,ID: " + container.getId());
  251. }
  252. }