44
55from api .models import UuidAuditedModel
66from api .exceptions import DeisException , AlreadyExists
7- from scheduler import KubeException
87
98
109class 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