def update_progress(self): """Updates progress to lease. This call back to the lease is essential to keep the lease alive across long running read operations. :raises: VimException, VimFaultException, VimAttributeException, VimSessionOverLoadException, VimConnectionException """ percent = int(float(self._bytes_read) / self._vmdk_size * 100) LOG.debug( "Calling VIM API to update read progress of VMDK file" " with URL = %(url)s to %(percent)d%%.", { 'url': self._url, 'percent': percent }) try: self._session.invoke_api(self._session.vim, 'HttpNfcLeaseProgress', self._lease, percent=percent) LOG.debug( "Updated read progress of VMDK file with " "URL = %(url)s to %(percent)d%%.", { 'url': self._url, 'percent': percent }) except exceptions.VimException: with excutils.save_and_reraise_exception(): LOG.exception( _LE("Error occurred while updating the " "read progress of VMDK file with URL = %s."), self._url)
def update_progress(self): """Updates progress to lease. This call back to the lease is essential to keep the lease alive across long running read operations. :raises: VimException, VimFaultException, VimAttributeException, VimSessionOverLoadException, VimConnectionException """ percent = int(float(self._bytes_read) / self._vmdk_size * 100) LOG.debug("Calling VIM API to update read progress of VMDK file" " with URL = %(url)s to %(percent)d%%.", {'url': self._url, 'percent': percent}) try: self._session.invoke_api(self._session.vim, 'HttpNfcLeaseProgress', self._lease, percent=percent) LOG.debug("Updated read progress of VMDK file with " "URL = %(url)s to %(percent)d%%.", {'url': self._url, 'percent': percent}) except exceptions.VimException: with excutils.save_and_reraise_exception(): LOG.exception(_LE("Error occurred while updating the " "read progress of VMDK file with URL = %s."), self._url)
def _func(*args, **kwargs): func_name = f.__name__ result = None try: LOG.debug( "Invoking %(func_name)s; retry count is " "%(retry_count)d.", { 'func_name': func_name, 'retry_count': self._retry_count }) result = f(*args, **kwargs) LOG.debug( "Function %(func_name)s returned successfully " "after %(retry_count)d retries.", { 'func_name': func_name, 'retry_count': self._retry_count }) except self._exceptions: with excutils.save_and_reraise_exception() as ctxt: LOG.warn(_LW("Exception which is in the suggested list of " "exceptions occurred while invoking function:" " %s."), func_name, exc_info=True) if (self._max_retry_count != -1 and self._retry_count >= self._max_retry_count): LOG.error( _LE("Cannot retry upon suggested exception " "since retry count (%(retry_count)d) " "reached max retry count " "(%(max_retry_count)d)."), { 'retry_count': self._retry_count, 'max_retry_count': self._max_retry_count }) else: ctxt.reraise = False self._retry_count += 1 self._sleep_time += self._inc_sleep_time return self._sleep_time except Exception: with excutils.save_and_reraise_exception(): LOG.exception( _LE("Exception which is not in the " "suggested list of exceptions occurred " "while invoking %s."), func_name) raise loopingcall.LoopingCallDone(result)
def _invoke_api(module, method, *args, **kwargs): LOG.debug("Invoking method %(module)s.%(method)s.", {'module': module, 'method': method}) try: api_method = getattr(module, method) return api_method(*args, **kwargs) except exceptions.VimFaultException as excep: # If this is due to an inactive session, we should re-create # the session and retry. if exceptions.NOT_AUTHENTICATED in excep.fault_list: # The NotAuthenticated fault is set by the fault checker # due to an empty response. An empty response could be a # valid response; for e.g., response for the query to # return the VMs in an ESX server which has no VMs in it. # Also, the server responds with an empty response in the # case of an inactive session. Therefore, we need a way to # differentiate between these two cases. if self._is_current_session_active(): LOG.debug("Returning empty response for " "%(module)s.%(method)s invocation.", {'module': module, 'method': method}) return [] else: # empty response is due to an inactive session excep_msg = ( _("Current session: %(session)s is inactive; " "re-creating the session while invoking " "method %(module)s.%(method)s.") % {'session': _trunc_id(self._session_id), 'module': module, 'method': method}) LOG.warn(excep_msg, exc_info=True) self._create_session() raise exceptions.VimConnectionException(excep_msg, excep) else: # no need to retry for other VIM faults like # InvalidArgument # Raise specific exceptions here if possible if excep.fault_list: LOG.debug("Fault list: %s", excep.fault_list) fault = excep.fault_list[0] raise exceptions.get_fault_class(fault)(unicode(excep)) raise except exceptions.VimConnectionException: with excutils.save_and_reraise_exception(): # Re-create the session during connection exception. LOG.warn(_LW("Re-creating session due to connection " "problems while invoking method " "%(module)s.%(method)s."), {'module': module, 'method': method}, exc_info=True) self._create_session()
def _func(*args, **kwargs): func_name = f.__name__ result = None try: LOG.debug("Invoking %(func_name)s; retry count is " "%(retry_count)d.", {'func_name': func_name, 'retry_count': self._retry_count}) result = f(*args, **kwargs) LOG.debug("Function %(func_name)s returned successfully " "after %(retry_count)d retries.", {'func_name': func_name, 'retry_count': self._retry_count}) except self._exceptions: with excutils.save_and_reraise_exception() as ctxt: LOG.warn(_LW("Exception which is in the suggested list of " "exceptions occurred while invoking function:" " %s."), func_name, exc_info=True) if (self._max_retry_count != -1 and self._retry_count >= self._max_retry_count): LOG.error(_LE("Cannot retry upon suggested exception " "since retry count (%(retry_count)d) " "reached max retry count " "(%(max_retry_count)d)."), {'retry_count': self._retry_count, 'max_retry_count': self._max_retry_count}) else: ctxt.reraise = False self._retry_count += 1 self._sleep_time += self._inc_sleep_time return self._sleep_time except Exception: with excutils.save_and_reraise_exception(): LOG.exception(_LE("Exception which is not in the " "suggested list of exceptions occurred " "while invoking %s."), func_name) raise loopingcall.LoopingCallDone(result)
def _poll_lease(self, lease): """Poll the state of the given lease. When the lease is ready, the event (param done) is notified. In case of any error, appropriate exception is set in the event. :param lease: lease whose state is to be polled """ LOG.debug("Invoking VIM API to read state of lease: %s.", lease) try: state = self.invoke_api(vim_util, 'get_object_property', self.vim, lease, 'state') except exceptions.VimException: with excutils.save_and_reraise_exception(): LOG.exception( _LE("Error occurred while checking " "state of lease: %s."), lease) else: if state == 'ready': LOG.debug("Lease: %s is ready.", lease) raise loopingcall.LoopingCallDone() elif state == 'initializing': LOG.debug("Lease: %s is initializing.", lease) elif state == 'error': LOG.debug("Invoking VIM API to read lease: %s error.", lease) error_msg = self._get_error_message(lease) excep_msg = _("Lease: %(lease)s is in error state. Details: " "%(error_msg)s.") % { 'lease': lease, 'error_msg': error_msg } LOG.error(excep_msg) raise exceptions.VimException(excep_msg) else: # unknown state excep_msg = _("Unknown state: %(state)s for lease: " "%(lease)s.") % { 'state': state, 'lease': lease } LOG.error(excep_msg) raise exceptions.VimException(excep_msg)
def _poll_lease(self, lease): """Poll the state of the given lease. When the lease is ready, the event (param done) is notified. In case of any error, appropriate exception is set in the event. :param lease: lease whose state is to be polled """ LOG.debug("Invoking VIM API to read state of lease: %s.", lease) try: state = self.invoke_api(vim_util, 'get_object_property', self.vim, lease, 'state') except exceptions.VimException: with excutils.save_and_reraise_exception(): LOG.exception(_LE("Error occurred while checking " "state of lease: %s."), lease) else: if state == 'ready': LOG.debug("Lease: %s is ready.", lease) raise loopingcall.LoopingCallDone() elif state == 'initializing': LOG.debug("Lease: %s is initializing.", lease) elif state == 'error': LOG.debug("Invoking VIM API to read lease: %s error.", lease) error_msg = self._get_error_message(lease) excep_msg = _("Lease: %(lease)s is in error state. Details: " "%(error_msg)s.") % {'lease': lease, 'error_msg': error_msg} LOG.error(excep_msg) raise exceptions.VimException(excep_msg) else: # unknown state excep_msg = _("Unknown state: %(state)s for lease: " "%(lease)s.") % {'state': state, 'lease': lease} LOG.error(excep_msg) raise exceptions.VimException(excep_msg)
def _poll_task(self, task): """Poll the given task until completion. If the task completes successfully, the method returns the task info using the input event (param done). In case of any error, appropriate exception is set in the event. :param task: managed object reference of the task """ LOG.debug("Invoking VIM API to read info of task: %s.", task) try: task_info = self.invoke_api(vim_util, 'get_object_property', self.vim, task, 'info') except exceptions.VimException: with excutils.save_and_reraise_exception(): LOG.exception(_("Error occurred while reading info of " "task: %s."), task) else: if task_info.state in ['queued', 'running']: if hasattr(task_info, 'progress'): LOG.debug("Task: %(task)s progress is %(progress)s%%.", {'task': task, 'progress': task_info.progress}) elif task_info.state == 'success': LOG.debug("Task: %s status is success.", task) raise loopingcall.LoopingCallDone(task_info) else: error_msg = six.text_type(task_info.error.localizedMessage) excep_msg = _("Task: %(task)s failed with error: " "%(error)s.") % {'task': task, 'error': error_msg} LOG.error(excep_msg) error = task_info.error name = error.fault.__class__.__name__ task_ex = exceptions.get_fault_class(name)(error_msg) raise task_ex
def _poll_task(self, task): """Poll the given task until completion. If the task completes successfully, the method returns the task info using the input event (param done). In case of any error, appropriate exception is set in the event. :param task: managed object reference of the task """ LOG.debug("Invoking VIM API to read info of task: %s.", task) try: task_info = self.invoke_api(vim_util, 'get_object_property', self.vim, task, 'info') except exceptions.VimException: with excutils.save_and_reraise_exception(): LOG.exception( _LE("Error occurred while reading info of " "task: %s."), task) else: if task_info.state in ['queued', 'running']: if hasattr(task_info, 'progress'): LOG.debug("Task: %(task)s progress is %(progress)s%%.", { 'task': task, 'progress': task_info.progress }) elif task_info.state == 'success': LOG.debug("Task: %s status is success.", task) raise loopingcall.LoopingCallDone(task_info) else: error_msg = six.text_type(task_info.error.localizedMessage) excep_msg = _("Task: %(task)s failed with error: " "%(error)s.") % { 'task': task, 'error': error_msg } LOG.error(excep_msg) error = task_info.error name = error.fault.__class__.__name__ task_ex = exceptions.get_fault_class(name)(error_msg) raise task_ex
def _invoke_api(module, method, *args, **kwargs): LOG.debug("Invoking method %(module)s.%(method)s.", { 'module': module, 'method': method }) try: api_method = getattr(module, method) return api_method(*args, **kwargs) except exceptions.VimFaultException as excep: # If this is due to an inactive session, we should re-create # the session and retry. if exceptions.NOT_AUTHENTICATED in excep.fault_list: # The NotAuthenticated fault is set by the fault checker # due to an empty response. An empty response could be a # valid response; for e.g., response for the query to # return the VMs in an ESX server which has no VMs in it. # Also, the server responds with an empty response in the # case of an inactive session. Therefore, we need a way to # differentiate between these two cases. if self._is_current_session_active(): LOG.debug( "Returning empty response for " "%(module)s.%(method)s invocation.", { 'module': module, 'method': method }) return [] else: # empty response is due to an inactive session excep_msg = ( _("Current session: %(session)s is inactive; " "re-creating the session while invoking " "method %(module)s.%(method)s.") % { 'session': _trunc_id(self._session_id), 'module': module, 'method': method }) LOG.warn(excep_msg, exc_info=True) self._create_session() raise exceptions.VimConnectionException( excep_msg, excep) else: # no need to retry for other VIM faults like # InvalidArgument # Raise specific exceptions here if possible if excep.fault_list: LOG.debug("Fault list: %s", excep.fault_list) raise exceptions.get_fault_class(excep.fault_list[0]) raise except exceptions.VimConnectionException: with excutils.save_and_reraise_exception(): # Re-create the session during connection exception. LOG.warn(_LW("Re-creating session due to connection " "problems while invoking method " "%(module)s.%(method)s."), { 'module': module, 'method': method }, exc_info=True) self._create_session()