66
77from __future__ import unicode_literals
88import importlib
9+ import logging
910import os
1011import subprocess
1112
2627from utils import dict_diff
2728
2829
30+
31+ logger = logging .getLogger (__name__ )
32+
2933# import user-defined configuration management module
3034CM = importlib .import_module (settings .CM_MODULE )
3135
@@ -445,6 +449,11 @@ def run(self, command, **kwargs):
445449 return tasks .run_node .delay (self , command ).wait ()
446450
447451
452+ def log_event (app , msg , level = logging .INFO ):
453+ msg = "{}: {}" .format (app .id , msg )
454+ logger .log (level , msg )
455+
456+
448457@python_2_unicode_compatible
449458class App (UuidAuditedModel ):
450459 """
@@ -546,6 +555,7 @@ def run(self, command):
546555 'deis/slugrunner' ])
547556 env_args = ' ' .join (["-e '{k}={v}'" .format (** locals ())
548557 for k , v in release .config .values .items ()])
558+ log_event (self , "deis run '{}'" .format (command ))
549559 command = "sudo docker run {env_args} {docker_args} {command}" .format (** locals ())
550560 return node .run (command )
551561
@@ -559,6 +569,8 @@ def scale(self, app, structure, **kwargs):
559569 # increment new container nums off the most recent container
560570 all_containers = app .container_set .all ().order_by ('-created' )
561571 container_num = 1 if not all_containers else all_containers [0 ].num + 1
572+ msg = 'Containers scaled ' + ' ' .join (
573+ "{}={}" .format (k , v ) for k , v in requested_containers .items ())
562574 # iterate and scale by container type (web, worker, etc)
563575 changed = False
564576 for container_type in requested_containers .keys ():
@@ -593,6 +605,7 @@ def scale(self, app, structure, **kwargs):
593605 containers .append (c )
594606 container_num += 1
595607 diff -= 1
608+ log_event (app , msg )
596609 return changed
597610
598611 def balance (self , formation , ** kwargs ):
@@ -601,6 +614,7 @@ def balance(self, formation, **kwargs):
601614 # get the next container number (e.g. web.19)
602615 container_num = 1 if not all_containers else all_containers [0 ].num + 1
603616 changed = False
617+ app = None
604618 # iterate by unique container type
605619 for container_type in set ([c .type for c in all_containers ]):
606620 # map node container counts => { 2: [b3, b4], 3: [ b1, b2 ] }
@@ -638,6 +652,8 @@ def balance(self, formation, **kwargs):
638652 ct = len (n .container_set .filter (type = container_type ))
639653 n_map .setdefault (ct , []).append (n )
640654 changed = True
655+ if app :
656+ log_event (app , 'Containers balanced' )
641657 return changed
642658
643659
@@ -718,7 +734,7 @@ class Meta:
718734 unique_together = (('app' , 'uuid' ),)
719735
720736 def __str__ (self ):
721- return "{0}-{1}" .format (self .app .id , self .sha [8 : ])
737+ return "{0}-{1}" .format (self .app .id , self .sha [: 7 ])
722738
723739
724740@python_2_unicode_compatible
@@ -748,7 +764,7 @@ class Meta:
748764 unique_together = (('app' , 'uuid' ),)
749765
750766 def __str__ (self ):
751- return "{0}-{1}" .format (self .app .id , self .sha )
767+ return "{0}-{1}" .format (self .app .id , self .sha [: 7 ] )
752768
753769 @classmethod
754770 def push (cls , push ):
@@ -931,9 +947,30 @@ def _purge_user_from_cm(**kwargs):
931947 kwargs ['instance' ].purge ()
932948
933949
934- # use django signals to synchronize database updates with
935- # the configuration management backend
950+ def _log_build_created (** kwargs ):
951+ if kwargs .get ('created' ):
952+ build = kwargs ['instance' ]
953+ log_event (build .app , "Build {} created" .format (build ))
954+
955+
956+ def _log_release_created (** kwargs ):
957+ if kwargs .get ('created' ):
958+ release = kwargs ['instance' ]
959+ log_event (release .app , "Release {} created" .format (release ))
960+
961+
962+ def _log_config_updated (** kwargs ):
963+ config = kwargs ['instance' ]
964+ log_event (config .app , "Config {} updated" .format (config ))
965+
966+
967+ # Connect Django model signals
968+ # Sync database updates with the configuration management backend
936969post_save .connect (_publish_to_cm , sender = App , dispatch_uid = 'api.models' )
937970post_save .connect (_publish_to_cm , sender = Formation , dispatch_uid = 'api.models' )
938971post_save .connect (_publish_user_to_cm , sender = User , dispatch_uid = 'api.models' )
939972post_delete .connect (_purge_user_from_cm , sender = User , dispatch_uid = 'api.models' )
973+ # Log significant app-related events
974+ post_save .connect (_log_build_created , sender = Build , dispatch_uid = 'api.models' )
975+ post_save .connect (_log_release_created , sender = Release , dispatch_uid = 'api.models' )
976+ post_save .connect (_log_config_updated , sender = Config , dispatch_uid = 'api.models' )
0 commit comments