jupyterhub_config.py 4.7 KB

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