1- import json
21import logging
3- from django .conf import settings
42from django .db import models
53from django .contrib .auth import get_user_model
64from api .utils import dict_diff
@@ -43,20 +41,20 @@ def __str__(self):
4341
4442 def previous (self ):
4543 """
46- Return the previous Release to this one.
44+ Return the previous Config to this one.
4745
48- :return: the previous :class:`Release `, or None
46+ :return: the previous :class:`Config `, or None
4947 """
5048 configs = self .app .config_set
5149 if self .pk :
5250 configs = configs .exclude (pk = self .pk )
5351
5452 try :
55- # Get the Release previous to this one
56- prev_release = configs .latest ()
57- except Release .DoesNotExist :
58- prev_release = None
59- return prev_release
53+ # Get the Config previous to this one
54+ prev_config = configs .latest ()
55+ except Config .DoesNotExist :
56+ prev_config = None
57+ return prev_config
6058
6159 def diff (self , config = None ):
6260 old_config = config if config else self .previous ()
@@ -76,7 +74,7 @@ def save(self, **kwargs):
7674 # usually means a totally new app
7775 previous_config = self .app .config_set .latest ()
7876
79- for attr in ['tags' , ' registry' , 'values' , 'lifecycle_post_start' ,
77+ for attr in ['registry' , 'values' , 'lifecycle_post_start' ,
8078 'lifecycle_pre_stop' , 'termination_grace_period' ]:
8179 data = getattr (previous_config , attr , {}).copy ()
8280 new_data = getattr (self , attr , {}).copy ()
@@ -88,7 +86,7 @@ def save(self, **kwargs):
8886 self ._set_registry ()
8987 self ._set_tags (previous_config )
9088 except Config .DoesNotExist :
91- self ._set_tags ({'tags' : {}})
89+ self ._set_tags (previous_config = {'tags' : {}})
9290
9391 return super (Config , self ).save (** kwargs )
9492
@@ -117,34 +115,31 @@ def _set_registry(self):
117115
118116 def _set_tags (self , previous_config ):
119117 """verify the tags exist on any nodes as labels"""
120- if not self .tags :
121- if settings .DRYCC_DEFAULT_CONFIG_TAGS :
122- try :
123- tags = json .loads (settings .DRYCC_DEFAULT_CONFIG_TAGS )
124- self .tags = tags
125- except json .JSONDecodeError as e :
126- logger .exception (e )
127- return
118+ data = getattr (previous_config , 'tags' , {}).copy ()
119+ new_data = getattr (self , 'tags' , {}).copy ()
120+ # remove config keys if a null value is provided
121+ for procfile_type , values in new_data .items ():
122+ if not values :
123+ # error if unsetting non-existing key
124+ if procfile_type not in data :
125+ raise UnprocessableEntity (
126+ '{} does not exist under {}' .format (procfile_type , 'tags' ))
127+ data .pop (procfile_type )
128128 else :
129- return
130-
131- # Get all nodes with label selectors
132- nodes = self .scheduler ().node .get (labels = self .tags ).json ()
133- if nodes ['items' ]:
134- return
135-
136- labels = ['{}={}' .format (key , value ) for key , value in self .tags .items ()]
137- message = 'No nodes matched the provided labels: {}' .format (', ' .join (labels ))
138-
139- # Find out if there are any other tags around
140- old_tags = getattr (previous_config , 'tags' )
141- if old_tags :
142- old = ['{}={}' .format (key , value ) for key , value in old_tags .items ()]
143- new = set (labels ) - set (old )
144- if new :
145- message += ' - Addition of {} is the cause' .format (', ' .join (new ))
146-
147- raise DryccException (message )
129+ if not self .scheduler ().node .get (labels = values ).json ()['items' ]:
130+ labels = ['{}={}' .format (key , value ) for key , value in values .items ()]
131+ message = 'No nodes matched the provided labels: {}' .format (', ' .join (labels ))
132+ # Find out if there are any other tags around
133+ old_tags = previous_config .tags .get (procfile_type , {})
134+ if old_tags :
135+ old = ['{}={}' .format (key , value ) for key , value in old_tags .items ()]
136+ new = set (labels ) - set (old )
137+ if new :
138+ message += ' - Addition of {} is the cause' .format (', ' .join (new ))
139+ raise DryccException (message )
140+ data [procfile_type ] = self ._merge_data (
141+ 'tags' , data .get (procfile_type , {}), values )
142+ setattr (self , 'tags' , data )
148143
149144 def _set_limits (self , previous_config ):
150145 data = getattr (previous_config , 'limits' , {}).copy ()
0 commit comments