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))
Example #2
0
    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 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 ")
Example #4
0
    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 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 ")
Example #6
0
    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 __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 __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", self.path.getPath())
     PathCopyObject.logger.debug("prepareAsSource() CWD: " + os.getcwd())
    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 _removePath(self,path,onerror=None):
     """
     Removes given path or paths.
     @param path: Path to file or directory to delete
     @type path: string|list<string>
     """
     if isinstance(path, basestring):
         if os.path.exists(path) or os.path.islink(path):
             self.logger.debug("Remove " + path)
             if not os.path.islink(path) and os.path.isdir(path):
                 ComSystem.execMethod(shutil.rmtree, path)
             else:
                 ComSystem.execMethod(os.remove, path)
         else:
             if onerror:
                 onerror(path)
             else:
                 self.logger.debug("_removePath(%s) does not exist. Skipping." %path)
     else:
         for _path in path:
             self._removePath(_path)
 def _removePath(self, path, onerror=None):
     """
     Removes given path or paths.
     @param path: Path to file or directory to delete
     @type path: string|list<string>
     """
     if isinstance(path, basestring):
         if os.path.exists(path) or os.path.islink(path):
             self.logger.debug("Remove " + path)
             if not os.path.islink(path) and os.path.isdir(path):
                 ComSystem.execMethod(shutil.rmtree, path)
             else:
                 ComSystem.execMethod(os.remove, path)
         else:
             if onerror:
                 onerror(path)
             else:
                 self.logger.debug(
                     "_removePath(%s) does not exist. Skipping." % path)
     else:
         for _path in path:
             self._removePath(_path)
Example #12
0
 def doModifications(self, file):
     save = True
     if self.hasAttribute("nobackup"):
         if self.getAttribute("nobackup") == "1":
             save = False
     ComSystem.execMethod(self.doRegexpModifications, file, save)
 def exists(self):
     return ComSystem.execMethod(os.path.exists, self.getDeviceName())
Example #14
0
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 doModifications(self, file):
     save=True
     if self.hasAttribute("nobackup"):
         if self.getAttribute("nobackup") == "1":
             save=False
     ComSystem.execMethod(self.doRegexpModifications, file, save)
 def exists(self):
    return ComSystem.execMethod(os.path.exists, self.getDeviceName())
 def testExecMethodErr(self):
     ComSystem.setExecMode(None)
     result = ComSystem.execMethod(ComSystem.execLocalGetResult, self.cmd1, True)
     self.assertEquals(result, self.cmd1_result[3])
 def testExecMethodErrSim(self):
     ComSystem.setExecMode(ComSystem.SIMULATE)
     result = ComSystem.execMethod(ComSystem.execLocalGetResult, self.cmd1, True)
     self.assertEquals(result, True)
    def delete(self,recursive=True, symbolic=True, force=False):
        """
        Deletes cdsl from filesystem and inventoryfile
        @param recursive: if set delete subcdsls (e.g. /host/shared/host when removing /host/shared)
        @type  recursive: Boolean
        @param symbolic: If set remove only symbolic links, if not set also remove content, too. Default: True (means only symbolic)
        @type  symbolic: Boolean
        @param force: Removes the cdsl independent from if all contents can be found or not. Default: False
        @type  force: Boolean
        """
        from comoonics.ComPath import Path
        from comoonics.cdsl import getNodeFromPath, isSubPath, commonpath, subpathsto

        if not force and not self.exists():
            raise CdslDoesNotExistException("Cdsl with source " + self.src + " does not exist, cannot delete.")
        if not force and 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=recursive, force=force, symbolic=symbolic)
        
        if self.getChilds():
            raise CdslHasChildren("Cdsl %s has children but no recursive option specified." %self.src)
        _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
            if os.path.lexists(_path):
                _delpaths.append(_path)

        for _path in self.getDestPaths():
            if not symbolic:
                _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, not force)
                    _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()        parent2nd=None
        parent=self.getParent()
            
        subpaths=list()
        siblings=self.getSiblings()
        if len(siblings) > 0:
            longestcommon=""
            for sibling in siblings:
                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
        # * if we have a parent of same type and no siblings:  clean up to parent
        elif parent != None and parent.getParent() != None:
            for _path in subpathsto(parent.src, self.src):
                subpaths.append(_path)
        else:
            for _path in subpathsto("", self.src):
                subpaths.append(_path)
                
        for path in subpaths:
            if str(path) != str(self.src):
                for prefix in prefixes:
                    if os.path.lexists(os.path.join(prefix, path)):
                        _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 testExecMethodErrSim(self):
     ComSystem.setExecMode(ComSystem.SIMULATE)
     result = ComSystem.execMethod(ComSystem.execLocalGetResult, self.cmd1,
                                   True)
     self.assertEquals(result, True)
    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 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 prepareAsDest(self):
     """ writes all metadata to archive"""
     self.log.debug("prepareAsDest()")
     ComSystem.execMethod(self.serializer.serialize, self.metadata)
    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)
Example #25
0
 def prepareAsDest(self):
     ''' writes all metadata to archive'''
     self.log.debug("prepareAsDest()")
     ComSystem.execMethod(self.serializer.serialize, self.metadata)
 def testExecMethodErr(self):
     ComSystem.setExecMode(None)
     result = ComSystem.execMethod(ComSystem.execLocalGetResult, self.cmd1,
                                   True)
     self.assertEquals(result, self.cmd1_result[3])