Skip to content

Commit

Permalink
create/delete jupyter when create/delete user
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjseagull committed Aug 28, 2024
1 parent af2edc6 commit 62f8520
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 5 deletions.
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 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
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

0 comments on commit 62f8520

Please sign in to comment.