This repository has been archived by the owner on Jul 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
deploy.py
670 lines (595 loc) · 17.8 KB
/
deploy.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# vim:set sw=4 ts=4 et:
#
# Copyright (c) 2016-2017 Torchbox Ltd.
#
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely. This software is provided 'as-is', without any express or implied
# warranty.
import subprocess, json, humanfriendly
from base64 import b64encode
from sys import stdin, stdout, stderr
from os import environ
from passlib.hash import md5_crypt
import kubectl
from util import strip_hostname
# make_service: create a Service resource for the given arguments.
def make_service(args):
service = {
'apiVersion': 'v1',
'kind': 'Service',
'metadata': {
'name': args.name,
'namespace': args.namespace,
},
'spec': {
'ports': [
{
'name': 'http',
'port': 80,
'protocol': 'TCP',
'targetPort': 'http',
},
],
'selector': {
'app': args.name
},
'type': 'ClusterIP',
},
}
return service
# make_ingress: create an Ingress resource for the given arguments.
# returns: API object data structure
def make_ingress(args):
# The basic Ingress
ingress = {
'apiVersion': 'extensions/v1beta1',
'kind': 'Ingress',
'metadata': {
'name': args.name,
'namespace': args.namespace,
'annotations': {}
},
'spec': {
'rules': [
{
'host': strip_hostname(hostname),
'http': {
'paths': [
{
'backend': {
'serviceName': args.name,
'servicePort': 80,
},
},
],
},
} for hostname in args.hostname
],
},
}
# Add htauth
secrets = []
if len(args.htauth_address):
ingress['metadata']['annotations']\
['ingress.kubernetes.io/whitelist-source-range'] = \
",".join(args.htauth_address)
if len(args.htauth_user):
ingress['metadata']['annotations'].update({
'ingress.kubernetes.io/auth-type': 'basic',
'ingress.kubernetes.io/auth-realm': args.htauth_realm,
'ingress.kubernetes.io/auth-satisfy': args.htauth_satisfy,
'ingress.kubernetes.io/auth-secret': args.name+'-htaccess',
})
htpasswd = ""
for auth in args.htauth_user:
(u,p) = auth.split(":", 1)
htpasswd += u + ":" + md5_crypt.hash(p) + "\n"
secrets.append({
'apiVersion': 'v1',
'kind': 'Secret',
'metadata': {
'name': args.name+'-htaccess',
'namespace': args.namespace,
},
'type': 'Opaque',
'data': {
'auth': b64encode(htpasswd.encode('utf-8')).decode('ascii'),
},
})
# Add ACME TLS
if args.acme:
ingress['metadata']['annotations']['kubernetes.io/tls-acme'] = 'true'
ingress['spec']['tls'] = [{
'hosts': [ strip_hostname(hostname) ],
'secretName': strip_hostname(hostname) + '-tls',
} for hostname in args.hostname]
return (ingress, secrets)
# make_pod: create a basic Pod for the given arguments
def make_pod(args):
pod = {
'metadata': {
'labels': {
'app': args.name,
}
},
'spec': {
'containers': [],
'volumes': [],
},
}
return pod
# make_deployment: convert the given Pod into a Deployment for the given args.
def make_deployment(pod, args):
deployment = {
'apiVersion': 'extensions/v1beta1',
'kind': 'Deployment',
'metadata': {
'name': args.name,
'namespace': args.namespace,
'annotations': {},
},
'spec': {
'replicas': args.replicas,
'selector': {
'matchLabels': {
'app': args.name,
},
},
'template': pod,
},
}
if args.strategy == 'rollingupdate':
deployment['spec']['strategy'] = {
'type': 'RollingUpdate',
'rollingUpdate': {
'maxSurge': 1,
'maxUnavailable': 0,
},
}
else:
deployment['spec']['strategy'] = {
'type': 'Recreate',
}
return deployment
# make_pvc: create a PVC from the given argument
def make_pvc(arg, args):
(volslug, path) = arg.split(':', 1)
name = args.name + '-' + volslug
pvc = {
'apiVersion': 'v1',
'kind': 'PersistentVolumeClaim',
'metadata': {
'namespace': args.namespace,
'name': name,
},
'spec': {
'accessModes': [ 'ReadWriteMany' ],
'resources': {
'requests': {
'storage': '1Gi',
},
},
},
}
pvcvolume = {
'name': volslug,
'persistentVolumeClaim': {
'claimName': name,
}
}
pvcmount = {
'name': volslug,
'mountPath': path,
}
return (pvc, pvcvolume, pvcmount)
# make_app_container: create the application container.
def make_app_container(args):
# We add some empty values here so it's easier to modify this template later
app_container = {
'name': 'app',
'image': args.image,
'imagePullPolicy': args.image_pull_policy,
'resources': {
'limits': {},
'requests': {},
},
'volumeMounts': [],
'env': [],
'envFrom': [],
}
# Resource limits
if args.cpu_limit:
app_container['resources']['limits']['cpu'] = args.cpu_limit
if args.cpu_request:
app_container['resources']['requests']['cpu'] = args.cpu_request
if args.memory_limit != 'none':
app_container['resources']['limits']['memory'] = \
humanfriendly.parse_size(args.memory_limit, binary=True)
if args.memory_request != 'none':
app_container['resources']['requests']['memory'] = \
humanfriendly.parse_size(args.memory_request, binary=True)
return app_container
# make_redis_container: create a Redis container based on args.
def make_redis_container(args):
container = {
'name': 'redis',
'image': "redis:alpine",
'imagePullPolicy': 'Always',
'args': [
'--maxmemory', args.redis_cache,
'--maxmemory-policy', 'allkeys-lru',
],
}
env = {
'name': 'CACHE_URL',
'value': 'redis://localhost:6379/0',
}
return (container, env)
# make_postgres: create a Postgres container for the given args.
def make_postgres(args):
postgres = {
'name': 'postgres',
'image': "postgres:" + args.postgres + "-alpine",
'imagePullPolicy': 'Always',
'volumeMounts': [
{
'name': 'postgres',
'mountPath': '/var/lib/postgresql/data',
},
],
}
env = {
'name': 'DATABASE_URL',
'value': 'postgres://postgres:postgres@localhost/postgres',
}
pvc = {
'apiVersion': 'v1',
'kind': 'PersistentVolumeClaim',
'metadata': {
'namespace': args.namespace,
'name': args.name + '-postgres',
},
'spec': {
'accessModes': [ 'ReadWriteMany' ],
'resources': {
'requests': {
'storage': '1Gi',
},
},
},
}
volume = {
'name': 'postgres',
'persistentVolumeClaim': {
'claimName': args.name + '-postgres',
}
}
return (postgres, env, volume, pvc)
# make_secret: create a Secret object based on args.
def make_secret(args):
secret = {
'apiVersion': 'v1',
'kind': 'Secret',
'metadata': {
'name': args.name,
'namespace': args.namespace,
},
'type': 'Opaque',
'data': {}
}
for s in args.secret:
(var, value) = s.split('=', 1)
secret['data'][var] = b64encode(value.encode('utf-8')).decode('ascii')
return secret
# make_database: create a torchbox.com/v1.Database based on args.
def make_database(args):
# Due to Kubernetes bug #53379 (https://github.com/kubernetes/kubernetes/issues/53379)
# we cannot unconditionally include the database in the manifest; it will
# fail to apply correctly when the database provisioner is using CRD
# instead of TPR. As a workaround, attempt to check whether the database
# already exists. This is not a very good check because any failure of
# kubectl will be treated as the database not existing, but it will do to
# make deployments work until the Kubernetes bug is fixed.
#
# This should be removed once #53379 is fixed, and we will mark the
# affected Kubernetes releases as unsupported for -D.
provision_db = True
items = []
if args.undeploy == False:
stdout.write('checking if database already exists (bug #53379 workaround)...\n')
kargs = kubectl.get_kubectl_args(args)
kargs.extend([ 'get', 'database', args.name ])
kubectl_p = subprocess.Popen(kargs,
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
kubectl_p.communicate()
if kubectl_p.returncode == 0:
stdout.write('database exists; will not replace\n')
provision_db = False
else:
stdout.write('database does not exist; will create\n')
if provision_db:
items.append({
'apiVersion': 'torchbox.com/v1',
'kind': 'Database',
'metadata': {
'namespace': args.namespace,
'name': args.name,
},
'spec': {
'class': 'default',
'secretName': args.name+'-database',
'type': args.database,
},
})
env = {
'name': 'DATABASE_URL',
'valueFrom': {
'secretKeyRef': {
'name': args.name+'-database',
'key': 'database-url',
},
},
}
return (items, env)
# make_manifest: create a manifest based on our arguments.
def make_manifest(args):
items = []
attached_resources = []
pod = make_pod(args)
app = make_app_container(args)
pod['spec']['containers'].append(app)
# Configure any requested PVCs
for vol in args.volume:
(pvc, pvcvolume, pvcmount) = make_pvc(vol, args)
items.append(pvc)
app['volumeMounts'].append(pvcmount)
pod['spec']['volumes'].append(pvcvolume)
attached_resources.append({
'kind': 'volume',
'name': pvc['metadata']['name'],
})
# Add Secret environment variables
if len(args.secret) > 0:
secret = make_secret(args)
items.append(secret)
app['envFrom'].append({
'secretRef': {
'name': args.name,
}
})
attached_resources.append({
'kind': 'secret',
'name': secret['metadata']['name'],
})
# Add (non-secret) environment variables
for env in args.env:
envbits = env.split('=', 1)
if len(envbits) == 1:
envbits.append(environ.get(envbits[0], ''))
app['env'].append({
'name': envbits[0],
'value': envbits[1]
})
if args.database is not None:
(db_items, db_env) = make_database(args)
items.extend(db_items)
app['env'].append(db_env)
attached_resources.append({
'kind': 'database',
'name': args.name,
})
# Add Redis container
if args.redis_cache is not None:
(redis, redis_env) = make_redis_container(args)
pod['spec']['containers'].append(redis)
app['env'].append(redis_env)
# Add Postgres container
if args.postgres is not None:
(postgres, pg_env, pg_volume, pg_pvc) = make_postgres(args)
pod['spec']['containers'].append(postgres)
pod['spec']['volumes'].append(pg_volume)
app['env'].append(pg_env)
items.append(pg_pvc)
attached_resources.append({
'kind': 'volume',
'name': pg_pvc['metadata']['name'],
})
# If any hostnames are configured, create a Service and some Ingresses.
if len(args.hostname):
app['ports'] = [
{
'name': 'http',
'containerPort': args.port,
'protocol': 'TCP',
}
]
# Service
service = make_service(args)
items.append(service)
attached_resources.append({
'kind': 'service',
'name': service['metadata']['name'],
})
# Ingress
(ingress, secrets) = make_ingress(args)
items.append(ingress)
attached_resources.append({
'kind': 'ingress',
'name': ingress['metadata']['name'],
})
# Secrets (only present if using http authentication)
items.extend(secrets)
attached_resources.extend([{
'kind': 'secret',
'name': secret['metadata']['name']
} for secret in secrets])
# Create our deployment last, so it can reference other resources.
deployment = make_deployment(pod, args)
deployment['metadata']['annotations']\
['kdtool.torchbox.com/attached-resources'] = json.dumps(attached_resources)
items.append(deployment)
# Convert our items array into a List.
spec = {
'apiVersion': 'v1',
'kind': 'List',
'items': items,
}
return spec
# Deploy an application.
def deploy(args):
if args.manifest:
spec = load_manifest(args, args.manifest)
else:
spec = make_manifest(args)
if args.json:
print(json.dumps(spec))
exit(0)
else:
exit(kubectl.apply_manifest(spec, args))
deploy.help = "deploy an application"
deploy.arguments = (
( ('-H', '--hostname'), {
'type': str,
'action': 'append',
'default': [],
'help': 'Hostname to expose the application on'
}),
( ('-A', '--acme'), {
'action': 'store_true',
'help': 'Issue Let\'s Encrypt (ACME) TLS certificate',
}),
( ('-M', '--manifest'), {
'type': str,
'metavar': 'FILE',
'help': 'Deploy from Kubernetes manifest with environment substitution',
}),
( ('-r', '--replicas'), {
'type': int,
'default': 1,
'help': 'Number of replicas to create',
}),
( ('-P', '--image-pull-policy'), {
'type': str,
'choices': ('IfNotPresent', 'Always'),
'default': 'IfNotPresent',
'help': 'Image pull policy',
}),
( ('-e', '--env'), {
'type': str,
'action': 'append',
'default': [],
'metavar': 'VARNAME=VALUE',
'help': 'Set environment variable',
}),
( ('-s', '--secret'), {
'type': str,
'action': 'append',
'default': [],
'metavar': 'VARNAME=VALUE',
'help': 'Set secret environment variable',
}),
( ('-v', '--volume'), {
'type': str,
'action': 'append',
'default': [],
'metavar': 'PATH',
'help': 'Attach persistent filesystem storage at PATH',
}),
( ('-p', '--port'), {
'type': int,
'default': 80,
'help': 'HTTP port the application listens on',
}),
( ('-j', '--json'), {
'action': 'store_true',
'help': 'Print JSON instead of applying to cluster',
}),
( ('-U', '--undeploy'), {
'action': 'store_true',
'help': 'Remove existing application',
}),
( ('-n', '--dry-run'), {
'action': 'store_true',
'help': 'Pass --dry-run to kubectl',
}),
( ('-D', '--database'), {
'type': str,
'choices': ('mysql', 'postgresql'),
'help': 'Provision database',
}),
( ('--htauth-user',), {
'type': str,
'action': 'append',
'default': [],
'metavar': 'USERNAME:PASSWORD',
'help': 'Add HTTP authentication username/password',
}),
( ('--htauth-address',), {
'type': str,
'action': 'append',
'default': [],
'metavar': 'ipaddress[/prefix]',
'help': 'Add HTTP authentication address',
}),
( ('--htauth-satisfy',), {
'type': str,
'default': 'any',
'choices': ('any', 'all'),
'help': 'HTTP authentication satisfy policy',
}),
( ('--htauth-realm',), {
'type': str,
'default': 'Authentication required',
'help': 'HTTP authentication realm',
}),
( ('--postgres',), {
'type': str,
'metavar': '9.6',
'help': 'Attach PostgreSQL database at $DATABASE_URL',
}),
( ('--redis-cache',), {
'type': str,
'metavar': '64m',
'help': 'Attach Redis database at $CACHE_URL',
}),
( ('--memory-request',), {
'type': str,
'default': 'none',
'help': 'Required memory allocation',
}),
( ('--memory-limit',), {
'type': str,
'default': 'none',
'help': 'Memory limit',
}),
( ('--cpu-request',), {
'type': float,
'default': 0,
'help': 'Number of dedicated CPU cores',
}),
( ('--cpu-limit',), {
'type': float,
'default': 0,
'help': 'CPU core use limit',
}),
( ('--strategy',), {
'type': str,
'choices': ('rollingupdate', 'recreate'),
'default': 'rollingupdate',
'help': 'Deployment update strategy',
}),
( ('image',), {
'type': str,
'help': 'Docker image to deploy',
}),
( ('name',), {
'type': str,
'help': 'Application name',
})
)
commands = {
'deploy': deploy,
}