@@ -301,10 +301,15 @@ def _command_announceable(self):
301301 @transition (field = state , source = INITIALIZED , target = CREATED )
302302 def create (self ):
303303 image = self .release .image
304+ kwargs = {}
305+ if self .release .config .limit is not None :
306+ kwargs = {'memory' : self .release .config .limit .memory ,
307+ 'cpu' : self .release .config .limit .cpu }
304308 self ._scheduler .create (name = self ._job_id ,
305309 image = image ,
306310 command = self ._command ,
307- use_announcer = self ._command_announceable ())
311+ use_announcer = self ._command_announceable (),
312+ ** kwargs )
308313
309314 @close_db_connections
310315 @transition (field = state ,
@@ -327,10 +332,15 @@ def deploy(self, release):
327332 new_job_id = self ._job_id
328333 image = self .release .image
329334 c_type = self .type
335+ kwargs = {}
336+ if self .release .config .limit is not None :
337+ kwargs = {'memory' : self .release .config .limit .memory ,
338+ 'cpu' : self .release .config .limit .cpu }
330339 self ._scheduler .create (name = new_job_id ,
331340 image = image ,
332341 command = self ._command .format (** locals ()),
333- use_announcer = self ._command_announceable ())
342+ use_announcer = self ._command_announceable (),
343+ ** kwargs )
334344 self ._scheduler .start (new_job_id , self ._command_announceable ())
335345 # destroy old container
336346 self ._scheduler .destroy (old_job_id , self ._command_announceable ())
@@ -416,6 +426,28 @@ class Config(UuidAuditedModel):
416426 owner = models .ForeignKey (settings .AUTH_USER_MODEL )
417427 app = models .ForeignKey ('App' )
418428 values = JSONField (default = '{}' , blank = True )
429+ limit = models .ForeignKey ('Limit' , null = True )
430+
431+ class Meta :
432+ get_latest_by = 'created'
433+ ordering = ['-created' ]
434+ unique_together = (('app' , 'uuid' ),)
435+
436+ def __str__ (self ):
437+ return "{}-{}" .format (self .app .id , self .uuid [:7 ])
438+
439+
440+ @python_2_unicode_compatible
441+ class Limit (UuidAuditedModel ):
442+ """
443+ Set of resource limits applied by the scheduler
444+ during runtime execution of the Application.
445+ """
446+
447+ owner = models .ForeignKey (settings .AUTH_USER_MODEL )
448+ app = models .ForeignKey ('App' )
449+ memory = JSONField (default = '{}' , blank = True )
450+ cpu = JSONField (default = '{}' , blank = True )
419451
420452 class Meta :
421453 get_latest_by = 'created'
@@ -507,12 +539,14 @@ def previous(self):
507539 prev_release = None
508540 return prev_release
509541
510- def save (self , * args , ** kwargs ):
542+ def save (self , * args , ** kwargs ): # noqa
511543 if not self .summary :
512544 self .summary = ''
513545 prev_release = self .previous ()
514546 # compare this build to the previous build
515547 old_build = prev_release .build if prev_release else None
548+ old_config = prev_release .config if prev_release else None
549+ old_limit = prev_release .config .limit if prev_release else None
516550 # if the build changed, log it and who pushed it
517551 if self .version == 1 :
518552 self .summary += "{} created initial release" .format (self .app .owner )
@@ -521,10 +555,8 @@ def save(self, *args, **kwargs):
521555 self .summary += "{} deployed {}" .format (self .build .owner , self .build .sha [:7 ])
522556 else :
523557 self .summary += "{} deployed {}" .format (self .build .owner , self .build .image )
524- # compare this config to the previous config
525- old_config = prev_release .config if prev_release else None
526558 # if the config data changed, log the dict diff
527- if self .config != old_config :
559+ elif self .config != old_config :
528560 dict1 = self .config .values
529561 dict2 = old_config .values if old_config else {}
530562 diff = dict_diff (dict1 , dict2 )
@@ -540,11 +572,25 @@ def save(self, *args, **kwargs):
540572 if self .summary :
541573 self .summary += ' and '
542574 self .summary += "{} {}" .format (self .config .owner , changes )
543- if not self .summary :
544- if self .version == 1 :
545- self .summary = "{} created the initial release" .format (self .owner )
546- else :
547- self .summary = "{} changed nothing" .format (self .owner )
575+ # if the limit changes, log the dict diff
576+ elif self .config .limit != old_limit :
577+ changes = []
578+ old_mem = old_limit .memory if old_limit else {}
579+ diff = dict_diff (self .limit .memory , old_mem )
580+ if diff .get ('added' ) or diff .get ('changed' ) or diff .get ('deleted' ):
581+ changes .append ('memory' )
582+ old_cpu = old_limit .cpu if old_limit else {}
583+ diff = dict_diff (self .limit .cpu , old_cpu )
584+ if diff .get ('added' ) or diff .get ('changed' ) or diff .get ('deleted' ):
585+ changes .append ('cpu' )
586+ if changes :
587+ changes = 'changed limits for ' + ', ' .join (changes )
588+ self .summary += "{} {}" .format (self .config .owner , changes )
589+ if not self .summary :
590+ if self .version == 1 :
591+ self .summary = "{} created the initial release" .format (self .owner )
592+ else :
593+ self .summary = "{} changed nothing" .format (self .owner )
548594 super (Release , self ).save (* args , ** kwargs )
549595
550596
0 commit comments