From f96f9e3c25f023977bb978c0a54d25d62656a89f Mon Sep 17 00:00:00 2001 From: Mark Syms Date: Mon, 5 Oct 2020 09:42:02 +0100 Subject: [PATCH] Address some flake8 issues Signed-off-by: Mark Syms --- drivers/SR.py | 113 +++++++++++++++++++++++-------------------- drivers/SRCommand.py | 108 ++++++++++++++++++++++------------------- 2 files changed, 118 insertions(+), 103 deletions(-) diff --git a/drivers/SR.py b/drivers/SR.py index f12e783f1..65a9c3c5e 100755 --- a/drivers/SR.py +++ b/drivers/SR.py @@ -2,13 +2,13 @@ # # Copyright (C) Citrix Systems Inc. # -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published # by the Free Software Foundation; version 2.1 only. # -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License @@ -22,8 +22,11 @@ import xml.dom.minidom import errno import xs_errors -import XenAPI, xmlrpclib, util -import copy, os +import XenAPI +import xmlrpclib +import util +import copy +import os import traceback MOUNT_BASE = '/var/run/sr-mount' @@ -34,28 +37,35 @@ # LUN per VDI key for XenCenter LUNPERVDI = "LUNperVDI" + class SRException(Exception): """Exception raised by storage repository operations""" errno = errno.EINVAL + def __init__(self, reason): Exception.__init__(self, reason) def toxml(self): return xmlrpclib.dumps(xmlrpclib.Fault(int(self.errno), str(self)), "", True) + class SROSError(SRException): """Wrapper for OSError""" def __init__(self, errno, reason): self.errno = errno Exception.__init__(self, reason) + backends = [] + + def registerSR(SRClass): - """Register SR with handler. All SR subclasses should call this in + """Register SR with handler. All SR subclasses should call this in the module file """ backends.append(SRClass) + def driver(type): """Find the SR for the given dconf string""" for d in backends: @@ -63,6 +73,7 @@ def driver(type): return d raise xs_errors.XenError('SRUnknownType') + class SR(object): """Semi-abstract storage repository object. @@ -82,7 +93,7 @@ def handles(type): handles = staticmethod(handles) def __init__(self, srcmd, sr_uuid): - """Base class initializer. All subclasses should call SR.__init__ + """Base class initializer. All subclasses should call SR.__init__ in their own initializers. @@ -108,7 +119,7 @@ def __init__(self, srcmd, sr_uuid): self.sr_ref = self.srcmd.params.get('sr_ref') - if 'device_config' in self.srcmd.params: + if 'device_config' in self.srcmd.params: if self.dconf.get("SRmaster") == "true": os.environ['LVM_SYSTEM_DIR'] = MASTER_LVM_CONF @@ -185,20 +196,20 @@ def from_uuid(session, sr_uuid): from SRCommand import SRCommand cmd = SRCommand(module.DRIVER_INFO) - cmd.dconf = device_config - cmd.params = { 'session_ref' : session._session, - 'host_ref' : host_ref, - 'device_config' : device_config, - 'sr_ref' : sr_ref, - 'sr_uuid' : sr_uuid, - 'command' : 'nop' } + cmd.dconf = device_config + cmd.params = {'session_ref': session._session, + 'host_ref': host_ref, + 'device_config': device_config, + 'sr_ref': sr_ref, + 'sr_uuid': sr_uuid, + 'command': 'nop'} return target(cmd, sr_uuid) def block_setscheduler(self, dev): try: realdev = os.path.realpath(dev) - disk = util.diskFromPartition(realdev) + disk = util.diskFromPartition(realdev) # the normal case: the sr default scheduler (typically noop), # potentially overridden by SR.other_config:scheduler @@ -221,7 +232,7 @@ def _addLUNperVDIkey(self): try: self.session.xenapi.SR.add_to_sm_config(self.sr_ref, LUNPERVDI, "true") except: - pass + pass def create(self, uuid, size): """Create this repository. @@ -243,7 +254,7 @@ def delete(self, uuid): This operation IS idempotent -- it will succeed if the repository exists and can be deleted or if the repository does not exist. The caller must ensure that all VDIs are deactivated and detached - and that the SR itself has been detached before delete(). + and that the SR itself has been detached before delete(). The call will FAIL if any VDIs in the SR are in use. Returns: @@ -263,9 +274,9 @@ def update(self, uuid): """ # no-op unless individual backends implement it return - + def attach(self, uuid): - """Initiate local access to the SR. Initialises any + """Initiate local access to the SR. Initialises any device state required to access the substrate. Idempotent. @@ -285,7 +296,7 @@ def after_master_attach(self, uuid): self.scan(uuid) def detach(self, uuid): - """Remove local access to the SR. Destroys any device + """Remove local access to the SR. Destroys any device state initiated by the sr_attach() operation. Idempotent. All VDIs must be detached in order for the operation @@ -337,11 +348,11 @@ def replay(self, uuid): SRUnimplementedMethod """ raise xs_errors.XenError('Unimplemented') - + def content_type(self, uuid): """Returns the 'content_type' of an SR as a string""" return xmlrpclib.dumps((str(self.sr_vditype),), "", True) - + def load(self, sr_uuid): """Post-init hook""" pass @@ -372,22 +383,22 @@ def _toxml(self): element = dom.createElement("sr") dom.appendChild(element) - #Add default uuid, physical_utilisation, physical_size and + # Add default uuid, physical_utilisation, physical_size and # virtual_allocation entries - for attr in ('uuid', 'physical_utilisation', 'virtual_allocation', \ + for attr in ('uuid', 'physical_utilisation', 'virtual_allocation', 'physical_size'): try: aval = getattr(self, attr) except AttributeError: - raise xs_errors.XenError('InvalidArg', \ - opterr='Missing required field [%s]' % attr) + raise xs_errors.XenError( + 'InvalidArg', opterr='Missing required field [%s]' % attr) entry = dom.createElement(attr) - element.appendChild(entry) + element.appendChild(entry) textnode = dom.createTextNode(str(aval)) entry.appendChild(textnode) - #Add the default_vdi_visibility entry + # Add the default_vdi_visibility entry entry = dom.createElement('default_vdi_visibility') element.appendChild(entry) if not self.default_vdi_visibility: @@ -395,8 +406,8 @@ def _toxml(self): else: textnode = dom.createTextNode('True') entry.appendChild(textnode) - - #Add optional label and description entries + + # Add optional label and description entries for attr in ('label', 'description'): try: aval = getattr(self, attr) @@ -404,7 +415,7 @@ def _toxml(self): continue if aval: entry = dom.createElement(attr) - element.appendChild(entry) + element.appendChild(entry) textnode = dom.createTextNode(str(aval)) entry.appendChild(textnode) @@ -417,7 +428,7 @@ def _toxml(self): self.vdis[uuid]._toxml(dom, vdinode) return dom - + def _fromxml(self, str, tag): dom = xml.dom.minidom.parseString(str) objectlist = dom.getElementsByTagName(tag)[0] @@ -438,7 +449,7 @@ def _isvalidpathstring(self, path): continue elif char.isdigit(): continue - elif char in ['/','-','_','.',':']: + elif char in ['/', '-', '_', '.', ':']: continue else: return False @@ -465,7 +476,7 @@ def _mpathinit(self): if self.mpath != "true": self.mpath = "false" self.mpathhandle = "null" - + if not os.path.exists("/opt/xensource/sm/mpath_%s.py" % self.mpathhandle): raise IOError("File does not exist = %s" % self.mpathhandle) except: @@ -480,7 +491,7 @@ def _mpathHandle(self): else: self.mpathmodule.deactivate() - def _pathrefresh(self, obj, load = True): + def _pathrefresh(self, obj, load=True): SCSIid = getattr(self, 'SCSIid') self.dconf['device'] = self.mpathmodule.path(SCSIid) if load: @@ -493,7 +504,7 @@ def _setMultipathableFlag(self, SCSIid=''): self.session.xenapi.SR.set_sm_config(self.sr_ref, sm_config) if self.mpath == "true" and len(SCSIid): - cmd = ['/opt/xensource/sm/mpathcount.py',SCSIid] + cmd = ['/opt/xensource/sm/mpathcount.py', SCSIid] util.pread2(cmd) except: pass @@ -531,7 +542,7 @@ def __init__(self, sr): for vdi in sr.vdis.values(): # We initialise the sm_config field with the values from the database # The sm_config_overrides contains any new fields we want to add to - # sm_config, and also any field to delete (by virtue of having + # sm_config, and also any field to delete (by virtue of having # sm_config_overrides[key]=None) try: if not hasattr(vdi, "sm_config"): @@ -559,10 +570,10 @@ def __init__(self, sr): for location in existing: sm_vdi = self.get_sm_vdi(location) xenapi_vdi = self.get_xenapi_vdi(location) - sm_vdi.uuid = util.default(sm_vdi, "uuid", lambda: xenapi_vdi['uuid']) - + sm_vdi.uuid = util.default(sm_vdi, "uuid", lambda: xenapi_vdi['uuid']) + # Only consider those whose configuration looks different - self.existing = filter(lambda x:not(self.get_sm_vdi(x).in_sync_with_xenapi_record(self.get_xenapi_vdi(x))), existing) + self.existing = filter(lambda x: not(self.get_sm_vdi(x).in_sync_with_xenapi_record(self.get_xenapi_vdi(x))), existing) if len(self.new) != 0: util.SMlog("new VDIs on disk: " + repr(self.new)) @@ -578,7 +589,7 @@ def get_xenapi_vdi(self, location): return self.__xenapi_records[self.__xenapi_locations[location]] def all_xenapi_locations(self): - return set(self.__xenapi_locations.keys()) + return set(self.__xenapi_locations.keys()) def synchronise_new(self): """Add XenAPI records for new disks""" @@ -586,7 +597,7 @@ def synchronise_new(self): vdi = self.get_sm_vdi(location) util.SMlog("Introducing VDI with location=%s" % (vdi.location)) vdi._db_introduce() - + def synchronise_gone(self): """Delete XenAPI record for old disks""" for location in self.gone: @@ -596,24 +607,22 @@ def synchronise_gone(self): self.sr.forget_vdi(vdi['uuid']) except XenAPI.Failure as e: if util.isInvalidVDI(e): - util.SMlog("VDI %s not found, ignoring exception" \ - % vdi['uuid']) + util.SMlog("VDI %s not found, ignoring exception" % + vdi['uuid']) else: - raise + raise def synchronise_existing(self): """Update existing XenAPI records""" for location in self.existing: vdi = self.get_sm_vdi(location) - + util.SMlog("Updating VDI with location=%s uuid=%s" % (vdi.location, vdi.uuid)) vdi._db_update() - + def synchronise(self): """Perform the default SM -> xenapi synchronisation; ought to be good enough for most plugins.""" self.synchronise_new() self.synchronise_gone() self.synchronise_existing() - - diff --git a/drivers/SRCommand.py b/drivers/SRCommand.py index e5edb2672..a5c5e982e 100755 --- a/drivers/SRCommand.py +++ b/drivers/SRCommand.py @@ -2,13 +2,13 @@ # # Copyright (C) Citrix Systems Inc. # -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published # by the Free Software Foundation; version 2.1 only. # -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License @@ -23,7 +23,8 @@ import sys import xs_errors import xmlrpclib -import SR, util +import SR +import util import blktap2 import resetvdis import os @@ -32,7 +33,7 @@ "vdi_update", "vdi_create", "vdi_delete", "vdi_snapshot", "vdi_clone", "vdi_resize", "vdi_resize_online", "vdi_attach", "vdi_detach", "vdi_activate", "vdi_deactivate", "vdi_attach_from_config", "vdi_detach_from_config", - "vdi_generate_config", "vdi_compose", "vdi_epoch_begin", + "vdi_generate_config", "vdi_compose", "vdi_epoch_begin", "vdi_epoch_end", "vdi_enable_cbt", "vdi_disable_cbt", "vdi_data_destroy", "vdi_list_changed_blocks"] @@ -43,18 +44,18 @@ } EXCEPTION_TYPE = { - "sr_scan" : "SRScan", - "vdi_init" : "VDILoad", - "vdi_create" : "VDICreate", - "vdi_delete" : "VDIDelete", - "vdi_attach" : "VDIUnavailable", - "vdi_detach" : "VDIUnavailable", - "vdi_activate" : "VDIUnavailable", - "vdi_deactivate" : "VDIUnavailable", - "vdi_resize" : "VDIResize", - "vdi_resize_online" : "VDIResize", - "vdi_snapshot" : "VDISnapshot", - "vdi_clone" : "VDIClone", + "sr_scan": "SRScan", + "vdi_init": "VDILoad", + "vdi_create": "VDICreate", + "vdi_delete": "VDIDelete", + "vdi_attach": "VDIUnavailable", + "vdi_detach": "VDIUnavailable", + "vdi_activate": "VDIUnavailable", + "vdi_deactivate": "VDIUnavailable", + "vdi_resize": "VDIResize", + "vdi_resize_online": "VDIResize", + "vdi_snapshot": "VDISnapshot", + "vdi_clone": "VDIClone" } @@ -72,7 +73,7 @@ def __init__(self, driver_info): def parse(self): if len(sys.argv) != 2: util.SMlog("Failed to parse commandline; wrong number of arguments; argv = %s" % (repr(sys.argv))) - raise xs_errors.XenError('BadRequest') + raise xs_errors.XenError('BadRequest') # Debug logging of the actual incoming command from the caller. # util.SMlog( "" ) @@ -85,7 +86,7 @@ def parse(self): try: params, methodname = xmlrpclib.loads(sys.argv[1]) self.cmd = methodname - params = params[0] # expect a single struct + params = params[0] # expect a single struct self.params = params # params is a dictionary @@ -95,8 +96,8 @@ def parse(self): if 'vdi_uuid' in params: self.vdi_uuid = params['vdi_uuid'] elif self.cmd == "vdi_create": - self.vdi_uuid = util.gen_uuid () - + self.vdi_uuid = util.gen_uuid() + except Exception as e: util.SMlog("Failed to parse commandline; exception = %s argv = %s" % (str(e), repr(sys.argv))) raise xs_errors.XenError('BadRequest') @@ -123,9 +124,10 @@ def run(self, sr): except blktap2.TapdiskFailed as e: util.logException('tapdisk failed exception: %s' % e) raise xs_errors.XenError('TapdiskFailed', - os.strerror(e.get_error().get_error_code())) + os.strerror( + e.get_error().get_error_code())) - except blktap2.TapdiskExists as e: + except blktap2.TapdiskExists as e: util.logException('tapdisk exists exception: %s' % e) raise xs_errors.XenError('TapdiskAlreadyRunning', e.__str__()) @@ -177,17 +179,19 @@ def _run_locked(self, sr): def _run(self, sr, target): dconf_type = sr.dconf.get("type") if not dconf_type or not NO_LOGGING.get(dconf_type) or \ - not self.cmd in NO_LOGGING[dconf_type]: + self.cmd not in NO_LOGGING[dconf_type]: if 'device_config' in self.params: - util.SMlog("%s %s" % (self.cmd, util.hidePasswdInParams(self.params,'device_config'))) + util.SMlog("%s %s" % ( + self.cmd, util.hidePasswdInParams( + self.params, 'device_config'))) else: util.SMlog("%s %s" % (self.cmd, repr(self.params))) - caching_params = dict((k, self.params.get(k)) for k in \ - [blktap2.VDI.CONF_KEY_ALLOW_CACHING, - blktap2.VDI.CONF_KEY_MODE_ON_BOOT, - blktap2.VDI.CONF_KEY_CACHE_SR, - blktap2.VDI.CONF_KEY_O_DIRECT]) + caching_params = dict((k, self.params.get(k)) for k in + [blktap2.VDI.CONF_KEY_ALLOW_CACHING, + blktap2.VDI.CONF_KEY_MODE_ON_BOOT, + blktap2.VDI.CONF_KEY_CACHE_SR, + blktap2.VDI.CONF_KEY_O_DIRECT]) if self.cmd == 'vdi_create': # These are the fields owned by the backend, passed on the @@ -203,8 +207,8 @@ def _run(self, sr, target): if not util.isLegalXMLString(target.label) \ or not util.isLegalXMLString(target.description): - raise xs_errors.XenError('IllegalXMLChar', \ - opterr = 'The name and/or description you supplied contains one or more unsupported characters. The name and/or description must contain valid XML characters. See http://www.w3.org/TR/2004/REC-xml-20040204/#charsets for more information.') + raise xs_errors.XenError('IllegalXMLChar', + opterr='The name and/or description you supplied contains one or more unsupported characters. The name and/or description must contain valid XML characters. See http://www.w3.org/TR/2004/REC-xml-20040204/#charsets for more information.') target.ty = self.params['vdi_type'] target.metadata_of_pool = self.params['args'][3] @@ -225,8 +229,8 @@ def _run(self, sr, target): if not util.isLegalXMLString(name_label) \ or not util.isLegalXMLString(description): - raise xs_errors.XenError('IllegalXMLChar', \ - opterr = 'The name and/or description you supplied contains one or more unsupported characters. The name and/or description must contain valid XML characters. See http://www.w3.org/TR/2004/REC-xml-20040204/#charsets for more information.') + raise xs_errors.XenError('IllegalXMLChar', + opterr='The name and/or description you supplied contains one or more unsupported characters. The name and/or description must contain valid XML characters. See http://www.w3.org/TR/2004/REC-xml-20040204/#charsets for more information.') return target.update(self.params['sr_uuid'], self.vdi_uuid) @@ -236,16 +240,17 @@ def _run(self, sr, target): elif self.cmd == 'vdi_delete': if 'VDI_CONFIG_CBT' in util.sr_get_capability( - self.params['sr_uuid']): + self.params['sr_uuid']): return target.delete(self.params['sr_uuid'], - self.vdi_uuid, data_only = False) + self.vdi_uuid, data_only=False) else: return target.delete(self.params['sr_uuid'], self.vdi_uuid) elif self.cmd == 'vdi_attach': target = blktap2.VDI(self.vdi_uuid, target, self.driver_info) writable = self.params['args'][0] == 'true' - return target.attach(self.params['sr_uuid'], self.vdi_uuid, writable, caching_params = caching_params) + return target.attach(self.params['sr_uuid'], self.vdi_uuid, + writable, caching_params=caching_params) elif self.cmd == 'vdi_detach': target = blktap2.VDI(self.vdi_uuid, target, self.driver_info) @@ -255,14 +260,14 @@ def _run(self, sr, target): return target.snapshot(self.params['sr_uuid'], self.vdi_uuid) elif self.cmd == 'vdi_clone': - return target.clone(self.params['sr_uuid'], self.vdi_uuid) + return target.clone(self.params['sr_uuid'], self.vdi_uuid) elif self.cmd == 'vdi_resize': return target.resize(self.params['sr_uuid'], self.vdi_uuid, long(self.params['args'][0])) elif self.cmd == 'vdi_resize_online': return target.resize_online(self.params['sr_uuid'], self.vdi_uuid, long(self.params['args'][0])) - + elif self.cmd == 'vdi_activate': target = blktap2.VDI(self.vdi_uuid, target, self.driver_info) writable = self.params['args'][0] == 'true' @@ -272,12 +277,12 @@ def _run(self, sr, target): elif self.cmd == 'vdi_deactivate': target = blktap2.VDI(self.vdi_uuid, target, self.driver_info) return target.deactivate(self.params['sr_uuid'], self.vdi_uuid, - caching_params) + caching_params) elif self.cmd == 'vdi_epoch_begin': if caching_params.get(blktap2.VDI.CONF_KEY_MODE_ON_BOOT) != "reset": return - if not "VDI_RESET_ON_BOOT/2" in self.driver_info['capabilities']: + if "VDI_RESET_ON_BOOT/2" not in self.driver_info['capabilities']: raise xs_errors.XenError('Unimplemented') return target.reset_leaf(self.params['sr_uuid'], self.vdi_uuid) @@ -299,7 +304,7 @@ def _run(self, sr, target): return target.attach(self.params['sr_uuid'], self.vdi_uuid, True, True) elif self.cmd == 'vdi_detach_from_config': - extras= {} + extras = {} if target.sr.driver_config.get("ATTACH_FROM_CONFIG_WITH_TAPDISK"): target = blktap2.VDI(self.vdi_uuid, target, self.driver_info) extras['deactivate'] = True @@ -331,7 +336,7 @@ def _run(self, sr, target): elif self.cmd == 'sr_probe': txt = sr.probe() - util.SMlog( "sr_probe result: %s" % util.splitXmlText( txt, showContd=True ) ) + util.SMlog("sr_probe result: %s" % util.splitXmlText(txt, showContd=True)) # return the XML document as a string return xmlrpclib.dumps((txt,), "", True) @@ -343,7 +348,7 @@ def _run(self, sr, target): sr_uuid = self.params['sr_uuid'] resetvdis.reset_sr(sr.session, util.get_this_host(), - sr_uuid, is_master) + sr_uuid, is_master) if is_master: # Schedule a scan only when attaching on the SRmaster @@ -359,14 +364,15 @@ def _run(self, sr, target): return sr.detach(self.params['sr_uuid']) elif self.cmd == 'sr_content_type': - return sr.content_type(self.params['sr_uuid']) + return sr.content_type(self.params['sr_uuid']) elif self.cmd == 'sr_scan': return sr.scan(self.params['sr_uuid']) else: util.SMlog("Unknown command: %s" % self.cmd) - raise xs_errors.XenError('BadRequest') + raise xs_errors.XenError('BadRequest') + def run(driver, driver_info): """Convenience method to run command on the given driver""" @@ -378,8 +384,8 @@ def run(driver, driver_info): sr.direct = True ret = cmd.run(sr) - if ret == None: - print(util.return_nil ()) + if ret is None: + print(util.return_nil()) else: print(ret) @@ -397,6 +403,6 @@ def run(driver, driver_info): if isinstance(e, SR.SRException): print(e.toxml()) else: - print(xmlrpclib.dumps(xmlrpclib.Fault(1200, str(e)), "", True)) + print(xmlrpclib.dumps(xmlrpclib.Fault(1200, str(e)), "", True)) sys.exit(0)