-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathload_db_state_to_k8s.py
More file actions
76 lines (63 loc) · 2.81 KB
/
load_db_state_to_k8s.py
File metadata and controls
76 lines (63 loc) · 2.81 KB
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
import logging
from django.core.management.base import BaseCommand
from django.shortcuts import get_object_or_404
from api.models.key import Key
from api.models.app import App
from api.models.domain import Domain
from api.models.certificate import Certificate
from api.models.service import Service
from api.exceptions import DryccException, AlreadyExists
logger = logging.getLogger(__name__)
class Command(BaseCommand):
"""Management command for publishing Drycc platform state from the database
to k8s.
"""
def _deploy(self, app, release):
if release.build is None:
print('WARNING: {} has no build associated with '
'its latest release. Skipping deployment...'.format(app))
return
try:
app.deploy(release)
except AlreadyExists as error:
logger.debug(error)
print('WARNING: {} has a deployment in progress. '
'Skipping deployment...'.format(app))
except DryccException as error:
logger.exception(error)
print('ERROR: There was a problem deploying {} '
'due to {}'.format(app, str(error)))
def handle(self, *args, **options):
"""Publishes Drycc platform state from the database to kubernetes."""
print("Publishing DB state to kubernetes...")
self.save_apps()
# certificates have to be attached to domains to create k8s secrets
for cert in Certificate.objects.all():
for domain in cert.domains:
domain = get_object_or_404(Domain, domain=domain)
cert.attach_in_kubernetes(domain)
# deploy apps
print("Deploying available applications")
for app in App.objects.all():
release = app.release_set.filter(failed=False).latest()
if release.canary:
self._deploy(app, app.release_set.filter(failed=False, canary=False).latest())
self._deploy(app, release)
print("Done Publishing DB state to kubernetes.")
def save_apps(self):
"""Saves important Django data models to the database."""
for app in App.objects.all():
try:
app.save()
app.config_set.latest().save()
app.tls_set.latest().sync()
except DryccException as error:
print('ERROR: Problem saving to model {} for {}'
'due to {}'.format(str(App.__name__), str(app), str(error)))
for model in (Key, Domain, Certificate, Service):
for obj in model.objects.all():
try:
obj.save()
except DryccException as error:
print('ERROR: Problem saving to model {} for {}'
'due to {}'.format(str(model.__name__), str(obj), str(error)))