Files
OBIJupyterHub/obijupyterhub/jupyterhub_config.py

124 lines
4.4 KiB
Python
Raw Permalink Normal View History

2025-10-14 17:40:41 +02:00
import os
2025-10-16 01:07:07 +02:00
import logging
from pathlib import Path
2025-10-14 17:40:41 +02:00
2025-10-15 15:55:43 +02:00
# Base configuration coucou
2025-10-15 07:10:44 +02:00
c.JupyterHub.spawner_class = 'dockerspawner.DockerSpawner'
# Enable debug logs
c.JupyterHub.log_level = 'DEBUG'
c.Spawner.debug = True
2025-11-05 17:29:12 +01:00
VOLUMES_BASE_PATH = '/volumes/users' # Path as seen from JupyterHub container (for user dirs)
HOST_VOLUMES_PATH = os.environ.get('HOST_VOLUMES_PATH', '/volumes') # Real path on host machine (parent dir)
2025-10-16 01:07:07 +02:00
2025-10-15 07:10:44 +02:00
# Docker image to use for student containers
c.DockerSpawner.image = 'jupyterhub-student:latest'
# Docker network (create with: docker network create jupyterhub-network)
c.DockerSpawner.network_name = 'jupyterhub-network'
# Connection to OrbStack Docker socket from the hub container
c.DockerSpawner.client_kwargs = {'base_url': 'unix:///var/run/docker.sock'}
# IMPORTANT: Internal URL for communication between containers
# The hub container communicates with user containers via Docker network
c.JupyterHub.hub_ip = '0.0.0.0'
c.JupyterHub.hub_connect_ip = 'jupyterhub'
2025-10-16 01:07:07 +02:00
c.DockerSpawner.environment = {
# R package library in read-only course directory under work/
'R_LIBS_USER': '/home/jovyan/work/R_packages',
'R_LIBS_SITE': '/home/jovyan/work/course/R_packages:/usr/local/lib/R/site-library:/usr/lib/R/site-library'
}
2025-10-15 07:10:44 +02:00
# Network configuration for student containers
c.DockerSpawner.use_internal_ip = True
c.DockerSpawner.network_name = 'jupyterhub-network'
c.DockerSpawner.extra_host_config = {'network_mode': 'jupyterhub-network'}
# Remove containers after disconnection (optional, set to False to keep containers)
c.DockerSpawner.remove = True
# Container naming
c.DockerSpawner.name_template = "jupyter-{username}"
# Volume mounting to persist student data
2025-10-15 07:15:05 +02:00
# Set root to work/ - everything is persistent
notebook_dir = '/home/jovyan/work'
2025-10-15 07:10:44 +02:00
c.DockerSpawner.notebook_dir = notebook_dir
2025-10-15 07:15:05 +02:00
# Personal volume for each student + shared volumes under work/
2025-10-16 01:07:07 +02:00
# Pre-spawn hook to create user directory
async def create_user_dir(spawner):
"""Create user directory if it doesn't exist"""
user_dir = os.path.join(VOLUMES_BASE_PATH, spawner.user.name)
Path(user_dir).mkdir(parents=True, exist_ok=True)
2025-11-05 17:29:12 +01:00
# Change owner to jovyan user (UID 1000, GID 100 in Jupyter images)
os.chown(user_dir, 1000, 100)
2025-10-16 01:07:07 +02:00
os.chmod(user_dir, 0o755)
2025-11-05 17:29:12 +01:00
spawner.log.info(f"Created user directory with correct permissions: {user_dir}")
2025-10-16 01:07:07 +02:00
c.Spawner.pre_spawn_hook = create_user_dir
2025-10-15 07:10:44 +02:00
c.DockerSpawner.volumes = {
2025-11-05 17:29:12 +01:00
# Personal volume - bind mount from REAL host path
f'{HOST_VOLUMES_PATH}/users/{{username}}': '/home/jovyan/work',
2025-10-15 07:15:05 +02:00
# Shared volume between all students - under work/
2025-11-05 17:29:12 +01:00
f'{HOST_VOLUMES_PATH}/shared': '/home/jovyan/work/shared',
2025-10-15 07:15:05 +02:00
# Shared read-only volume for course files - under work/
2025-11-05 17:29:12 +01:00
f'{HOST_VOLUMES_PATH}/course': {
2025-10-15 07:15:05 +02:00
'bind': '/home/jovyan/work/course',
2025-10-15 07:10:44 +02:00
'mode': 'ro' # read-only
}
}
# Memory and CPU configuration (adjust according to your needs)
2025-11-17 14:22:19 +01:00
c.DockerSpawner.mem_limit = '6G'
2025-10-15 07:10:44 +02:00
c.DockerSpawner.cpu_limit = 1.0
# User configuration - Simple password authentication for lab
from jupyterhub.auth import DummyAuthenticator
class SimplePasswordAuthenticator(DummyAuthenticator):
"""Simple authenticator with a shared password for everyone"""
def check_allowed(self, username, authentication=None):
"""Check if user is allowed"""
if authentication is None:
return False
provided_password = authentication.get('password', '')
2025-10-15 07:15:05 +02:00
# Admin user with special password
if username == 'admin':
admin_password = os.environ.get('JUPYTERHUB_ADMIN_PASSWORD', 'admin2025')
return provided_password == admin_password
# Regular students with shared password
student_password = os.environ.get('JUPYTERHUB_PASSWORD', 'metabar2025')
return provided_password == student_password
2025-10-15 07:10:44 +02:00
c.JupyterHub.authenticator_class = SimplePasswordAuthenticator
# To create a list of allowed users, uncomment and modify:
# c.Authenticator.allowed_users = {'student1', 'student2', 'student3'}
# Or allow any user with the correct password:
c.Authenticator.allow_all = True
# Admin configuration
c.Authenticator.admin_users = {'admin'}
# Listening port
2025-10-15 15:55:43 +02:00
c.JupyterHub.bind_url = 'http://0.0.0.0:8000/jupyter/'
2025-10-15 07:10:44 +02:00
# Timeout
c.Spawner.start_timeout = 300
c.Spawner.http_timeout = 120
2025-10-16 01:07:07 +02:00
# Post-start hook to create R_packages directory after volumes are mounted
c.DockerSpawner.cmd = ['start-notebook.sh']