Skip to content

Commit d155e92

Browse files
authored
ref(apps): move AppSettings scheduler logic to App model for simplicity (#993)
1 parent 4e0c5a3 commit d155e92

2 files changed

Lines changed: 49 additions & 47 deletions

File tree

rootfs/api/models/app.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,3 +841,31 @@ def _build_env_vars(self, build_type, version, image, envs):
841841
# merge envs on top of default to make envs win
842842
default_env.update(envs)
843843
return default_env
844+
845+
def maintenance_mode(self, mode):
846+
"""
847+
Turn application maintenance mode on/off
848+
"""
849+
service = self._fetch_service_config(self.id)
850+
old_service = service.copy() # in case anything fails for rollback
851+
852+
try:
853+
service['metadata']['annotations']['router.deis.io/maintenance'] = str(mode).lower()
854+
self._scheduler.update_service(self.id, self.id, data=service)
855+
except KubeException as e:
856+
self._scheduler.update_service(self.id, self.id, data=old_service)
857+
raise ServiceUnavailable(str(e)) from e
858+
859+
def routable(self, routable):
860+
"""
861+
Turn on/off if an application is publically routable
862+
"""
863+
service = self._fetch_service_config(self.id)
864+
old_service = service.copy() # in case anything fails for rollback
865+
866+
try:
867+
service['metadata']['labels']['router.deis.io/routable'] = str(routable).lower()
868+
self._scheduler.update_service(self.id, self.id, data=service)
869+
except KubeException as e:
870+
self._scheduler.update_service(self.id, self.id, data=old_service)
871+
raise ServiceUnavailable(str(e)) from e

rootfs/api/models/appsettings.py

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from api.models import UuidAuditedModel
66
from api.exceptions import DeisException, AlreadyExists
7-
from scheduler import KubeException
87

98

109
class AppSettings(UuidAuditedModel):
@@ -25,62 +24,36 @@ class Meta:
2524
def __str__(self):
2625
return "{}-{}".format(self.app.id, str(self.uuid)[:7])
2726

28-
def set_maintenance(self, maintenance):
29-
namespace = self.app.id
30-
service = self._fetch_service_config(namespace)
31-
old_service = service.copy() # in case anything fails for rollback
32-
33-
try:
34-
service['metadata']['annotations']['router.deis.io/maintenance'] = str(maintenance)
35-
self._scheduler.update_service(namespace, namespace, data=service)
36-
except Exception as e:
37-
self._scheduler.update_service(namespace, namespace, data=old_service)
38-
raise KubeException(str(e)) from e
39-
40-
def set_routable(self, routable):
41-
namespace = self.app.id
42-
service = self._fetch_service_config(namespace)
43-
old_service = service.copy() # in case anything fails for rollback
44-
45-
try:
46-
service['metadata']['labels']['router.deis.io/routable'] = str(routable).lower()
47-
self._scheduler.update_service(namespace, namespace, data=service)
48-
except Exception as e:
49-
self._scheduler.update_service(namespace, namespace, data=old_service)
50-
raise KubeException(str(e)) from e
51-
5227
def update_maintenance(self, previous_settings):
53-
prev_maintenance = getattr(previous_settings, 'maintenance', None)
54-
new_maintenance = getattr(self, 'maintenance', None)
55-
# If no previous settings, assume this is first timeout
56-
# and set the default maintenance as false
28+
old = getattr(previous_settings, 'maintenance', None)
29+
new = getattr(self, 'maintenance', None)
30+
# If no previous settings then assume it is the first record and default to true
5731
if not previous_settings:
5832
setattr(self, 'maintenance', False)
59-
self.set_maintenance(False)
33+
self.app.maintenance_mode(False)
6034
# if nothing changed copy the settings from previous
61-
elif new_maintenance is None and prev_maintenance is not None:
62-
setattr(self, 'maintenance', prev_maintenance)
63-
elif prev_maintenance != new_maintenance:
64-
self.set_maintenance(new_maintenance)
65-
self.summary += "{} changed maintenance mode from {} to {}".format(self.owner, prev_maintenance, new_maintenance) # noqa
35+
elif new is None and old is not None:
36+
setattr(self, 'maintenance', old)
37+
elif old != new:
38+
self.app.maintenance_mode(new)
39+
self.summary += ["{} changed maintenance mode from {} to {}".format(self.owner, old, new)] # noqa
6640

6741
def update_routable(self, previous_settings):
68-
old_routable = getattr(previous_settings, 'routable', None)
69-
new_routable = getattr(self, 'routable', None)
70-
# If no previous settings, assume this is first timeout
71-
# and set the default maintenance as true
42+
old = getattr(previous_settings, 'routable', None)
43+
new = getattr(self, 'routable', None)
44+
# If no previous settings then assume it is the first record and default to true
7245
if not previous_settings:
7346
setattr(self, 'routable', True)
74-
self.set_routable(True)
47+
self.app.routable(True)
7548
# if nothing changed copy the settings from previous
76-
elif new_routable is None and old_routable is not None:
77-
setattr(self, 'routable', old_routable)
78-
elif old_routable != new_routable:
79-
self.set_routable(new_routable)
80-
self.summary += "{} changed routablity from {} to {}".format(self.owner, old_routable, new_routable) # noqa
49+
elif new is None and old is not None:
50+
setattr(self, 'routable', old)
51+
elif old != new:
52+
self.app.routable(new)
53+
self.summary += ["{} changed routablity from {} to {}".format(self.owner, old, new)]
8154

8255
def save(self, *args, **kwargs):
83-
self.summary = ''
56+
self.summary = []
8457
previous_settings = None
8558
try:
8659
previous_settings = self.app.appsettings_set.latest()
@@ -97,6 +70,7 @@ def save(self, *args, **kwargs):
9770
if not self.summary and previous_settings:
9871
self.delete()
9972
raise AlreadyExists("{} changed nothing".format(self.owner))
100-
self.app.log('summary of app setting changes: {}'.format(self.summary), logging.DEBUG)
10173

74+
summary = ' '.join(self.summary)
75+
self.app.log('summary of app setting changes: {}'.format(summary), logging.DEBUG)
10276
return super(AppSettings, self).save(**kwargs)

0 commit comments

Comments
 (0)