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 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 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 formatDevice(self, device): __cmd = self.getMkfsCmd() + self.getOptionsString( ) + device.getDevicePath() __rc, __ret = ComSystem.execLocalStatusOutput(__cmd) #self.getLog().debug("formatDevice: \n" , __cmd + __ret) if __rc != 0: raise ComException(__cmd + __ret)
def copyPartitionTable(source_device, destination_device): __cmd = CMD_SFDISK + " -d " + source_device.getDeviceName() + " | " + CMD_SFDISK \ + " " + destination_device.getDeviceName() __rc, __ret = ComSystem.execLocalStatusOutput(__cmd) log.debug("copyPartitionTable " + __ret) if __rc != 0: raise ComException(__cmd)
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 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 labelDevice(self, device, label): __devicePath = device.getDevicePath() __cmd = self.labelCmd + " " + __devicePath + " " + label __rc, __ret = ComSystem.execLocalStatusOutput(__cmd) log.debug("labelDevice: " + __cmd + ": " + __ret) if __rc: raise ComException(__cmd + __ret) self.setAttribute("label", label)
def formatDevice(self, device): __cmd = self.getMkfsCmd() + self.getOptionsString() + device.getDevicePath() if not os.path.exists(device.getDevicePath()): raise IOError("Devicepath %s does not exist." %device.getDevicePath()) __rc, __ret = ComSystem.execLocalStatusOutput(__cmd) #self.getLog().debug("formatDevice: \n" , __cmd + __ret) if __rc != 0: raise ComException(__cmd + __ret)
def labelDevice(self, device, label): __devicePath = device.getDevicePath() __cmd = self.labelCmd + " " + __devicePath + " " + label __rc, __ret = ComSystem.execLocalStatusOutput(__cmd) log.debug("labelDevice: " + __cmd + ": " + __ret) if __rc: raise ComException(__cmd + __ret) self.setAttribute("label", label)
def formatDevice(self, device): __devicePath = device.getDevicePath() if not os.path.exists(__devicePath): raise IOError("Devicepath %s does not exist." %__devicePath) __cmd = self.getMkfsCmd() + " " + __devicePath __rc, __ret = ComSystem.execLocalStatusOutput(__cmd) log.debug("formatDevice: " + __cmd + ": " + __ret) if __rc != 0: raise ComException(__cmd + __ret)
def getLabelFromDevice(self, device): # BUG: Cannot function!!!! __devicePath = device.getDevicePath() __cmd = self.labelCmd + " " + __devicePath __rc, __ret = ComSystem.execLocalStatusOutput(__cmd) log.debug("getLabel: " + __cmd + ": " + __ret) if __rc: raise ComException(__cmd + __ret) return __ret
def getLabelFromDevice(self, device): # BUG: Cannot function!!!! __devicePath= device.getDevicePath() __cmd = self.labelCmd + " " + __devicePath __rc, __ret = ComSystem.execLocalStatusOutput(__cmd) log.debug("getLabel: " + __cmd + ": " + __ret) if __rc: raise ComException(__cmd + __ret) return __ret
def doPost(self): super(ISOFSModificationset, self).doPost() self.replayJournal() self.commitJournal() ISOFSModificationset.logger.debug("doPost() CWD: " + os.getcwd()) __cmd = self._get_mkisofs_command() __rc, __ret = ComSystem.execLocalStatusOutput(__cmd) ISOFSModificationset.logger.debug("mkisofs: " + __cmd + ": " + __ret) if __rc != 0: raise ComException(__cmd + __ret)
def umountDir(self, mountpoint): """ umount a filesystem with the use of the mountpoint mountpoint: ComMountPoint.MountPoint """ __cmd = self.cmd_umount + " " + mountpoint.getAttribute("name") __rc, __ret = ComSystem.execLocalStatusOutput(__cmd) log.debug("umount: " + __cmd + ": " + __ret) if __rc != 0: raise ComException(__cmd + __ret) self.unlinkLockfile()
def umountDev(self, device): """ umount a filesystem with the use of the device name device: ComDevice.Device """ __cmd = self.cmd_umount + " " + device.getDevicePath() __rc, __ret = ComSystem.execLocalStatusOutput(__cmd) log.debug("umount:" + __cmd + ": " + __ret) if __rc != 0: raise ComException(__cmd + __ret) self.unlinkLockfile()
def umountDev(self, device): """ umount a filesystem with the use of the device name device: ComDevice.Device """ __cmd = self.cmd_umount + " " + device.getDevicePath() __rc, __ret = ComSystem.execLocalStatusOutput(__cmd) log.debug("umount:" + __cmd + ": " + __ret) if __rc != 0: raise ComException(__cmd + __ret) self.unlinkLockfile()
def umountDir(self, mountpoint): """ umount a filesystem with the use of the mountpoint mountpoint: ComMountPoint.MountPoint """ __cmd = self.cmd_umount + " " + mountpoint.getAttribute("name") __rc, __ret = ComSystem.execLocalStatusOutput(__cmd) log.debug("umount: " + __cmd + ": " + __ret) if __rc != 0: raise ComException(__cmd + __ret) self.unlinkLockfile()
def getLabelFromDevice(self, device): # BUG: Cannot function!!!! __devicePath= device.getDevicePath() if not os.path.exists(__devicePath): raise IOError("Devicepath %s does not exist." %__devicePath) __cmd = self.labelCmd + " " + __devicePath __rc, __ret = ComSystem.execLocalStatusOutput(__cmd) log.debug("getLabel: " + __cmd + ": " + __ret) if __rc: raise ComException(__cmd + __ret) return __ret
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 doModifications(self, file): # TODO create bckup of file ? # TODO raise Warning Exception cmd = "cp -a " cmd += file.getAttribute("sourcefile") cmd += " " cmd += file.getAttribute("name") rc, ret = ComSystem.execLocalStatusOutput(cmd) if rc: ComLog.getLogger("CopyModification").error("doCopy: " + cmd + " " + ret) else: ComLog.getLogger("CopyModification").debug("doCopy: " + cmd +" "+ ret)
def copyPartitionTable(source_device, destination_device): __cmd = ( CMD_SFDISK + " -d " + source_device.getDeviceName() + " | " + CMD_SFDISK + " " + destination_device.getDeviceName() ) __rc, __ret = ComSystem.execLocalStatusOutput(__cmd) log.debug("copyPartitionTable " + __ret) if __rc != 0: raise ComException(__cmd)
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 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 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 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 __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 fromXML(options, xmlfile): from comoonics import XmlTools import xml.dom if options.xml=="-": logger.debug("Parsing document from stdin") doc = XmlTools.parseXMLFP(sys.stdin, options.xmlvalidate) elif os.path.isfile(options.xml): logger.debug("Parsing document %s " % options.xml) doc = XmlTools.parseXMLFile(options.xml, options.xmlvalidate) if options.clusterconf: from xml import xpath if options.nodename != None or options.nodename != "": (rc, options.nodename)=ComSystem.execLocalStatusOutput("cman_tool status | grep 'Node name:'") logger.debug("options.nodename: %s" %options.nodename) options.nodename=options.nodename.split(" ")[2] _xmlnodepath='/cluster/clusternodes/clusternode[@name="%s"]/com_info/fenceackserver' %(options.nodename) logger.debug("Nodename: %s, path: %s" %(options.nodename, _xmlnodepath)) node=xpath.Evaluate(_xmlnodepath, doc)[0] elif options.xmlnodepath and options.xmlnodepath != "": from xml import xpath logger.debug("Path2Config: %s" %options.xmlnodepath) node=xpath.Evaluate(options.xmlnodepath, doc)[0] else: node=doc.documentElement if node.hasAttribute("port"): options.port=node.getAttribute("port") if node.hasAttribute("user"): options.user=node.getAttribute("user") if node.hasAttribute("passwd"): options.password=node.getAttribute("passwd") if node.hasAttribute("bind"): options.bind=node.getAttribute("bind") sslnodes=node.getElementsByTagName("ssl") if sslnodes: options.ssl=True if node.hasAttribute("keyfile"): options.ssl_keyfile=node.getAttribute("keyfile") if node.hasAttribute("certfile"): options.ssl_certfile=node.getAttribute("certfile") if node.hasAttribute("verifyfile"): options.ssl_verifyfile=node.getAttribute("verifyfile") return
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: dest2=open(file.getAttribute("name"), 'w') else: dest2=dest if options | re.MULTILINE: dest2.write(re.compile(search, options).sub(replace, "".join(lines))) else: for line in lines: dest2.write(re.compile(search, options).sub(replace, line)) if not dest: dest2.close() except IOError, ioe: RegexpModification.logger.error(ioe)
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 __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 testExecLocalStatusOutput(self): ComSystem.setExecMode(None) result = ComSystem.execLocalStatusOutput(self.cmd1, self.cmd1_simout) self.assertEquals(result, self.cmd1_result[4])
def testExecLocalStatusOutputSim(self): ComSystem.setExecMode(ComSystem.SIMULATE) result = ComSystem.execLocalStatusOutput(self.cmd1, self.cmd1_simout) self.assertEquals(result, self.cmd1_sim_result[4])
def formatDevice(self, device): __cmd = self.getMkfsCmd() + self.getOptionsString() + device.getDevicePath() __rc, __ret = ComSystem.execLocalStatusOutput(__cmd) #self.getLog().debug("formatDevice: \n" , __cmd + __ret) if __rc != 0: raise ComException(__cmd + __ret)
def testExecLocalStatusOutput(self): ComSystem.setExecMode(None) result = ComSystem.execLocalStatusOutput(self.cmd1, self.cmd1_simout) self.assertEquals(result, self.cmd1_result[4])
def testExecLocalStatusOutputFalse(self): ComSystem.setExecMode(None) result = ComSystem.execLocalStatusOutput("/bin/false", "output of /bin/false, execLocalStatusOutput") self.assertEquals(result, (256, ""))
def testExecLocalStatusOutputSim(self): ComSystem.setExecMode(ComSystem.SIMULATE) result = ComSystem.execLocalStatusOutput(self.cmd1, self.cmd1_simout) self.assertEquals(result, self.cmd1_sim_result[4])
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)
def testExecLocalStatusOutputFalse(self): ComSystem.setExecMode(None) result = ComSystem.execLocalStatusOutput( "/bin/false", "output of /bin/false, execLocalStatusOutput") self.assertEquals(result, (256, ""))
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()) if not os.path.exists(self.src): raise CdslDoesNotExistException("File %s for cdsl does not exist (cwd: %s). Cannot create." %(self.src, self.getBasePath())) self.cdslRepository.updateInfrastructure(nodes=self.getNodenames()) # _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 = "" # 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) # if symlink and relative adapt to the relativness. if os.path.islink(self.src) and not self.src.startswith(os.sep): self.logger.debug("Creating link from %s => %s" %(os.path.join(_relativepath, self.src), destpath)) ComSystem.execMethod(os.symlink, os.path.join(self._pathdepth(self.getCDSLLinkPath())*"../", os.path.realpath(self.src)[1:]), destpath) else: # Let's copy the data self.logger.debug("Copy Files: " + self.src + " => " + destpath) ComSystem.execLocalStatusOutput("cp -a " + self.src + " " + destpath) # if cdsl is shared we need to copy only once. if self.isShared(): break # 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.isdir(sourcepath): ComSystem.execMethod(shutil.rmtree, sourcepath) if os.path.exists(sourcepath): ComSystem.execMethod(os.remove, 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)