From aa815fe510b9fce08dfb14415fbac5e3a40c2a4c Mon Sep 17 00:00:00 2001 From: whb <17803890193@163.com> Date: Fri, 2 Aug 2024 17:15:25 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E8=BD=AC=E6=8D=A2html?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/stu/JupyterhubController.java | 329 +++++++++++++++--- 1 file changed, 273 insertions(+), 56 deletions(-) diff --git a/src/main/java/com/sztzjy/financial_bigdata/controller/stu/JupyterhubController.java b/src/main/java/com/sztzjy/financial_bigdata/controller/stu/JupyterhubController.java index f98c67b..08f64f3 100644 --- a/src/main/java/com/sztzjy/financial_bigdata/controller/stu/JupyterhubController.java +++ b/src/main/java/com/sztzjy/financial_bigdata/controller/stu/JupyterhubController.java @@ -8,15 +8,23 @@ import com.sztzjy.financial_bigdata.util.file.IFileUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.mime.HttpMultipartMode; +import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.entity.mime.content.ByteArrayBody; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; +import java.io.*; import java.util.List; /** @@ -36,19 +44,19 @@ public class JupyterhubController { @ApiOperation("创建容器") @GetMapping("/jumpJupyerHub") @AnonymousAccess - public ResultEntity jumpJupyerHub(@ApiParam("案例名")String caseName) { + public ResultEntity jumpJupyerHub(@ApiParam("案例名") String caseName) { String username = IdUtil.fastSimpleUUID(); - String substring = username.substring(2,9); - String name = "zy" +substring; + String substring = username.substring(2, 9); + String name = "zy" + substring; try { JupyterHubTokenManager.createUser(name); System.out.println("创建用户成功!"); - JupyterHubTokenManager.startServer(name,caseName); + JupyterHubTokenManager.startServer(name, caseName); System.out.println("启动服务!"); String url = JupyterHubTokenManager.generateToken(name); - return new ResultEntity<>(HttpStatus.OK,"容器创建成功!",url); + return new ResultEntity<>(HttpStatus.OK, "容器创建成功!", url); } catch (Exception e) { throw new RuntimeException(e); @@ -58,20 +66,20 @@ public class JupyterhubController { /** - * - * 1.进入页面首先发送请求判断数据是否存在 - * 2.不存在直接调用上传接口,将数据传输过来 格式 文件夹-(ipynb,data/数据集) - * - * 教师端上传案例直接上传到对应文件夹下, 教师端管理时候 - * - */ + * 1.进入页面首先发送请求判断数据是否存在 + * 2.不存在直接调用上传接口,将数据传输过来 格式 文件夹-(ipynb,data/数据集) + *
+ * 教师端上传案例直接上传到对应文件夹下, 教师端管理时候
+ */
@ApiOperation("判断数据集是否存在")
@GetMapping("/fileExistState")
@AnonymousAccess
- public ResultEntity fileExistState(@ApiParam("案例名")String caseName) {
+ public ResultEntity fileExistState(@ApiParam("案例名") String caseName) {
//存储文件路径
String path = "/etc/jupyterhub/data/";
+
+
String newCreatFile = path + caseName;
File dataFile = new File(newCreatFile);
@@ -81,10 +89,54 @@ public class JupyterhubController {
//创建文件夹
dataFile.mkdir();
+ //修改权限
+ try {
+ // 创建 chmod 命令以递归方式修改权限
+ String[] command = {"chmod", "-R", "777", newCreatFile};
+ // 创建一个新的进程来执行Python代码
+ Process process = Runtime.getRuntime().exec(command);
+
+
+ // 获取进程的输入流
+ BufferedReader inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));
+
+ // 获取进程的输出流
+ BufferedReader errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream()));
+
+
+ // 读取Python代码的输出
+ String line;
+ StringBuilder output = new StringBuilder();
+ while ((line = inputStream.readLine()) != null) {
+ output.append(line).append("\n");
+ }
+
+ // 读取Python代码的错误信息
+ StringBuilder errors = new StringBuilder();
+ while ((line = errorStream.readLine()) != null) {
+ errors.append(line).append("\n");
+ }
+
+ // 等待进程执行完成
+ int exitCode = process.waitFor();
+
+ if (exitCode == 0) {
+ // 执行成功,输出Python代码的结果
+ System.out.println("权限修改成功!");
+
+ } else {
+ // 执行失败,输出错误信息
+ System.err.println("权限修改失败了!\n" + errors.toString());
+
+ }
+ } catch (IOException | InterruptedException e) {
+ e.printStackTrace();
+ }
+
//判断有无ipynb文件,进行迁移
// 使用File类表示这个文件
- String fileResource = path+caseName+".ipynb";
+ String fileResource = path + caseName + ".ipynb";
File myFile = new File(fileResource);
@@ -126,33 +178,33 @@ public class JupyterhubController {
if (exitCode == 0) {
// 执行成功,输出Python代码的结果
System.out.println("文件迁移成功!");
- return new ResultEntity<>(HttpStatus.OK,"数据不存在!",false);
+ return new ResultEntity<>(HttpStatus.OK, "数据不存在!", false);
} else {
// 执行失败,输出错误信息
- System.err.println("文件迁移失败了!\n"+ errors.toString());
- return new ResultEntity<>(HttpStatus.OK,"数据不存在!",false);
+ System.err.println("文件迁移失败了!\n" + errors.toString());
+ return new ResultEntity<>(HttpStatus.OK, "数据不存在!", false);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
} else {
- return new ResultEntity<>(HttpStatus.OK,"数据不存在!",false);
+ return new ResultEntity<>(HttpStatus.OK, "数据不存在!", false);
}
- return new ResultEntity<>(HttpStatus.OK,"数据不存在!",false);
+ return new ResultEntity<>(HttpStatus.OK, "数据不存在!", false);
- }else {
+ } else {
- //判断子目录是否有data文件
+ //判断子目录是否有data文件
- String sonPath = newCreatFile +"/data";
+ String sonPath = newCreatFile + "/data";
File sonFile = new File(sonPath);
- if (sonFile.exists()){
- return new ResultEntity<>(HttpStatus.OK,"数据存在!",true);
- }else {
- return new ResultEntity<>(HttpStatus.OK,"数据不存在!",false);
+ if (sonFile.exists()) {
+ return new ResultEntity<>(HttpStatus.OK, "数据存在!", true);
+ } else {
+ return new ResultEntity<>(HttpStatus.OK, "数据不存在!", false);
}
}
@@ -162,10 +214,10 @@ public class JupyterhubController {
@ApiOperation("数据集不存在文件上传")
@PostMapping("/uploadFile")
@AnonymousAccess
- public ResultEntity uploadFile(@ApiParam("文件")List