Esempio n. 1
0
    def apply(self, context, output):
        if is_installed(context, self.resource):
            return False

        env = {
            "DEBIAN_FRONTEND": "noninteractive",
        }

        # the search returned 1, package is not installed, continue and install
        # it
        command = ["apt-get", "install", "-q",
                   "-y", self.resource.name.as_string()]

        try:
            context.change(ShellCommand(command, env=env))
        except error.SystemError as exc:
            if exc.returncode == 100:
                try:
                    context.change(
                        ShellCommand(["apt-get", "update", "-q", "-y"], env=env))
                    context.change(ShellCommand(command, env=env))
                except error.SystemError as exc:
                    raise error.AptError(
                        "%s with what looked like a recoverable error, but it wasn't (return code %d)" %
                        (self.resource, exc.returncode))
            else:
                raise error.AptError(
                    "%s failed with return code %d" %
                    (self.resource, exc.returncode))

        return True
Esempio n. 2
0
 def action(self, context, action, *args):
     context.change(
         ShellCommand(
             self.get_hg_command(action, *args),
             user=self.resource.user.as_string(),
             cwd=self.resource.name.as_string(),
         ))
Esempio n. 3
0
    def apply(self, context, output):
        changed = False
        info = self.get_group_info(context)

        if info["exists"]:
            command = ["groupmod"]
        else:
            command = ["groupadd"]
            changed = True

        gid = self.resource.gid.resolve()
        if gid and info["gid"] != gid:
            command.extend(["--gid", self.resource.gid])

        command.extend([self.resource.name])

        if not changed:
            return False

        try:
            context.change(ShellCommand(command))
        except error.SystemError as exc:
            raise error.InvalidGroup("%s on %s failed with return code %d" %
                                     (command[0], self.resource, exc.returncode))

        return True
Esempio n. 4
0
    def apply(self, context, output):
        name = self.resource.name.as_string()

        self.check_path(context, name)

        try:
            self.get_mount(context, name)
            return

        except KeyError:
            command = ["mount"]

            fs_type = self.resource.fs_type.as_string()
            if fs_type:
                if fs_type == "bind":
                    command.append("--bind")
                else:
                    command.extend(("-t", fs_type))
            command.append(self.resource.device)
            command.append(self.resource.name)

            options = self.resource.options.resolve()
            if options:
                command.extend(("-o", options))

            context.change(ShellCommand(command=command, ))
            return True
Esempio n. 5
0
    def apply(self, context, output):
        name = self.resource.name.as_string()

        if context.transport.lexists(name):
            if not context.transport.islink(name):
                raise error.InvalidProvider("%r: %s exists and is not a link" %
                                            (self, name))
            context.change(ShellCommand(["/bin/rm", self.resource.name]))
            return True
        return False
Esempio n. 6
0
 def apply(self, context, output):
     name = self.resource.name.as_string()
     if context.transport.exists(name):
         if not context.transport.isfile(name):
             raise error.InvalidProvider(
                 "%s exists and is not a file" % name)
         context.change(ShellCommand(["rm", self.resource.name]))
         changed = True
     else:
         output.debug(
             "File %s missing already so not removed" % name)
         changed = False
     return changed
Esempio n. 7
0
    def apply(self, context, output):
        name = self.resource.name.as_string()

        if context.transport.exists(
                name) and not context.transport.isdir(name):
            raise error.InvalidProviderError(
                "%r: %s exists and is not a directory" % (self, name))
        if context.transport.exists(name):
            context.change(ShellCommand(["/bin/rmdir", self.resource.name]))
            changed = True
        else:
            changed = False
        return changed
Esempio n. 8
0
    def _update_links(self, context, goal):
        # We turn our "goal" symlinks into a set and use a glob to get a set of
        # all symlinks in an rc.d directory for the current service name
        # The difference between the 2 sets are the links we need to create
        # and the links we need to remove
        target = set(goal)
        current = set(
            glob.glob("/etc/rc*.d/[SK][0-9][0-9]%s" % self.resource.name.as_string()))

        need_deleting = current - target
        need_creating = target - current

        if not need_deleting and not need_creating:
            return False

        for ln in need_deleting:
            context.change(ShellCommand(["rm", ln]))

        for ln in need_creating:
            context.change(
                ShellCommand(["ln", "-s", "/etc/init.d/%s" % self.resource.name.as_string(), ln]))

        return True
