#NEWLINE# def do_bounce( bounce_func , drain_method , config , new_app_running , happy_new_tasks , old_app_live_happy_tasks , old_app_live_unhappy_tasks , old_app_draining_tasks , service , bounce_method , serviceinstance , cluster , instance , marathon_jobid , client , soa_dir ) : #NEWLINE# #INDENT# def log_bounce_action( line , level = 'debug' ) : #NEWLINE# #INDENT# return _log ( service = service , line = line , component = 'deploy' , level = level , cluster = cluster , instance = instance ) #NEWLINE# #UNINDENT# if any ( [ ( not new_app_running ) , old_app_live_happy_tasks . keys ( ) ] ) : #NEWLINE# #INDENT# log_bounce_action ( line = ' ' . join ( [ ( '%s bounce in progress on %s.' % ( bounce_method , serviceinstance ) ) , ( 'New marathon app %s %s.' % ( marathon_jobid , ( 'exists' if new_app_running else 'not created yet' ) ) ) , ( '%d new tasks to bring up.' % ( config [ 'instances' ] - len ( happy_new_tasks ) ) ) , ( '%d old tasks receiving traffic and happy.' % len ( bounce_lib . flatten_tasks ( old_app_live_happy_tasks ) ) ) , ( '%d old tasks unhappy.' % len ( bounce_lib . flatten_tasks ( old_app_live_unhappy_tasks ) ) ) , ( '%d old tasks draining.' % len ( bounce_lib . flatten_tasks ( old_app_draining_tasks ) ) ) , ( '%d old apps.' % len ( old_app_live_happy_tasks . keys ( ) ) ) ] ) , level = 'event' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# send_sensu_bounce_keepalive ( service = service , instance = instance , cluster = cluster , soa_dir = soa_dir ) #NEWLINE# #UNINDENT# all_draining_tasks = set ( ) #NEWLINE# actions = bounce_func ( new_config = config , new_app_running = new_app_running , happy_new_tasks = happy_new_tasks , old_app_live_happy_tasks = old_app_live_happy_tasks , old_app_live_unhappy_tasks = old_app_live_unhappy_tasks ) #NEWLINE# if ( actions [ 'create_app' ] and ( not all_draining_tasks ) ) : #NEWLINE# #INDENT# log_bounce_action ( line = ( '%s bounce creating new app with app_id %s' % ( bounce_method , marathon_jobid ) ) ) #NEWLINE# bounce_lib . create_marathon_app ( marathon_jobid , config , client ) #NEWLINE# #UNINDENT# if ( len ( actions [ 'tasks_to_drain' ] ) > 0 ) : #NEWLINE# #INDENT# tasks_to_drain_by_app_id = defaultdict ( set ) #NEWLINE# for task in actions [ 'tasks_to_drain' ] : #NEWLINE# #INDENT# tasks_to_drain_by_app_id [ task . app_id ] . add ( task ) #NEWLINE# #UNINDENT# for ( app_id , tasks ) in tasks_to_drain_by_app_id . items ( ) : #NEWLINE# #INDENT# log_bounce_action ( line = ( '%s bounce draining %d old tasks with app_id %s' % ( bounce_method , len ( tasks ) , app_id ) ) ) #NEWLINE# #UNINDENT# for task in actions [ 'tasks_to_drain' ] : #NEWLINE# #INDENT# all_draining_tasks . add ( task ) #NEWLINE# drain_method . drain ( task ) #NEWLINE# #UNINDENT# #UNINDENT# for ( app , tasks ) in old_app_draining_tasks . items ( ) : #NEWLINE# #INDENT# for task in tasks : #NEWLINE# #INDENT# all_draining_tasks . add ( task ) #NEWLINE# #UNINDENT# #UNINDENT# tasks_to_kill = set ( ) #NEWLINE# for task in all_draining_tasks : #NEWLINE# #INDENT# if drain_method . is_safe_to_kill ( task ) : #NEWLINE# #INDENT# tasks_to_kill . add ( task ) #NEWLINE# log_bounce_action ( line = ( '%s bounce killing drained task %s' % ( bounce_method , task . id ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# kill_given_tasks ( client = client , task_ids = [ task . id for task in tasks_to_kill ] , scale = True ) #NEWLINE# apps_to_kill = [ ] #NEWLINE# for app in old_app_live_happy_tasks . keys ( ) : #NEWLINE# #INDENT# if ( app != ( '/%s' % marathon_jobid ) ) : #NEWLINE# #INDENT# live_happy_tasks = old_app_live_happy_tasks [ app ] #NEWLINE# live_unhappy_tasks = old_app_live_unhappy_tasks [ app ] #NEWLINE# draining_tasks = old_app_draining_tasks [ app ] #NEWLINE# if ( 0 == len ( ( ( ( live_happy_tasks | live_unhappy_tasks ) | draining_tasks ) - tasks_to_kill ) ) ) : #NEWLINE# #INDENT# apps_to_kill . append ( app ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if apps_to_kill : #NEWLINE# #INDENT# log_bounce_action ( line = ( '%s bounce removing old unused apps with app_ids: %s' % ( bounce_method , ', ' . join ( apps_to_kill ) ) ) ) #NEWLINE# bounce_lib . kill_old_ids ( apps_to_kill , client ) #NEWLINE# #UNINDENT# all_old_tasks = set . union ( set ( ) , * old_app_live_happy_tasks . values ( ) ) #NEWLINE# all_old_tasks = set . union ( all_old_tasks , * old_app_live_unhappy_tasks . values ( ) ) #NEWLINE# all_old_tasks = set . union ( all_old_tasks , * old_app_draining_tasks . values ( ) ) #NEWLINE# if all ( [ ( apps_to_kill or tasks_to_kill ) , ( apps_to_kill == old_app_live_happy_tasks . keys ( ) ) , ( tasks_to_kill == all_old_tasks ) ] ) : #NEWLINE# #INDENT# log_bounce_action ( line = ( '%s bounce on %s finishing. Now running %s' % ( bounce_method , serviceinstance , marathon_jobid ) ) , level = 'event' )