-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjupyterhub_config.py
386 lines (308 loc) · 12 KB
/
jupyterhub_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
# Configuration file for JupyterHub
import os
# pre-spawn settings
NB_UID = 65534
NB_GID = 65534
CUDA = 'cuda' in os.environ['HOSTNODE']
c = get_config()
# read users/teams & images
import os, yaml
with open('/srv/jupyterhub/config.yaml', 'r') as cfgfile:
cfg = yaml.load(cfgfile, Loader=yaml.FullLoader)
team_map = cfg['users']
# Whitlelist users and admins # google: remove @gmail.com
c.Authenticator.allowed_users = list(team_map.keys())
c.Authenticator.admin_users = admin = set()
for u, team in team_map.items():
if 'admin' in team:
admin.add(u)
# Spawn single-user servers as Docker containers
# CustomDockerSpawner
# form to select image
def get_options_form(spawner):
username = spawner.user.name # .split('@')[0]
teams = cfg['users'][username]
images = cfg['images']
# list of image letters for user
img = {k:v for k,v in images.items() if k in teams }
images = [] # unique list
for t,i in img.items():
for k in i:
if k not in images:
images.append(k)
if not CUDA:
images = [i for i in images if i != 'G']
# dict of image label:build
available_images = cfg['available_images']
allowed_images = [v for k,v in available_images.items() if k in images]
images=[]
for i in allowed_images:
images = images | i.items()
allowed_images = dict(images)
allowed_images = dict(sorted(allowed_images.items(), key=lambda x: x[0]))
# prepare form
if len(allowed_images) > 1:
option_t = '<option value="{image}" {selected}>{label}</option>'
options = [
option_t.format(
image=image, label=label, selected='selected' if image == spawner.image else ''
)
for label, image in allowed_images.items()
]
return """
<br><br>
<h3>Select an image</h3><br><br>{havecuda}<br><br><b>User: {username}</b><br><br>
<select class="form-control" name="image" required autofocus>
{options}
</select>
""".format(options=options, username=username, havecuda='All can run CUDA' if CUDA else '')
else:
spawner.image = [v for k,v in allowed_images.items()][0]
c.DockerSpawner.options_form = get_options_form
def set_sudo(spawner):
username = spawner.user.name
teams = cfg['users'][username]
if 'sudo' in teams:
return 'yes'
else:
return 'no'
def set_USER(spawner):
username = spawner.user.name
if username[0:4].isnumeric():
return username.upper()
else:
return username
def set_HOME(spawner):
return '/home/' + spawner.user.name
def set_UID(spawner):
UID = cfg['users'][spawner.user.name][0]['uid']
if UID >= 1 and UID < 65536:
return UID
else:
return 1000
def set_GID(spawner):
GID = cfg['users'][spawner.user.name][1]['gid']
if GID >= 1 and GID < 65536:
return GID
else:
return 100
c.DockerSpawner.environment = {
'NB_USER': set_USER,
'NB_UID': set_UID,
'NB_GID': set_GID,
'NB_UMASK':'002',
'CHOWN_HOME':'yes',
'GRANT_SUDO': set_sudo,
}
home_dir = os.environ.get('HOME_DIR') # data/
# notebook_dir = '/home/' + spawner.user.name
# c.DockerSpawner.notebook_dir = notebook_dir
from dockerspawner import DockerSpawner
class CustomDockerSpawner(DockerSpawner):
# mount volumes by team
def start(self):
username = set_USER(self)
# username = self.user.name
# home dir
self.volumes[f"{home_dir}/{username.split('@')[0]}"] = {
'bind': '/home/' + username ,
'mode': 'rw',
}
# copy system /etc/group file
self.volumes['/etc/group'] = {
'bind': '/tmp/group',
'mode': 'ro',
}
# mount /srv from files in /singleuser/srv/setup
self.volumes[os.environ.get('JHUB_DIR')+'/singleuser/srv/setup'] = {
'bind': '/srv',
'mode': 'ro',
}
# user specific mounts as in config.yaml
teams = cfg['users'][self.user.name] # lowercase
mounts = cfg['mounts']
mounts = {k:v for k,v in mounts.items() if k in teams }
for k,v in mounts.items():
for h,d in v.items():
self.volumes[h] = { 'bind': d[0].replace('USER',username), 'mode': d[1] }
return super().start()
# c.JupyterHub.spawner_class = 'dockerspawner.DockerSpawner'
c.JupyterHub.spawner_class = CustomDockerSpawner
# hub runs as 'root',
c.DockerSpawner.extra_create_kwargs = {
'user': 'root',
'hostname': 'hub',
}
# nvidia
# /dev/shm 64M > 16G
if CUDA:
c.DockerSpawner.extra_host_config = {
'runtime': 'nvidia',
'shm_size': '16gb'
}
# JupyterHub requires a single-user instance of the Notebook server, so we
# default to using the `start-singleuser.sh` script included in the
# jupyter/docker-stacks *-notebook images as the Docker run command when
# spawning containers. Optionally, you can override the Docker run command
# using the DOCKER_SPAWN_CMD environment variable.
spawn_cmd = "start-singleuser.sh"
c.DockerSpawner.extra_create_kwargs.update({ 'command': spawn_cmd })
# 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
# Pass the network name as argument to spawned containers
c.DockerSpawner.extra_host_config.update({ 'network_mode': network_name })
# 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 }
# external proxy
c.JupyterHub.cleanup_servers = False
# tells the hub to not stop servers when the hub restarts (proxy runs separately).
c.ConfigurableHTTPProxy.should_start = False
# tells the hub that the proxy should not be started (because you start it yourself).
c.ConfigurableHTTPProxy.auth_token = os.environ.get('CONFIGPROXY_AUTH_TOKEN')
# token for authenticating communication with the proxy.
c.ConfigurableHTTPProxy.api_url = 'http://jupyterproxy:8001'
# the URL which the hub uses to connect to the proxy’s API.
# Remove containers once they are stopped
c.DockerSpawner.remove_containers = True
# User containers will access hub by container name on the Docker network
c.JupyterHub.base_url = '/jhub/'
c.JupyterHub.hub_ip = 'jupyterhub'
c.JupyterHub.hub_port = 8080
# don't need because we are behind an https reverse proxy
# # TLS config: requires generating certificates
# c.JupyterHub.port = 443
# c.JupyterHub.ssl_key = os.environ['SSL_KEY']
# c.JupyterHub.ssl_cert = os.environ['SSL_CERT']
# Persist hub data on volume mounted inside container
data_dir = '/data'
c.JupyterHub.cookie_secret_file = os.path.join(data_dir,
'jupyterhub_cookie_secret')
c.JupyterHub.db_url = f'sqlite:///{data_dir}/jupyterhub.sqlite'
# c.JupyterHub.db_url = 'postgresql://postgres:{password}@{host}/{db}'.format(
# host=os.environ['POSTGRES_HOST'],
# password=os.environ['POSTGRES_PASSWORD'],
# db=os.environ['POSTGRES_DB'],
# )
# reset database
# c.JupyterHub.reset_db = False
# Authenticate users
'''
# GitHub
c.JupyterHub.authenticator_class = 'oauthenticator.GitHubOAuthenticator'
c.GitHubOAuthenticator.oauth_callback_url = os.environ['OAUTH_CALLBACK_URL']
# Native
# admin users in c.Authenticator.admin_users are automatically authorized when signup
c.JupyterHub.authenticator_class = 'nativeauthenticator.NativeAuthenticator'
'''
##### multioauth
# https://github.com/jupyterhub/oauthenticator/issues/136
from traitlets import List
from jupyterhub.auth import Authenticator
def url_path_join(*parts):
return '/'.join([p.strip().strip('/') for p in parts])
class MultiOAuthenticator(Authenticator):
authenticators = List(help="The subauthenticators to use", config=True)
def __init__(self, *arg, **kwargs):
super().__init__(*arg, **kwargs)
self._authenticators = []
for authenticator_klass, url_scope, configs in self.authenticators:
c = self.trait_values()
c.update(configs)
self._authenticators.append({"instance": authenticator_klass(**c), "url_scope": url_scope})
def get_custom_html(self, base_url):
html = []
for authenticator in self._authenticators:
login_service = authenticator["instance"].login_service
if login_service == 'User/Pass':
url = url_path_join(authenticator["url_scope"], "login")
else:
url = url_path_join(authenticator["url_scope"], "oauth_login")
# html.append(
# f"""
# <div class="service-login">
# <a role="button" class='btn btn-jupyter btn-lg' href='{url}'>
# Sign in with {login_service}
# </a>
# </div>
# """
# )
return "\n".join(html)
def get_handlers(self, app):
routes = []
for _authenticator in self._authenticators:
for path, handler in _authenticator["instance"].get_handlers(app):
class SubHandler(handler):
authenticator = _authenticator["instance"]
routes.append((f'{_authenticator["url_scope"]}{path}', SubHandler))
return routes
c.JupyterHub.authenticator_class = MultiOAuthenticator
from oauthenticator.github import GitHubOAuthenticator
from oauthenticator.google import GoogleOAuthenticator
from nativeauthenticator import NativeAuthenticator
#from oauthenticator.azuread import AzureAdOAuthenticator
c.MultiOAuthenticator.authenticators = [
(GitHubOAuthenticator, '/github', {
'client_id': os.environ['GITHUB_CLIENT_ID'],
'client_secret': os.environ['GITHUB_CLIENT_SECRET'],
'oauth_callback_url': os.environ['GITHUB_CALLBACK_URL']
}),
(GoogleOAuthenticator, '/google', {
'client_id': os.environ['GOOGLE_CLIENT_ID'],
'client_secret': os.environ['GOOGLE_CLIENT_SECRET'],
'oauth_callback_url': os.environ['GOOGLE_CALLBACK_URL'],
'login_service': 'Google'
}),
(NativeAuthenticator, '/', {
'login_service': 'User/Pass'
}),
]
import nativeauthenticator
c.JupyterHub.template_paths = [f"{os.path.dirname(nativeauthenticator.__file__)}/templates/"]
# template modified to allow github/google oauth
# ["/usr/local/lib/python3.8/dist-packages/nativeauthenticator/templates/"]
# google
# https://oauthenticator.readthedocs.io/en/latest/api/gen/oauthenticator.google.html
c.GoogleOAuthenticator.hosted_domain = ['gmail.com']
c.GoogleOAuthenticator.login_service = 'Google'
c.GoogleOAuthenticator.delete_invalid_users = True
c.NativeAuthenticator.check_common_password = True
c.NativeAuthenticator.minimum_password_length = 8
c.NativeAuthenticator.allowed_failed_logins = 3
c.NativeAuthenticator.enable_signup = True
# recaptcha config
# https://www.google.com/recaptcha/admin/site/500725121/settings
c.NativeAuthenticator.recaptcha_key = os.environ['RECAPCHA_KEY']
c.NativeAuthenticator.recaptcha_secret = os.environ['RECAPCHA_SECRET']
c.NativeAuthenticator.tos = 'Acepto las <a href="https://remote.genrisk.org/CDU.html" target="_blank">condiciones de uso</a>'
## enable authentication state0
c.MultiOAuthenticator.enable_auth_state = True
import warnings
if 'JUPYTERHUB_CRYPT_KEY' not in os.environ:
warnings.warn(
"Need JUPYTERHUB_CRYPT_KEY env for persistent auth_state.\n"
" export JUPYTERHUB_CRYPT_KEY=$(openssl rand -hex 32)"
)
c.CryptKeeper.keys = [ os.urandom(32) ]
pass
'''
# remove idle notebooks after inactive time
# https://github.com/jupyterhub/jupyterhub-idle-culler
import sys
c.JupyterHub.services = [
{
'name': 'idle-culler',
'admin': True,
'command': [sys.executable, '-m', 'jupyterhub_idle_culler', '--timeout=3600'],
}
]
'''
# max simultaneous users
c.JupyterHub.concurrent_spawn_limit = 10
# user limits
# c.Spawner.cpu_limit = 2 # cores
# c.Spawner.mem_limit = 8G