def __init__(self, plugin, args): self.cmdline = False if len(args) == 2 and PluginRunner._is_number(args[1]): try: fd = int(args[1]) self.tp = _transport.TransPort( socket.fromfd(fd, socket.AF_UNIX, socket.SOCK_STREAM)) #At this point we can return errors to the client, so we can #inform the client if the plug-in fails to create itself try: self.plugin = plugin() except Exception as e: exception_info = sys.exc_info() self.tp.send_error(0, -32099, 'Error instantiating plug-in ' + str(e)) raise exception_info[1], None, exception_info[2] except Exception: error(traceback.format_exc()) error('Plug-in exiting.') sys.exit(2) else: self.cmdline = True cmd_line_wrapper(plugin)
def cim_wrapper(*args, **kwargs): try: return method(*args, **kwargs) except LsmError as lsm: raise except CIMError as ce: error_code, desc = ce if error_code == 0: if "Socket error" in desc: if "Errno 111" in desc: raise LsmError(ErrorNumber.NETWORK_CONNREFUSED, "Connection refused") if "Errno 113" in desc: raise LsmError(ErrorNumber.NETWORK_HOSTDOWN, "Host is down") elif "SSL error" in desc: raise LsmError(ErrorNumber.TRANSPORT_COMMUNICATION, desc) elif "The web server returned a bad status line": raise LsmError(ErrorNumber.TRANSPORT_COMMUNICATION, desc) elif "HTTP error" in desc: raise LsmError(ErrorNumber.TRANSPORT_COMMUNICATION, desc) raise LsmError(ErrorNumber.PLUGIN_BUG, desc) except pywbem.cim_http.AuthError as ae: raise LsmError(ErrorNumber.PLUGIN_AUTH_FAILED, "Unauthorized user") except pywbem.cim_http.Error as te: raise LsmError(ErrorNumber.NETWORK_ERROR, str(te)) except Exception as e: error("Unexpected exception:\n" + traceback.format_exc()) raise LsmError(ErrorNumber.PLUGIN_BUG, str(e), traceback.format_exc())
def nstor_wrapper(*args, **kwargs): try: return method(*args, **kwargs) except LsmError: raise except Exception as e: error("Unexpected exception:\n" + traceback.format_exc()) raise LsmError(ErrorNumber.PLUGIN_BUG, str(e), traceback.format_exc())
def cim_wrapper(*args, **kwargs): try: return method(*args, **kwargs) except LsmError: raise except wbem.CIMError as ce: error_code = ce.args[0] desc = ce.args[1] if error_code == 0: if 'Socket error' in desc: if 'Errno 111' in desc: raise LsmError(ErrorNumber.NETWORK_CONNREFUSED, 'Connection refused') if 'Errno 113' in desc: raise LsmError(ErrorNumber.NETWORK_HOSTDOWN, 'Host is down') if 'Errno 104' in desc: raise LsmError(ErrorNumber.NETWORK_CONNREFUSED, 'Connection reset by peer') # We know we have a socket error of some sort, lets # report a generic network error with the string from the # library. raise LsmError(ErrorNumber.NETWORK_ERROR, str(ce)) elif 'SSL error' in desc: raise LsmError(ErrorNumber.TRANSPORT_COMMUNICATION, desc) elif 'The web server returned a bad status line': raise LsmError(ErrorNumber.TRANSPORT_COMMUNICATION, desc) elif 'HTTP error' in desc: raise LsmError(ErrorNumber.TRANSPORT_COMMUNICATION, desc) raise LsmError(ErrorNumber.PLUGIN_BUG, desc) except AuthError: raise LsmError(ErrorNumber.PLUGIN_AUTH_FAILED, "Unauthorized user") except Error as te: raise LsmError(ErrorNumber.NETWORK_ERROR, str(te)) except Exception as e: error("Unexpected exception:\n" + traceback.format_exc()) raise LsmError(ErrorNumber.PLUGIN_BUG, str(e), traceback.format_exc())
def pools(self, search_key=None, search_value=None, flags=0): pools_list = self._request("get_all_names", "volume", [""]) pools = [] for pool in pools_list: try: if pool == 'syspool': continue pool_info = self._request("get_child_props", "volume", [str(pool), ""]) pools.append( Pool( pool_info['name'], pool_info['name'], Pool.ELEMENT_TYPE_VOLUME | Pool.ELEMENT_TYPE_VOLUME_THIN | Pool.ELEMENT_TYPE_FS, 0, NexentaStor._to_bytes(pool_info['size']), NexentaStor._to_bytes(pool_info['free']), Pool.STATUS_UNKNOWN, '', self.system.id)) except LsmError as e: error('nstor:pools: %s' % str(e)) pass return search_property(pools, search_key, search_value)
def run(self): # Don't need to invoke this when running stand alone as a cmdline if self.cmdline: return need_shutdown = False msg_id = 0 try: while True: try: # result = None msg = self.tp.read_req() method = msg['method'] msg_id = msg['id'] params = msg['params'] # Check to see if this plug-in implements this operation # if not return the expected error. if hasattr(self.plugin, method): if params is None: result = getattr(self.plugin, method)() else: result = getattr(self.plugin, method)(**msg['params']) else: raise LsmError(ErrorNumber.NO_SUPPORT, "Unsupported operation") self.tp.send_resp(result) if method == 'plugin_register': need_shutdown = True if method == 'plugin_unregister': # This is a graceful plugin_unregister need_shutdown = False self.tp.close() break except ValueError as ve: error(traceback.format_exc()) self.tp.send_error(msg_id, -32700, str(ve)) except AttributeError as ae: error(traceback.format_exc()) self.tp.send_error(msg_id, -32601, str(ae)) except LsmError as lsm_err: self.tp.send_error(msg_id, lsm_err.code, lsm_err.msg, lsm_err.data) except _SocketEOF: # Client went away and didn't meet our expectations for protocol, # this error message should not be seen as it shouldn't be # occurring. if need_shutdown: error('Client went away, exiting plug-in') except socket.error as se: if se.errno == errno.EPIPE: error('Client went away, exiting plug-in') else: error("Unhandled exception in plug-in!\n" + traceback.format_exc()) except Exception: error("Unhandled exception in plug-in!\n" + traceback.format_exc()) try: self.tp.send_error(msg_id, ErrorNumber.PLUGIN_BUG, "Unhandled exception in plug-in", str(traceback.format_exc())) except Exception: pass finally: if need_shutdown: # Client wasn't nice, we will allow plug-in to cleanup self.plugin.plugin_unregister() sys.exit(2)
def run(self): #Don't need to invoke this when running stand alone as a cmdline if self.cmdline: return need_shutdown = False msg_id = 0 try: while True: try: #result = None msg = self.tp.read_req() method = msg['method'] msg_id = msg['id'] params = msg['params'] #Check to see if this plug-in implements this operation #if not return the expected error. if hasattr(self.plugin, method): if params is None: result = getattr(self.plugin, method)() else: result = getattr(self.plugin, method)( **msg['params']) else: raise LsmError(ErrorNumber.NO_SUPPORT, "Unsupported operation") self.tp.send_resp(result) if method == 'plugin_register': need_shutdown = True if method == 'plugin_unregister': #This is a graceful plugin_unregister need_shutdown = False self.tp.close() break except ValueError as ve: error(traceback.format_exc()) self.tp.send_error(msg_id, -32700, str(ve)) except AttributeError as ae: error(traceback.format_exc()) self.tp.send_error(msg_id, -32601, str(ae)) except LsmError as lsm_err: self.tp.send_error(msg_id, lsm_err.code, lsm_err.msg, lsm_err.data) except _SocketEOF: #Client went away and didn't meet our expectations for protocol, #this error message should not be seen as it shouldn't be occuring. if need_shutdown: error('Client went away, exiting plug-in') except Exception: error("Unhandled exception in plug-in!\n" + traceback.format_exc()) try: self.tp.send_error(msg_id, ErrorNumber.PLUGIN_BUG, "Unhandled exception in plug-in", str(traceback.format_exc())) except Exception: pass finally: if need_shutdown: #Client wasn't nice, we will allow plug-in to cleanup self.plugin.plugin_unregister() sys.exit(2)