From c8389c485510ad537deb851fb35ecd33ea7f3f38 Mon Sep 17 00:00:00 2001 From: hotwa Date: Mon, 27 May 2024 13:01:20 +0800 Subject: [PATCH] update bak --- jupyterhub_config.py.bak | 142 +++++++++++++++++++++++++-------------- 1 file changed, 91 insertions(+), 51 deletions(-) diff --git a/jupyterhub_config.py.bak b/jupyterhub_config.py.bak index d621fc1..43f1cfa 100755 --- a/jupyterhub_config.py.bak +++ b/jupyterhub_config.py.bak @@ -1,68 +1,108 @@ +# Copyright (c) Jupyter Development Team. +# Distributed under the terms of the Modified BSD License. + +# Configuration file for JupyterHub import os -from pathlib import Path -from dockerspawner import DockerSpawner -c = get_config() -c.Application.log_level = 'DEBUG' +c = get_config() # noqa: F821 -# 基本的JupyterHub配置 -c.JupyterHub.cookie_secret_file = os.path.expanduser('~/.jupyterhub/jupyterhub_cookie_secret') -db_file = os.path.expanduser('~/.jupyterhub/jupyterhub.sqlite') -c.JupyterHub.db_url = f'sqlite:///{db_file}' -c.ConfigurableHTTPProxy.pid_file = os.path.expanduser('~/.jupyterhub/jupyterhub-proxy.pid') +# We rely on environment variables to configure JupyterHub so that we +# avoid having to rebuild the JupyterHub container every time we change a +# configuration parameter. -# Authenticator 设置 -c.JupyterHub.authenticator_class = 'jupyterhub.auth.PAMAuthenticator' -c.PAMAuthenticator.encoding = 'utf8' -c.Authenticator.admin_users = set() -c.Authenticator.allowed_users = set() -c.LocalAuthenticator.create_system_users = True +# from dockerspawner import DockerSpawner -# Spawner 设置 -c.Spawner.ip = '127.0.0.1' -c.Spawner.cmd = ['jupyter-labhub'] -c.Spawner.default_url = '/lab' -c.LocalProcessSpawner.shell_cmd = ["bash", "-l", "-c"] -c.Spawner.notebook_dir = '~' -c.Spawner.args = ['--allow-root', "--KernelSpecManager.ensure_native_kernel=False", '--NotebookApp.allow_origin_pat=https://.*vscode-cdn\\.net', '--NotebookApp.iopub_data_rate_limit=10000000'] +# class MyDockerSpawner(DockerSpawner): +# def start(self): +# # 启动父类的start方法 +# self.user_options['environment']['JUPYTER_ENABLE_NBEXTENSIONS'] = 'true' +# self.user_options['cmd'] = [ +# 'bash', +# '-c', +# 'pip install nglview jupyter_packaging -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com && jupyter nbextension enable nglview --py --sys-prefix && jupyter labextension install nglview-js-widgets && jupyter labextension install @jupyter-widgets/jupyterlab-manager && start-singleuser.sh' +# ] +# return super().start() + +# Spawn single-user servers as Docker containers +c.Authenticator.allow_all = True +c.JupyterHub.spawner_class = "dockerspawner.DockerSpawner" -# 环境变量保持 -c.Spawner.env_keep = ['PATH', 'PYTHONPATH', 'LD_LIBRARY_PATH', 'ENV1', 'ENV2'] +# Spawn containers from this image +c.DockerSpawner.image = os.environ["DOCKER_NOTEBOOK_IMAGE"] -# JupyterHub 服务配置 -c.JupyterHub.ip = '0.0.0.0' -c.JupyterHub.port = 9000 -c.JupyterHub.shutdown_on_logout = True -c.JupyterHub.statsd_prefix = 'jupyterhub' -c.JupyterHub.page_title = 'JupyterHub Service' +# Connect containers to this Docker network +network_name = os.environ["DOCKER_NETWORK_NAME"] +c.DockerSpawner.use_internal_ip = True +c.DockerSpawner.network_name = network_name -# Dockerspawner 配置(如果需要启用) -c.JupyterHub.spawner_class = DockerSpawner -c.DockerSpawner.allowed_images='*' +# Explicitly set notebook directory because we'll be mounting a volume to it. +# Most `jupyter/docker-stacks` *-notebook images run the Notebook server as +# user `jovyan`, and set the notebook directory to `/home/jovyan/work`. +# We follow the same convention. +notebook_dir = os.environ.get("DOCKER_NOTEBOOK_DIR", "/home/jovyan/work") +c.DockerSpawner.notebook_dir = notebook_dir -# Docker 守护进程的地址 -c.DockerSpawner.docker_host = 'unix:///var/run/docker.sock' +# Mount the real user's Docker volume on the host to the notebook user's +# notebook directory in the container +# c.DockerSpawner.volumes = {"jupyterhub-user-{username}": notebook_dir} +# Mount the real user's Docker volume on the host to the notebook user's +# notebook directory in the container +c.DockerSpawner.volumes = { + "jupyterhub-user-{username}": notebook_dir, + "/mnt/mydrive": "/home/jovyan/work/mydrive", + "/mnt/mydrive/project/docker-jupyterhub/id_rsa": "/home/jovyan/.ssh/id_rsa", +} -# 使用的 Docker 镜像 -c.DockerSpawner.image = 'quay.io/jupyter/scipy-notebook' -# 删除容器当它停止时 +# Remove containers once they are stopped c.DockerSpawner.remove = True -# 设置网络(如果您有特定的 Docker 网络配置) -# c.DockerSpawner.network_name = 'jupyterhub' +# For debugging arguments passed to spawned containers +c.DockerSpawner.debug = True +# c.Application.log_level = 'DEBUG' -# JupyterHub 的连接地址,用于 DockerSpawner 内部通信 -# 如果 JupyterHub 运行在同一 Docker 网络中,可以使用 Docker 容器名称 -# c.JupyterHub.hub_connect_ip = 'jupyterhub' +# User containers will access hub by container name on the Docker network +c.JupyterHub.hub_ip = 'jupyterhub' +c.JupyterHub.hub_port = 8080 -# 其他配置... +# Persist hub data on volume mounted inside container +c.JupyterHub.cookie_secret_file = "/data/jupyterhub_cookie_secret" +c.JupyterHub.db_url = "sqlite:////data/jupyterhub.sqlite" -# 注意:下面这行配置是不必要的,因为您已经使用 Unix 套接字 -# c.DockerSpawner.docker_host = 'tcp://docker-daemon-host:2375' -# 如果使用TLS(根据需要取消注释) -# os.environ['DOCKER_TLS_VERIFY'] = '1' -# os.environ['DOCKER_CERT_PATH'] = '/path/to/certificates' +# Authenticate users with Native Authenticator +c.JupyterHub.authenticator_class = "nativeauthenticator.NativeAuthenticator" + +# Allow anyone to sign-up without approval +c.NativeAuthenticator.open_signup = True + +# Allowed admins +admin = os.environ.get("JUPYTERHUB_ADMIN") +if admin: + c.Authenticator.admin_users = [admin] + +# c.DockerSpawner.extra_create_kwargs.update({ +# "environment": {"JUPYTER_ENABLE_LAB": "yes"} +# }) + +# 启动jupyter时候增加跨域支持, 否则反向代理的时候出现问题 +# --NotebookApp.iopub_data_rate_limit=10000000 给nglview使用 +c.DockerSpawner.extra_create_kwargs.update({ + "environment": {"NOTEBOOK_ARGS": "--NotebookApp.allow_origin='*' --NotebookApp.iopub_data_rate_limit=10000000"} +}) + + +# 要支持正则匹配的域名请求,可以通过设置 allow_origin_pat 参数来实现。这个参数允许你使用正则表达式来匹配允许跨域请求的域名。例如,如果你想允许所有以 .example.com 结尾的域名进行跨域请求,可以在 jupyterhub_config.py 文件中添加如下配置: + +# c.Spawner.environment = { +# 'JUPYTERHUB_CORS': '{"allow_origin_pat": "https?://.*\\.example\\.com"}' +# } + +# GPU 和网络配置 +c.DockerSpawner.extra_host_config = { + 'runtime': 'nvidia' +} +c.DockerSpawner.environment = { + 'NVIDIA_DRIVER_CAPABILITIES': 'compute,utility', + 'NVIDIA_VISIBLE_DEVICES': 'all' +} -# 其他配置(根据需要添加) -# ...