jupyterhub_config.py 4.9 KB

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