def api_set_endpoint(self, api_endpoint): """ Sets the API endpoint to use. default is random endpoint """ logging.debug(self.__class__.__name__ + ': ' + sys._getframe().f_code.co_name) logobj(api_endpoint) self.api_endpoint = api_endpoint
def api_set_timeout(self, timeout): """ Sets the API timeout to <timeout> seconds :param timeout: <timeout> seconds """ logging.debug(self.__class__.__name__ + ': ' + sys._getframe().f_code.co_name) logobj(timeout) self.api_timeout = timeout
def _api_get_client(self): """ Returns an instance of ecsclient.client.Client """ logging.debug(self.__class__.__name__ + ': ' + sys._getframe().f_code.co_name) url = "{0}://{1}:{2}".format(API_PROTOCOL, self.api_endpoint, API_PORT) logobj(url) return Client('3', username=self.ecs.get_root_user(), password=self.ecs.get_root_pass(), token_endpoint=url + '/login', ecs_endpoint=url, verify_ssl=self.api_verify_ssl, request_timeout=self.api_timeout)
def ping(conf, c, w, x): """ Check ECS Management API Endpoint(s) """ """ Ping ECS management API for connectivity :param conf: Click object containing the configuration :param c: continuous ping :param w: (with -c) seconds to wait between pings :param x: exit upon successful PONG :return: retval """ def do_ping(): if c: msg = " (CTRL-C to break)" else: msg = "" o("Pinging endpoint {}...{}".format(conf.api_endpoint, msg)) pinging = True while pinging is True: try: resp_dict = conf.api_client.user_info.whoami() if resp_dict is not None: if resp_dict['common_name'] is not None: o('PONG: api_endpoint={} username={} {}'.format( conf.api_endpoint, resp_dict['common_name'], conf.diag_dt_status_text())) if x: pinging = False else: raise ECSClientException( "Unexpected response from API") except requests.ConnectionError or httplib.HTTPException: o("FAIL: API service unavailable {}".format( conf.diag_dt_status_text())) try: del conf.api_client if not c: sys.exit(1) except AssertionError: if not c: sys.exit(1) except ECSClientException as e: if 'Connection refused' in e.message: o('WAIT: API service is not alive. This is likely temporary.' ) elif 'connection failed' in e.message: o('WAIT: API service is alive but ECS is not. This is likely temporary.' ) elif 'Invalid username or password' in e.message: o('WAIT: Invalid username or password. If ECS authsvc is bootstrapping, this is likely temporary.' ) elif 'Non-200' in e.message: o('WAIT: ECS API internal error. If ECS services are still bootstrapping, this is likely temporary.' ) elif 'Read timed out' in e.message: o('WAIT: ECS API timed out. If ECS services are still bootstrapping, this is likely temporary.' ) else: o('FAIL: Unexpected response from API client: {0}'.format( e)) if not c: raise if not c: pinging = False if c and pinging is True: time.sleep(w) vdc_list = conf.ecs.get_vdc_names() if vdc_list is not None: # o('vdc_list={}'.format(vdc_list)) if len(vdc_list) > 1: o('Pinging endpoints for {} VDCs:'.format(len(vdc_list))) for vdc in vdc_list: o('\t{}: {}'.format(vdc, conf.ecs.get_vdc_endpoint(vdc))) endpoint_list = [ conf.ecs.get_vdc_endpoint(vdc) for vdc in vdc_list ] logobj(endpoint_list) else: endpoint_list = [ conf.ecs.get_vdc_endpoint(conf.ecs.get_vdc_primary()) ] logobj(endpoint_list) for endpoint in endpoint_list: conf.api_set_endpoint(endpoint) conf.api_reset() do_ping() else: do_ping()
def ping(conf, c, w, x): """ Check ECS Management API Endpoint(s) """ """ Ping ECS management API for connectivity :param conf: Click object containing the configuration :param c: continuous ping :param w: (with -c) seconds to wait between pings :param x: exit upon successful PONG :return: retval """ def do_ping(): if c: msg = " (CTRL-C to break)" else: msg = "" o("Pinging endpoint {}...{}".format(conf.api_endpoint, msg)) pinging = True while pinging is True: try: resp_dict = conf.api_client.user_info.whoami() if resp_dict is not None: if resp_dict['common_name'] is not None: dt_status = conf.diag_dt_status() if x: if dt_status['status'] is True: pinging = False o('PONG: api_endpoint={} username={} {}'.format(conf.api_endpoint, resp_dict['common_name'], dt_status['text'])) else: pinging = True o('WAIT: api_endpoint={} username={} {}'.format(conf.api_endpoint, resp_dict['common_name'], dt_status['text'])) else: raise ECSClientException("Unexpected response from API") except requests.ConnectionError or httplib.HTTPException: dt_status = conf.diag_dt_status() o("FAIL: API service unavailable {}".format(dt_status['text'])) try: del conf.api_client if not c: sys.exit(1) except AssertionError: if not c: sys.exit(1) except ECSClientException as e: if 'Connection refused' in e.message: o('WAIT: API service is not alive. This is likely temporary.') elif 'connection failed' in e.message: o('WAIT: API service is alive but ECS is not. This is likely temporary.') elif 'Invalid username or password' in e.message: o('WAIT: Invalid username or password. If ECS authsvc is bootstrapping, this is likely temporary.') elif 'Non-200' in e.message: o('WAIT: ECS API internal error. If ECS services are still bootstrapping, this is likely temporary.') elif 'Read timed out' in e.message: o('WAIT: ECS API timed out. If ECS services are still bootstrapping, this is likely temporary.') else: o('FAIL: Unexpected response from API client: {0}'.format(e)) if not c: raise if not c: pinging = False if c and pinging is True: time.sleep(w) vdc_list = conf.ecs.get_vdc_names() if vdc_list is not None: # o('vdc_list={}'.format(vdc_list)) if len(vdc_list) > 1: o('Pinging endpoints for {} VDCs:'.format(len(vdc_list))) for vdc in vdc_list: o('\t{}: {}'.format(vdc, conf.ecs.get_vdc_endpoint(vdc))) endpoint_list = [conf.ecs.get_vdc_endpoint(vdc) for vdc in vdc_list] logobj(endpoint_list) else: endpoint_list = [conf.ecs.get_vdc_endpoint(conf.ecs.get_vdc_primary())] logobj(endpoint_list) for endpoint in endpoint_list: conf.api_set_endpoint(endpoint) conf.api_reset() do_ping() else: do_ping()