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 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 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 getLiveCDDevice(self): _devs=["/dev/cdrom"] for _dev in _devs: _cmd="isoinfo -d -i %s | grep COMOONICS" %_dev _rc, _rv, _err = ComSystem.execLocalGetResult(_cmd, err=True) if _rc == 0: return [_dev] return []
def counters(self): #buf=array.array("c") #buf.fromstring(struct.pack("I", 1)) #buf.fromstring("get_counter") #buf.fromstring(4096*" ") #buf.fromstring(struct.pack("I", 4096)) # FIXME: As ioctls don't work jet we'll do it with gfs_tool directly should be ported back sometime # But Mr. Hl. said pragamtic!! #buf=struct.pack("I12s513sI", 1, "get_counters", 512*" ", 512) #self.logger.debug("size of buf: %u" %len(buf)) #fd=os.open(self.mountpoint, os.O_RDONLY) #if fcntl.ioctl(fd, GFS.GFS_SUPER_IOCTL, buf) != 0: # self.logger.error("Could not get counters on filesystem %s" %self.mountpoint) # return -1 #os.close(fd) #return buf (_rc,_output)=ComSystem.execLocalGetResult("%s %s %s" %(GFS.GFS_TOOL, GFS.COUNTERS_CMD, self.mountpoint)) _counters=dict() if _rc != 0: return _counters for _line in _output: _line=_line.strip() if not _line or _line=="": continue _re=GFS.COUNTERS_MATCH_NUMBER.match(_line) if _re: _counters[_re.group("key")]=int(_re.group("value")) else: _re=GFS.COUNTERS_MATCH_PERCENTAGE.match(_line) if _re: _counters[_re.group("key")]=float(_re.group("value")) else: self.logger.debug("Could not match line \"%s\"" %_line) return _counters
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 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 countersValues(self): _counters=self.counters() (_rc,_output)=ComSystem.execLocalGetResult("date +\"%F-%H-%M-%S\"") _time=_output _output=_output[0].splitlines()[0] buf=_output+"," for _value in _counters.values(): buf+=str(_value)+"," return buf[:-1]
def findLabel(self, label): """ try to find Device with label returns Device """ import ComDevice __cmd="/sbin/findfs LABEL=" + label __rc, __path = ComSystem.execLocalGetResult(__cmd) if not __rc: raise ComException("device with label " + label + "not found") return ComDevice.Device(__path[0])
def createArchive(self, source, cdir=None): ''' creates an archive from the whole source tree stays in the same filesystem ''' if not cdir: cdir=os.getcwd() __cmd = TarArchiveHandler.TAR +" "+" ".join(self.getCommandOptions())+" -c --one-file-system " + self.compression + " -f " \ + self.tarfile + " -C " + cdir + " " + source __rc, __rv = ComSystem.execLocalGetResult(__cmd) if __rc >> 8 != 0: raise RuntimeError("running %s failed" %__cmd)
def hasPartitionTable(self): """ checks wether the disk has a partition table or not """ #__cmd = CMD_SFDISK + " -Vq " + self.getDeviceName() + " >/dev/null 2>&1" #if ComSystem.execLocal(__cmd): # return False #return True __cmd = CMD_SFDISK + " -l " + self.getDeviceName() rc, std, err = ComSystem.execLocalGetResult(__cmd, True) if rc!=0: return False if (" ".join(err).upper().find("ERROR")) > 0: return False return True
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))
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 testExecLocalGetResultSim(self): ComSystem.setExecMode(ComSystem.SIMULATE) result = ComSystem.execLocalGetResult(self.cmd1, False, self.cmd1_simout) self.assertEquals(result, self.cmd1_sim_result[2])
def testExecLocalGetResultErrSim(self): ComSystem.setExecMode(ComSystem.SIMULATE) result = ComSystem.execLocalGetResult(self.cmd1, True, self.cmd1_simout, self.cmd1_simerr) self.assertEquals(result, self.cmd1_sim_result[3])
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 testExecLocalGetResultErr(self): ComSystem.setExecMode(None) result = ComSystem.execLocalGetResult(self.cmd1, True, self.cmd1_simout) self.assertEquals(result, self.cmd1_result[3])
def testExecLocalGetResultSim(self): ComSystem.setExecMode(ComSystem.SIMULATE) result = ComSystem.execLocalGetResult(self.cmd1, False, self.cmd1_simout) self.assertEquals(result, self.cmd1_sim_result[2])
def testExecLocalGetResultErrSim(self): ComSystem.setExecMode(ComSystem.SIMULATE) result = ComSystem.execLocalGetResult(self.cmd1, True, self.cmd1_simout, self.cmd1_simerr) self.assertEquals(result, self.cmd1_sim_result[3])
def testExecLocalGetResultErr(self): ComSystem.setExecMode(None) result = ComSystem.execLocalGetResult(self.cmd1, True, self.cmd1_simout) self.assertEquals(result, self.cmd1_result[3])