2121from django .utils .encoding import python_2_unicode_compatible
2222from django_fsm import FSMField , transition
2323from django_fsm .signals import post_transition
24+ from json_field .fields import JSONField
2425
2526from api import fields , tasks
2627from registry import publish_release
@@ -87,7 +88,7 @@ class Cluster(UuidAuditedModel):
8788 domain = models .CharField (max_length = 128 )
8889 hosts = models .CharField (max_length = 256 )
8990 auth = models .TextField ()
90- options = fields . JSONField (default = '{}' , blank = True )
91+ options = JSONField (default = {} , blank = True )
9192
9293 def __str__ (self ):
9394 return self .id
@@ -122,7 +123,7 @@ class App(UuidAuditedModel):
122123 owner = models .ForeignKey (settings .AUTH_USER_MODEL )
123124 id = models .SlugField (max_length = 64 , unique = True )
124125 cluster = models .ForeignKey ('Cluster' )
125- structure = fields . JSONField (default = '{}' , blank = True )
126+ structure = JSONField (default = {} , blank = True )
126127
127128 class Meta :
128129 permissions = (('use_app' , 'Can use app' ),)
@@ -135,7 +136,7 @@ def url(self):
135136 return self .id + '.' + self .cluster .domain
136137
137138 def create (self , * args , ** kwargs ):
138- config = Config .objects .create (owner = self .owner , app = self , values = {} )
139+ config = Config .objects .create (owner = self .owner , app = self )
139140 build = Build .objects .create (owner = self .owner , app = self , image = settings .DEFAULT_BUILD )
140141 Release .objects .create (version = 1 , owner = self .owner , app = self , config = config , build = build )
141142
@@ -308,10 +309,8 @@ def _command_announceable(self):
308309 @transition (field = state , source = INITIALIZED , target = CREATED )
309310 def create (self ):
310311 image = self .release .image
311- kwargs = {}
312- if self .release .config .limit is not None :
313- kwargs = {'memory' : self .release .config .limit .memory ,
314- 'cpu' : self .release .config .limit .cpu }
312+ kwargs = {'memory' : self .release .config .memory ,
313+ 'cpu' : self .release .config .cpu }
315314 self ._scheduler .create (name = self ._job_id ,
316315 image = image ,
317316 command = self ._command ,
@@ -339,10 +338,8 @@ def deploy(self, release):
339338 new_job_id = self ._job_id
340339 image = self .release .image
341340 c_type = self .type
342- kwargs = {}
343- if self .release .config .limit is not None :
344- kwargs = {'memory' : self .release .config .limit .memory ,
345- 'cpu' : self .release .config .limit .cpu }
341+ kwargs = {'memory' : self .release .config .memory ,
342+ 'cpu' : self .release .config .cpu }
346343 self ._scheduler .create (name = new_job_id ,
347344 image = image ,
348345 command = self ._command .format (** locals ()),
@@ -411,7 +408,7 @@ class Build(UuidAuditedModel):
411408
412409 # optional fields populated by builder
413410 sha = models .CharField (max_length = 40 , blank = True )
414- procfile = fields . JSONField (default = '{}' , blank = True )
411+ procfile = JSONField (default = {} , blank = True )
415412 dockerfile = models .TextField (blank = True )
416413
417414 class Meta :
@@ -432,29 +429,9 @@ class Config(UuidAuditedModel):
432429
433430 owner = models .ForeignKey (settings .AUTH_USER_MODEL )
434431 app = models .ForeignKey ('App' )
435- values = fields .JSONField (default = '{}' , blank = True )
436- limit = models .ForeignKey ('Limit' , null = True )
437-
438- class Meta :
439- get_latest_by = 'created'
440- ordering = ['-created' ]
441- unique_together = (('app' , 'uuid' ),)
442-
443- def __str__ (self ):
444- return "{}-{}" .format (self .app .id , self .uuid [:7 ])
445-
446-
447- @python_2_unicode_compatible
448- class Limit (UuidAuditedModel ):
449- """
450- Set of resource limits applied by the scheduler
451- during runtime execution of the Application.
452- """
453-
454- owner = models .ForeignKey (settings .AUTH_USER_MODEL )
455- app = models .ForeignKey ('App' )
456- memory = fields .JSONField (default = '{}' , blank = True )
457- cpu = fields .JSONField (default = '{}' , blank = True )
432+ values = JSONField (default = {}, blank = True )
433+ memory = JSONField (default = {}, blank = True )
434+ cpu = JSONField (default = {}, blank = True )
458435
459436 class Meta :
460437 get_latest_by = 'created'
@@ -553,7 +530,6 @@ def save(self, *args, **kwargs): # noqa
553530 # compare this build to the previous build
554531 old_build = prev_release .build if prev_release else None
555532 old_config = prev_release .config if prev_release else None
556- old_limit = prev_release .config .limit if prev_release else None
557533 # if the build changed, log it and who pushed it
558534 if self .version == 1 :
559535 self .summary += "{} created initial release" .format (self .app .owner )
@@ -579,15 +555,14 @@ def save(self, *args, **kwargs): # noqa
579555 if self .summary :
580556 self .summary += ' and '
581557 self .summary += "{} {}" .format (self .config .owner , changes )
582- # if the limit changes, log the dict diff
583- if self .config .limit != old_limit :
558+ # if the limits changed (memory or cpu), log the dict diff
584559 changes = []
585- old_mem = old_limit .memory if old_limit else {}
586- diff = dict_diff (self .config .limit . memory , old_mem )
560+ old_mem = old_config .memory if old_config else {}
561+ diff = dict_diff (self .config .memory , old_mem )
587562 if diff .get ('added' ) or diff .get ('changed' ) or diff .get ('deleted' ):
588563 changes .append ('memory' )
589- old_cpu = old_limit .cpu if old_limit else {}
590- diff = dict_diff (self .config .limit . cpu , old_cpu )
564+ old_cpu = old_config .cpu if old_config else {}
565+ diff = dict_diff (self .config .cpu , old_cpu )
591566 if diff .get ('added' ) or diff .get ('changed' ) or diff .get ('deleted' ):
592567 changes .append ('cpu' )
593568 if changes :
0 commit comments