def replayJournal(self): """ replays the journal from top to buttom. The last inserted method first. """ self.__journal__.reverse() for je in self.__journal__: ComLog.getLogger(JournaledObject.__logStrLevel__).debug("Journalentry: %s, %s" %(je.method, je.ref.__class__)) undomethod="" try: if inspect.ismodule(je.ref): undomethod=self.__undomap__[str(je.ref.__name__+"."+je.method)] else: undomethod=self.__undomap__[str(je.ref.__class__.__name__+"."+je.method)] ComLog.getLogger(JournaledObject.__logStrLevel__).debug("Undomethod: %s(%s)" % (undomethod, je.params)) if not je.params or (type(je.params)==list and len(je.params) == 0): # changed call of replay # __class__.__dict__[undomethod] does not work with inherited methods #je.ref.__class__.__dict__[undomethod](je.ref) getattr(je.ref,undomethod)() elif type(je.params)!=list and type(je.params)!=tuple: getattr(je.ref, undomethod)(je.params) elif len(je.params) == 1: #je.ref.__class__.__dict__[undomethod](je.ref, je.params[0]) getattr(je.ref,undomethod)(je.params[0]) else: #je.ref.__class__.__dict__[undomethod](je.ref, je.params) getattr(je.ref,undomethod)(*je.params) except Exception, e: ComLog.getLogger(JournaledObject.__logStrLevel__).warn("Caught exception e during journal replay of method %s => %s on %s: %s" % (je.method, undomethod, je.ref.__class__.__name__, e)) ComLog.debugTraceLog(ComLog.getLogger(JournaledObject.__logStrLevel__))
def getIP(self): """ @return: Returns ip-address of interface @rtype: string """ #optional attribute, return empty string if not set try: self.log.debug("get ip attribute: " + self.getAttribute(ComoonicsClusterRepository.attribute_netdev_ip)) # special case for dhcp we'll return the given ipaddress if self.isDHCP(): from comoonics import ComSystem import re try: output=ComSystem.execLocalOutput("PATH=/sbin:/usr/sbin ip addr show %s" %self.getName(), True) _mac=re.search("link/ether (?P<mac>\S+)", output).group("mac") _ip=re.search(".*inet (?P<ip>[0-9.]+)", output).group("ip") if _mac.upper() == self.getMac().upper(): return _ip else: return self.getAttribute(ComoonicsClusterRepository.attribute_netdev_ip) except: ComLog.debugTraceLog(self.log) return self.getAttribute(ComoonicsClusterRepository.attribute_netdev_ip) else: return self.getAttribute(ComoonicsClusterRepository.attribute_netdev_ip) except NameError: return ""
def _fromDB(self): _rs=self._getRecordSet() if _rs.num_rows()>0: _obj=_rs.fetch_row(1, 1) for _key, _value in _obj[0].items(): setattr(self, self.__dict__["schemarev"][_key], _value) else: ComLog.debugTraceLog(self.logger) raise NoDataFoundForObjectException("No data found for object. ID: %s/%s" %(str(self.tablename), str(self.id))) self._fromdb=True
def getBlockDeviceForWWWNLun(wwwn, lun, hosts=None): """ returns the scsidevicename of wwwn, lun combination searching on all hosts or the given ones. """ blockdev_file=None if not hosts: try: hosts=getFCHosts() except SCSIException: ComLog.debugTraceLog(log) hosts=getQlaHosts() for host in hosts: device_dir=FCPATH_HOST_DEVICE %(host) try: for device_file in os.listdir(device_dir): match=re.match(SCSITARGET_PATTERN, device_file) if match: (scsi_hostid, scsi_busid, scsi_id)=match.groups() _wwwn=getWWWN(scsi_hostid, scsi_busid, scsi_id) log.debug("Found WWWN: %s==%s" %(_wwwn, wwwn)) if _wwwn==wwwn: blockdev_file=FCPATH_HOST_LUN %(host, int(scsi_hostid), int(scsi_busid), int(scsi_id), int(scsi_hostid), int(scsi_busid), int(scsi_id), int(scsi_hostid), int(scsi_busid), int(scsi_id), int(lun)) for filename in os.listdir(blockdev_file): if filename.startswith("block:"): blockdev_file=os.path.join(blockdev_file, filename) break except IOError: hostid=getHostId(host) busid=0 qlafile=QLA_SCSIPATH_PROC+"/%u" %(int(hostid)) if os.path.exists(qlafile): qla=open(qlafile, "r") regexp_lun=re.compile(QLALUN_PATTERN) regexp_port=re.compile(QLAPORT_PATTERN) scsi_ids=list() for line in qla: match=regexp_port.match(line) if match: (scsi_hostid, scsi_id, wwpn, wwnn, _x, _y)=match.groups() log.debug("match: %s, %s, %s" %(wwpn, wwnn, scsi_id)) if wwnn==wwwn or wwpn==wwwn: scsi_ids.append(int(scsi_id)) if not match: match=regexp_lun.match(line) if match: (scsi_id, scsi_lun)=match.groups() if int(scsi_id) in scsi_ids and int(scsi_lun) == int(lun): blockdev_file=SCSIPATH_TARGET_BLOCK %(int(hostid), int(busid), int(scsi_id), int(scsi_lun)) qla.close() if blockdev_file and os.path.exists(blockdev_file): log.debug("blockdevfile: %s" %(blockdev_file)) if os.path.exists(blockdev_file): return "/dev/"+os.path.basename(os.readlink(blockdev_file)) else: raise SCSIException("Could not find blockdevice for wwwn: %s, lun: %u" %(wwwn, int(lun)))
def _fromDB(self): _rs = self._getRecordSet() if _rs.num_rows() > 0: _obj = _rs.fetch_row(1, 1) for _key, _value in _obj[0].items(): setattr(self, self.__dict__["schemarev"][_key], _value) else: ComLog.debugTraceLog(self.logger) raise NoDataFoundForObjectException( "No data found for object. ID: %s/%s" % (str(self.tablename), str(self.id))) self._fromdb = True
def createPartitionsParted(self): import parted import ComParted if not self.exists(): raise ComException("Device %s not found" % self.getDeviceName()) phelper=ComParted.PartedHelper() #IDEA compare the partition configurations for update #1. delete all aprtitions dev=parted.PedDevice.get(self.getDeviceName()) try: disk=parted.PedDisk.new(dev) disk.delete_all() except parted.error: #FIXME use generic disk types disk=dev.disk_new_fresh(parted.disk_type_get("msdos")) # create new partitions for com_part in self.getAllPartitions(): type=com_part.getPartedType() if self.sameSize(): size=com_part.getPartedSize(dev) else: size=com_part.getPartedSizeOptimum(dev) flags=com_part.getPartedFlags() self.log.debug("creating partition: size: %i" % size ) phelper.add_partition(disk, type, size, flags) disk.commit() self.commit() #dev.sync() #dev.close() # run partx if the device is a multipath device self.log.debug("ComHostDisk: checking for multipath devices") if self.isDMMultipath(): self.log.debug("Device %s is a dm_multipath device, adding partitions" %self.getDeviceName()) __cmd=CMD_KPARTX + " -d " + self.getDeviceName() try: __ret = ComSystem.execLocalOutput(__cmd, True, "") self.log.debug(__ret) __cmd=CMD_KPARTX + " -a " + self.getDeviceName() __ret = ComSystem.execLocalOutput(__cmd, True, "") self.log.debug(__ret) #FIXME: crappy fix to give os some time to create devicefiles. time.sleep(10) except ComSystem.ExecLocalException, ele: ComLog.debugTraceLog(self.log) self.log.debug("Could not execute %s. Error %s" %(ele.cmd, ele))
def _handleError(self, _exception): if isinstance(_exception, ExecLocalException): self.catiflogger.info("%s: Failed[%s]" %(self.__cmd, _exception.rc)) else: self.catiflogger.info("%s: Failed[]" %(self.__cmd)) if self.errors=="raise": raise _exception elif self.errors=="ignore": ComLog.debugTraceLog(CatifModification.logger) pass else: ComLog.debugTraceLog(CatifModification.logger) if self._log: self._log.write("Error occured: %s" %_exception)
def getXPathFromXMLFile(xpath, filename): try: from comoonics import XmlTools document = XmlTools.parseXMLFile(filename) return XmlTools.evaluateXPath(xpath, document.documentElement) except ImportError: from comoonics import ComLog ComLog.debugTraceLog() import xml.dom.minidom from xml.xpath import Evaluate import os filep = os.fdopen(os.open(filename, os.O_RDONLY)) doc = xml.dom.minidom.parse(filep) return Evaluate(xpath, doc.documentElement)
def getXPathFromXMLFile(xpath, filename): try: from comoonics import XmlTools document=XmlTools.parseXMLFile(filename) return XmlTools.evaluateXPath(xpath, document.documentElement) except ImportError: from comoonics import ComLog ComLog.debugTraceLog() import xml.dom.minidom from xml.xpath import Evaluate import os filep = os.fdopen(os.open(filename, os.O_RDONLY)) doc=xml.dom.minidom.parse(filep) return Evaluate(xpath, doc.documentElement)
def __init__(self, *params, **kwds): """ Valid constructors are __init__(element, doc) __init__(source, dest) __init__(source=source, dest=dest) """ if (len(params) == 2 and isinstance(params[0], CopyObject) and isinstance(params[1], CopyObject)) or ( kwds and kwds.has_key("source") and kwds.has_key("dest") ): source = None dest = None if len(params) == 2: source = params[0] dest = params[1] else: source = kwds["source"] dest = kwds["dest"] doc = xml.dom.getDOMImplementation().createDocument(None, self.TAGNAME, None) element = doc.documentElement element.setAttribute("type", "filesystem") element.appendChild(source.getElement()) element.appendChild(dest.getElement()) Copyset.__init__(self, element, doc) self.source = source self.dest = dest elif len(params) == 2: element = params[0] doc = params[1] Copyset.__init__(self, element, doc) try: __source = element.getElementsByTagName("source")[0] self.source = getCopyObject(__source, doc) except Exception, e: ComLog.getLogger(__logStrLevel__).warning(e) ComLog.debugTraceLog(ComLog.getLogger(__logStrLevel__)) raise ComException( 'Source for filesystem copyset with name "%s" is not defined' % (self.getAttribute("name", "unknown")) ) try: __dest = element.getElementsByTagName("destination")[0] self.dest = getCopyObject(__dest, doc) except Exception, e: ComLog.getLogger(__logStrLevel__).warning(e) raise ComException( 'Destination for filesystem copyset with name "%s" is not defined' % (self.getAttribute("name", "unknown")) )
def addDOMElement(self, element, name=None): '''adds an DOM Element as member name''' from comoonics import XmlTools if name == None: name=self.getNextFileName() fd, path = tempfile.mkstemp() file = os.fdopen(fd, "w") XmlTools.toPrettyXMLFP(element, file) file.close() try: self.ahandler.addFile(path, name) os.unlink(path) except Exception, e: os.unlink(path) ComLog.debugTraceLog(Archive.log) raise e
def addDOMElement(self, element, name=None): '''adds an DOM Element as member name''' from comoonics import XmlTools if name == None: name = self.getNextFileName() fd, path = tempfile.mkstemp() file = os.fdopen(fd, "w") XmlTools.toPrettyXMLFP(element, file) file.close() try: self.ahandler.addFile(path, name) os.unlink(path) except Exception, e: os.unlink(path) ComLog.debugTraceLog(Archive.log) raise e
def __init__(self, *params, **kwds): """ Valid constructors are __init__(element, doc) __init__(source, dest) __init__(source=source, dest=dest) """ if (len(params)==2 and isinstance(params[0], CopyObject) and isinstance(params[1], CopyObject)) or \ (kwds and kwds.has_key("source") and kwds.has_key("dest")): source = None dest = None if len(params) == 2: source = params[0] dest = params[1] else: source = kwds["source"] dest = kwds["dest"] doc = xml.dom.getDOMImplementation().createDocument( None, self.TAGNAME, None) element = doc.documentElement element.setAttribute("type", "filesystem") element.appendChild(source.getElement()) element.appendChild(dest.getElement()) Copyset.__init__(self, element, doc) self.source = source self.dest = dest elif len(params) == 2: element = params[0] doc = params[1] Copyset.__init__(self, element, doc) try: __source = element.getElementsByTagName('source')[0] self.source = CopyObject(__source, doc) except Exception, e: ComLog.getLogger(__logStrLevel__).warning(e) ComLog.debugTraceLog(ComLog.getLogger(__logStrLevel__)) raise ComException( "Source for filesystem copyset with name \"%s\" is not defined" % (self.getAttribute("name", "unknown"))) try: __dest = element.getElementsByTagName('destination')[0] self.dest = CopyObject(__dest, doc) except Exception, e: ComLog.getLogger(__logStrLevel__).warning(e) raise ComException( "Destination for filesystem copyset with name \"%s\" is not defined" % (self.getAttribute("name", "unknown")))
def doAction(self): """ Executes the action found with the given name in the global action repository. """ self._fromDB() self.setStartTime() self.logger.info("Executing job %s at %s" %(self, self.starttime)) self.errorcode=0 try: _action=Action(dbhandle=self.db, job=self) _action.doAction() self.errormessage="Successfully executed job %(job)s at %(endtime)s" except JobException, e: self.logger.error(e) ComLog.debugTraceLog(self.logger) self.errorcode=e.errorcode self.errormessage=e.__str__()
def map2realDMName(device, prefixpath="/dev/mapper"): """ Maps the given devicemapper name to the real one (first created). Should be executed on every proper device mapper device. """ if os.path.islink(device): # more recent versions of multipath will not use device files but symbolic links to the # dm-* devices. Those links are relative and need therefore be converted to absolute # paths. return os.path.realpath(os.path.join(os.path.dirname(device), os.readlink(device))) else: cmd="%s info -c --noheadings -o name %s" %(CMD_DMSETUP, device) try: return os.path.join(prefixpath, ComSystem.execLocalOutput(cmd, True, "")[:-1]) except ComSystem.ExecLocalException: ComLog.debugTraceLog() return device
def getBlockDeviceForUID(uid): """ Returns the block devicefile for the given uid """ if os.path.exists(SCSIPATH_2_DEVICE): for target in os.listdir(SCSIPATH_2_DEVICE): try: blockdev_path=SCSIPATH_2_DEVICE+"/"+target+"/device/block" if os.path.exists(blockdev_path): blockdev=os.path.basename(os.readlink(blockdev_path)) _uid=ComSystem.execLocalOutput(SCSIID_CMD_GETUID+"/block/"+blockdev)[0].splitlines()[0] log.debug("getBlockDeviceForUID(): %s == %s" %(uid, _uid)) if _uid==uid: return "/dev/"+blockdev except: ComLog.debugTraceLog(log) pass else: raise SCSIException("Syspath %s does not exist. Old Linux or no Linux or no sys mounted??" %(SYSPATH_2_BLOCK))
def getBlockDeviceForUID(uid): """ Returns the block devicefile for the given uid """ if os.path.exists(SCSIPATH_2_DEVICE): for target in os.listdir(SCSIPATH_2_DEVICE): try: blockdev_path = SCSIPATH_2_DEVICE + "/" + target + "/device/block" if os.path.exists(blockdev_path): blockdev = os.path.basename(os.readlink(blockdev_path)) _uid = ComSystem.execLocalOutput( SCSIID_CMD_GETUID + "/block/" + blockdev)[0].splitlines()[0] log.debug("getBlockDeviceForUID(): %s == %s" % (uid, _uid)) if _uid == uid: return "/dev/" + blockdev except: ComLog.debugTraceLog(log) pass else: raise SCSIException( "Syspath %s does not exist. Old Linux or no Linux or no sys mounted??" % (SYSPATH_2_BLOCK))
def replayJournal(self): """ replays the journal from top to buttom. The last inserted method first. """ self.__journal__.reverse() for je in self.__journal__: ComLog.getLogger(JournaledObject.__logStrLevel__).debug( "Journalentry: %s, %s" % (je.method, je.ref.__class__)) undomethod = "" try: if inspect.ismodule(je.ref): undomethod = self.__undomap__[str(je.ref.__name__ + "." + je.method)] else: undomethod = self.__undomap__[str( je.ref.__class__.__name__ + "." + je.method)] ComLog.getLogger(JournaledObject.__logStrLevel__).debug( "Undomethod: %s(%s)" % (undomethod, je.params)) if not je.params or (type(je.params) == list and len(je.params) == 0): # changed call of replay # __class__.__dict__[undomethod] does not work with inherited methods #je.ref.__class__.__dict__[undomethod](je.ref) getattr(je.ref, undomethod)() elif type(je.params) != list and type(je.params) != tuple: getattr(je.ref, undomethod)(je.params) elif len(je.params) == 1: #je.ref.__class__.__dict__[undomethod](je.ref, je.params[0]) getattr(je.ref, undomethod)(je.params[0]) else: #je.ref.__class__.__dict__[undomethod](je.ref, je.params) getattr(je.ref, undomethod)(*je.params) except Exception, e: ComLog.getLogger(JournaledObject.__logStrLevel__).warn( "Caught exception e during journal replay of method %s => %s on %s: %s" % (je.method, undomethod, je.ref.__class__.__name__, e)) ComLog.debugTraceLog( ComLog.getLogger(JournaledObject.__logStrLevel__))
def isValidLVPath(path, doc=None): """ returns True if this path is a valid path to a logical volume and if that volume either activated or not exists. Supported paths are /dev/mapper/<vg_name>-<lv_name> or /dev/<vg_name>/<lv_name>. """ try: (vgname, lvname)=LogicalVolume.splitLVPath(path) if not doc: doc=xml.dom.getDOMImplementation().createDocument(None, None, None) vg=VolumeGroup(vgname, doc) vg.init_from_disk() lv=LogicalVolume(lvname, vg, doc) lv.init_from_disk() return True except LinuxVolumeManager.LVMCommandException: return False except LinuxVolumeManager.LVMException: # ComLog.debugTraceLog(LogicalVolume.log) return False except RuntimeError: # If any command fails BZ #72 ComLog.debugTraceLog(LogicalVolume.log) return False
def stabilize(self): self.commit() #dev.sync() #dev.close() # run partx if the device is a multipath device self.log.debug("ComHostDisk: checking for multipath devices") if self.isDMMultipath(): devicename=HostDisk.map2realDMName(self.getDeviceName()) self.log.debug("Device %s is a dm_multipath device, adding partitions" %devicename) __cmd=CMD_KPARTX + " " + OPTS_KPARTX +" -d " + devicename try: __ret = ComSystem.execLocalOutput(__cmd, True, "") self.log.debug(__ret) __cmd=CMD_KPARTX + " " + OPTS_KPARTX + " -a " + devicename __ret = ComSystem.execLocalOutput(__cmd, True, "") self.log.debug(__ret) #FIXME: crappy fix to give os some time to create devicefiles. time.sleep(10) except ComSystem.ExecLocalException, ele: ComLog.debugTraceLog(self.log) self.log.debug("Could not execute %s. Error %s" %(ele.cmd, ele))
def cleanupDest(self): if self.disk.hasPartitionTable(): self.disk.savePartitionTable(self.__tmp.name) self.journal(self.disk, "savePartitionTable", self.__tmp.name) else: self.journal(self.disk, "noPartitionTable") # if disk already contians LVM configuration remove it pvs=list() from comoonics.storage.ComLVM import PhysicalVolume, LinuxVolumeManager try: pv=PhysicalVolume(self.disk.getAttribute("name"), self.getDocument()) pv.init_from_disk() pvs.append(pv) except LinuxVolumeManager.LVMCommandException: try: for partition in self.disk.getAllPartitions(): pv=PhysicalVolume(self.disk.getDeviceName()+self.disk.getPartitionDelim()+partition.getAttribute("name"), self.getDocument()) pv.init_from_disk() pvs.append(pv) except LinuxVolumeManager.LVMCommandException: ComLog.debugTraceLog(self.logger) if not pvs or len(pvs)==0: self.log.debug("Could not find LVM physical volume on device %s. OK." %self.disk.getAttribute("name")) for pv in pvs: try: for lv in LinuxVolumeManager.lvlist(pv.parentvg): lv.remove() pv.parentvg.remove() pv.remove() except LinuxVolumeManager.LVMCommandException: ComLog.debugTraceLog(self.logger) self.log.info("Could not remove LVM configuration from device %s. Will continue nevertheless." %pv.getAttribute("name")) self.disk.createPartitions() self.disk.restore()
def isValidLVPath(path, doc=None): """ returns True if this path is a valid path to a logical volume and if that volume either activated or not exists. Supported paths are /dev/mapper/<vg_name>-<lv_name> or /dev/<vg_name>/<lv_name>. """ try: (vgname, lvname) = LogicalVolume.splitLVPath(path) if not doc: doc = xml.dom.getDOMImplementation().createDocument( None, None, None) vg = VolumeGroup(vgname, doc) vg.init_from_disk() lv = LogicalVolume(lvname, vg, doc) lv.init_from_disk() return True except LinuxVolumeManager.LVMCommandException: return False except LinuxVolumeManager.LVMException: # ComLog.debugTraceLog(LogicalVolume.log) return False except RuntimeError: # If any command fails BZ #72 ComLog.debugTraceLog(LogicalVolume.log) return False
def getBlockDeviceForWWWNLun(wwwn, lun, hosts=None): """ returns the scsidevicename of wwwn, lun combination searching on all hosts or the given ones. """ blockdev_file = None if not hosts: try: hosts = getFCHosts() except SCSIException: ComLog.debugTraceLog(log) hosts = getQlaHosts() for host in hosts: device_dir = FCPATH_HOST_DEVICE % (host) if os.path.isdir(device_dir): for device_file in os.listdir(device_dir): match = re.match(SCSITARGET_PATTERN, device_file) if match: (scsi_hostid, scsi_busid, scsi_id) = match.groups() _wwwn = getWWWN(scsi_hostid, scsi_busid, scsi_id) log.debug("Found WWWN: %s==%s" % (_wwwn, wwwn)) if _wwwn == wwwn: blockdev_file = FCPATH_HOST_LUN % ( host, int(scsi_hostid), int(scsi_busid), int(scsi_id), int(scsi_hostid), int(scsi_busid), int(scsi_id), int(lun)) blockdev_file += "/block" hostid = getHostId(host) busid = 0 qlafile = QLA_SCSIPATH_PROC + "/%u" % (int(hostid)) if os.path.exists(qlafile): qla = open(qlafile, "r") regexp_lun = re.compile(QLALUN_PATTERN) regexp_port = re.compile(QLAPORT_PATTERN) nodename = None scsi_ids = list() for line in qla: match = regexp_port.match(line) if match: (scsi_hostid, scsi_id, wwpn, wwnn, _x, _y) = match.groups() log.debug("match: %s, %s, %s" % (wwpn, wwnn, scsi_id)) if wwnn == wwwn or wwpn == wwwn: scsi_ids.append(int(scsi_id)) if not match: match = regexp_lun.match(line) if match: (scsi_id, scsi_lun) = match.groups() if int(scsi_id) in scsi_ids and int(scsi_lun) == int( lun): blockdev_file = SCSIPATH_TARGET_BLOCK % ( int(hostid), int(busid), int(scsi_id), int(scsi_lun)) qla.close() if blockdev_file and os.path.exists(blockdev_file): log.debug("blockdevfile: %s" % (blockdev_file)) if os.path.exists(blockdev_file): return "/dev/" + os.path.basename(os.readlink(blockdev_file)) else: raise SCSIException( "Could not find blockdevice for wwwn: %s, lun: %u" % (wwwn, int(lun)))