Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create/delete jupyter when create/delete user #23

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ project(":wedpr-components-transport").projectDir=file("wedpr-components/transpo
include ":wedpr-components-jupyter-intergration"
project(":wedpr-components-jupyter-intergration").projectDir=file("wedpr-components/env-integration/jupyter")

include "wedpr-components-hook"
project(":wedpr-components-hook").projectDir=file("wedpr-components/hook")

include "wedpr-components-user"
project(":wedpr-components-user").projectDir=file("wedpr-components/user")

Expand Down
1 change: 1 addition & 0 deletions wedpr-components/env-integration/jupyter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ dependencies{
compile project(":wedpr-components-mybatis")
compile project(":wedpr-components-uuid")
compile project(":wedpr-components-sys-config")
compile project(":wedpr-components-hook")
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
*/

package com.webank.wedpr.components.integration.jupyter.service.impl;
package com.webank.wedpr.components.integration.jupyter.core;

import com.webank.wedpr.core.config.WeDPRConfig;

Expand All @@ -24,6 +24,7 @@ public class JupyterConfig {
private static String JUPYTER_HOST_CONFIGUATINON_KEY =
WeDPRConfig.apply("wedpr.jupyter.host_configuration_key", "jupyter_entrypoints");
private static String JUPYTER_ENTRYPOINT_SPLITTER = ";";
private static String JUPYTER_MODULE = "jupyter-integration";

public static String getJupyterHostConfiguatinonKey() {
return JUPYTER_HOST_CONFIGUATINON_KEY;
Expand All @@ -36,4 +37,8 @@ public static Integer getMaxJupyterPerHost() {
public static String getJupyterEntrypointSplitter() {
return JUPYTER_ENTRYPOINT_SPLITTER;
}

public static String getJupyterModule() {
return JUPYTER_MODULE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
* the License.
*
*/
package com.webank.wedpr.components.integration.jupyter.service.impl;
package com.webank.wedpr.components.integration.jupyter.core;

import com.webank.wedpr.components.integration.jupyter.core.JupyterStatus;
import com.webank.wedpr.components.integration.jupyter.dao.JupyterInfoDO;
import com.webank.wedpr.components.integration.jupyter.dao.JupyterMapper;
import com.webank.wedpr.components.meta.sys.config.dao.SysConfigMapper;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2017-2025 [webank-wedpr]
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.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 governing permissions and limitations under
* the License.
*
*/

package com.webank.wedpr.components.integration.jupyter.hook;

import com.webank.wedpr.components.hook.UserHook;
import com.webank.wedpr.components.integration.jupyter.core.JupyterManager;
import com.webank.wedpr.core.config.WeDPRCommonConfig;
import com.webank.wedpr.core.utils.WeDPRException;
import org.apache.commons.lang3.StringUtils;

public class JupyterUserCallback implements UserHook.UserCallback {
private final JupyterManager jupyterManager;

public JupyterUserCallback(JupyterManager jupyterManager) {
this.jupyterManager = jupyterManager;
}

@Override
public boolean interruptOnException() {
return false;
}

@Override
public void onCreated(String user) throws Exception {
this.jupyterManager.allocateJupyter(user, WeDPRCommonConfig.getAgency());
}

// do nothing
@Override
public void onUpdated(String user) throws Exception {}

@Override
public void onDeleted(String user) throws Exception {
if (StringUtils.isBlank(user)) {
throw new WeDPRException("Delete jupyter failed, must specify the jupyter user");
}
this.jupyterManager.deleteJupyter(user, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@

package com.webank.wedpr.components.integration.jupyter.service.impl;

import com.webank.wedpr.components.hook.UserHook;
import com.webank.wedpr.components.integration.jupyter.core.JupyterConfig;
import com.webank.wedpr.components.integration.jupyter.core.JupyterManager;
import com.webank.wedpr.components.integration.jupyter.dao.JupyterInfoDO;
import com.webank.wedpr.components.integration.jupyter.dao.JupyterMapper;
import com.webank.wedpr.components.integration.jupyter.hook.JupyterUserCallback;
import com.webank.wedpr.components.integration.jupyter.service.JupyterService;
import com.webank.wedpr.components.meta.sys.config.dao.SysConfigMapper;
import com.webank.wedpr.core.config.WeDPRCommonConfig;
Expand All @@ -33,11 +37,14 @@ public class JupyterServiceImpl implements JupyterService {

private @Autowired SysConfigMapper sysConfigMapper;
private @Autowired JupyterMapper jupyterMapper;
private @Autowired UserHook userHook;
private JupyterManager jupyterManager;

@PostConstruct
public void init() {
this.jupyterManager = new JupyterManager(sysConfigMapper, jupyterMapper);
userHook.registerUserCallback(
JupyterConfig.getJupyterModule(), new JupyterUserCallback(this.jupyterManager));
}
/**
* allocate the jupyter environment for given user
Expand All @@ -46,6 +53,7 @@ public void init() {
* @param agency the agency of the person
* @return success or failed
*/
@Override
public String allocate(String user, String agency) throws Exception {
return this.jupyterManager.allocateJupyter(user, agency);
}
Expand All @@ -56,6 +64,7 @@ public String allocate(String user, String agency) throws Exception {
* @param condition
* @return
*/
@Override
public List<JupyterInfoDO> queryJupyters(
boolean admin, String queryUser, JupyterInfoDO condition) throws Exception {
if (!admin) {
Expand All @@ -71,6 +80,7 @@ public List<JupyterInfoDO> queryJupyters(
* @param id specify the jupyter to destory
* @return success/failed
*/
@Override
public boolean destroy(boolean admin, String currentUser, String id) {
// the admin can delete all jupyter
if (admin) {
Expand All @@ -85,6 +95,7 @@ public boolean destroy(boolean admin, String currentUser, String id) {
* @param id the jupyter id
* @return success/failed
*/
@Override
public JupyterInfoDO open(String currentUser, String id) throws Exception {
return this.jupyterManager.openJupyter(currentUser, id);
}
Expand All @@ -95,6 +106,7 @@ public JupyterInfoDO open(String currentUser, String id) throws Exception {
* @param id specify the jupyter to close
* @return success/failed
*/
@Override
public JupyterInfoDO close(String currentUser, String id) throws Exception {
return this.jupyterManager.closeJupyter(currentUser, id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@
</if>
</update>
<delete id="deleteJupyterInfo" parameterType="java.lang.String">
public int deleteJupyterInfo(@Param("id")String id);
delete from `wedpr_jupyter_table` where `id` = #{id}
delete from `wedpr_jupyter_table` where 1 = 1
<if test="id != null and id !=''">
and `id` = #{id}
</if>
<if test="owner != null and owner !=''">
and `owner` = #{owner}
</if>
Expand Down
7 changes: 7 additions & 0 deletions wedpr-components/hook/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Apply the java-library plugin to add support for Java Library
plugins {
id 'java'
}
dependencies{
compile project(":wedpr-core-utils")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2017-2025 [webank-wedpr]
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.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 governing permissions and limitations under
* the License.
*
*/

package com.webank.wedpr.components.hook;

import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class UserHook {
private static final Logger logger = LoggerFactory.getLogger(UserHook.class);

public interface UserCallback {
boolean interruptOnException();

void onCreated(String user) throws Exception;

void onUpdated(String user) throws Exception;

void onDeleted(String user) throws Exception;
}

public enum Action {
CREATE_USER,
UPDATE_USER,
DELETE_USER
}

private Map<String, UserCallback> callbacks;

public synchronized void registerUserCallback(String module, UserCallback callback) {
callbacks.put(module, callback);
}

private synchronized void triggerCallback(Action action, String user) throws Exception {
if (callbacks.isEmpty()) {
return;
}
for (String module : callbacks.keySet()) {
UserCallback callback = callbacks.get(module);
try {
switch (action) {
case CREATE_USER:
{
callback.onCreated(user);
continue;
}
case UPDATE_USER:
{
callback.onUpdated(user);
continue;
}
case DELETE_USER:
{
callback.onDeleted(user);
continue;
}
default:
continue;
}
} catch (Exception e) {
logger.warn("Trigger callback for module {} failed, reason: ", module, e);
if (callback.interruptOnException()) {
throw e;
}
}
}
}

public synchronized void onUserCreated(String user) throws Exception {
triggerCallback(Action.CREATE_USER, user);
}

public synchronized void onUserUpdated(String user) throws Exception {
triggerCallback(Action.UPDATE_USER, user);
}

public synchronized void onUserDeleted(String user) throws Exception {
triggerCallback(Action.DELETE_USER, user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.webank.wedpr.components.meta.setting.template.dao.SettingTemplateDO;
import com.webank.wedpr.core.config.WeDPRCommonConfig;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

@JsonIgnoreProperties(ignoreUnknown = true)
public class TemplateSettingRequest {
Expand All @@ -44,9 +45,13 @@ public void checkUpdate(String owner, Boolean admin) {
}
}

public void setOwnerInfo(String owner) {
public void setOwnerInfo(boolean admin, String owner) {
for (SettingTemplateDO template : templateList) {
template.setOwner(owner);
// non-admin user, set owner to the login user
// not set the owner, set the owner to the login user
if (!admin || StringUtils.isBlank(template.getOwner())) {
template.setOwner(owner);
}
template.setAgency(WeDPRCommonConfig.getAgency());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ public class TemplateSettingServiceImpl implements TemplateSettingService {
public WeDPRResponse batchInsertTemplateSettings(
Boolean admin, String user, TemplateSettingRequest settings) {
settings.checkCreate(admin);
if (!admin) {
settings.setOwnerInfo(user);
}
settings.setOwnerInfo(admin, user);

int result = this.settingTemplateMapper.insertSettings(settings.getTemplateList());
WeDPRResponse response =
new WeDPRResponse(Constant.WEDPR_SUCCESS, Constant.WEDPR_SUCCESS_MSG);
response.setData(response);
response.setData(result);
return response;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
values
<foreach collection="templateList" item = "template" index = "index" separator=",">
(#{template.id}, #{template.name}, #{template.type}, #{template.owner}, #{template.agency},
#{template.setting}, #{template.createTime})
#{template.setting}, NOW())
</foreach>
</insert>
<update id="updateSettings" parameterType="java.util.List">
Expand Down Expand Up @@ -89,7 +89,10 @@
<if test="condition.name != null and condition.name !=''">
and `name` like concat(#{condition.name}, '%')
</if>
<if test="condition.owner != null and condition.owner !=''">
<if test="condition.type != null and condition.type !=''">
and `type` = condition.type
</if>
<if test="condition.owner != null and condition.owner !=''">
and `owner` = #{condition.owner}
</if>
<if test="condition.agency != null and condition.agency !=''">
Expand Down
1 change: 1 addition & 0 deletions wedpr-components/user/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ dependencies {
compile project(":wedpr-core-protocol")
compile project(":wedpr-components-uuid")
compile project(":wedpr-components-mybatis")
compile project(":wedpr-components-hook")
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.webank.wedpr.components.hook.UserHook;
import com.webank.wedpr.components.token.auth.TokenUtils;
import com.webank.wedpr.components.token.auth.model.GroupInfo;
import com.webank.wedpr.components.token.auth.model.UserToken;
Expand Down Expand Up @@ -55,6 +56,7 @@ public class WedprGroupController {
@Autowired private WedprUserRoleService wedprUserRoleService;

@Autowired private ApplicationEventPublisher applicationEventPublisher;
@Autowired private UserHook userHook;

/**
* 创建用户组,检查用户组名是否存在,创建用户组失败时记录警告日志
Expand Down Expand Up @@ -351,6 +353,10 @@ public WeDPRResponse deleteGroupUser(
LambdaQueryWrapper<WedprGroupDetail> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
lambdaQueryWrapper2.eq(WedprGroupDetail::getUsername, username);
wedprGroupDetailService.remove(lambdaQueryWrapper2);

// call userHook
userHook.onUserDeleted(username);

CreateWedprGroupResponse createWedprGroupResponse = new CreateWedprGroupResponse();
createWedprGroupResponse.setGroupId(groupId);
wedprResponse.setData(createWedprGroupResponse);
Expand Down
Loading
Loading