Esempio n. 9
0
    def apply(self, context, output):
        try:
            context.transport.getpwnam(
                self.resource.name.as_string().encode("utf-8"))
        except KeyError:
            # If we get a key errror then there is no such user. This is good.
            return False

        command = ["userdel", self.resource.name]

        try:
            context.change(ShellCommand(command))
        except error.SystemError as exc:
            raise error.UserAddError(
                "Removing user %s failed with return code %d" % (self.resource, exc.returncode))

        return True
Esempio n. 10
0
    def apply(self, context, output):
        try:
            context.transport.getgrnam(
                self.resource.name.as_string().encode("utf-8"))
        except KeyError:
            # If we get a key errror then there is no such group. This is good.
            return False

        command = ["groupdel", self.resource.name]

        try:
            context.change(ShellCommand(command))
        except error.SystemError as exc:
            raise error.InvalidGroup(
                "groupdel on %s failed with return code %d" % (self.resource, exc.returncode))

        return True
Esempio n. 11
0
    def apply(self, context, output):
        if not is_installed(context, self.resource):
            return False

        env = {
            "DEBIAN_FRONTEND": "noninteractive",
        }

        command = ["apt-get", "remove", "-q", "-y"]
        if self.resource.purge.as_bool():
            command.append("--purge")
        command.append(self.resource.name.as_string())

        try:
            context.change(ShellCommand(command, env=env))
        except error.SystemError as exc:
            raise error.AptError(
                "%s failed to uninstall with return code %d" % (self.resource, exc.returncode))

        return True
Esempio n. 12
0
 def do(self, context, action):
     try:
         context.change(ShellCommand(self.get_command(action)))
     except error.SystemError as exc:
         raise error.CommandError(
             "%s failed with return code %d" % (action, exc.returncode))
Esempio n. 13
0
    def apply(self, context, output):
        changed = False
        name = self.resource.name.as_string()
        to = self.resource.to.as_string()
        uid = None
        gid = None
        mode = None
        isalink = False

        if not context.transport.exists(to):
            if not context.simulate:
                raise error.DanglingSymlink(
                    "Destination of symlink %r does not exist" % to)
            output.info("Destination of sylink %r does not exist" % to)

        owner = self._get_owner(context)
        group = self._get_group(context)

        try:
            linkto = context.transport.readlink(name)
            isalink = True
        except OSError:
            isalink = False

        if not isalink or linkto != to:
            if context.transport.lexists(name):
                context.change(
                    ShellCommand(["/bin/rm", "-rf", self.resource.name]))

            context.change(
                ShellCommand(
                    ["/bin/ln", "-s", self.resource.to, self.resource.name]))
            changed = True

        try:
            linkto = context.transport.readlink(name)
            isalink = True
        except OSError:
            isalink = False

        if not isalink and not context.simulate:
            raise error.OperationFailed(
                "Did not create expected symbolic link")

        if isalink:
            uid, gid, mode = self._stat(context)

        if owner and owner != uid:
            context.change(
                ShellCommand([
                    "/bin/chown", "-h", self.resource.owner, self.resource.name
                ]))
            changed = True

        if group and group != gid:
            context.change(
                ShellCommand([
                    "/bin/chgrp", "-h", self.resource.group, self.resource.name
                ]))
            changed = True

        return changed
Esempio n. 14
0
    def apply(self, context, output):
        creates = self.resource.creates.as_string()
        if creates and context.transport.exists(creates):
            #logging.info("%r: %s exists, not executing" % (self.resource, self.resource.creates))
            return False

        touch = self.resource.touch.as_string()
        if touch and context.transport.exists(touch):
            return False

        unless = self.resource.unless.as_string()
        if unless:
            try:
                if context.transport.execute(
                        unless,
                        user=self.resource.user.as_string(),
                        cwd=self.resource.cwd.as_string(),
                )[0] == 0:
                    return False

            except error.InvalidUser as exc:
                # If a simulation and user missing then we can run our 'unless'
                # guard. We bail out with True so that Yaybu treates the
                # resource as applied.
                if context.simulate:
                    output.info(
                        "User '%s' not found; assuming this recipe will create it"
                        % self.resource.user.as_string())
                    return True
                raise

            except error.InvalidGroup as exc:
                # If a simulation and group missing then we can run our 'unless'
                # guard. We bail out with True so that Yaybu treates the
                # resource as applied.
                if context.simulate:
                    output.info(
                        "Group '%s' not found; assuming this recipe will create it"
                        % self.resource.group.as_string())
                    return True
                raise

        command = self.resource.command.as_string()
        if command:
            commands = [self.resource.command]
        else:
            commands = list(self.resource.commands.get_iterable())

        for command in commands:
            try:
                context.change(
                    ShellCommand(
                        command,
                        cwd=self.resource.cwd.as_string() or None,
                        env=self.resource.environment.resolve() or None,
                        user=self.resource.user.as_string(),
                        group=self.resource.group.as_string() or None,
                        umask=self.resource.umask.as_int(),
                    ))
            except error.SystemError as exc:
                returncode = self.resource.returncode.as_int(default=0)
                rc = exc.returncode
                if rc != returncode:
                    raise error.CommandError("%s failed with return code %d" %
                                             (self.resource, rc))

        if self.resource.touch.as_string():
            context.change(ShellCommand(["touch", self.resource.touch]))

        return True
