示例#1
0
    def action_create(self):
        path = self.resource.path

        if sudo.path_lexists(path):
            oldpath = os.path.realpath(path)
            if oldpath == self.resource.to:
                return
            if not sudo.path_islink(path):
                raise Fail(
                    "%s trying to create a symlink with the same name as an existing file or directory"
                    % self.resource)
            Logger.info("%s replacing old symlink to %s" %
                        (self.resource, oldpath))
            sudo.unlink(path)

        if self.resource.hard:
            if not sudo.path_exists(self.resource.to):
                raise Fail(
                    "Failed to apply %s, linking to nonexistent location %s" %
                    (self.resource, self.resource.to))
            if sudo.path_isdir(self.resource.to):
                raise Fail(
                    "Failed to apply %s, cannot create hard link to a directory (%s)"
                    % (self.resource, self.resource.to))

            Logger.info("Creating hard %s" % self.resource)
            sudo.link(self.resource.to, path)
        else:
            if not sudo.path_exists(self.resource.to):
                Logger.info("Warning: linking to nonexistent location %s" %
                            self.resource.to)

            Logger.info("Creating symbolic %s to %s" %
                        (self.resource, self.resource.to))
            sudo.symlink(self.resource.to, path)
示例#2
0
    def action_create(self):
        path = self.resource.path

        if not sudo.path_exists(path):
            Logger.info("Creating directory %s since it doesn't exist." %
                        self.resource)

            # dead links should be followed, else we gonna have failures on trying to create directories on top of them.
            if self.resource.follow:
                # Follow symlink until the tail.
                followed_links = set()
                while sudo.path_islink(path):
                    if path in followed_links:
                        raise Fail(
                            "Applying %s failed, looped symbolic links found while resolving %s"
                            % (self.resource, path))
                    followed_links.add(path)
                    prev_path = path
                    path = sudo.readlink(path)

                    if not os.path.isabs(path):
                        path = os.path.join(os.path.dirname(prev_path), path)

                if path != self.resource.path:
                    Logger.info(
                        "Following the link {0} to {1} to create the directory"
                        .format(self.resource.path, path))

            if self.resource.create_parents:
                sudo.makedirs(path, self.resource.mode or 0755)
            else:
                dirname = os.path.dirname(path)
                if not sudo.path_isdir(dirname):
                    raise Fail(
                        "Applying %s failed, parent directory %s doesn't exist"
                        % (self.resource, dirname))

                try:
                    sudo.makedir(path, self.resource.mode or 0755)
                except Exception as ex:
                    # race condition (somebody created the file before us)
                    if "File exists" in str(ex):
                        sudo.makedirs(path, self.resource.mode or 0755)
                    else:
                        raise

        if not sudo.path_isdir(path):
            raise Fail("Applying %s failed, file %s already exists" %
                       (self.resource, path))

        _ensure_metadata(
            path,
            self.resource.owner,
            self.resource.group,
            mode=self.resource.mode,
            cd_access=self.resource.cd_access,
            recursive_ownership=self.resource.recursive_ownership,
            recursive_mode_flags=self.resource.recursive_mode_flags,
            recursion_follow_links=self.resource.recursion_follow_links,
            safemode_folders=self.resource.safemode_folders)