Ver código fonte

docker exec gass

huangxingxing 8 meses atrás
pai
commit
857e21f640

+ 35 - 18
src/main/java/com/miniframe/bisiness/system/D00008Service.java

@@ -551,32 +551,49 @@ public class D00008Service extends D00008BaseModel implements ExecProcessFlow {
 	}
 	//异步执行
 	@SneakyThrows
-	public void exeGass1(Integer aid, Integer jid) throws Exception{
+	public void exeGass2(Integer aid, Integer jid) throws Exception{
 
-		CompletableFuture<Void> cf = CompletableFuture.supplyAsync(()->{
+		try {
+			LogService.addLog(aid,jid,"gass","求解——————————————————开始");
+			DockerExe.stopDocker(aid,"gass");
+			DockerExe.fireGass(aid, jid, "gass");
+		}catch (Exception e) {
+			LogService.addLog(aid,jid,"gass","求解——————————————————失败");
+			JobError(jid);
+			e.printStackTrace();
+		}
+		CompletableFuture<Void> cf = CompletableFuture.supplyAsync(()-> {
 			try {
-				Runtime runtime = Runtime.getRuntime();
-				Process p =null;
-				LogService.addLog(aid,jid,"Gass1","求解——————————————————开始");
-				p= runtime.exec("sh "+TemplateGenerator.BPATH + "/" + aid + "/" + jid + "/" + "/gas" + "/run.sh");
-				InputStream fis = p.getInputStream();
-				InputStreamReader isr = new InputStreamReader(fis);
-				BufferedReader br = new BufferedReader(isr);
-				String line = null;
-				while ((line = br.readLine()) != null) {
-					LogService.addLog(aid,jid,"Gass1",line);
-					System.out.println(line);
-				}
+				DockerExe.getDockerLogs(aid,"gass",new ResultCallback.Adapter<Frame>() {
+					@Override
+					public void onNext(Frame frame) {
+						LogService.addLog(aid,jid,"gass",new String(frame.getPayload()));
+						System.out.print(new String(frame.getPayload()));
+						super.onNext(frame);
+					}
+					@Override
+					public void onError(Throwable throwable) {
+						System.err.println("日志获取失败");
+						throwable.printStackTrace();
+						super.onError(throwable);
+					}
+					@Override
+					public void onComplete() {
+						System.out.println("日志获取完成");
+						super.onComplete();
+					}
+				});
+
 				//监测点数据保存
 				saveMonitorValue(aid,jid);
 				//路径保存
 				saveWayValue(aid,jid);
-				LogService.addLog(aid,jid,"Gass1","求解——————————————————成功");
+				LogService.addLog(aid,jid,"gass","求解——————————————————成功");
 				JobSucces(jid);
-			} catch (IOException | BusinessException e) {
-				LogService.addLog(aid,jid,"Gass1","求解——————————————————失败");
-				e.printStackTrace();
+			} catch (Exception e) {
+				LogService.addLog(aid,jid,"gass","求解——————————————————失败");
 				JobError(jid);
+				e.printStackTrace();
 			}
 			return null;
 		});

+ 55 - 0
src/main/java/com/miniframe/solverjob/DockerExe.java

@@ -106,6 +106,61 @@ public class DockerExe {
         // 使用 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")
+                .withTarget("/home/disaster/gas")
+                .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()