forked from 2i2c-org/infrastructure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub.py
497 lines (432 loc) · 20 KB
/
hub.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
from auth import KeyProvider
import hashlib
import hmac
import json
import os
import sys
import subprocess
import tempfile
from contextlib import contextmanager, redirect_stderr, redirect_stdout
from textwrap import dedent
from pathlib import Path
import pytest
from ruamel.yaml import YAML
from build import build_image
from utils import decrypt_file
# Without `pure=True`, I get an exception about str / byte issues
yaml = YAML(typ='safe', pure=True)
class Cluster:
"""
A single k8s cluster we can deploy to
"""
def __init__(self, spec):
self.spec = spec
self.hubs = [
Hub(self, hub_yaml)
for hub_yaml in self.spec['hubs']
]
self.support = self.spec.get('support', {})
def build_image(self):
self.ensure_docker_credhelpers()
build_image(self.spec['image_repo'])
@contextmanager
def auth(self):
if self.spec['provider'] == 'gcp':
yield from self.auth_gcp()
elif self.spec['provider'] == 'aws':
yield from self.auth_aws()
else:
raise ValueError(f'Provider {self.spec["provider"]} not supported')
def ensure_docker_credhelpers(self):
"""
Setup credHelper for current hub's image registry.
Most image registries (like ECR, GCP Artifact registry, etc) use
a docker credHelper (https://docs.docker.com/engine/reference/commandline/login/#credential-helpers)
to authenticate, rather than a username & password. This requires an
entry per registry in ~/.docker/config.json.
This method ensures the appropriate credential helper is present
"""
image_name = self.spec['image_repo']
registry = image_name.split('/')[0]
helper = None
# pkg.dev is used by Google Cloud Artifact registry
if registry.endswith('pkg.dev'):
helper = 'gcloud'
if helper is not None:
dockercfg_path = os.path.expanduser('~/.docker/config.json')
try:
with open(dockercfg_path) as f:
config = json.load(f)
except FileNotFoundError:
config = {}
helpers = config.get('credHelpers', {})
if helpers.get(registry) != helper:
helpers[registry] = helper
config['credHelpers'] = helpers
with open(dockercfg_path, 'w') as f:
json.dump(config, f, indent=4)
def deploy_support(self):
cert_manager_url = 'https://charts.jetstack.io'
cert_manager_version = 'v1.3.1'
print("Adding cert-manager chart repo...")
subprocess.check_call([
'helm', 'repo', 'add', 'jetstack', cert_manager_url,
])
print("Updating cert-manager chart repo...")
subprocess.check_call([
'helm', 'repo', 'update',
])
print("Provisioning cert-manager...")
subprocess.check_call([
'helm', 'upgrade', '--install', '--create-namespace',
'--namespace', 'cert-manager',
'cert-manager', 'jetstack/cert-manager',
'--version', cert_manager_version,
'--set', 'installCRDs=true'
])
print("Done!")
print("Provisioning support charts...")
subprocess.check_call([
'helm', 'dep', 'up', 'support'
])
support_dir = Path(__file__).parent.parent / 'support'
support_secrets_file = support_dir / 'secrets.yaml'
with tempfile.NamedTemporaryFile(mode='w') as f, decrypt_file(support_secrets_file) as secret_file:
yaml.dump(self.support.get('config', {}), f)
f.flush()
subprocess.check_call([
'helm', 'upgrade', '--install', '--create-namespace',
'--namespace', 'support',
'support', str(support_dir),
'-f', secret_file, '-f', f.name,
'--wait'
])
print("Done!")
def auth_aws(self):
"""
Reads `aws` nested config and temporarily sets environment variables
like `KUBECONFIG`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY`
before trying to authenticate with the `aws eks update-kubeconfig` or
the `kops export kubecfg --admin` commands.
Finally get those environment variables to the original values to prevent
side-effects on existing local configuration.
"""
config = self.spec['aws']
key_path = config['key']
cluster_type = config['clusterType']
cluster_name = config['clusterName']
region = config['region']
if cluster_type == 'kops':
state_store = config['stateStore']
with tempfile.NamedTemporaryFile() as kubeconfig:
orig_kubeconfig = os.environ.get('KUBECONFIG', None)
orig_access_key_id = os.environ.get('AWS_ACCESS_KEY_ID', None)
orig_secret_access_key = os.environ.get('AWS_SECRET_ACCESS_KEY', None)
try:
with decrypt_file(key_path) as decrypted_key_path:
decrypted_key_abspath = os.path.abspath(decrypted_key_path)
if not os.path.isfile(decrypted_key_abspath):
raise FileNotFoundError(
'The decrypted key file does not exist')
with open(decrypted_key_abspath) as f:
creds = json.load(f)
os.environ['AWS_ACCESS_KEY_ID'] = creds["AccessKey"]['AccessKeyId']
os.environ['AWS_SECRET_ACCESS_KEY'] = creds["AccessKey"]['SecretAccessKey']
os.environ['KUBECONFIG'] = kubeconfig.name
if cluster_type == 'kops':
subprocess.check_call([
'kops', 'export', 'kubecfg', '--admin',
f'--name={cluster_name}',
f'--state={state_store}'
])
else:
subprocess.check_call([
'aws', 'eks', 'update-kubeconfig',
f'--name={cluster_name}',
f'--region={region}'
])
yield
finally:
if orig_kubeconfig is not None:
os.environ['KUBECONFIG'] = orig_kubeconfig
if orig_access_key_id is not None:
os.environ['AWS_ACCESS_KEY_ID'] = orig_access_key_id
if orig_kubeconfig is not None:
os.environ['AWS_SECRET_ACCESS_KEY'] = orig_secret_access_key
def auth_gcp(self):
config = self.spec['gcp']
key_path = config['key']
project = config['project']
# If cluster is regional, it'll have a `region` key set.
# Else, it'll just have a `zone` key set. Let's respect either.
location = config.get('zone', config.get('region'))
cluster = config['cluster']
with tempfile.NamedTemporaryFile() as kubeconfig:
orig_kubeconfig = os.environ.get('KUBECONFIG')
try:
os.environ['KUBECONFIG'] = kubeconfig.name
with decrypt_file(key_path) as decrypted_key_path:
subprocess.check_call([
'gcloud', 'auth',
'activate-service-account',
'--key-file', os.path.abspath(decrypted_key_path)
])
subprocess.check_call([
'gcloud', 'container', 'clusters',
# --zone works with regions too
f'--zone={location}',
f'--project={project}',
'get-credentials', cluster
])
yield
finally:
if orig_kubeconfig is not None:
os.environ['KUBECONFIG'] = orig_kubeconfig
class Hub:
"""
A single, deployable JupyterHub
"""
def __init__(self, cluster, spec):
self.cluster = cluster
self.spec = spec
def get_generated_config(self, auth_provider: KeyProvider, secret_key):
"""
Generate config automatically for each hub
WARNING: MIGHT CONTAINS SECRET VALUES!
"""
generated_config = {
'jupyterhub': {
'proxy': {
'https': {
'hosts': [self.spec['domain']]
}
},
'ingress': {
'hosts': [self.spec['domain']],
'tls': [
{
'secretName': 'https-auto-tls',
'hosts': [self.spec['domain']]
}
]
},
'singleuser': {
# If image_repo isn't set, just have an empty image dict
'image': {'name': self.cluster.spec['image_repo']} if 'image_repo' in self.cluster.spec else {},
},
'hub': {
'config': {},
'initContainers': [
{
'name': 'templates-clone',
'image': 'alpine/git',
'args': [
'clone',
'--',
'https://github.com/2i2c-org/pilot-homepage',
'/srv/repo',
],
'securityContext': {
'runAsUser': 1000,
'allowPrivilegeEscalation': False,
'readOnlyRootFilesystem': True,
},
'volumeMounts': [
{
'name': 'custom-templates',
'mountPath': '/srv/repo'
}
]
}
],
'extraContainers': [
{
'name': 'templates-sync',
'image': 'alpine/git',
'workingDir': '/srv/repo',
'command': ['/bin/sh'],
'args': [
'-c',
dedent(
f'''\
while true; do git fetch origin;
if [[ $(git ls-remote --heads origin {self.spec["name"]} | wc -c) -ne 0 ]]; then
git reset --hard origin/{self.spec["name"]};
else
git reset --hard origin/master;
fi
sleep 5m; done
'''
)
],
'securityContext': {
'runAsUser': 1000,
'allowPrivilegeEscalation': False,
'readOnlyRootFilesystem': True,
},
'volumeMounts': [
{
'name': 'custom-templates',
'mountPath': '/srv/repo'
}
]
}
],
'extraVolumes': [
{
'name': 'custom-templates',
'emptyDir': {}
}
],
'extraVolumeMounts':[
{
'mountPath': '/usr/local/share/jupyterhub/custom_templates',
'name': 'custom-templates',
'subPath': 'templates'
},
{
'mountPath': '/usr/local/share/jupyterhub/static/extra-assets',
'name': 'custom-templates',
'subPath': 'extra-assets'
}
]
}
},
}
#
# Allow explicilty ignoring auth0 setup
if self.spec['auth0'].get('enabled', True):
# Auth0 sends users back to this URL after they authenticate
callback_url = f"https://{self.spec['domain']}/hub/oauth_callback"
# Users are redirected to this URL after they log out
logout_url = f"https://{self.spec['domain']}"
client = auth_provider.ensure_client(
name=self.spec['auth0'].get('application_name', f"{self.cluster.spec['name']}-{self.spec['name']}"),
callback_url=callback_url,
logout_url=logout_url,
connection_name=self.spec['auth0']['connection'],
connection_config=self.spec['auth0'].get(self.spec['auth0']['connection'], {}),
)
# FIXME: We're hardcoding Auth0OAuthenticator here
# We should *not*. We need dictionary merging in code, so
# these can all exist fine.
generated_config['jupyterhub']['hub']['config']['JupyterHub']['authenticator_class'] = 'oauthenticator.auth0.Auth0OAuthenticator'
generated_config['jupyterhub']['hub']['config']['Auth0OAuthenticator'] = auth_provider.get_client_creds(client, self.spec['auth0']['connection'])
return self.apply_hub_template_fixes(generated_config, secret_key)
def unset_env_var(self, env_var, old_env_var_value):
"""
If the old environment variable's value exists, replace the current one with the old one
If the old environment variable's value does not exist, delete the current one
"""
if env_var in os.environ:
del os.environ[env_var]
if (old_env_var_value is not None):
os.environ[env_var] = old_env_var_value
def apply_hub_template_fixes(self, generated_config, secret_key):
"""
Modify generated_config based on what hub template we're using.
Different hub templates require different pre-set config. For example,
anything deriving from 'basehub' needs all config to be under a 'basehub'
config. dask hubs require apiTokens, etc.
Ideally, these would be done declaratively. Untile then, let's put all of
them in this function.
"""
hub_template = self.spec['template']
# Generate a token for the hub health service
hub_health_token = hmac.new(secret_key, 'health-'.encode() + self.spec['name'].encode(), hashlib.sha256).hexdigest()
# Describe the hub health service
generated_config.setdefault('jupyterhub', {}).setdefault('hub', {}).setdefault('services', {})['hub-health'] = {
'apiToken': hub_health_token,
'admin': True
}
docs_token = hmac.new(secret_key, f'docs-{self.spec["name"]}'.encode(), hashlib.sha256).hexdigest()
if 'docs_service' in self.spec['config'].keys() and self.spec['config']['docs_service']['enabled']:
generated_config['jupyterhub']['hub']['services']['docs'] = {
'url': f'http://docs-service.{self.spec["name"]}',
'apiToken': docs_token
}
# FIXME: Have a templates config somewhere? Maybe in Chart.yaml
# FIXME: This is a hack. Fix it.
if hub_template != 'basehub':
generated_config = {
'basehub': generated_config
}
# LOLSOB FIXME
if hub_template == 'daskhub':
gateway_token = hmac.new(secret_key, 'gateway-'.encode() + self.spec['name'].encode(), hashlib.sha256).hexdigest()
generated_config['dask-gateway'] = {
'gateway': {
'auth': {
'jupyterhub': { 'apiToken': gateway_token }
}
}
}
generated_config['basehub']['jupyterhub']['hub']['services']['dask-gateway'] = { 'apiToken': gateway_token }
return generated_config
def deploy(self, auth_provider, secret_key, skip_hub_health_test=False):
"""
Deploy this hub
"""
# Ensure helm charts are up to date
os.chdir("hub-templates")
subprocess.check_call(["helm", "dep", "up", "basehub"])
if self.spec["template"] == "daskhub":
subprocess.check_call(["helm", "dep", "up", "daskhub"])
os.chdir("..")
# Check if this cluster has any secret config. If yes, read it in.
secret_config_path = Path(os.getcwd()) / "secrets/config/hubs" / f'{self.cluster.spec["name"]}.cluster.yaml'
if os.path.exists(secret_config_path):
with decrypt_file(secret_config_path) as decrypted_file_path:
with open(decrypted_file_path) as f:
secret_config = yaml.load(f)
hubs = secret_config["hubs"]
secret_hub_config = next((hub for i, hub in enumerate(hubs) if hubs[i]["name"] == self.spec["name"]), None)
secret_hub_config = secret_hub_config["config"]
else:
secret_hub_config = {}
generated_values = self.get_generated_config(auth_provider, secret_key)
with tempfile.NamedTemporaryFile(mode='w') as values_file, tempfile.NamedTemporaryFile(mode='w') as generated_values_file, tempfile.NamedTemporaryFile(mode='w') as secret_values_file:
json.dump(self.spec['config'], values_file)
json.dump(generated_values, generated_values_file)
json.dump(secret_hub_config, secret_values_file)
values_file.flush()
generated_values_file.flush()
secret_values_file.flush()
cmd = [
'helm', 'upgrade', '--install', '--create-namespace', '--wait',
'--namespace', self.spec['name'],
self.spec['name'], os.path.join('hub-templates', self.spec['template']),
# Ordering matters here - config explicitly mentioned in clu should take
# priority over our generated values. Based on how helm does overrides, this means
# we should put the config from config/hubs last.
'-f', generated_values_file.name,
'-f', values_file.name,
'-f', secret_values_file.name,
]
print(f"Running {' '.join(cmd)}")
# Can't test without deploying, since our service token isn't set by default
subprocess.check_call(cmd)
if not skip_hub_health_test:
# FIXMEL: Clean this up
if self.spec['template'] != 'basehub':
service_api_token = generated_values["basehub"]["jupyterhub"]["hub"]["services"]["hub-health"]["apiToken"]
else:
service_api_token = generated_values["jupyterhub"]["hub"]["services"]["hub-health"]["apiToken"]
hub_url = f'https://{self.spec["domain"]}'
# On failure, pytest prints out params to the test that failed.
# This can contain sensitive info - so we hide stderr
# FIXME: Don't use pytest - just call a function instead
print("Running hub health check...")
with open(os.devnull, 'w') as dn, redirect_stderr(dn), redirect_stdout(dn):
exit_code = pytest.main([
"-q",
"deployer/tests",
"--hub-url", hub_url,
"--api-token", service_api_token,
"--hub-type", self.spec['template']
])
if exit_code != 0:
print("Health check failed!", file=sys.stderr)
sys.exit(exit_code)
else:
print("Health check succeeded!")