|
|
|
@ -0,0 +1,237 @@
|
|
|
|
|
package com.sztzjy.financial_bigdata.controller.stu;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.IdUtil;
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
|
|
|
|
|
import com.sztzjy.financial_bigdata.config.Constant;
|
|
|
|
|
import org.apache.http.HttpEntity;
|
|
|
|
|
import org.apache.http.HttpResponse;
|
|
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
|
|
|
|
|
public class JupyterHubTokenManager {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void createUser(String username) throws Exception {
|
|
|
|
|
// 创建 HTTP 客户端
|
|
|
|
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
|
|
|
|
// 创建 POST 请求
|
|
|
|
|
HttpPost postRequest = new HttpPost(Constant.JUPYTERHUB_API_URL + "/users/" + username);
|
|
|
|
|
postRequest.setHeader("Authorization", "token " + Constant.ADMIN_TOKEN);
|
|
|
|
|
|
|
|
|
|
// 发送请求
|
|
|
|
|
try (CloseableHttpResponse response = httpClient.execute(postRequest)) {
|
|
|
|
|
int statusCode = response.getStatusLine().getStatusCode();
|
|
|
|
|
String responseBody = EntityUtils.toString(response.getEntity());
|
|
|
|
|
|
|
|
|
|
if (statusCode == 201) {
|
|
|
|
|
System.out.println("User " + username + " 创建成功!");
|
|
|
|
|
|
|
|
|
|
// 准备 chpasswd 命令的输入内容
|
|
|
|
|
String passwordEntry = username + ":123qwe";
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
System.out.println("开始执行密码命令");
|
|
|
|
|
String[] command = {"chpasswd"};
|
|
|
|
|
// 创建一个新的进程来执行 chpasswd 命令
|
|
|
|
|
Process process = Runtime.getRuntime().exec(command);
|
|
|
|
|
|
|
|
|
|
// 将用户名和密码对写入进程的标准输入
|
|
|
|
|
try (OutputStream os = process.getOutputStream()) {
|
|
|
|
|
os.write(passwordEntry.getBytes());
|
|
|
|
|
os.flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取进程的输入流
|
|
|
|
|
BufferedReader inputStream = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
|
|
|
|
// 获取进程的错误流
|
|
|
|
|
BufferedReader errorStream = new BufferedReader(new InputStreamReader(process.getErrorStream()));
|
|
|
|
|
|
|
|
|
|
// 读取命令的输出
|
|
|
|
|
String line;
|
|
|
|
|
StringBuilder output = new StringBuilder();
|
|
|
|
|
while ((line = inputStream.readLine()) != null) {
|
|
|
|
|
output.append(line).append("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 读取命令的错误信息
|
|
|
|
|
StringBuilder errors = new StringBuilder();
|
|
|
|
|
while ((line = errorStream.readLine()) != null) {
|
|
|
|
|
errors.append(line).append("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.out.println("等待进程执行完成");
|
|
|
|
|
// 等待进程执行完成
|
|
|
|
|
int exitCode = process.waitFor();
|
|
|
|
|
|
|
|
|
|
if (exitCode == 0) {
|
|
|
|
|
// 执行成功
|
|
|
|
|
System.out.println("密码创建成功!");
|
|
|
|
|
} else {
|
|
|
|
|
// 执行失败,输出错误信息
|
|
|
|
|
System.err.println("密码创建失败!\n" + errors.toString());
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException | InterruptedException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println("失败的创建用户 " + username + ". Status code: " + statusCode + ", Response: " + responseBody);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取登录 token
|
|
|
|
|
public static String generateToken(String username) throws IOException {
|
|
|
|
|
CloseableHttpClient client = HttpClients.createDefault();
|
|
|
|
|
HttpPost request = new HttpPost(Constant.JUPYTERHUB_API_URL + "/users/" + username + "/tokens");
|
|
|
|
|
request.addHeader("Authorization", "Bearer " + Constant.ADMIN_TOKEN);
|
|
|
|
|
request.addHeader("Content-Type", "application/json");
|
|
|
|
|
|
|
|
|
|
String json = "{\"note\": \"" + username + "\"}";
|
|
|
|
|
StringEntity entity = new StringEntity(json);
|
|
|
|
|
request.setEntity(entity);
|
|
|
|
|
|
|
|
|
|
HttpResponse response = client.execute(request);
|
|
|
|
|
HttpEntity responseEntity = response.getEntity();
|
|
|
|
|
String responseBody = EntityUtils.toString(responseEntity);
|
|
|
|
|
|
|
|
|
|
if (response.getStatusLine().getStatusCode() == 201) {
|
|
|
|
|
|
|
|
|
|
// 解析 JSON 响应体
|
|
|
|
|
JSONObject jsonObject = JSON.parseObject(responseBody);
|
|
|
|
|
// 提取生成的令牌
|
|
|
|
|
String token = jsonObject.getString("token");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println("Token : " + token);
|
|
|
|
|
|
|
|
|
|
String url = "http://jrdsj.sztzjy.com:8000/user/" + username + "/?token=" + token;
|
|
|
|
|
|
|
|
|
|
return url;
|
|
|
|
|
} else {
|
|
|
|
|
System.err.println("Failed to generate token. Status code: " + response.getStatusLine().getStatusCode());
|
|
|
|
|
System.err.println("Response: " + responseBody);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 启动用户的 Jupyter 服务器
|
|
|
|
|
public static void startServer(String username,String caseName) throws IOException {
|
|
|
|
|
|
|
|
|
|
// 启动服务器后将需要的文件复制到刚创建的容器内 模拟实现文件挂载
|
|
|
|
|
CloseableHttpClient client = HttpClients.createDefault();
|
|
|
|
|
HttpPost request = new HttpPost(Constant.JUPYTERHUB_API_URL + "/users/" + username + "/server");
|
|
|
|
|
request.addHeader("Authorization", "Bearer " + Constant.ADMIN_TOKEN);
|
|
|
|
|
request.addHeader("Content-Type", "application/json");
|
|
|
|
|
|
|
|
|
|
HttpResponse response = client.execute(request);
|
|
|
|
|
HttpEntity responseEntity = response.getEntity();
|
|
|
|
|
String responseBody = EntityUtils.toString(responseEntity);
|
|
|
|
|
|
|
|
|
|
if (response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 201) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println("服务成功的启动: " + username);
|
|
|
|
|
|
|
|
|
|
if (caseName == null)
|
|
|
|
|
{
|
|
|
|
|
System.out.println("服务成功的启动: " + username+"不需要挂载文件!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//需要挂载的文件
|
|
|
|
|
String path = "/etc/jupyterhub/data/"+caseName+"/.";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
String dockerName = "jupyter-"+username;
|
|
|
|
|
|
|
|
|
|
String[] command = {"docker", "cp", path ,dockerName+":/home/jovyan/work"};
|
|
|
|
|
// 创建一个新的进程来执行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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
System.err.println("Failed to start server for user " + username + ". Status code: " + response.getStatusLine().getStatusCode());
|
|
|
|
|
System.err.println("Response: " + responseBody);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
|
|
|
|
|
String s = IdUtil.fastSimpleUUID();
|
|
|
|
|
System.out.println(s);
|
|
|
|
|
|
|
|
|
|
// String username = "wang1";
|
|
|
|
|
// createUser(username);
|
|
|
|
|
// try {
|
|
|
|
|
// // 创建用户
|
|
|
|
|
// // createUser(username);
|
|
|
|
|
//// 启动用户
|
|
|
|
|
// startServer(username);
|
|
|
|
|
// // 生成 token
|
|
|
|
|
// String token = generateToken(username);
|
|
|
|
|
// if (token != null) {
|
|
|
|
|
// System.out.println("Access your Jupyter environment at:"+ token);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// } catch (IOException e) {
|
|
|
|
|
// System.err.println("Error managing JupyterHub user: " + e.getMessage());
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
}
|