@@ -14,7 +14,8 @@ class AppSettings(UuidAuditedModel):
1414
1515 owner = models .ForeignKey (settings .AUTH_USER_MODEL , on_delete = models .PROTECT )
1616 app = models .ForeignKey ('App' , on_delete = models .CASCADE )
17- maintenance = models .BooleanField (default = False )
17+ maintenance = models .NullBooleanField (default = None )
18+ routable = models .NullBooleanField (default = None )
1819
1920 class Meta :
2021 get_latest_by = 'created'
@@ -36,30 +37,66 @@ def set_maintenance(self, maintenance):
3637 self ._scheduler .update_service (namespace , namespace , data = old_service )
3738 raise KubeException (str (e )) from e
3839
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+
52+ 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
57+ if not previous_settings :
58+ setattr (self , 'maintenance' , False )
59+ self .set_maintenance (False )
60+ # 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
66+
67+ 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
72+ if not previous_settings :
73+ setattr (self , 'routable' , True )
74+ self .set_routable (True )
75+ # 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
81+
3982 def save (self , * args , ** kwargs ):
40- summary = ''
83+ self . summary = ''
4184 previous_settings = None
4285 try :
4386 previous_settings = self .app .appsettings_set .latest ()
4487 except AppSettings .DoesNotExist :
4588 pass
4689
47- prev_maintenance = getattr (previous_settings , 'maintenance' , None )
48- new_maintenance = getattr (self , 'maintenance' )
49-
5090 try :
51- if new_maintenance is None and prev_maintenance is not None :
52- setattr (self , 'maintenance' , prev_maintenance )
53- elif prev_maintenance != new_maintenance :
54- self .set_maintenance (new_maintenance )
55- summary += "{} changed maintenance mode from {} to {}" .format (self .owner , prev_maintenance , new_maintenance ) # noqa
91+ self .update_maintenance (previous_settings )
92+ self .update_routable (previous_settings )
5693 except Exception as e :
5794 self .delete ()
5895 raise DeisException (str (e )) from e
5996
60- if not summary and previous_settings :
97+ if not self . summary and previous_settings :
6198 self .delete ()
6299 raise AlreadyExists ("{} changed nothing" .format (self .owner ))
63- self .app .log ('summary of app setting changes: {}' .format (summary ), logging .DEBUG )
100+ self .app .log ('summary of app setting changes: {}' .format (self . summary ), logging .DEBUG )
64101
65102 return super (AppSettings , self ).save (** kwargs )
0 commit comments