Files
jupyter-collection/.hub/jupyter_config_backup.py

101 lines
3.6 KiB
Python
Raw Normal View History

2025-10-22 19:40:22 +08:00
"""
按组挂载 Git 资源只读 + 自动建用户加组
资源路径 /home/zhangyi/jupyter-collection
控制文件 /home/zhangyi/jupyter-collection/.hub/resource_map.ini
"""
2025-10-22 20:53:55 +08:00
2025-10-22 11:20:52 +08:00
import os, grp, pwd, shutil, subprocess, configparser
2025-10-22 11:07:27 +08:00
from pathlib import Path
from jupyterhub.spawner import Spawner
2025-10-22 20:53:55 +08:00
# ---------- 路径常量 ----------
SHARED_ROOT = Path("/home/zhangyi/jupyter-collection") # Git 资源仓库
USER_HOME = Path("/home/jupyter-{username}") # 用户家目录模板
CTRL_FILE = SHARED_ROOT / ".hub" / "resource_map.ini" # 组-资源-用户映射
2025-10-22 11:07:27 +08:00
2025-10-22 19:40:22 +08:00
# ---------- 工具:读控制表 ----------
2025-10-22 11:07:27 +08:00
def load_ctrl():
2025-10-22 20:53:55 +08:00
"""返回 (组->资源, 组->用户) 两个 dict"""
2025-10-22 11:07:27 +08:00
cfg = configparser.ConfigParser()
cfg.read(CTRL_FILE)
2025-10-22 20:53:55 +08:00
res_map = {g: [r.strip() for r in v.split(",")] for g, v in cfg.items("map")}
usr_map = {g: [u.strip() for u in v.split(",")] for g, v in cfg.items("users")}
2025-10-22 11:07:27 +08:00
return res_map, usr_map
2025-10-22 20:53:55 +08:00
# ========== 最早钩子:预创建家目录 ==========
def ensure_homedir(spawner):
"""systemd 启动前保证目录存在且属主正确"""
username = spawner.user.name
user_dir = Path(str(USER_HOME).format(username=username))
if not user_dir.exists():
user_dir.mkdir(mode=0o700, parents=True)
shutil.chown(user_dir, user=username, group=username)
c.Spawner.pre_spawn_hook = ensure_homedir
# ========== 认证后钩子:自动建用户+加组 ==========
2025-10-22 11:07:27 +08:00
def ensure_user_and_groups(authenticator, handler, authentication):
username = authentication['name']
res_map, usr_map = load_ctrl()
wanted_groups = [g for g, users in usr_map.items() if username in users]
2025-10-22 19:40:22 +08:00
if not wanted_groups:
2025-10-22 20:53:55 +08:00
return authentication
2025-10-22 11:07:27 +08:00
2025-10-22 20:53:55 +08:00
for g in wanted_groups: # 确保组存在
2025-10-22 11:07:27 +08:00
subprocess.run(["groupadd", "-f", g], check=False)
2025-10-22 19:40:22 +08:00
if subprocess.run(["id", "-u", username], capture_output=True).returncode != 0:
subprocess.run([
"useradd", "-m", "-s", "/bin/bash",
"-d", str(USER_HOME).format(username=username), username
], check=False)
2025-10-22 11:07:27 +08:00
2025-10-22 20:53:55 +08:00
for g in wanted_groups: # 加组
2025-10-22 11:07:27 +08:00
subprocess.run(["usermod", "-aG", g, username], check=False)
return authentication
c.Authenticator.post_auth_hook = ensure_user_and_groups
2025-10-22 20:53:55 +08:00
# ========== spawn 钩子:清 shared + 只读挂载 ==========
2025-10-22 21:02:48 +08:00
2025-10-22 20:53:55 +08:00
def prepare_user(spawner):
2025-10-22 11:07:27 +08:00
username = spawner.user.name
user_dir = Path(str(USER_HOME).format(username=username))
2025-10-22 21:02:48 +08:00
# ① 确保家目录 700 + 属主正确(首次/后续都适用)
user_dir.mkdir(mode=0o700, parents=True, exist_ok=True)
shutil.chown(user_dir, user=username, group=username)
# ② 只清 shared保留用户其它文件
2025-10-22 19:40:22 +08:00
shared = user_dir / "shared"
if shared.exists():
shutil.rmtree(shared)
shared.mkdir(parents=True, exist_ok=True)
shutil.chown(shared, user=username, group=username)
2025-10-22 11:07:27 +08:00
2025-10-22 20:53:55 +08:00
# 读取控制表
2025-10-22 11:07:27 +08:00
res_map, _ = load_ctrl()
2025-10-22 20:53:55 +08:00
# 获取用户系统组
2025-10-22 11:07:27 +08:00
try:
2025-10-22 11:20:52 +08:00
pwnam = pwd.getpwnam(username)
gid_list = os.getgrouplist(username, pwnam.pw_gid)
2025-10-22 11:07:27 +08:00
except (KeyError, OSError):
return
user_groups = {grp.getgrgid(g).gr_name for g in gid_list}
2025-10-22 20:53:55 +08:00
# 组装 bind_mountsSystemdSpawner 语法)
2025-10-22 19:40:22 +08:00
bind_list = []
2025-10-22 11:07:27 +08:00
for grp_name in user_groups:
for res in res_map.get(grp_name, []):
res_path = SHARED_ROOT / res
if res_path.is_dir():
2025-10-22 11:24:22 +08:00
bind_list.append({
'source': str(res_path),
'target': str(shared / res),
'read-only': True,
})
2025-10-22 19:40:22 +08:00
spawner.bind_mounts = bind_list
2025-10-22 11:07:27 +08:00
c.Spawner.pre_spawn_hook = prepare_user