Esempio n. 15
0
    def apply(self, context, output):
        info = self.get_user_info(context)

        if info['exists']:
            command = ['usermod']
            changed = False  # we may not change anything yet
        else:
            command = ['useradd', '-N']
            changed = True  # we definitely make a change

        name = self.resource.name.as_string()

        fullname = self.resource.fullname.as_string(default='')
        if fullname and info["gecos"] != fullname:
            command.extend(["--comment", self.resource.fullname])
            changed = True

        password = self.resource.password.as_string(default='')
        if password and not info["exists"]:
            command.extend(["--password", self.resource.password])
            changed = True

        home = self.resource.home.as_string(default='')
        if home and info["dir"] != home:
            command.extend(["--home", self.resource.home])
            changed = True

        uid = self.resource.uid.as_string(default='')
        if uid and info["uid"] != int(uid):
            command.extend(["--uid", self.resource.uid])
            changed = True

        gid = self.resource.gid.as_string(default='')
        group = self.resource.group.as_string(default='')
        if gid or group:
            if gid:
                gid = int(gid)
                if gid != info["gid"]:
                    command.extend(["--gid", self.resource.gid])
                    changed = True
            else:
                try:
                    gid = context.transport.getgrnam(group).gr_gid
                except KeyError:
                    if not context.simulate:
                        raise error.InvalidGroup(
                            "Group '%s' is not valid" % group)
                    output.info(
                        "Group '%s' doesn't exist; assuming recipe already created it" % group)
                    gid = "GID_CURRENTLY_UNASSIGNED"

                if gid != info["gid"]:
                    command.extend(["--gid", str(gid)])
                    changed = True

        groups = self.resource.groups.resolve()  # as_list(default=[])
        if groups:
            desired_groups = set(groups)
            current_groups = set(
                g.gr_name for g in context.transport.getgrall() if name in g.gr_mem)

            append = self.resource.append.resolve()
            if append and len(desired_groups - current_groups) > 0:
                if info["exists"]:
                    command.append("-a")
                command.extend(
                    ["-G", ",".join(desired_groups - current_groups)])
                changed = True
            elif not append and desired_groups != current_groups:
                command.extend(["-G", ",".join(desired_groups)])
                changed = True

        shell = self.resource.shell.as_string(default='')
        if shell and shell != info["shell"]:
            command.extend(["--shell", str(self.resource.shell)])
            changed = True

        disabled_login = self.resource.disabled_login.resolve()
        if disabled_login and not info["disabled-login"]:
            command.extend(["--password", "!"])
            changed = True

        system = self.resource.system.resolve()
        if not info["exists"] and system:
            command.extend(["--system"])
            changed = True

        command.extend(["-m", self.resource.name])

        if changed:
            try:
                context.change(ShellCommand(command))
            except error.SystemError as exc:
                raise error.UserAddError(
                    "useradd returned error code %d" % exc.returncode)
        return changed
Esempio n. 16
0
 def svn(self, context, action, *args, **kwargs):
     command = self.get_svn_args(action, *args, **kwargs)
     sc = ShellCommand(command, user=self.resource.user.as_string())
     context.change(sc)
     return sc.returncode, sc.stdout, sc.stderr
Esempio n. 17
0
    def apply(self, context, output):
        for w in self.resource.watch.as_list():
            if context.transport.exists(w):
                context.change(ShellCommand(["touch", "-ac", w]))

        return True