-
Notifications
You must be signed in to change notification settings - Fork 0
/
jupyterhub_config-workspace.py
154 lines (115 loc) · 4.62 KB
/
jupyterhub_config-workspace.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
# Authenticate users against OpenShift OAuth provider.
c.JupyterHub.authenticator_class = "openshift"
from oauthenticator.openshift import OpenShiftOAuthenticator
OpenShiftOAuthenticator.scope = ['user:full']
client_id = '%s-%s-users' % (application_name, namespace)
client_secret = os.environ['OAUTH_CLIENT_SECRET']
c.OpenShiftOAuthenticator.client_id = client_id
c.OpenShiftOAuthenticator.client_secret = client_secret
c.Authenticator.enable_auth_state = True
c.CryptKeeper.keys = [ client_secret.encode('utf-8') ]
c.OpenShiftOAuthenticator.oauth_callback_url = (
'https://%s/hub/oauth_callback' % public_hostname)
# Add any additional JupyterHub configuration settings.
c.KubeSpawner.extra_labels = {
'spawner': 'workspace',
'class': 'session',
'user': '{username}'
}
# Set up list of registered users and any users nominated as admins.
if os.path.exists('/opt/app-root/configs/admin_users.txt'):
with open('/opt/app-root/configs/admin_users.txt') as fp:
content = fp.read().strip()
if content:
c.Authenticator.admin_users = set(content.split())
if os.path.exists('/opt/app-root/configs/user_whitelist.txt'):
with open('/opt/app-root/configs/user_whitelist.txt') as fp:
c.Authenticator.whitelist = set(fp.read().strip().split())
# For workshops we provide each user with a persistent volume so they
# don't loose their work. This is mounted on /opt/app-root, so we need
# to copy the contents from the image into the persistent volume the
# first time using an init container.
volume_size = os.environ.get('JUPYTERHUB_VOLUME_SIZE')
if volume_size:
c.KubeSpawner.pvc_name_template = c.KubeSpawner.pod_name_template
c.KubeSpawner.storage_pvc_ensure = True
c.KubeSpawner.storage_capacity = volume_size
c.KubeSpawner.storage_access_modes = ['ReadWriteOnce']
c.KubeSpawner.volumes.extend([
{
'name': 'data',
'persistentVolumeClaim': {
'claimName': c.KubeSpawner.pvc_name_template
}
}
])
c.KubeSpawner.volume_mounts.extend([
{
'name': 'data',
'mountPath': '/opt/app-root',
'subPath': 'workspace'
}
])
c.KubeSpawner.init_containers.extend([
{
'name': 'setup-volume',
'image': '%s' % c.KubeSpawner.image_spec,
'command': [
'/opt/app-root/bin/setup-volume.sh',
'/opt/app-root',
'/mnt/workspace'
],
"resources": {
"limits": {
"memory": os.environ.get('NOTEBOOK_MEMORY', '128Mi')
},
"requests": {
"memory": os.environ.get('NOTEBOOK_MEMORY', '128Mi')
}
},
'volumeMounts': [
{
'name': 'data',
'mountPath': '/mnt'
}
]
}
])
# Make modifications to pod based on user and type of session.
from tornado import gen
@gen.coroutine
def modify_pod_hook(spawner, pod):
pod.spec.automount_service_account_token = True
# Grab the OpenShift user access token from the login state.
auth_state = yield spawner.user.get_auth_state()
access_token = auth_state['access_token']
# Set the session access token from the OpenShift login.
pod.spec.containers[0].env.append(
dict(name='OPENSHIFT_TOKEN', value=access_token))
# See if a template for the project name has been specified.
# Try expanding the name, substituting the username. If the
# result is different then we use it, not if it is the same
# which would suggest it isn't unique.
project = os.environ.get('OPENSHIFT_PROJECT')
if project:
name = project.format(username=spawner.user.name)
if name != project:
pod.spec.containers[0].env.append(
dict(name='PROJECT_NAMESPACE', value=name))
# Ensure project is created if it doesn't exist.
pod.spec.containers[0].env.append(
dict(name='OPENSHIFT_PROJECT', value=name))
return pod
c.KubeSpawner.modify_pod_hook = modify_pod_hook
# Setup culling of terminal instances if timeout parameter is supplied.
idle_timeout = os.environ.get('JUPYTERHUB_IDLE_TIMEOUT')
if idle_timeout and int(idle_timeout):
cull_idle_servers_cmd = ['/opt/app-root/bin/cull-idle-servers']
cull_idle_servers_cmd.append('--timeout=%s' % idle_timeout)
c.JupyterHub.services.extend([
{
'name': 'cull-idle',
'admin': True,
'command': cull_idle_servers_cmd,
}
])