66
77from __future__ import unicode_literals
88import importlib
9+ import logging
910import os
1011import subprocess
1112
2425from api import fields , tasks
2526from provider import import_provider_module
2627
28+
29+ logger = logging .getLogger (__name__ )
30+
2731# import user-defined configuration management module
2832CM = importlib .import_module (settings .CM_MODULE )
2933
@@ -443,6 +447,11 @@ def run(self, command, **kwargs):
443447 return tasks .run_node .delay (self , command ).wait ()
444448
445449
450+ def log_event (app , msg , level = logging .INFO ):
451+ msg = "{}: {}" .format (app .id , msg )
452+ logger .log (level , msg )
453+
454+
446455@python_2_unicode_compatible
447456class App (UuidAuditedModel ):
448457 """
@@ -544,6 +553,7 @@ def run(self, command):
544553 'deis/slugrunner' ])
545554 env_args = ' ' .join (["-e '{k}={v}'" .format (** locals ())
546555 for k , v in release .config .values .items ()])
556+ log_event (self , "deis run '{}'" .format (command ))
547557 command = "sudo docker run {env_args} {docker_args} {command}" .format (** locals ())
548558 return node .run (command )
549559
@@ -557,6 +567,8 @@ def scale(self, app, structure, **kwargs):
557567 # increment new container nums off the most recent container
558568 all_containers = app .container_set .all ().order_by ('-created' )
559569 container_num = 1 if not all_containers else all_containers [0 ].num + 1
570+ msg = 'Containers scaled ' + ' ' .join (
571+ "{}={}" .format (k , v ) for k , v in requested_containers .items ())
560572 # iterate and scale by container type (web, worker, etc)
561573 changed = False
562574 for container_type in requested_containers .keys ():
@@ -591,6 +603,7 @@ def scale(self, app, structure, **kwargs):
591603 containers .append (c )
592604 container_num += 1
593605 diff -= 1
606+ log_event (app , msg )
594607 return changed
595608
596609 def balance (self , formation , ** kwargs ):
@@ -599,6 +612,7 @@ def balance(self, formation, **kwargs):
599612 # get the next container number (e.g. web.19)
600613 container_num = 1 if not all_containers else all_containers [0 ].num + 1
601614 changed = False
615+ app = None
602616 # iterate by unique container type
603617 for container_type in set ([c .type for c in all_containers ]):
604618 # map node container counts => { 2: [b3, b4], 3: [ b1, b2 ] }
@@ -636,6 +650,8 @@ def balance(self, formation, **kwargs):
636650 ct = len (n .container_set .filter (type = container_type ))
637651 n_map .setdefault (ct , []).append (n )
638652 changed = True
653+ if app :
654+ log_event (app , 'Containers balanced' )
639655 return changed
640656
641657
@@ -716,7 +732,7 @@ class Meta:
716732 unique_together = (('app' , 'uuid' ),)
717733
718734 def __str__ (self ):
719- return "{0}-{1}" .format (self .app .id , self .sha [8 : ])
735+ return "{0}-{1}" .format (self .app .id , self .sha [: 7 ])
720736
721737
722738@python_2_unicode_compatible
@@ -746,7 +762,7 @@ class Meta:
746762 unique_together = (('app' , 'uuid' ),)
747763
748764 def __str__ (self ):
749- return "{0}-{1}" .format (self .app .id , self .sha )
765+ return "{0}-{1}" .format (self .app .id , self .sha [: 7 ] )
750766
751767 @classmethod
752768 def push (cls , push ):
@@ -877,9 +893,30 @@ def _purge_user_from_cm(**kwargs):
877893 kwargs ['instance' ].purge ()
878894
879895
880- # use django signals to synchronize database updates with
881- # the configuration management backend
896+ def _log_build_created (** kwargs ):
897+ if kwargs .get ('created' ):
898+ build = kwargs ['instance' ]
899+ log_event (build .app , "Build {} created" .format (build ))
900+
901+
902+ def _log_release_created (** kwargs ):
903+ if kwargs .get ('created' ):
904+ release = kwargs ['instance' ]
905+ log_event (release .app , "Release {} created" .format (release ))
906+
907+
908+ def _log_config_updated (** kwargs ):
909+ config = kwargs ['instance' ]
910+ log_event (config .app , "Config {} updated" .format (config ))
911+
912+
913+ # Connect Django model signals
914+ # Sync database updates with the configuration management backend
882915post_save .connect (_publish_to_cm , sender = App , dispatch_uid = 'api.models' )
883916post_save .connect (_publish_to_cm , sender = Formation , dispatch_uid = 'api.models' )
884917post_save .connect (_publish_user_to_cm , sender = User , dispatch_uid = 'api.models' )
885918post_delete .connect (_purge_user_from_cm , sender = User , dispatch_uid = 'api.models' )
919+ # Log significant app-related events
920+ post_save .connect (_log_build_created , sender = Build , dispatch_uid = 'api.models' )
921+ post_save .connect (_log_release_created , sender = Release , dispatch_uid = 'api.models' )
922+ post_save .connect (_log_config_updated , sender = Config , dispatch_uid = 'api.models' )
0 commit comments