def _wait_for_env_operation_finish(self, eb_client, env_name, original_request_id, pending_status, expected_health, operation_name, action_name, wait_timeout, poll_delay, include_deleted='false', initial_delay=0, quiet=False): ''' Loop polling environment status while it is in specified pending_status and/or health state, until status changes and/or health state meet expectation, or reach wait_timeout threshold. While polling retrieve events related to specified request_id or all recent events if not specified. ''' # Just return if not specify either pending status and health expectation if pending_status is None and expected_health is None: return if quiet: ori_prompt_level = prompt.get_level() prompt.set_level(OutputLevel.Quiet) prompt.action(BaseOpMessage.WaitForEnv.format(env_name, action_name)) prompt.info(BaseOpMessage.UserCanInterrupt) _time.sleep(initial_delay) # Wait before polling_start_time = _time.time() event_start_time = None if original_request_id is not None \ else misc.unixtime_to_utc(_time.time()) while _time.time() - polling_start_time < wait_timeout: # Retrieve related events log.info('Retrieving events for Environment "{0}" after UTC time {1}.'.\ format(env_name, event_start_time)) event_response = eb_client.describe_events( None, env_name, request_id=original_request_id, start_time=event_start_time) self._log_api_result(operation_name, 'DescribeEvents', event_response.result) # Output events related to environment launch if len(event_response.result) > 0: # Having new events event_response.result.reverse() for event in event_response.result: log.info('{0}\t{1}\t{2}'.format\ (event.event_date, event.severity, event.message)) prompt.plain('{0}\t{1}\t{2}'.format\ (event.event_date, event.severity, event.message)) event_start_time = misc.unixtime_to_utc( event.event_date_raw + 0.001) # else: # prompt.action(BaseOpMessage.Running) # Describe environment status env_response = eb_client.describe_environments( environment_names=env_name, include_deleted=include_deleted) if len(env_response.result) < 1: raise EnvironmentNotExistError( BaseOpMessage.EnvNotExist.format(env_name)) if pending_status is None: # No specified pending status if expected_health is not None \ and env_response.result[0].health.lower() == expected_health.lower(): # Meet with expected health, stop polling break else: # Has specified pending status if env_response.result[0].status.lower( ) != pending_status.lower(): # Not in pending status if expected_health is None: # No expected health, stop polling break elif env_response.result[0].health.lower( ) == expected_health.lower(): # Meet with expected health, stop polling break log.info('Received response for DescribeEnvironemnts call.') self._log_api_result(operation_name, 'DescribeEnvironments', env_response.result) _time.sleep(poll_delay) else: log.error('Breach timeout threshold of waiting environment {0}.'.\ format(action_name)) if quiet: prompt.set_level(ori_prompt_level) return env_response.result
def _wait_for_env_operation_finish(self, eb_client, env_name, original_request_id, pending_status, expected_health, operation_name, action_name, wait_timeout, poll_delay, include_deleted = u'false', initial_delay = 0, quiet = False ): ''' Loop polling environment status while it is in specified pending_status and/or health state, until status changes and/or health state meet expectation, or reach wait_timeout threshold. While polling retrieve events related to specified request_id or all recent events if not specified. ''' # Just return if not specify either pending status and health expectation if pending_status is None and expected_health is None: return if quiet: ori_prompt_level = prompt.get_level() prompt.set_level(OutputLevel.Quiet) prompt.action(BaseOpMessage.WaitForEnv.format(env_name, action_name)) prompt.info(BaseOpMessage.UserCanInterrupt) _time.sleep(initial_delay) # Wait before polling_start_time = _time.time() event_start_time = None if original_request_id is not None \ else misc.unixtime_to_utc(_time.time()) while _time.time() - polling_start_time < wait_timeout: # Retrieve related events log.info(u'Retrieving events for Environment "{0}" after UTC time {1}.'.\ format(env_name, event_start_time)) event_response = eb_client.describe_events(None, env_name, request_id = original_request_id, start_time = event_start_time) self._log_api_result(operation_name, u'DescribeEvents', event_response.result) # Output events related to environment launch if len(event_response.result) > 0: # Having new events event_response.result.reverse() for event in event_response.result: log.info(u'{0}\t{1}\t{2}'.format\ (event.event_date, event.severity, event.message)) prompt.plain(u'{0}\t{1}\t{2}'.format\ (event.event_date, event.severity, event.message)) event_start_time = misc.unixtime_to_utc(event.event_date_raw + 0.001) # else: # prompt.action(BaseOpMessage.Running) # Describe environment status env_response = eb_client.describe_environments(environment_names = env_name, include_deleted = include_deleted) if len(env_response.result) < 1: raise EnvironmentNotExistError(BaseOpMessage.EnvNotExist.format(env_name)) if pending_status is None: # No specified pending status if expected_health is not None \ and env_response.result[0].health.lower() == expected_health.lower(): # Meet with expected health, stop polling break; else: # Has specified pending status if env_response.result[0].status.lower() != pending_status.lower(): # Not in pending status if expected_health is None: # No expected health, stop polling break; elif env_response.result[0].health.lower() == expected_health.lower(): # Meet with expected health, stop polling break; log.info(u'Received response for DescribeEnvironemnts call.') self._log_api_result(operation_name, u'DescribeEnvironments', env_response.result) _time.sleep(poll_delay) else: log.error(u'Breach timeout threshold of waiting environment {0}.'.\ format(action_name)) if quiet: prompt.set_level(ori_prompt_level) return env_response.result