forked from cassinyio/SwarmSpawner
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjupyter_config_user_hook.py
83 lines (67 loc) · 2.42 KB
/
jupyter_config_user_hook.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
import os
import shelve
# Configuration file for jupyterhub.
user_start_id = 10000
cur_path = os.path.join("/srv/jupyterhub/")
db_path = os.path.join(cur_path, "user_uid.db")
# Simple method to generate a uid for the user
def simple_user_id(spawner):
user = spawner.user
if hasattr(spawner.user, "uid") and spawner.user.uid is not None:
spawner.log.info("Pre-Spawn, user {} already has id {}".format(user, user.uid))
return False
spawner.log.info("Pre-Spawn, creating id for {}".format(user))
user_id = None
with shelve.open(db_path) as db:
ids = list(db.keys())
if not ids:
# First user
new_id = str(user_start_id)
db[new_id] = user.name
user_id = new_id
else:
# check if user exists
usernames = list(db.values())
if user.name not in usernames:
new_id = str(int(ids[-1]) + 1)
db[new_id] = user.name
user_id = new_id
else:
# fetch existing id
for uid, username in db.items():
if username == user.name:
user_id = uid
break
if not user_id:
spawner.log.error("Pre-Spawn, failed to aquire a uid for {}".format(user))
return False
spawner.user.uid = user_id
c = get_config()
c.JupyterHub.spawner_class = "jhub.SwarmSpawner"
c.JupyterHub.authenticator_class = "jhubauthenticators.DummyAuthenticator"
c.DummyAuthenticator.password = "password"
c.JupyterHub.ip = "0.0.0.0"
c.JupyterHub.hub_ip = "0.0.0.0"
c.JupyterHub.cleanup_servers = False
# First pulls can be really slow, so let's give it a big timeout
c.SwarmSpawner.start_timeout = 60 * 5
c.SwarmSpawner.jupyterhub_service_name = "jupyterhub"
c.SwarmSpawner.networks = ["jupyterhub_default"]
c.SwarmSpawner.use_user_options = True
c.SwarmSpawner.pre_spawn_hook = simple_user_id
c.SwarmSpawner.images = [
{
"name": "Base Notebook",
"image": "ucphhpc/base-notebook",
"env": {
"NB_USER": "{service_owner}",
"NB_UID": "{uid}",
"HOME": "/home/{service_owner}",
"CHOWN_HOME": "yes",
"GRANT_SUDO": "no",
},
"uid_gid": "root",
"command": "/bin/bash -c 'mkdir -p /home/{service_owner}; /usr/local/bin/start-notebook.sh'",
"args": ["--NotebookApp.default_url=/lab"],
},
]