|
| 1 | +# Copyright (c) Jupyter Development Team. |
| 2 | +# Distributed under the terms of the Modified BSD License. |
| 3 | + |
| 4 | +# Configuration file for JupyterHub |
| 5 | +import os |
| 6 | + |
| 7 | +c = get_config() |
| 8 | + |
| 9 | +def create_dir_hook(spawner): |
| 10 | + username = spawner.user.name # get the username |
| 11 | + volume_path = os.path.join('/user-data', username) |
| 12 | + if not os.path.exists(volume_path): |
| 13 | + # create a directory with umask 0755 |
| 14 | + # hub and container user must have the same UID to be writeable |
| 15 | + # still readable by other users on the system |
| 16 | + os.mkdir(volume_path, 0o755) |
| 17 | + os.chown(volume_path, 1000,100) |
| 18 | + # now do whatever you think your user needs |
| 19 | + # ... |
| 20 | + pass |
| 21 | + |
| 22 | +# attach the hook function to the spawner |
| 23 | +c.Spawner.pre_spawn_hook = create_dir_hook |
| 24 | + |
| 25 | +# We rely on environment variables to configure JupyterHub so that we |
| 26 | +# avoid having to rebuild the JupyterHub container every time we change a |
| 27 | +# configuration parameter. |
| 28 | + |
| 29 | +# Spawn single-user servers as Docker containers |
| 30 | +c.JupyterHub.spawner_class = 'dockerspawner.DockerSpawner' |
| 31 | + |
| 32 | +# Spawn containers from this image |
| 33 | +c.DockerSpawner.image = os.environ['JUPYTERHUB_LOCAL_NOTEBOOK_IMAGE'] |
| 34 | + |
| 35 | +# JupyterHub requires a single-user instance of the Notebook server, so we |
| 36 | +# default to using the `start-singleuser.sh` script included in the |
| 37 | +# jupyter/docker-stacks *-notebook images as the Docker run command when |
| 38 | +# spawning containers. Optionally, you can override the Docker run command |
| 39 | +# using the DOCKER_SPAWN_CMD environment variable. |
| 40 | +spawn_cmd = os.environ.get('JUPYTERHUB_DOCKER_SPAWN_CMD', "start-singleuser.sh") |
| 41 | +c.DockerSpawner.extra_create_kwargs.update({ 'command': spawn_cmd }) |
| 42 | + |
| 43 | +# Connect containers to this Docker network |
| 44 | +network_name = os.environ.get('JUPYTERHUB_NETWORK_NAME','laradock_backend') |
| 45 | +c.DockerSpawner.use_internal_ip = True |
| 46 | +c.DockerSpawner.network_name = network_name |
| 47 | + |
| 48 | +# Pass the network name as argument to spawned containers |
| 49 | +c.DockerSpawner.extra_host_config = { 'network_mode': network_name, 'runtime': 'nvidia' } |
| 50 | +# c.DockerSpawner.extra_host_config = { 'network_mode': network_name, "devices":["/dev/nvidiactl","/dev/nvidia-uvm","/dev/nvidia0"] } |
| 51 | +# Explicitly set notebook directory because we'll be mounting a host volume to |
| 52 | +# it. Most jupyter/docker-stacks *-notebook images run the Notebook server as |
| 53 | +# user `jovyan`, and set the notebook directory to `/home/jovyan/work`. |
| 54 | +# We follow the same convention. |
| 55 | +# notebook_dir = os.environ.get('JUPYTERHUB_DOCKER_NOTEBOOK_DIR') or '/home/jovyan/work' |
| 56 | +notebook_dir = '/notebooks' |
| 57 | +c.DockerSpawner.notebook_dir = notebook_dir |
| 58 | + |
| 59 | +# Mount the real user's Docker volume on the host to the notebook user's |
| 60 | +# notebook directory in the container |
| 61 | +user_data = os.environ.get('JUPYTERHUB_USER_DATA','/jupyterhub') |
| 62 | +c.DockerSpawner.volumes = { |
| 63 | + user_data+'/{username}': notebook_dir |
| 64 | +} |
| 65 | + |
| 66 | +c.DockerSpawner.extra_create_kwargs.update({ 'user': 'root'}) |
| 67 | + |
| 68 | +# volume_driver is no longer a keyword argument to create_container() |
| 69 | +# c.DockerSpawner.extra_create_kwargs.update({ 'volume_driver': 'local' }) |
| 70 | +# Remove containers once they are stopped |
| 71 | +c.DockerSpawner.remove_containers = True |
| 72 | + |
| 73 | +# For debugging arguments passed to spawned containers |
| 74 | +c.DockerSpawner.debug = True |
| 75 | + |
| 76 | +# User containers will access hub by container name on the Docker network |
| 77 | +c.JupyterHub.hub_ip = 'jupyterhub' |
| 78 | +c.JupyterHub.hub_port = 8000 |
| 79 | + |
| 80 | +# TLS config |
| 81 | +c.JupyterHub.port = 80 |
| 82 | +# c.JupyterHub.ssl_key = os.environ['SSL_KEY'] |
| 83 | +# c.JupyterHub.ssl_cert = os.environ['SSL_CERT'] |
| 84 | + |
| 85 | +# Authenticate users with GitHub OAuth |
| 86 | +c.JupyterHub.authenticator_class = 'oauthenticator.GitHubOAuthenticator' |
| 87 | +c.GitHubOAuthenticator.oauth_callback_url = os.environ['JUPYTERHUB_OAUTH_CALLBACK_URL'] |
| 88 | +c.GitHubOAuthenticator.client_id = os.environ['JUPYTERHUB_OAUTH_CLIENT_ID'] |
| 89 | +c.GitHubOAuthenticator.client_secret = os.environ['JUPYTERHUB_OAUTH_CLIENT_SECRET'] |
| 90 | + |
| 91 | +# Persist hub data on volume mounted inside container |
| 92 | +data_dir = '/data' |
| 93 | + |
| 94 | +c.JupyterHub.cookie_secret_file = os.path.join(data_dir, |
| 95 | + 'jupyterhub_cookie_secret') |
| 96 | + |
| 97 | +print(os.environ) |
| 98 | + |
| 99 | +c.JupyterHub.db_url = 'postgresql://{user}:{password}@{host}/{db}'.format( |
| 100 | + user=os.environ['JUPYTERHUB_POSTGRES_USER'], |
| 101 | + host=os.environ['JUPYTERHUB_POSTGRES_HOST'], |
| 102 | + password=os.environ['JUPYTERHUB_POSTGRES_PASSWORD'], |
| 103 | + db=os.environ['JUPYTERHUB_POSTGRES_DB'], |
| 104 | +) |
| 105 | + |
| 106 | +# Whitlelist users and admins |
| 107 | +c.Authenticator.whitelist = whitelist = set() |
| 108 | +c.Authenticator.admin_users = admin = set() |
| 109 | +c.JupyterHub.admin_access = True |
| 110 | +pwd = os.path.dirname(__file__) |
| 111 | +with open(os.path.join(pwd, 'userlist')) as f: |
| 112 | + for line in f: |
| 113 | + if not line: |
| 114 | + continue |
| 115 | + parts = line.split() |
| 116 | + name = parts[0] |
| 117 | + print(name) |
| 118 | + whitelist.add(name) |
| 119 | + if len(parts) > 1 and parts[1] == 'admin': |
| 120 | + admin.add(name) |
| 121 | +admin.add('laradock') |
0 commit comments