def mount(self, device, mountpoint): """ mount a filesystem device: ComDevice.Device mountpoint: ComMountPoint.MountPoint """ __exclusive=self.getAttribute("exlock", "") __mkdir=self.getAttributeBoolean("mkdir", True) __mp=mountpoint.getAttribute("name") if not os.path.exists(__mp) and __mkdir: log.debug("Path %s does not exists. I'll create it." % __mp) ComSystem.execMethod(os.makedirs, __mp) if __exclusive and __exclusive != "" and os.path.exists(__exclusive): raise ComException("lockfile " + __exclusive + " exists!") __cmd = self.mkmountcmd(device, mountpoint) __rc, __ret = ComSystem.execLocalStatusOutput(__cmd) log.debug("mount:" + __cmd + ": " + __ret) if __rc != 0: raise ComException(__cmd + __ret) if __exclusive: __fd=open(__exclusive, 'w') __fd.write(device.getDevicePath() + " is mounted ")
def mount(self, device, mountpoint): """ mount a filesystem device: ComDevice.Device mountpoint: ComMountPoint.MountPoint """ exclusive=self.getAttribute("exlock", "") mkdir=self.getAttributeBoolean("mkdir", True) mp=mountpoint.getAttribute("name") if not isinstance(self, nfsFileSystem) and not os.path.exists(device.getDevicePath()): raise IOError("Devicepath %s does not exist." %device.getDevicePath()) if not os.path.exists(mp) and mkdir: log.debug("Path %s does not exists. I'll create it." % mp) ComSystem.execMethod(os.makedirs, mp) if exclusive and exclusive != "" and os.path.exists(exclusive): raise ComException("lockfile " + exclusive + " exists!") cmd = self.mkmountcmd(device, mountpoint) rc, ret = ComSystem.execLocalStatusOutput(cmd) log.debug("mount:" + cmd + ": " + ret) if rc != 0: raise ComException(cmd + ret) if exclusive: fd=open(exclusive, 'w') fd.write(device.getDevicePath() + " is mounted ")
def rescan(self, __hosts, __bus, __target, __lun): """ Rescan SCSI invokes a rescan via /sys/class/scsi_host/hostH/scan interface IDEAS INPUTS * host - name of scsi host ( - for all ) * bus - number of scsi bus (- for all ) * target - number of target ( - for all ) * lun - number of lun ( - for all ) """ __syspath = "/sys/class/scsi_host" if not os.path.isdir(__syspath): raise ComException(__syspath + " not found") if __hosts == "-": __hosts = self.getAllSCSIHosts() if not (ComUtils.isInt(__bus) or __bus == "-"): raise ComException(__bus + " is not valid to scan SCSI Bus") if not (ComUtils.isInt(__target) or __target == "-"): raise ComException(__bus + " is not valid to scan SCSI Target") if not (ComUtils.isInt(__lun) or __lun == "-"): raise ComException(__bus + " is not valid to scan SCSI Lun") print "Hosts: ", __hosts for __host in __hosts: ComSystem.execLocal( 'echo "' + __bus + '" "' + __target + '" "' + __lun + '" > ' + __syspath + "/" + __host + "/scan" )
def setSimulation(flag): if flag: try: from comoonics import ComSystem ComSystem.setExecMode(ComSystem.SIMULATE) except ImportError: pass
def create(self): """ Newly creates the logical volume """ LinuxVolumeManager.has_lvm() size="" if self.ondisk and self.getAttribute("overwrite", "false") == "true": self.remove() try: self.init_from_disk() except: pass if self.ondisk: raise LinuxVolumeManager.LVMAlreadyExistsException(self.__class__.__name__+"("+str(self.getAttribute("name"))+")") try: size=self.getAttribute("size") if int(self.getAttribute("size")) > int(self.parentvg.getAttribute("free")): ComLog.getLogger(self.__logStrLevel__).warn("Requested LV size %s is too big taking free %s" % (self.getAttribute("size"), self.parentvg.getAttribute("free"))) self.setAttribute("size", self.parentvg.getAttribute("free")) size=self.getAttribute("size") except NameError: if ComSystem.isSimulate(): size="1000" else: size=self.parentvg.getAttribute("free") LinuxVolumeManager.lvm('lvcreate', '-L %sM' %size, '-n %s' %str(self.getAttribute("name")), '%s' %str(self.parentvg.getAttribute("name"))) self.init_from_disk() if ComSystem.isSimulate(): self.ondisk=True
def testFilesystemCopyObject2(self): from comoonics import ComSystem _xml=""" <copyset type="filesystem" name="save-tmp"> <source type="filesystem"> <device id="sourcerootfs" name="/dev/vg_vmware_cluster_sr/lv_sharedroot" options="skipmount"> <filesystem type="gfs"/> <mountpoint name="/"> <option value="lock_nolock" name="lockproto"/> <option value="hdfhgg" name="locktable"/> </mountpoint> </device> </source> <destination type="filesystem"> <device id="destrootfs" name="/dev/vg_vmware_cluster_srC/lv_sharedroot"> <filesystem clustername="vmware_cluster" type="gfs"/> <mountpoint name="/var/lib/com-ec/dest"> <option value="lock_nolock" name="lockproto"/> <option value="jhdshf" name="locktable"/> </mountpoint> </device> </destination> </copyset> """ oldexecmode=ComSystem.getExecMode() ComSystem.setExecMode(ComSystem.SIMULATE) self.__testCopyset(_xml) ComSystem.setExecMode(oldexecmode)
def testLVMCopyObject(self): import comoonics.XmlTools from comoonics.enterprisecopy.ComCopyObject import CopyObject from comoonics import ComSystem sourcexml=""" <source type="lvm"> <volumegroup name='centos' free='32' numlvs='2' attrs='wz--n-' numpvs='1' serial='0' size='3456'> <physicalvolume attr='a-' size='3456' name='/dev/sdf2' free='32' format='lvm2'/> <logicalvolume origin='' size='512' name='swap' attrs='-wi-a-'/> <logicalvolume origin='' size='2912' name='system' attrs='-wi-a-'/> </volumegroup> </source> """ destxml=""" <destination type="lvm"> <volumegroup name="centos_new"> <physicalvolume name="/dev/sde"/> </volumegroup> </destination> """ ComSystem.setExecMode(ComSystem.SIMULATE) sourcedoc=comoonics.XmlTools.parseXMLString(sourcexml) destdoc=comoonics.XmlTools.parseXMLString(destxml) try: source=CopyObject(sourcedoc.documentElement, sourcedoc) dest=CopyObject(destdoc.documentElement, destdoc) dest.updateMetaData(source.getMetaData()) except Exception, e: self.assert_("Could not execute copyobject %s => %s. Exception %s." %(source, dest, e))
def __prepare(self): import os self.origpath=self.path.getPath() ComSystem.execMethod(self.path.mkdir) ComSystem.execMethod(self.path.pushd, self.path.getPath()) if not ComSystem.isSimulate(): self.journal(self.path, "pushd") PathCopyObject.logger.debug("prepareAsSource() CWD: " + os.getcwd())
def lock(self, sleeptime=-1, retries=-1, locktimeout=-1, suspend=-1): try: ComSystem.execLocalOutput("%s %s %s" %(self.COMMAND, self._buildOptions(sleeptime, retries, locktimeout, suspend), self.filename)) except ComSystem.ExecLocalException, ele: if ele.rc==73<<8: raise lockfileTimeout(self.filename) else: raise ele
def mount(self, device, mountpoint, readOnly=0, bindMount=0, instroot=""): if not self.isMountable(): return iutil.mkdirChain("%s/%s" %(instroot, mountpoint)) # if flags.selinux: # log.info("Could not mount nfs filesystem with selinux context enabled.") # return anacondalog.debug("nfsFileSystem: Mounting nfs %s => %s" %(device, "%s/%s" %(instroot, mountpoint))) ComSystem.execLocalOutput("mount -t nfs -o nolock %s %s/%s" %(device, instroot, mountpoint))
def isDMMultipath(self): if not os.path.exists(CMD_DMSETUP): return False __cmd="%s table %s --target=multipath 2>/dev/null | grep multipath &>/dev/null" % (CMD_DMSETUP, self.getDeviceName()) try: ComSystem.execLocalOutput(__cmd, True, "") return True except ComSystem.ExecLocalException: return False
def checkFs(self, device): """ check filesystem on device (virtual method) device: ComDevice.Device """ if self.getFsckCmd(): (__cmd)=self.getFsckCmd()+" "+device.getDeviceName() log.debug("checkFs: cmd: %s" %(__cmd)) ComSystem.execLocalOutput(__cmd) else: raise NotImplementedYetException("Method checkFs is not implemented by filesystem %s (class: %s)." %(self.getName(), self.__class__.__name__))
def _testMethod(self, method, execmode, *params): oldmode=ComSystem.getExecMode() ComSystem.clearSimCommands() ComSystem.setExecMode(execmode) try: method(*params) except Exception, e: import traceback traceback.print_exc() self.fail("Could not execute %s method on with parameters %s, Error: %s" %(method, params, e))
def scsi_remove_disk(host, disk_path): target=False if os.path.isdir(disk_path) and os.access(disk_path, os.W_OK): log.debug("scsi_remove_disk(%s/delete)" %(disk_path)) try: ComSystem.execLocalOutput("echo %s > %s" %(SCSIDELETE_CMD, disk_path+"/device/delete")) except Exception, e: log.debug("Error during scsi_remove_disk %s." %e) #remove=open(disk_path+"/delete", "w") #print >> remove, SCSIDELETE_CMD target=True
def init(self): import os.path ComSystem.setExecMode(ComSystem.SIMULATE) super(test_ClusterNodeNic, self).init() #create comclusterRepository Object self.clusterRepository = getClusterRepository(os.path.join(self._testpath, "cluster2.conf")) #create comclusterinfo object self.clusterInfo = getClusterInfo(self.clusterRepository) # setup the cashes for clustat for redhat cluster self.clusterInfo.helper.setSimOutput()
def scanOptions(self, device, mountpoint=None): """ Scans a mountded gfs2 and puts the meta information into the DOM raises ComException """ if mountpoint: mountpoint=mountpoint.getAttribute("name") else: mountpoint = device.scanMountPoint()[0] if not mountpoint: raise ComException("device " + device.getDevicePath() + " is not mounted.") cmd = CMD_GFS2_TOOL + " sb " + device.getDevicePath() + " all" rc, ret = ComSystem.execLocalGetResult(cmd) if rc != 0: raise ComException(cmd + ret) if ret[0] == ComSystem.SKIPPED: # Just to keep up working when SIMULATING self.setAttribute("bsize", "4096") self.setAttribute("lockproto", "lock_dlm") self.setAttribute("clustername", "testcluster") self.setAttribute("journals", "4") else: bsize=ComUtils.grepInLines(ret, " sb_bsize = ([0-9]*)")[0] log.debug("scan Options bsize: " + bsize) self.setAttribute("bsize", bsize) lockproto=ComUtils.grepInLines(ret, " sb_lockproto = (.*)")[0] log.debug("scan Options lockproto: " + lockproto) self.setAttribute("lockproto", lockproto) locktable=ComUtils.grepInLines(ret, " sb_locktable = .*?:(.*)") if len(locktable) == 1: log.debug("scan Options locktable: " + locktable[0]) self.setAttribute("locktable", locktable[0]) clustername=ComUtils.grepInLines(ret, " sb_locktable = (.*?):.*") if len(clustername) == 1: log.debug("scan Options clustername: " +clustername[0]) self.setAttribute("clustername", clustername[0]) # FIXME: Bug in gfs2_tool journals / does not work. Only for bindmounts on / if mountpoint == "/": mountpoint = device.scanMountPoint(mountpoint)[0] cmd = CMD_GFS2_TOOL + " journals " + mountpoint rc, ret = ComSystem.execLocalGetResult(cmd) if rc != 0: raise ComException(cmd + ret) journals=ComUtils.grepInLines(ret, "^([0-9])+ journal\(s\) found.")[0] log.debug("scan Options Journals: " +journals) self.setAttribute("journals", journals)
def _do(self): """ If need be does something """ from comoonics.scsi import ComSCSI if self.name == "rescan": ComSCSI.rescan(self.dest) elif self.name == "rescan_qla": ComSCSI.rescan_qla(self.dest) else: raise SCSIRequirementException("Unsupported SCSI Rescan name %s", self.name) ComSystem.execLocalOutput("udevsettle") stabilized.stabilized(file="/proc/partitions", iterations=10, type="hash")
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 check(*args, **kwds): ret=False try: if not kwds and not args: ComSystem.execLocalOutput("%s -qf $(which rpm)" %(RPMLinuxSystemInformation.RPM_CMD)) ret= True elif kwds.has_key("operatingsystem") and \ (re.compile("linux", re.I).search(kwds["operatingsystem"]) or \ re.compile("centos", re.I).search(kwds["operatingsystem"]) or \ re.compile("fedora", re.I).search(kwds["operatingsystem"]) or \ re.compile("redhat", re.I).search(kwds["operatingsystem"])): ret=True finally: return ret
def scanOptions(self, device, mountpoint=None): """ Scans a mountded gfs and puts the meta information into the DOM raises ComException """ if mountpoint: __mountpoint=mountpoint.getAttribute("name") else: __mountpoint, fstype = device.scanMountPoint() if not __mountpoint: raise ComException("device " + device.getDevicePath() + " is not mounted.") __cmd = CMD_GFS_TOOL + " getsb " + __mountpoint __rc, __ret = ComSystem.execLocalGetResult(__cmd) if __rc != 0: raise ComException(__cmd + __ret) if __ret[0] == ComSystem.SKIPPED: # Just to keep up working when SIMULATING self.setAttribute("bsize", "4096") self.setAttribute("lockproto", "lock_dlm") self.setAttribute("clustername", "testcluster") self.setAttribute("journals", "4") else: __bsize=ComUtils.grepInLines(__ret, " sb_bsize = ([0-9]*)")[0] log.debug("scan Options bsize: " + __bsize) self.setAttribute("bsize", __bsize) __lockproto=ComUtils.grepInLines(__ret, " sb_lockproto = (.*)")[0] log.debug("scan Options lockproto: " + __lockproto) self.setAttribute("lockproto",__lockproto) __locktable=ComUtils.grepInLines(__ret, " sb_locktable = .*?:(.*)") if len(__locktable) == 1: log.debug("scan Options locktable: " + __locktable[0]) self.setAttribute("locktable", __locktable[0]) __clustername=ComUtils.grepInLines(__ret, " sb_locktable = (.*?):.*") if len(__clustername) == 1: log.debug("scan Options clustername: " +__clustername[0]) self.setAttribute("clustername", __clustername[0]) __cmd = CMD_GFS_TOOL + " df " + __mountpoint __rc, __ret = ComSystem.execLocalGetResult(__cmd) if __rc != 0: raise ComException(__cmd + __ret) __journals=ComUtils.grepInLines(__ret, " Journals = ([0-9]+)")[0] log.debug("scan Options Journals: " +__journals) self.setAttribute("journals", __journals)
def create(self): """ Creates this volumegroup """ LinuxVolumeManager.has_lvm() if self.ondisk and self.getAttribute("overwrite", "false") == "true": for lv in self.lvs: lv.remove() self.remove() try: self.init_from_disk() except: pass if self.ondisk: raise LinuxVolumeManager.LVMAlreadyExistsException(self.__class__.__name__+"("+str(self.getAttribute("name"))+")") _cmdoptions=list() try: _cmdoptions.append("--physicalextentsize %sk" % self.getAttribute("pe_size")) except: pass if self.isClustered(): _cmdoptions.append("--clustered y") else: _cmdoptions.append("--clustered n") LinuxVolumeManager.lvm('vgcreate', '%s %s %s' % (" ".join(_cmdoptions), str(self.getAttribute("name")), ' '.join(self.getPhysicalVolumeMap().keys()))) self.init_from_disk() if ComSystem.isSimulate(): self.ondisk=True
def savePartitionTable(self, filename): """ saves the Disks partition table in sfdisk format to <filename> Throws ComException on error """ __cmd = self.getDumpStdout() + " > " + filename __ret = ComSystem.execLocalOutput(__cmd, True, "saved partition table") self.log.debug("savePartitionTable( " + filename + "):\n " + __ret)
def setUp(self): from comoonics import ComSystem ComSystem.setExecMode(ComSystem.SIMULATE) from comoonics import ComLog import logging import inspect import os.path ComLog.setLevel(logging.DEBUG) self.helper=getClusterHelper() path=os.path.dirname(inspect.getfile(self.__class__)) f=open(os.path.join(path, self.OUTPUT_TEST_FILE)) import StringIO buf=StringIO.StringIO() for line in f: buf.write(line) self.TEST_OUTPUT=buf.getvalue()
def extractFile(self, name, dest): ''' extracts a file or directory from archiv' to destination dest ''' __cmd = TarArchiveHandler.TAR +" "+" ".join(self.getCommandOptions())+" -x " + self.compression + " -f " \ + self.tarfile + " -C " + dest + " " + name __rc, __rv = ComSystem.execLocalGetResult(__cmd) if __rc >> 8 != 0: raise RuntimeError("running %s failed" %__cmd)
def formatDevice(self, device): __devicePath = device.getDevicePath() __cmd = self.getMkfsCmd() + " " + __devicePath __rc, __ret = ComSystem.execLocalStatusOutput(__cmd) log.debug("formatDevice: " + __cmd + ": " + __ret) if __rc != 0: raise ComException(__cmd + __ret)
def doRealModifications(self): """ calls doCatifAction for every action """ _dest=self.getAttribute("destination", "./") self.errors=self.getAttribute("errors", "log") _childs=self.getElement().getElementsByTagName(self.child) for _child in _childs: _name=_child.getAttribute("name") _names=list() _names.append(_name) try: if re.findall("\$\(.+\)", _name): _names=re.split("\s+", str(ComSystem.execLocalOutput("echo %s" %_name)[0])[:-1]) CatifModification.logger.debug("doRealModifications() _names: %s" %_names) except ExecLocalException: _names=None _log=None if self.hasAttribute("log"): _log=self.getAttribute("log") elif _child.hasAttribute("log"): _log=_child.getAttribute("log") if not _names: continue for _name in _names: try: if _name and _name != "": self.doCatifAction(_name, _dest, _log) except ExecLocalException, _ale: self._handleError(_ale) except pexpect.ExceptionPexpect, ep: self._handleError(ep)
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 _copyData(self): # FIXME: this implementation is VERY POOR!! This should be fixed without any instance casting of classes on # the end of the child tree. There should be a abstract class FilesystemCopyObject and PathCopyObject # derive from!!!! # 1. copy fs/path to fs/path ComLog.getLogger(__logStrLevel__).debug("doCopy: instance(self.source: %s), instance(self.dest: %s)" %(self.source.__class__, self.dest.__class__)) ComLog.getLogger(__logStrLevel__).debug("doCopy: isinstance(%s, PathCopyObject): %s" %(self.source.__class__, isinstance(self.source, PathCopyObject))) if isinstance(self.source, FilesystemCopyObject) or isinstance(self.source, PathCopyObject): if isinstance(self.source, FilesystemCopyObject): if not self.source.filesystem.isCopyable(): return True mountpoint=self.source.getMountpoint().getAttribute("name") ComLog.getLogger(__logStrLevel__).debug("doCopy: isinstance(%s, PathCopyObject): %s" %(self.dest.__class__, isinstance(self.dest, PathCopyObject))) if isinstance(self.dest, FilesystemCopyObject) or isinstance(self.dest, PathCopyObject): __cmd = self._getFSCopyCommand() try: __out = ComSystem.execLocalOutput(__cmd, True) except ComSystem.ExecLocalException, ele: raise RSyncError(ele) #ComLog.getLogger(__logStrLevel__).debug("doCopy: " + __cmd +" "+ __ret) return True # 2. copy fs to archive elif isinstance(self.dest, ArchiveCopyObject): # try: archive=self.dest.getDataArchive() archive.createArchive("./", mountpoint) return True
def init_from_disk(self): """ Initializes this logical volume from disk and reads all attributes and sets them """ LinuxVolumeManager.has_lvm() try: rv=LinuxVolumeManager.lvmarray('lvdisplay', '-C', '--noheadings', '--units b', '--nosuffix', '--separator : ', str(self.parentvg.getAttribute("name"))+"/"+str(self.getAttribute("name"))) self.ondisk=False #FIXME # an exception should be thrown, if lvdisplay output has changed the syntax. # do we really need the for loop ? for line in rv: try: if line.count(":") == 8: (lvname, vgname, attrs, size, origin, snap, move, log, copy) = line.strip().split(':') else: #This is for newer lvm implementations. (lvname, vgname, attrs, size, origin, snap, move, log, copy, convert) = line.strip().split(':') self.setAttribute("attrs", attrs) self.setAttribute("size", long(math.floor(long(size) / (1024 * 1024)))) self.setAttribute("origin", origin) except: #FIXME # this should be fixed to get an exception if the values cannot be parsed. continue if not ComSystem.isSimulate(): self.ondisk=True except LinuxVolumeManager.LVMCommandException: pass
def queryStatusElement(self, **kwds): import xml.dom self.__setfromkwds("clusterstatus_cmd", kwds, self.clusterstatus_cmd) self.__setfromkwds("clusterstatus_opts", kwds, self.clusterstatus_opts) asValue=self.__getfromkwds("asvalue", kwds, True) delimitor=self.__getfromkwds("delimitor", kwds, " ") query=self.__getfromkwds("query", kwds, None) __output=self.__getfromkwds("output", kwds, None) self.log.debug("queryStatusElement: query=%s" %query) try: # create Reader object _dom=comoonics.XmlTools.parseXMLString(ComSystem.execLocalOutput(self.getClusterStatusCmd(True, delimitor), True, __output)) if not query: return _dom.documentElement else: _tmp1 = comoonics.XmlTools.evaluateXPath(query, _dom.documentElement) _tmp2 = None if asValue: _tmp2=list() for i in range(len(_tmp1)): if isinstance(_tmp1[i], xml.dom.Node) and _tmp1[i].nodeType == xml.dom.Node.ATTRIBUTE_NODE: _tmp2.append(_tmp1[i].value) else: _tmp2.append(_tmp1[i]) return delimitor.join(_tmp2) else: return comoonics.XmlTools.toPrettyXML(_tmp1[0]) except ComSystem.ExecLocalException, error: warnings.warn("Could not query the running cluster with %s. No active values will be available." %self.clusterstatus_cmd) self.log.debug("Error: %s" %error) return None
def init(self): import os.path from comoonics.cluster.ComClusterRepository import ClusterRepository from comoonics.cluster.ComClusterInfo import ClusterInfo import logging from comoonics import ComSystem ComSystem.setExecMode(ComSystem.SIMULATE) super(test_ClusterInfo, self).init() #create comclusterRepository Object self.clusterRepository = ClusterRepository( os.path.join(self._testpath, "cluster2.conf")) #create comclusterinfo object self.clusterInfo = ClusterInfo(self.clusterRepository) # setup the cashes for clustat for redhat cluster ComLog.setLevel(logging.DEBUG) self.clusterInfo.helper.setSimOutput() self.nics = list() for node in self.clusterInfo.getNodes(): node.helper.output = self.clusterInfo.helper.output for nic in node.getNics(): self.nics.append(nic)
def doModifications(self, _file): # TODO create bckup of file ? # TODO raise Warning Exception __cmd = "mv -f " __cmd += _file.getAttribute("sourcefile") __cmd += " " __cmd += _file.getAttribute("name") __rc, __ret = ComSystem.execLocalStatusOutput(__cmd) if __rc: ComLog.getLogger("MoveModification").error("doMove: " + __cmd + " " + __ret) else: ComLog.getLogger("MoveModification").debug("doMove: " + __cmd + " " + __ret)
def getBootDevice(self): _labels = ["/boot", "boot", "/bootsr", "bootsr"] _devs = [] for _label in _labels: try: _part = ComSystem.execLocalOutput("findfs LABEL=%s" % _label) ComLog.getLogger(__logStrLevel__).debug("detected disk %s" % _part) _device = self._normalizeDisk(_part[0])[0] ComLog.getLogger(__logStrLevel__).debug("normalized disk %s" % _device) _devs.append(_device) except Exception: pass #ComLog.errorTraceLog() return _devs
def ismount(path): """ Uses os.path.ismount (see L{os.path} for details) and wrap it to detect bind mounts. """ from comoonics import ComSystem import os.path if os.path.ismount(path): return True _tmp1 = ComSystem.execLocalStatusOutput("mount")[1] if not _tmp1: return False _tmp2 = _tmp1.split("\n") for line in _tmp2: if os.path.realpath(line.split()[2]) == os.path.realpath(path): return True return False
def test_tools(tools=["sg_persist"]): import os notfoundtools = list() try: from comoonics import ComSystem if ComSystem.isSimulate(): return notfoundtools except: pass for tool in tools: found = False for path in os.environ['PATH'].split(":"): cmd = os.path.join(path, tool) if os.path.exists(cmd) and os.access(cmd, os.X_OK): found = True if not found: notfoundtools.append(tool) return notfoundtools
def installBootloaderGrub(self): # To DO # Add some checks if grub install was successfull. __part = self.scanBootloaderGrub() # This is not working with all devices (e.g. cciss, mpath) # So I removed the part. #__exp=re.compile("[0-9]*") #__dev=__exp.sub("",self.getDeviceName()) __dev = self.getDeviceName() __cmd = """grub --batch 2>/dev/null <<EOF | grep "succeeded" > /dev/null device (hd0) """ + __dev + """ root """ + __part + """ setup (hd0) quit EOF """ if ComSystem.execLocal(__cmd): raise ComException("cannot install grub on " + __dev)
def doRegexpModifications(self, file, save=True, dest=None): __search = self.getAttribute("search") __replace = self.getAttribute("replace") if self.hasAttribute("options"): __options = self.getREOptions(self.getAttribute("options")) else: __options = self.DEFAULT_OPTIONS if save: __cmd = list() __cmd.append(CMD_CP) __cmd.append(file.getAttribute("name")) __cmd.append(file.getAttribute("name") + self.SAVESTRING) __rc, __ret = ComSystem.execLocalStatusOutput(" ".join(__cmd)) if __rc: RegexpModification.logger.error(" ".join(__cmd) + " " + __ret) else: RegexpModification.logger.debug(" ".join(__cmd) + " " + __ret) try: if file.hasAttribute("sourcefile"): __source = open(file.getAttribute("sourcefile")) else: __source = open(file.getAttribute("name")) __lines = __source.readlines() __source.close() if not dest: __dest = open(file.getAttribute("name"), 'w') else: __dest = dest if __options | re.MULTILINE: __dest.write( re.compile(__search, __options).sub(__replace, "".join(__lines))) else: for line in __lines: __dest.write( re.compile(__search, __options).sub(__replace, line)) if not dest: __dest.close() except IOError, ioe: RegexpModification.logger.error(ioe)
def check(*args, **kwds): ret = False try: if not kwds and not args: # SystemInformation.log.debug("Checking for cluster availability") if os.path.exists( RedhatClusterSystemInformation.REDHAT_CLUSTER_CONF ) and ComSystem.execLocal( "%s >/dev/null 2>&1" % (RedhatClusterSystemInformation.CLUSTAT_CMD)) == 0: # SystemInformation.log.debug("OK") ret = True # else: # SystemInformation.log.debug("FAILED") else: if kwds.has_key("type") and kwds["type"] == "cluster": ret = True finally: return ret
def doRealModifications(self): import xml.dom import StringIO if self.hasAttribute("command"): __cmd=self.getAttribute("command") else: buf=StringIO.StringIO() element=self.getElement() child=element.firstChild while child != None: if child.nodeType==xml.dom.Node.TEXT_NODE: buf.write(child.data) child=child.nextSibling __cmd=buf.getvalue() __rc, __ret, __err = ComSystem.execLocalGetResult(__cmd, True) if __rc: raise ComException(__cmd + ": out:" + " ".join(__ret) + \ " err: " + " ".join(__err)) else: ComLog.getLogger("ExecutionModification").debug(__cmd + ": return:" + " ".join(__ret) + \ " err: " + " ".join(__err))
def exec_cmd(cmd, key=None): import sys if not key: key = cmd try: from comoonics import ComSystem (rc, out, err) = ComSystem.execLocalGetResult( cmd, True, cmds.get("key", dict()).get("simoutput", None), cmds.get("key", dict()).get("simerror", None)) except ImportError: if sys.version[:3] < "2.4": import popen2 child = popen2.Popen3(cmd, err) rc = child.wait() out = child.fromchild.readlines() err = child.childerr.readlines() else: import subprocess p = subprocess.Popen([cmd], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) p.wait() rc = p.returncode out = p.stdout.readlines() err = p.stderr.readlines() if cmds.has_key(key): cmds[key]["executed"] = True cmds[key]["rc"] = rc cmds[key]["output"] = out cmds[key]["error"] = err if rc == 0: return out else: raise IOError( "Could not execute cmd %s. Errorcode: %u, Output: %s, Error: %s" % (cmd, rc, out, err))
def queryStatusElement(self, **kwds): import xml.dom self.__setfromkwds("clusterstatus_cmd", kwds, self.clusterstatus_cmd) self.__setfromkwds("clusterstatus_opts", kwds, self.clusterstatus_opts) asValue = self.__getfromkwds("asvalue", kwds, True) delimitor = self.__getfromkwds("delimitor", kwds, " ") query = self.__getfromkwds("query", kwds, None) __output = self.__getfromkwds("output", kwds, None) self.log.debug("queryStatusElement: query=%s" % query) from comoonics import ComSystem try: # create Reader object _dom = comoonics.XmlTools.parseXMLString( ComSystem.execLocalOutput( self.getClusterStatusCmd(True, delimitor), True, __output)) if not query: return _dom.documentElement else: _tmp1 = comoonics.XmlTools.evaluateXPath( query, _dom.documentElement) _tmp2 = None if asValue: _tmp2 = list() for i in range(len(_tmp1)): if isinstance( _tmp1[i], xml.dom.Node ) and _tmp1[i].nodeType == xml.dom.Node.ATTRIBUTE_NODE: _tmp2.append(_tmp1[i].value) else: _tmp2.append(_tmp1[i]) return delimitor.join(_tmp2) else: return comoonics.XmlTools.toPrettyXML(_tmp1[0]) except ComSystem.ExecLocalException, error: warnings.warn( "Could not query the running cluster with %s. No active values will be available." % self.clusterstatus_cmd) self.log.debug("Error: %s" % error) return None
def cmd(self, cmd, params=None, match=None): self.last_cmd = cmd if not match: match = HP_EVA_SSSU.MATCH_COMMANDSTATUS self.last_cmd = cmd + HP_EVA_SSSU.DELIM + self.toParams(params) mylogger.log(CMD_LOG_LEVEL, self.last_cmd) if not ComSystem.askExecModeCmd(self.last_cmd): return 0 self.sssu_shell.sendline(self.last_cmd) self.sssu_shell.expect(match) self.last_output = self.sssu_shell.before self.xml_output = None if self.sssu_shell.match == pexpect.EOF: return 0 else: if isinstance(self.sssu_shell.match, pexpect.ExceptionPexpect): raise CommandError(1, self.last_cmd, self.sssu_shell.before) else: thematch = self.sssu_shell.match #mylogger.debug("self.sssu_shell.match.group(1): %s" %(thematch.group(1))) self.last_error_code = int(thematch.group(1)) if self.last_error_code == 0: self.xml_output = self.LastOutput2XML() return self.last_error_code else: # Check for the special case when system is managed by another agent to get the right back _match = self.MATCH_MANAGED_ERROR.search(self.last_output) if _match and self.managed_overwrite: mylogger.warn( "SSSU Warning: System is managed by another agent (%s, %s). Overwriting." % (_match.group(1), _match.group(2))) self.setSystem(self.system, "manage") self.cmd(cmd, params, match) return 0 raise CommandError(self.last_error_code, self.last_cmd, self.last_output)
def create(self): """ Creates this volumegroup """ LinuxVolumeManager.has_lvm() if self.ondisk and self.getAttribute("overwrite", "false") == "true": for lv in self.lvs: lv.remove() self.remove() try: self.init_from_disk() except: pass if self.ondisk: raise LinuxVolumeManager.LVMAlreadyExistsException( self.__class__.__name__ + "(" + str(self.getAttribute("name")) + ")") _cmdoptions = list() try: _cmdoptions.append("--physicalextentsize %sk" % self.getAttribute("pe_size")) except: pass if self.isClustered(): _cmdoptions.append("--clustered y") else: _cmdoptions.append("--clustered n") LinuxVolumeManager.lvm( 'vgcreate', '%s %s %s' % (" ".join(_cmdoptions), str(self.getAttribute("name")), ' '.join( self.getPhysicalVolumeMap().keys()))) self.init_from_disk() if ComSystem.isSimulate(): self.ondisk = True
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 init_from_disk(self): """ Initializes this logical volume from disk and reads all attributes and sets them """ LinuxVolumeManager.has_lvm() try: rv = LinuxVolumeManager.lvmarray( 'lvdisplay', '-C', '--noheadings', '--units b', '--nosuffix', '--separator : ', str(self.parentvg.getAttribute("name")) + "/" + str(self.getAttribute("name"))) self.ondisk = False #FIXME # an exception should be thrown, if lvdisplay output has changed the syntax. # do we really need the for loop ? for line in rv: try: if line.count(":") == 8: (lvname, vgname, attrs, size, origin, snap, move, log, copy) = line.strip().split(':') else: #This is for newer lvm implementations. (lvname, vgname, attrs, size, origin, snap, move, log, copy, convert) = line.strip().split(':') self.setAttribute("attrs", attrs) self.setAttribute( "size", long(math.floor(long(size) / (1024 * 1024)))) self.setAttribute("origin", origin) except: #FIXME # this should be fixed to get an exception if the values cannot be parsed. continue if not ComSystem.isSimulate(): self.ondisk = True except LinuxVolumeManager.LVMCommandException: pass
def _copyData(self): # FIXME: this implementation is VERY POOR!! This should be fixed without any instance casting of classes on # the end of the child tree. There should be a abstract class FilesystemCopyObject and PathCopyObject # derive from!!!! # 1. copy fs/path to fs/path ComLog.getLogger(__logStrLevel__).debug( "doCopy: instance(self.source: %s), instance(self.dest: %s)" % (self.source.__class__, self.dest.__class__)) ComLog.getLogger(__logStrLevel__).debug( "doCopy: isinstance(%s, PathCopyObject): %s" % (self.source.__class__, isinstance(self.source, PathCopyObject))) if isinstance(self.source, FilesystemCopyObject) or isinstance( self.source, PathCopyObject): if isinstance(self.source, FilesystemCopyObject): if not self.source.filesystem.isCopyable(): return True mountpoint = self.source.getMountpoint().getAttribute("name") ComLog.getLogger(__logStrLevel__).debug( "doCopy: isinstance(%s, PathCopyObject): %s" % (self.dest.__class__, isinstance(self.dest, PathCopyObject))) if isinstance(self.dest, FilesystemCopyObject) or isinstance( self.dest, PathCopyObject): __cmd = self._getFSCopyCommand() try: __out = ComSystem.execLocalOutput(__cmd, True) except ComSystem.ExecLocalException, ele: raise RSyncError(ele) #ComLog.getLogger(__logStrLevel__).debug("doCopy: " + __cmd +" "+ __ret) return True # 2. copy fs to archive elif isinstance(self.dest, ArchiveCopyObject): # try: archive = self.dest.getDataArchive() archive.createArchive("./", mountpoint) return True
def __doCopy_alt(self): __tmp = os.tempnam("/tmp") if not self.needPartitionTableUpdate(): ComLog.getLogger("Copyset").debug( "partition tables are the same. No need to update") else: if self.destination.hasPartitionTable(): self.destination.savePartitionTable(__tmp) self.journal(self.destination, "savePartitionTable", __tmp) else: self.journal(self.destination, "noPartitionTable") if self.source.hasPartitionTable(): __cmd = self.source.getDumpStdout() __cmd += " | " __cmd += self.destination.getRestoreStdin(True) __rc, __ret = ComSystem.execLocalStatusOutput(__cmd) ComLog.getLogger("Copyset").debug(__cmd + ": " + __ret) if __rc != 0: raise ComException(__cmd + __ret) else: if not self.destination.deletePartitionTable(): raise ComException( "Partition table on device %s coud not be deleted", self.destination.getDeviceName())
def scan_release(self): _ret = ComSystem.execLocalOutput("uname -r") return [ _ret[0].strip("\n") ]
class BaseTestFileSystem(unittest.TestCase): xml = None filesystem = None device = None mountpoint = None commands = None simmethods = None name = "" label = "mylabel" def setUp(self): from comoonics import XmlTools import StringIO from comoonics.storage.ComDevice import Device from comoonics.storage.ComMountpoint import MountPoint from comoonics.storage.ComFileSystem import getFileSystem xmlfp = StringIO.StringIO(self.xml) doc = XmlTools.parseXMLFP(xmlfp) __device = doc.documentElement self.device = Device(__device, doc) __fs = __device.getElementsByTagName("filesystem")[0] self.filesystem = getFileSystem(__fs, doc) __mp = __device.getElementsByTagName("mountpoint")[0] self.mountpoint = MountPoint(__mp, doc) def _testMethod(self, method, execmode, *params): oldmode = ComSystem.getExecMode() ComSystem.clearSimCommands() ComSystem.setExecMode(execmode) try: method(*params) except Exception, e: import traceback traceback.print_exc() self.fail( "Could not execute %s method on with parameters %s, Error: %s" % (method, params, e)) if self.simmethods and ComSystem.isSimulate(): errormethods = list() for simmethod in ComSystem.getSimCommands(): matched = False for simmethod2 in self.simmethods: if not matched and isinstance(simmethod2, basestring): matched = simmethod == simmethod2 elif not matched: # Must be re matched = simmethod2.match(simmethod) if matched == False: errormethods.append(simmethod) if len(errormethods) > 0: bufsimmethods = list() for simmethod in self.simmethods: if isinstance(simmethod, basestring): bufsimmethods.append(simmethod) else: bufsimmethods.append(simmethod.pattern) self.fail( """The commands being executed are not to be found in the commands that must have been executed. Commands that could not be matched are: %s Executed commands are: %s Expected commands are: %s """ % ("\n".join(errormethods), "\n".join( ComSystem.getSimCommands()), "\n".join(bufsimmethods))) ComSystem.setExecMode(oldmode)
def doModifications(self, file): save = True if self.hasAttribute("nobackup"): if self.getAttribute("nobackup") == "1": save = False ComSystem.execMethod(self.doRegexpModifications, file, save)
import sys import os sys.path.append("../lib") from comoonics import ComScsi from comoonics import ComSystem ComSystem.setExecMode("ask") #print os.listdir("/sys/class") scsi=ComScsi.SCSI() scsi.rescan("-", "-", "-", "-") scsi.rescan("-", "0", "1", "2") scsi.rescan("-", "x", "-", "-")
def prepareAsDest(self): ''' writes all metadata to archive''' self.log.debug("prepareAsDest()") ComSystem.execMethod(self.serializer.serialize, self.metadata)
def scan_processor(self): _ret = ComSystem.execLocalOutput("uname -p") return [ _ret[0].strip("\n") ]
def delete(self, recursive=True, force=True): """ Deletes cdsl from filesystem and inventoryfile @param force: If not set remove only symbolic links, if set remove content, too @type force: Boolean @param recursive: if set delete subcdsls (e.g. /host/shared/host when removing /host/shared) @type recursive: Boolean """ from comoonics.ComPath import Path from comoonics.cdsl import getNodeFromPath, isSubPath, commonpath, subpathsto if not self.exists(): raise CdslDoesNotExistException("Cdsl with source " + self.src + " does not exist, cannot delete.") if not self.isShared() and not self.isHostdependent(): raise CdslUnsupportedTypeException( self.type + " is not a supported cdsl type.") #verify if cdsl contains other cdsl, if true delete these first #assume completeness of inventoryfile if recursive == True: _tmp = self.getChilds() for cdsl in _tmp: cdsl.delete(recursive, force) _cwd = Path() _cwd.pushd(self.getBasePath()) #delete or move cdsl from filesystem first is from second to if second=None it is removed _delpaths = list() _movepairs = dict() _delpaths2 = list() for _path in self.getSourcePaths(): # This one is always a link to be removed _delpaths.append(_path) for _path in self.getDestPaths(): if force: _delpaths.append(_path) else: self.logger.debug(".delete(%s): Skipping path %s" % (self.src, _path)) if self.isShared(): _movepairs[_path] = self.src else: _nodeid = getNodeFromPath(_path, self.cdslRepository) _movepairs[_path] = "%s.%s" % (self.src, _nodeid) # _delpaths[_path]=self. # tidy up the rest # Means: # * if we have siblings: clean up to longest common path with all siblings prefixes = list() if self.isHostdependent(): for nodename in self.getNodenames(): prefixes.append( os.path.join(self.cdslRepository.getTreePath(), nodename)) elif self.isShared(): prefixes.append(self.cdslRepository.getSharedTreepath()) # Let's find our parent of same type parent2nd = None if self.getParent() != None and self.getParent().getParent() != None: parent2nd = self.getParent().getParent() subpaths = list() if len(self.getSiblings()) > 0: longestcommon = "" for sibling in self.getSiblings(): common = commonpath(self.src, sibling.src) # while common and common != longestcommon: if isSubPath(common, longestcommon): longestcommon = common for _path in subpathsto(longestcommon, self.src): subpaths.append(_path) # * if we have a parent of same type and no siblings: clean up to parent elif parent2nd != None: for _path in subpathsto(parent2nd.src, self.src): subpaths.append(_path) # * if we don't have a parent and no siblings: clean up to root+mountpoint else: for _path in subpathsto("", self.src): subpaths.append(_path) for path in subpaths: if str(path) != str(self.src): for prefix in prefixes: _delpaths2.append(os.path.join(prefix, path)) self.logger.debug("delpaths2: %s" % _delpaths2) self.logger.debug("delete: cwd: %s" % _cwd) self._removePath(_delpaths) for _from, _to in _movepairs.items(): if not _to: self._removePath(_from) else: # for _delpath in _delpaths: # if os.path.samefile(_delpath, _to): # _delpaths.remove(_delpath) if os.path.islink(_to): self._removePath(_to) shutil.move(_from, _to) # We must remove paths from the delpaths that have been moved to as they are # for _delpath in _delpaths: # if os.path.samefile(_to, _delpath): # _delpaths.remove(_delpath) self._removePath(_delpaths2) _deleted = ComSystem.execMethod(self.cdslRepository.delete, self) _cwd.popd() self.logger.debug("Delete CDSL from Inventoryfile") #delete cdsl also from xml inventory file #self.cdslRepository.delete(self) return _deleted
def getPartitionTable(self): try: rv = ComSystem.execLocalOutput(self.getDumpStdout(), True, "getPartitionTable") return rv except ComSystem.ExecLocalException: return list()
def deletePartitionTable(self): """ deletes the partition table """ __cmd = CMD_DD + " if=/dev/zero of=" + self.getDeviceName() + " bs=512 count=2 >/dev/null 2>&1" if ComSystem.execLocal(__cmd): return False return self.rereadPartitionTable()
"mount -t ocfs2 -o defaults /dev/vg_vmware_cluster_sr/lv_sharedroot /", "umount /dev/vg_vmware_cluster_sr/lv_sharedroot", "umount /", "yes | mkfs -t ocfs2 -F /dev/vg_vmware_cluster_sr/lv_sharedroot", "ocfs2.fsck -y /dev/vg_vmware_cluster_sr/lv_sharedroot", "tunefs.ocfs2 -L /dev/vg_vmware_cluster_sr/lv_sharedroot mylabel", "tunefs.ocfs2 -L /dev/vg_vmware_cluster_sr/lv_sharedroot" ] self.name = "ocfs2" BaseTestFileSystem.__init__(self, method) testclasses = [ TestGfsFileSystem, TestExt2FileSystem, TestExt3FileSystem, TestOcfs2FileSystem ] testmethods = [ "testMount", "testUmountDev", "testUmountDir", "testGetName", "testFormatDevice", "testLabelDevice", "testCheckFs", "testGetLabel" ] if __name__ == "__main__": oldmode = ComSystem.getExecMode() ComSystem.setExecMode(ComSystem.SIMULATE) #import sys;sys.argv = ['', 'Test.testName'] suite = unittest.TestSuite() for testclass in testclasses: for testmethod in testmethods: suite.addTest(testclass(testmethod)) runner = unittest.TextTestRunner() runner.run(suite) ComSystem.setExecMode(oldmode)
class ArchiveRequirement(Requirement): """ Requirement Class for handling archive files. The attribute format defines the type of archive. Until now only "cpio" is supported as type. A xml document fragment for a archive requirement could look as follows: <device refid="bootfs"> <modification type="copy"> <requirement type="archive" format="cpio" name="/initrd_sr-2.6.9-34.0.1.ELsmp.img" dest="/tmp/test"/> <file name="etc/cluster.conf" sourcefile="/etc/copysets/lilr10023/cluster.conf"/> </modification> </device> """ """ Static methods and objects/attributes """ __logStrLevel__ = "ArchiveRequirement" """ Public methods """ def __init__(self, element, doc): """ Creates a new requirement instance """ Requirement.__init__(self, element, doc) if not self.hasAttribute("format"): raise ArchiveRequirementException( "Format has to be defined for %s" % self.__class__.__name__) if not self.getAttribute("format") == "cpio": raise ArchiveRequirementException( "Format %s is not implemented for %s" % (self.getAttribute("format"), self.__class__.__name__)) if not self.hasAttribute("name") or not self.hasAttribute("dest"): raise ArchiveRequirementException( "Either name or destination not defined in element") self.order = Requirement.BOTH def check(self): """ Returns true if checks have to be performed """ return not self.hasAttribute("check") or not self.getAttribute( "check") == "no" def doPre(self): """ Unpacks the given file to dest """ srcfile = self.getAttribute("name") destfile = self.getAttribute("dest") __mkdir = self.getAttributeBoolean("mkdir", default=True) if not ComSystem.execMethod(os.path.exists, destfile) and __mkdir: ComLog.getLogger(ArchiveRequirement.__logStrLevel__).debug( "Path %s does not exists. I'll create it." % destfile) os.makedirs(destfile) if self.check() and not ComSystem.isSimulate(): if not os.access(srcfile, os.R_OK) or not os.access( destfile, os.F_OK) or not os.access(destfile, os.W_OK): raise ArchiveRequirementException( "Either srcfile %s is not readable or dest %s is not writeable" % (srcfile, destfile)) __cmd = "rm -rf %s/*" % destfile (rc, rv) = ComSystem.execLocalGetResult(__cmd) if rc >> 8 != 0: raise RuntimeError("running \"%s\" failed: %u, %s" % (__cmd, rc, rv)) self.olddir = os.curdir ComSystem.execMethod(os.chdir, destfile) __cmd = "gzip -cd %s | cpio -i" % srcfile (rc, rv, stderr) = ComSystem.execLocalGetResult(__cmd, True) if rc >> 8 != 0: raise RuntimeError("running \"%s\" failed: %u, %s, %s" % (__cmd, rc, rv, stderr)) def do(self): """ If need be does something """ pass def doPost(self): """ Does something afterwards """ srcfile = self.getAttribute("name") destfile = self.getAttribute("dest") if self.check() and not ComSystem.isSimulate(): if not os.access(srcfile, os.R_OK) or not os.access( destfile, os.F_OK) or not os.access(destfile, os.W_OK): raise ArchiveRequirementException( "Either srcfile %s is not readable or dest %s is not writeable" % (srcfile, destfile)) ComSystem.execMethod(os.chdir, destfile) __cmd = "cp %s %s" % (srcfile, srcfile + self.getAttribute("bak_suffix", ".bak")) try: (rc, rv, stderr) = ComSystem.execLocalGetResult(__cmd, True) if rc >> 8 != 0: raise RuntimeError( "running \"%s\" failed: %u, %s, errors: %s" % (__cmd, rc, rv, stderr)) except RuntimeError, re: ComLog.getLogger(ArchiveRequirement.__logStrLevel__).warn( "Cannot backup sourcefile %s=%s, %s." % (srcfile, srcfile + ".bak", re)) __cmd = "find . | cpio -o -c |gzip -9 > %s" % (srcfile) (rc, rv, stderr) = ComSystem.execLocalGetResult(__cmd, True) ComSystem.execMethod(os.chdir, self.olddir) if rc >> 8 != 0: raise RuntimeError("running \"%s\" failed: %u, %s, errors: %s" % (__cmd, rc, rv, stderr)) __cmd = "rm -rf %s/*" % destfile (rc, rv) = ComSystem.execLocalGetResult(__cmd) if rc >> 8 != 0: raise RuntimeError("running \"%s\" failed: %u, %s" % (__cmd, rc, rv))
def commit(self, force=False): """ Commit new or changed cdsl to filesystem and inventoryfile @param force: skip Backup when set and overwrite existing files/directories/links @type force: Boolean """ from comoonics.ComPath import Path from comoonics.cdsl import isSubPath #important pathes to work with cdsls ##### # Chancel creation of cdsl if it already exists # or a cdsl of another type with same src exists # or creation is insane because underlying structure # is of same type as cdsl which should be created ##### if self.exists(): self.logger.debug("CDSL already exists, chancel commit") raise CdslAlreadyExists("Cdsl %s is already existant." % self.src) if self.isShared() and (self.getParent() == None or self.getParent().isShared()): self.logger.debug( "The cdsl %s to be shared back seems to recide already in a shared area." % self.src) raise CdslOfSameType( "The cdsl %s to be shared seems to recide already in a shared area." % self.src) if self.isHostdependent( ) and self.getParent() != None and self.getParent().isHostdependent(): self.logger.debug( "The cdsl %s to be hostdependent seems to recide alreay in an hostdependent area." % self.src) raise CdslOfSameType( "The cdsl %s to be hostdependent seems to recide alreay in an hostdependent area." % self.src) elif self.isShared(): if isSubPath(self.src, self.cdsltree_shared): self.logger.debug( "Given source is already part of a hostdependent CDSL") raise CdslAlreadyExists( "Cdsl %s is already a hostdependent cdsl." % self.src) elif self.isHostdependent(): if isSubPath(self.src, self.getCDSLLinkPath()): self.logger.debug( "Given source is already part of a hostdependent CDSL") raise CdslAlreadyExists("Cdsl %s is already a shared cdsl." % self.src) _path = Path() _path.pushd(self.getBasePath()) # _expanded=self.cdslRepository.expandCdsl(self) # parent=self.getParent() # if parent: # _tail=strippath(self.src, parent.src) # _expandedforparent=os.path.join(parent.cdslRepository.expandCdsl(parent), _tail) # else: # _expandedforparent=_expanded _depth = self._pathdepth(self.src) - 1 if _depth > 0: if self.isShared(): _depth = _depth + self._pathdepth( self.cdsltree) + 1 # 1 because we need to add the node elif self.isHostdependent() and self.getParent() != None: _depth = _depth + self._pathdepth(self.cdsltree_shared) _relativepath = _depth * "../" else: _relativepath = "" # flag to indicate if data has been backuped ignore if force is True once = False # First copy or move the files to the destpaths... for destpath in self.getDestPaths(): # Create the parentdir if it does not already exist parentdir = os.path.dirname(destpath) if not os.path.exists(parentdir): self.logger.debug("Create Directory " + parentdir) os.makedirs(parentdir) # Let's copy the data if self.isHostdependent(): # In case of hd we need to copy for each node self.logger.debug("Copy Files: " + self.src + " => " + destpath) ComSystem.execLocalStatusOutput("cp -a " + self.src + " " + destpath) else: # in case of shared we need to move or remove the files which are changed from hd to shared. if not once: self.logger.debug("Copying hd files once from " + self.src + " =>" + destpath) ComSystem.execLocalStatusOutput("cp -a %s %s" % (self.src, destpath)) once = True # createdefault destination # if self.isHostdependent(): # self.logger.debug("Copy Files: " + self.src + " => " + os.path.join(self.cdslRepository.getTree(), self.cdslRepository.getDefaultPath())) # ComSystem.execLocalStatusOutput("cp -a " + self.src + " " + self.cdslRepository.getDefaultPath()) if self.isHostdependent(): if force: self.logger.debug("Removing oldfile %s" % self.src) ComSystem.execLocalStatusOutput("rm -rf %s" % self.src) elif not force: self.logger.debug("Moving %s => %s.orig" % (self.src, self.src)) ComSystem.execLocalStatusOutput("mv %s %s.orig" % (self.src, self.src)) # Now create the symlinks for sourcepath in self.getSourcePaths(): if self.isShared(): # Either backup or remove! if not force: self.logger.debug("Backup Files: " + sourcepath + " => " + sourcepath + ".orig") ComSystem.execLocalStatusOutput("mv " + sourcepath + " " + sourcepath + ".orig") else: self.logger.debug("Remove Files: " + sourcepath) if os.path.isfile(sourcepath): ComSystem.execMethod(os.remove, sourcepath) else: ComSystem.execMethod(shutil.rmtree, sourcepath) src = os.path.join(_relativepath, self.cdslRepository.getSharedTreepath(), self.cdslRepository.expandCdsl(self)) dest = sourcepath elif self.isHostdependent(): src = os.path.join(_relativepath, self.cdslRepository.getLinkPath(), self.cdslRepository.expandCdsl(self)) dest = sourcepath self.logger.debug("Creating Link: %s => %s, currentpath: %s" % (src, dest, _path)) ComSystem.execMethod(os.symlink, src, dest) _path.popd() return ComSystem.execMethod(self.cdslRepository.commit, self)
''' Created on Feb 26, 2010 @author: marc ''' from comoonics import ComLog from comoonics import ComSystem from comoonics.storage.ComLVM import LogicalVolume, LinuxVolumeManager, PhysicalVolume, VolumeGroup import logging logging.basicConfig() ComLog.setLevel(logging.DEBUG) import unittest ComSystem.setExecMode(ComSystem.SIMULATE) class Test(unittest.TestCase): devicenames = { "/dev/sda": [LogicalVolume.LVMInvalidLVPathException], "/dev/cciss/c0d0p1": [["cciss", "c0d0p1"]], "/dev/mapper/abc-def": [["abc", "def"]], "/dev/abc/def": [["abc", "def"]], "/dev/mapper/abc_def": [LogicalVolume.LVMInvalidLVPathException, None], "/dev/abc/def/geh": [LogicalVolume.LVMInvalidLVPathException, None] } def testsplitLVPath(self): ComSystem.setExecMode(ComSystem.SIMULATE) for device in self.devicenames.keys(): print("device=" + device) expresult = self.devicenames[device][0] try: