def _create_session(self): """Creates a session with the VC host.""" delay = 1 while True: try: # Login and setup the session with the host for making # API calls self.vim = self._get_vim_object() session = self.vim.Login( self.vim.get_service_content().sessionManager, userName=self._host_username, password=self._host_password) # Terminate the earlier session, if possible ( For the sake of # preserving sessions as there is a limit to the number of # sessions we can have ) if self._session: try: self.vim.TerminateSession( self.vim.get_service_content().sessionManager, sessionId=[self._session.key]) except Exception: # This exception is something we can live with. It is # just an extra caution on our side. The session may # have been cleared. We could have made a call to # SessionIsActive, but that is an overhead because we # anyway would have to call TerminateSession. LOG.debug("TerminateSession failed", exc_info=True) self._session = session return except Exception: LOG.critical(_LC("Unable to connect to server at %(server)s, " "sleeping for %(seconds)s seconds"), { 'server': self._host_ip, 'seconds': delay }, exc_info=True) # exc_info logs the exception with the message time.sleep(delay) delay = min(2 * delay, 60)
def _create_session(self): """Creates a session with the VC/ESX host.""" delay = 1 while True: try: # Login and setup the session with the host for making # API calls self.vim = self._get_vim_object() session = self.vim.Login( self.vim.get_service_content().sessionManager, userName=self._host_username, password=self._host_password) # Terminate the earlier session, if possible ( For the sake of # preserving sessions as there is a limit to the number of # sessions we can have ) if self._session: try: self.vim.TerminateSession( self.vim.get_service_content().sessionManager, sessionId=[self._session.key]) except Exception: # This exception is something we can live with. It is # just an extra caution on our side. The session may # have been cleared. We could have made a call to # SessionIsActive, but that is an overhead because we # anyway would have to call TerminateSession. LOG.debug("TerminateSession failed", exc_info=True) self._session = session return except Exception: LOG.critical(_LC("Unable to connect to server at %(server)s, " "sleeping for %(seconds)s seconds"), {'server': self._host_ip, 'seconds': delay}, exc_info=True) # exc_info logs the exception with the message time.sleep(delay) delay = min(2 * delay, 60)
def __init__(self, init_only=None, v3mode=False): # TODO(cyeoh): bp v3-api-extension-framework. Currently load # all extensions but eventually should be able to exclude # based on a config file # TODO(oomichi): We can remove v3mode argument after moving all v3 APIs # to v2.1. def _check_load_extension(ext): if (self.init_only is None or ext.obj.alias in self.init_only) and isinstance(ext.obj, extensions.V3APIExtensionBase): # Check whitelist is either empty or if not then the extension # is in the whitelist if (not CONF.osapi_v3.extensions_whitelist or ext.obj.alias in CONF.osapi_v3.extensions_whitelist): # Check the extension is not in the blacklist if ext.obj.alias not in CONF.osapi_v3.extensions_blacklist: return self._register_extension(ext) else: LOG.warning(_LW("Not loading %s because it is " "in the blacklist"), ext.obj.alias) return False else: LOG.warning( _LW("Not loading %s because it is not in the " "whitelist"), ext.obj.alias) return False else: return False if not CONF.osapi_v3.enabled: LOG.info(_LI("V3 API has been disabled by configuration")) return self.init_only = init_only LOG.debug("v3 API Extension Blacklist: %s", CONF.osapi_v3.extensions_blacklist) LOG.debug("v3 API Extension Whitelist: %s", CONF.osapi_v3.extensions_whitelist) in_blacklist_and_whitelist = set( CONF.osapi_v3.extensions_whitelist).intersection( CONF.osapi_v3.extensions_blacklist) if len(in_blacklist_and_whitelist) != 0: LOG.warning(_LW("Extensions in both blacklist and whitelist: %s"), list(in_blacklist_and_whitelist)) self.api_extension_manager = stevedore.enabled.EnabledExtensionManager( namespace=self.api_extension_namespace(), check_func=_check_load_extension, invoke_on_load=True, invoke_kwds={"extension_info": self.loaded_extension_info}) if v3mode: mapper = PlainMapper() else: mapper = ProjectMapper() self.resources = {} # NOTE(cyeoh) Core API support is rewritten as extensions # but conceptually still have core if list(self.api_extension_manager): # NOTE(cyeoh): Stevedore raises an exception if there are # no plugins detected. I wonder if this is a bug. self._register_resources_check_inherits(mapper) self.api_extension_manager.map(self._register_controllers) missing_core_extensions = self.get_missing_core_extensions( self.loaded_extension_info.get_extensions().keys()) if not self.init_only and missing_core_extensions: LOG.critical(_LC("Missing core API extensions: %s"), missing_core_extensions) raise exception.CoreAPIMissing( missing_apis=missing_core_extensions) super(APIRouterV21, self).__init__(mapper)
def __init__(self, init_only=None): # TODO(cyeoh): bp v3-api-extension-framework. Currently load # all extensions but eventually should be able to exclude # based on a config file def _check_load_extension(ext): if (self.init_only is None or ext.obj.alias in self.init_only) and isinstance( ext.obj, extensions.V21APIExtensionBase): # Check whitelist is either empty or if not then the extension # is in the whitelist if (not CONF.osapi_v21.extensions_whitelist or ext.obj.alias in CONF.osapi_v21.extensions_whitelist): # Check the extension is not in the blacklist blacklist = CONF.osapi_v21.extensions_blacklist if ext.obj.alias not in blacklist: return self._register_extension(ext) return False if (CONF.osapi_v21.extensions_blacklist or CONF.osapi_v21.extensions_whitelist): LOG.warning( _LW('In the M release you must run all of the API. ' 'The concept of API extensions will be removed from ' 'the codebase to ensure there is a single Compute API.')) self.init_only = init_only LOG.debug("v21 API Extension Blacklist: %s", CONF.osapi_v21.extensions_blacklist) LOG.debug("v21 API Extension Whitelist: %s", CONF.osapi_v21.extensions_whitelist) in_blacklist_and_whitelist = set( CONF.osapi_v21.extensions_whitelist).intersection( CONF.osapi_v21.extensions_blacklist) if len(in_blacklist_and_whitelist) != 0: LOG.warning(_LW("Extensions in both blacklist and whitelist: %s"), list(in_blacklist_and_whitelist)) self.api_extension_manager = stevedore.enabled.EnabledExtensionManager( namespace=self.api_extension_namespace(), check_func=_check_load_extension, invoke_on_load=True, invoke_kwds={"extension_info": self.loaded_extension_info}) mapper = ProjectMapper() self.resources = {} # NOTE(cyeoh) Core API support is rewritten as extensions # but conceptually still have core if list(self.api_extension_manager): # NOTE(cyeoh): Stevedore raises an exception if there are # no plugins detected. I wonder if this is a bug. self._register_resources_check_inherits(mapper) self.api_extension_manager.map(self._register_controllers) missing_core_extensions = self.get_missing_core_extensions( self.loaded_extension_info.get_extensions().keys()) if not self.init_only and missing_core_extensions: LOG.critical(_LC("Missing core API extensions: %s"), missing_core_extensions) raise exception.CoreAPIMissing( missing_apis=missing_core_extensions) LOG.info(_LI("Loaded extensions: %s"), sorted(self.loaded_extension_info.get_extensions().keys())) super(APIRouterV21, self).__init__(mapper)
def _call_method(self, module, method, *args, **kwargs): """Calls a method within the module specified with args provided. """ args = list(args) retry_count = 0 while True: exc_info = False try: if not self._is_vim_object(module): # If it is not the first try, then get the latest # vim object if retry_count > 0: args = args[1:] args = [self.vim] + args retry_count += 1 temp_module = module for method_elem in method.split("."): temp_module = getattr(temp_module, method_elem) return temp_module(*args, **kwargs) except error_util.VimFaultException as excep: # If it is a Session Fault Exception, it may point # to a session gone bad. So we try re-creating a session # and then proceeding ahead with the call. exc_info = sys.exc_info() if error_util.NOT_AUTHENTICATED in excep.fault_list: # Because of the idle session returning an empty # RetrievePropertiesResponse and also the same is returned # when there is say empty answer to the query for # VMs on the host ( as in no VMs on the host), we have no # way to differentiate. We thus check if the session is # active if self._session_is_active(): return [] LOG.warning(_("Session %s is inactive!"), self._session.key) self._create_session() else: # No re-trying for errors for API call has gone through # and is the caller's fault. Caller should handle these # errors. e.g, InvalidArgument fault. # Raise specific exceptions here if possible if excep.fault_list: fault = excep.fault_list[0] raise error_util.get_fault_class(fault)(str(excep)) break except error_util.SessionOverLoadException: # For exceptions which may come because of session overload, # we retry exc_info = sys.exc_info() except error_util.SessionConnectionException: # For exceptions with connections we create the session exc_info = sys.exc_info() self._create_session() except Exception: # If it is a proper exception, say not having furnished # proper data in the SOAP call or the retry limit having # exceeded, we raise the exception exc_info = sys.exc_info() break LOG.debug("_call_method(session=%(key)s) failed. " "Module: %(module)s. " "Method: %(method)s. " "args: %(args)s. " "kwargs: %(kwargs)s. " "Iteration: %(n)s. ", {'key': self._session.key, 'module': module, 'method': method, 'args': args, 'kwargs': kwargs, 'n': retry_count}, exc_info=exc_info) # If retry count has been reached then break and # raise the exception if retry_count > self._api_retry_count: break time.sleep(TIME_BETWEEN_API_CALL_RETRIES) LOG.critical(_LC("In vmwareapi: _call_method (session=%s)"), self._session.key, exc_info=True) raise
def __init__(self, init_only=None, v3mode=False): def _check_load_extension(ext): if (self.init_only is None or ext.obj.alias in self.init_only) and isinstance(ext.obj, extensions.V21APIExtensionBase): # Check whitelist is either empty or if not then the extension # is in the whitelist if (not CONF.osapi_v21.extensions_whitelist or ext.obj.alias in CONF.osapi_v21.extensions_whitelist): # Check the extension is not in the blacklist blacklist = CONF.osapi_v21.extensions_blacklist if ext.obj.alias not in blacklist: return self._register_extension(ext) return False ### CONF.osapi_v21.enabled: True if not CONF.osapi_v21.enabled: LOG.info(_LI("V2.1 API has been disabled by configuration")) LOG.warning(_LW("In the M release you must run the v2.1 API.")) return ### CONF.osapi_v21.extensions_blacklist: [] ### CONF.osapi_v21.extensions_whitelist: [] if (CONF.osapi_v21.extensions_blacklist or CONF.osapi_v21.extensions_whitelist): LOG.warning( _LW('In the M release you must run all of the API. ' 'The concept of API extensions will be removed from ' 'the codebase to ensure there is a single Compute API.')) ### self.init_only: None self.init_only = init_only LOG.debug("v21 API Extension Blacklist: %s", CONF.osapi_v21.extensions_blacklist) LOG.debug("v21 API Extension Whitelist: %s", CONF.osapi_v21.extensions_whitelist) ### CONF.osapi_v21.extensions_whitelist: [] ### CONF.osapi_v21.extensions_blacklist: [] ### 获取上述两个list的交集 in_blacklist_and_whitelist = set( CONF.osapi_v21.extensions_whitelist).intersection( CONF.osapi_v21.extensions_blacklist) if len(in_blacklist_and_whitelist) != 0: LOG.warning(_LW("Extensions in both blacklist and whitelist: %s"), list(in_blacklist_and_whitelist)) ### namespace: 'nova.api.v21.extensions' ### check_fun: <function _check_load_extension at 0x5907f50> ### self.loaded_extension_info: <nova.api.openstack.compute.extension_info.LoadedExtensionInfo object at 0x55b0150> self.api_extension_manager = stevedore.enabled.EnabledExtensionManager( namespace=self.api_extension_namespace(), check_func=_check_load_extension, invoke_on_load=True, invoke_kwds={"extension_info": self.loaded_extension_info}) ### v3mode: False if v3mode: mapper = PlainMapper() else: mapper = ProjectMapper() self.resources = {} ### self.api_extension_manager: <stevedore.enabled.EnabledExtensionManager object at 0x57a51d0> ### mapper: <nova.api.openstack.ProjectMapper object at 0x57a5210> if list(self.api_extension_manager): self._register_resources_check_inherits(mapper) self.api_extension_manager.map(self._register_controllers) missing_core_extensions = self.get_missing_core_extensions( self.loaded_extension_info.get_extensions().keys()) if not self.init_only and missing_core_extensions: LOG.critical(_LC("Missing core API extensions: %s"), missing_core_extensions) raise exception.CoreAPIMissing( missing_apis=missing_core_extensions) LOG.info(_LI("Loaded extensions: %s"), sorted(self.loaded_extension_info.get_extensions().keys())) super(APIRouterV21, self).__init__(mapper)
def _call_method(self, module, method, *args, **kwargs): """Calls a method within the module specified with args provided. """ args = list(args) retry_count = 0 while True: exc_info = False try: if not self._is_vim_object(module): # If it is not the first try, then get the latest # vim object if retry_count > 0: args = args[1:] args = [self.vim] + args retry_count += 1 temp_module = module for method_elem in method.split("."): temp_module = getattr(temp_module, method_elem) return temp_module(*args, **kwargs) except error_util.VimFaultException as excep: # If it is a Session Fault Exception, it may point # to a session gone bad. So we try re-creating a session # and then proceeding ahead with the call. exc_info = sys.exc_info() if error_util.NOT_AUTHENTICATED in excep.fault_list: # Because of the idle session returning an empty # RetrievePropertiesResponse and also the same is returned # when there is say empty answer to the query for # VMs on the host ( as in no VMs on the host), we have no # way to differentiate. We thus check if the session is # active if self._session_is_active(): return [] LOG.warning(_("Session %s is inactive!"), self._session.key) self._create_session() else: # No re-trying for errors for API call has gone through # and is the caller's fault. Caller should handle these # errors. e.g, InvalidArgument fault. # Raise specific exceptions here if possible if excep.fault_list: fault = excep.fault_list[0] msg_excep = six.text_type(excep) raise error_util.get_fault_class(fault)(msg_excep) break except error_util.SessionOverLoadException: # For exceptions which may come because of session overload, # we retry exc_info = sys.exc_info() except error_util.SessionConnectionException: # For exceptions with connections we create the session exc_info = sys.exc_info() self._create_session() except Exception: # If it is a proper exception, say not having furnished # proper data in the SOAP call or the retry limit having # exceeded, we raise the exception exc_info = sys.exc_info() break LOG.debug("_call_method(session=%(key)s) failed. " "Module: %(module)s. " "Method: %(method)s. " "args: %(args)s. " "kwargs: %(kwargs)s. " "Iteration: %(n)s. ", {'key': self._session.key, 'module': module, 'method': method, 'args': args, 'kwargs': kwargs, 'n': retry_count}, exc_info=exc_info) # If retry count has been reached then break and # raise the exception if retry_count > self._api_retry_count: break time.sleep(TIME_BETWEEN_API_CALL_RETRIES) LOG.critical(_LC("In vmwareapi: _call_method (session=%s)"), self._session.key, exc_info=True) raise
def __init__(self, init_only=None): # TODO(cyeoh): bp v3-api-extension-framework. Currently load # all extensions but eventually should be able to exclude # based on a config file def _check_load_extension(ext): if (self.init_only is None or ext.obj.alias in self.init_only) and isinstance(ext.obj, extensions.V21APIExtensionBase): # Check whitelist is either empty or if not then the extension # is in the whitelist if (not CONF.osapi_v21.extensions_whitelist or ext.obj.alias in CONF.osapi_v21.extensions_whitelist): # Check the extension is not in the blacklist blacklist = CONF.osapi_v21.extensions_blacklist if ext.obj.alias not in blacklist: return self._register_extension(ext) return False if (CONF.osapi_v21.extensions_blacklist or CONF.osapi_v21.extensions_whitelist): LOG.warning( _LW('In the M release you must run all of the API. ' 'The concept of API extensions will be removed from ' 'the codebase to ensure there is a single Compute API.')) self.init_only = init_only LOG.debug("v21 API Extension Blacklist: %s", CONF.osapi_v21.extensions_blacklist) LOG.debug("v21 API Extension Whitelist: %s", CONF.osapi_v21.extensions_whitelist) in_blacklist_and_whitelist = set( CONF.osapi_v21.extensions_whitelist).intersection( CONF.osapi_v21.extensions_blacklist) if len(in_blacklist_and_whitelist) != 0: LOG.warning(_LW("Extensions in both blacklist and whitelist: %s"), list(in_blacklist_and_whitelist)) self.api_extension_manager = stevedore.enabled.EnabledExtensionManager( namespace=self.api_extension_namespace(), check_func=_check_load_extension, invoke_on_load=True, invoke_kwds={"extension_info": self.loaded_extension_info}) mapper = ProjectMapper() self.resources = {} # NOTE(cyeoh) Core API support is rewritten as extensions # but conceptually still have core if list(self.api_extension_manager): # NOTE(cyeoh): Stevedore raises an exception if there are # no plugins detected. I wonder if this is a bug. self._register_resources_check_inherits(mapper) self.api_extension_manager.map(self._register_controllers) missing_core_extensions = self.get_missing_core_extensions( self.loaded_extension_info.get_extensions().keys()) if not self.init_only and missing_core_extensions: LOG.critical(_LC("Missing core API extensions: %s"), missing_core_extensions) raise exception.CoreAPIMissing( missing_apis=missing_core_extensions) LOG.info(_LI("Loaded extensions: %s"), sorted(self.loaded_extension_info.get_extensions().keys())) super(APIRouterV21, self).__init__(mapper)