Skip to content

Commit ff221e4

Browse files
committed
Merge pull request #4394 from krancour/publish-etcd-keys-at-controller-start
feat(controller): publish etcd keys at controller start
2 parents 4a5606f + 2077ab7 commit ff221e4

6 files changed

Lines changed: 42 additions & 38 deletions

File tree

controller/api/management/__init__.py

Whitespace-only changes.

controller/api/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from django.core.management.base import BaseCommand
2+
3+
from api.models import Key, App, Domain, Certificate, Config
4+
5+
6+
class Command(BaseCommand):
7+
"""Management command for publishing Deis platform state from the database
8+
to etcd.
9+
"""
10+
def handle(self, *args, **options):
11+
"""Publishes Deis platform state from the database to etcd."""
12+
print "Publishing DB state to etcd..."
13+
for model in (Key, App, Domain, Certificate, Config):
14+
for obj in model.objects.all():
15+
obj.save()
16+
print "Done Publishing DB state to etcd."

controller/api/models.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,9 +1081,10 @@ def _log_config_updated(**kwargs):
10811081

10821082

10831083
def _log_domain_added(**kwargs):
1084-
domain = kwargs['instance']
1085-
msg = "domain {} added".format(domain)
1086-
log_event(domain.app, msg)
1084+
if kwargs.get('created'):
1085+
domain = kwargs['instance']
1086+
msg = "domain {} added".format(domain)
1087+
log_event(domain.app, msg)
10871088

10881089

10891090
def _log_domain_removed(**kwargs):
@@ -1093,8 +1094,9 @@ def _log_domain_removed(**kwargs):
10931094

10941095

10951096
def _log_cert_added(**kwargs):
1096-
cert = kwargs['instance']
1097-
logger.info("cert {} added".format(cert))
1097+
if kwargs.get('created'):
1098+
cert = kwargs['instance']
1099+
logger.info("cert {} added".format(cert))
10981100

10991101

11001102
def _log_cert_removed(**kwargs):
@@ -1127,10 +1129,13 @@ def _etcd_purge_user(**kwargs):
11271129
pass
11281130

11291131

1130-
def _etcd_create_app(**kwargs):
1132+
def _etcd_publish_app(**kwargs):
11311133
appname = kwargs['instance']
1132-
if kwargs['created']:
1134+
try:
11331135
_etcd_client.write('/deis/services/{}'.format(appname), None, dir=True)
1136+
except KeyError:
1137+
# Ignore error when the directory already exists.
1138+
pass
11341139

11351140

11361141
def _etcd_purge_app(**kwargs):
@@ -1143,9 +1148,8 @@ def _etcd_purge_app(**kwargs):
11431148

11441149
def _etcd_publish_cert(**kwargs):
11451150
cert = kwargs['instance']
1146-
if kwargs['created']:
1147-
_etcd_client.write('/deis/certs/{}/cert'.format(cert), cert.certificate)
1148-
_etcd_client.write('/deis/certs/{}/key'.format(cert), cert.key)
1151+
_etcd_client.write('/deis/certs/{}/cert'.format(cert), cert.certificate)
1152+
_etcd_client.write('/deis/certs/{}/key'.format(cert), cert.key)
11491153

11501154

11511155
def _etcd_purge_cert(**kwargs):
@@ -1167,13 +1171,12 @@ def _etcd_publish_config(**kwargs):
11671171
prevExist=True, dir=True, recursive=True)
11681172
except KeyError:
11691173
pass
1170-
if kwargs['created']:
1171-
for k, v in config.values.iteritems():
1172-
_etcd_client.write(
1173-
'/deis/config/{}/{}'.format(
1174-
config.app,
1175-
unicode(k).encode('utf-8').lower()),
1176-
unicode(v).encode('utf-8'))
1174+
for k, v in config.values.iteritems():
1175+
_etcd_client.write(
1176+
'/deis/config/{}/{}'.format(
1177+
config.app,
1178+
unicode(k).encode('utf-8').lower()),
1179+
unicode(v).encode('utf-8'))
11771180

11781181

11791182
def _etcd_purge_config(**kwargs):
@@ -1187,8 +1190,7 @@ def _etcd_purge_config(**kwargs):
11871190

11881191
def _etcd_publish_domains(**kwargs):
11891192
domain = kwargs['instance']
1190-
if kwargs['created']:
1191-
_etcd_client.write('/deis/domains/{}'.format(domain), domain.app)
1193+
_etcd_client.write('/deis/domains/{}'.format(domain), domain.app)
11921194

11931195

11941196
def _etcd_purge_domains(**kwargs):
@@ -1226,7 +1228,7 @@ def create_auth_token(sender, instance=None, created=False, **kwargs):
12261228
post_delete.connect(_etcd_purge_user, sender=get_user_model(), dispatch_uid='api.models')
12271229
post_save.connect(_etcd_publish_domains, sender=Domain, dispatch_uid='api.models')
12281230
post_delete.connect(_etcd_purge_domains, sender=Domain, dispatch_uid='api.models')
1229-
post_save.connect(_etcd_create_app, sender=App, dispatch_uid='api.models')
1231+
post_save.connect(_etcd_publish_app, sender=App, dispatch_uid='api.models')
12301232
post_delete.connect(_etcd_purge_app, sender=App, dispatch_uid='api.models')
12311233
post_save.connect(_etcd_publish_cert, sender=Certificate, dispatch_uid='api.models')
12321234
post_delete.connect(_etcd_purge_cert, sender=Certificate, dispatch_uid='api.models')

controller/bin/boot

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ sudo -E -u deis ./manage.py syncdb --migrate --noinput
8686
# spawn a gunicorn server in the background
8787
sudo -E -u deis gunicorn -c deis/gconf.py deis.wsgi &
8888

89+
./manage.py load_db_state_to_etcd
90+
8991
# smart shutdown on SIGTERM (SIGINT is handled by gunicorn)
9092
function on_exit() {
9193
GUNICORN_PID=$(cat /tmp/gunicorn.pid)

docs/managing_deis/backing_up_data.rst

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -197,24 +197,8 @@ Finishing up
197197

198198
Now that the data is restored, the rest of the cluster should come up normally with a ``deisctl start platform``.
199199

200-
The last task is to instruct the controller to re-write user keys, application data, and domains to etcd.
201-
Log into the machine which runs deis-controller and run the following. Note that the IP address to
202-
use in the ``export`` command should correspond to the IP of the host machine which runs this container.
203-
204-
.. code-block:: console
205-
206-
$ nse deis-controller
207-
$ cd /app
208-
$ export ETCD=172.17.8.100:4001
209-
./manage.py shell <<EOF
210-
from api.models import *
211-
[k.save() for k in Key.objects.all()]
212-
[a.save() for a in App.objects.all()]
213-
[d.save() for d in Domain.objects.all()]
214-
[c.save() for c in Certificate.objects.all()]
215-
[c.save() for c in Config.objects.all()]
216-
EOF
217-
$ exit
200+
The controller will automatically re-write user keys, application data, and domains from the
201+
restored database to etcd.
218202

219203
That's it! The cluster should be fully restored.
220204

0 commit comments

Comments
 (0)