Skip to content

Commit

Permalink
change nginx conf with velocity
Browse files Browse the repository at this point in the history
  • Loading branch information
GCS-ZHN committed May 16, 2021
1 parent bb2e2d3 commit 94c4fc5
Show file tree
Hide file tree
Showing 16 changed files with 224 additions and 79 deletions.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.gcszhn</groupId>
<artifactId>IDEonline</artifactId>
<version>1.1</version>
<version>1.2</version>
<packaging>jar</packaging>

<name>IDEonline</name>
Expand Down Expand Up @@ -227,6 +227,7 @@
<exclude>**/*.md</exclude>
<exclude>**/*.xml</exclude>
<exclude>**/*.txt</exclude>
<exclude>**/*.vm</exclude>
</excludes>
<!--严格检查-->
<strictCheck>true</strictCheck>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/gcszhn/server/Register.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public class Register {
@Autowired
UserService ua;
@GetMapping("/register")
public User register(@RequestParam String ac, @RequestParam String pwd) {
User user = ua.createUser(ac, pwd);
public User register(@RequestParam String ac, @RequestParam String pwd, @RequestParam String address) {
User user = ua.createUser(ac, pwd, address);
ua.registerAccount(user);
return user;
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/gcszhn/server/Validation.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ public Result doValidate() {
try {
if ((keypair instanceof String[])) {
String[] keys = (String[]) keypair;
User user = ua.createUser(account,
RSAEncrypt.decryptToString(passwd, keys[0]));
User user = ua.createUser(
account,
RSAEncrypt.decryptToString(passwd, keys[0]),
null);

status = userDaoService.verifyUser(user);
//密码必须正确才能更新节点属性
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/gcszhn/system/config/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/org/gcszhn/system/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
package org.gcszhn.system.service;

import org.gcszhn.system.service.obj.User;
import org.gcszhn.system.service.obj.UserMail;
import org.gcszhn.system.service.obj.UserNode;

/**
* 用户处理类,处理与User类相关的操作
* @author Zhang.H.N
* @version 1.0
* @version 1.1
*/
public interface UserService {
/**
Expand All @@ -48,11 +49,15 @@ public interface UserService {
* @param user 待注销用户
*/
public void cancelAccount(User user);
/**
* 向特定用户发送特定邮件
* @param user 用户名
* @param userMail 用户邮件配置
*/
public void sendMail(User user, UserMail userMail);
/**
* 向全体带有邮箱的注册用户发送邮件
* @param subject 主题
* @param tempfile 邮件模板
* @param contentType MINE格式类型
* @param userMail 用户邮件配置
*/
public void sendMailToAll(String subject, String tempfile, String contentType);
public void sendMailToAll(UserMail userMail);
}
15 changes: 15 additions & 0 deletions src/main/java/org/gcszhn/system/service/VelocityService.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright © 2021 <a href="mailto:[email protected]">Zhang.H.N</a>.
*
* Licensed under the Apache License, Version 2.0 (thie "License");
* You may not use this file except in compliance with the license.
* You may obtain a copy of the License at
*
* http://wwww.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language govering permissions and
* limitations under the License.
*/
package org.gcszhn.system.service;

import java.util.Map;
Expand Down
85 changes: 53 additions & 32 deletions src/main/java/org/gcszhn/system/service/impl/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.gcszhn.system.service.impl;

import java.io.IOException;
import java.io.InputStream;

import org.apache.logging.log4j.Level;
import org.apache.velocity.VelocityContext;
Expand All @@ -28,6 +27,7 @@
import org.gcszhn.system.service.UserService;
import org.gcszhn.system.service.VelocityService;
import org.gcszhn.system.service.obj.User;
import org.gcszhn.system.service.obj.UserMail;
import org.gcszhn.system.service.obj.UserNode;
import org.gcszhn.system.service.until.AppLog;
import org.gcszhn.system.service.until.ProcessInteraction;
Expand All @@ -38,6 +38,11 @@

import lombok.Getter;

/**
* 用户服务接口扩展类
* @author Zhang.H.N
* @version 1.1
*/
@Service
public class UserServiceImpl implements UserService {
/**Nginx配置模板 */
Expand Down Expand Up @@ -96,14 +101,14 @@ public void registerAccount(User user) {
try {
for (UserNode nc : user.getNodeConfigs()) {
StringBuilder cmd = new StringBuilder("docker -H 172.16.10.")
.append(nc.host)
.append(nc.getHost())
.append(" run -d --privileged=")
.append(nc.withPrivilege)
.append(nc.isWithPrivilege())
.append(" --restart=always")
.append(" --memory=24g")
.append(" --memory-swap=24g")
;
for (int[] portPair : nc.portMap) {
for (int[] portPair : nc.getPortMap()) {
cmd.append(" -p ").append(portPair[0]).append(":").append(portPair[1]);
}
cmd.append(" -v /public/home/")
Expand All @@ -116,12 +121,12 @@ public void registerAccount(User user) {
.append(tagPrefix)
.append(user.getAccount());

if (nc.enableGPU) {
if (nc.isEnableGPU()) {
cmd.append(" --gpus all");
}
cmd.append(" ").append(nc.image).append(" ").append(user.getAccount());
cmd.append(" ").append(nc.getImage()).append(" ").append(user.getAccount());
ProcessInteraction.localExec((Process p) -> {
AppLog.printMessage("Register successfully at node " + nc.host);
AppLog.printMessage("Register successfully at node " + nc.getHost());
}, cmd.toString().split(" "));
}
userDao.addUser(user);
Expand All @@ -136,9 +141,9 @@ public void cancelAccount(User user) {
return;
try {
for (UserNode nc : user.getNodeConfigs()) {
String cmd = "docker -H 172.16.10." + nc.host + " rm -fv " + tagPrefix + user.getAccount();
String cmd = "docker -H 172.16.10." + nc.getHost() + " rm -fv " + tagPrefix + user.getAccount();
ProcessInteraction.localExec((Process p) -> {
AppLog.printMessage("Degister successfully at node " + nc.host);
AppLog.printMessage("Degister successfully at node " + nc.getHost());
}, cmd.split(" "));
}
userDao.removeUser(user);
Expand All @@ -153,20 +158,28 @@ public void cancelAccount(User user) {
*/
private void writeIntoNginx(User user) {
try {
InputStream fis = UserService.class.getResourceAsStream(nginxTemp);
String temp = new String(fis.readAllBytes(), JSONConfig.DEFAULT_CHARSET);
fis.close();
temp = temp.replaceAll("\\$\\{USER\\}", user.getAccount());
String vscodeConf = "";
String jupyterConf = "";
for (UserNode nc : user.getNodeConfigs()) {
jupyterConf += temp.replaceAll("\\$\\{NODE\\}", nc.host + "").replaceAll("\\$\\{PORT\\}",
nc.portMap[0][0] + "") + "\n";
vscodeConf += temp.replaceAll("\\$\\{NODE\\}", nc.host + "").replaceAll("\\$\\{PORT\\}",
nc.portMap[1][0] + "") + "\n";
}
String cmd = String.format("echo '%s' > %s/jupyter/%s.conf", jupyterConf, nginxConfDir, user.getAccount());
cmd += String.format(" && echo '%s' > %s/vscode/%s.conf", vscodeConf, nginxConfDir, user.getAccount());
VelocityContext context = new VelocityContext();
context.put("user", user);

//生成jupyter nginx配置
context.put("jupyter", true);
String jupyterConf = velocityService.getResult(nginxTemp, context);

//生成vscode nginx配置
context.put("jupyter", false);
String vscodeConf = velocityService.getResult(nginxTemp, context);

//执行远程配置命令
String cmd = String.format(
"echo '%s' > %s/jupyter/%s.conf",
jupyterConf,
nginxConfDir,
user.getAccount());
cmd += String.format(
" && echo '%s' > %s/vscode/%s.conf",
vscodeConf,
nginxConfDir,
user.getAccount());
cmd += " && nginx -s reload";
ProcessInteraction.remoteExec(nginxHost, "idrb@sugon", true, (Process p) -> {
AppLog.printMessage("Register to nginx successfully!");
Expand Down Expand Up @@ -194,20 +207,28 @@ private void removeFromNginx(User user) {
e.printStackTrace();
}
}
@Async //异步发送,防止服务阻塞
@Override
public void sendMail(User user, UserMail userMail) {
try {
mailService.sendMail(
user.getAddress(), //邮件地址
userMail.getSubject(), //邮件主题
velocityService.getResult( //模板过滤
userMail.getVmfile(), //模板文件
userMail.getInitContext().apply(user)), //模板变量自定义处理
userMail.getContentType()); //邮件MINE类型
} catch (Exception e) {
AppLog.printMessage(e.getMessage(), Level.ERROR);
}
}
@Async
@Override
public void sendMailToAll(String subject, String vmfile, String contentType) {
public void sendMailToAll(UserMail userMail) {
try {
userDao.fetchUserList().forEach((User user)->{
if (user.getAddress()!=null) {
VelocityContext context = new VelocityContext();
context.put("user", user);
mailService.sendMail(
user.getAddress(),
subject,
velocityService.getResult(vmfile, context),
contentType
);
sendMail(user, userMail);
}
});
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright © 2021 <a href="mailto:[email protected]">Zhang.H.N</a>.
*
* Licensed under the Apache License, Version 2.0 (thie "License");
* You may not use this file except in compliance with the license.
* You may obtain a copy of the License at
*
* http://wwww.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language govering permissions and
* limitations under the License.
*/
package org.gcszhn.system.service.impl;

import java.io.StringWriter;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/gcszhn/system/service/obj/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void setNodeConfigs(UserNode... nodeConfigs) {
public boolean isContainNode(int node) {
if (nodeConfigs == null) return false;
for (UserNode nc : nodeConfigs) {
if (node == nc.host)
if (node == nc.getHost())
return true;
}
return false;
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/org/gcszhn/system/service/obj/UserMail.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright © 2021 <a href="mailto:[email protected]">Zhang.H.N</a>.
*
* Licensed under the Apache License, Version 2.0 (thie "License");
* You may not use this file except in compliance with the license.
* You may obtain a copy of the License at
*
* http://wwww.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language govering permissions and
* limitations under the License.
*/
package org.gcszhn.system.service.obj;

import java.util.function.Function;

import org.apache.velocity.VelocityContext;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
* 用户邮件的统一模板处理类
* @author Zhang.H.N
* @version 1.0
*/
@AllArgsConstructor
public class UserMail {
/**邮件主题 */
private @Getter String subject;
/**邮件模板文件路径 */
private @Getter String vmfile;
/**邮件MINE类型 */
private @Getter String contentType;
/**
* 用户邮件模板变量处理的方法。
* 由于不同的velocity模板会用到不同的变量值,例如用户信息的不同
* 因此需要使用该函数接口来自定义获取对应的velocityContext对象
* 接收参数User实例用于变化用户。
* */
private @Getter Function<User, VelocityContext> initContext;
}
14 changes: 8 additions & 6 deletions src/main/java/org/gcszhn/system/service/obj/UserNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,26 @@
import org.gcszhn.system.config.JSONConfig;
import org.gcszhn.system.service.until.SpringTools;

import lombok.Getter;

/**
* 注册节点类
* @author Zhang.H.N
* @version 1.0
* @version 1.1
*/
public class UserNode implements Serializable {
/**序列化ID */
public static final long serialVersionUID = 202104261748L;
/**使用节点 */
public int host;
private @Getter int host;
/**是否启用GPU,仅对含GPU节点有效 */
public boolean enableGPU = false;
private @Getter boolean enableGPU = false;
/**使用镜像 */
public String image;
private @Getter String image;
/**是否赋予最大权限 */
public boolean withPrivilege = false;
private @Getter boolean withPrivilege = false;
/**端口映射 */
public int[][] portMap;
private @Getter int[][] portMap;
/**
* 节点配置构造方法
* @param hostIndex 使用节点的节点列表索引
Expand Down
15 changes: 0 additions & 15 deletions src/main/resources/config/mail.temp

This file was deleted.

4 changes: 0 additions & 4 deletions src/main/resources/config/nginx.temp

This file was deleted.

Loading

0 comments on commit 94c4fc5

Please sign in to comment.