Exemple #1
0
    def __run_cmd(self, action, user, arguments=[], elevate=True):
        """
        Shortcut for running commands.

        Accepts the following parameters:
          * action: determines the command line utility to execute.
          * user: the user to perform the operation upon.
        """

        cmd = []
        cmd.append(where_is(self.ACTIONS[action]))
        cmd += arguments
        cmd.append(user)

        if elevate:
            cmd = elevate_cmd(cmd)

        proc = subprocess.Popen(cmd, stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)

        status = proc.wait()
        (stdout, stderr) = proc.communicate()

        return (status, stdout, stderr)
Exemple #2
0
def service_ctl(service, action, require_elevation=True, raise_exc=True):
    """
    Perform a service control action.

    Tolerant of both RHEL and Debian-style configurations.
    """

    try:
        service_util = where_is('service')
    except UtilityNotFoundError:
        try:
            service_util = where_is('invoke-rc.d')
        except UtilityNotFoundError:
            service_util = None

    if service_util:
        cmd = [service_util, service, action]
    else:
        cmd = ['/etc/init.d/%s' %(service), action]

    if require_elevation:
        cmd = elevate_cmd(cmd)

    result = subprocess.check_call(cmd)

    if result == 0:
        return True

    if raise_exc:
        raise ServiceControlError(service, action)
Exemple #3
0
    def provision(self):
        """
        Provision the service unit in a subprocess.

        Handle the initialisation of the provisioner utility and hand off the
        deployment of the unit to it. This is the method which should be called
        from agent modules, not do_provision().
        """

        # It's busy -- leave it well alone.
        #
        # Under normal circumstances it's impossible for this to happen, since
        # a call to wait() will block the thread. It may catch calls to other
        # threads, but we really need a true semaphore locking solution to
        # ensure robustness under load.
        try:
            if self.proc.poll() is None:
                raise ProvisioningError('attempted to provision whilst another '
                                        + 'operation was in progress')
        except AttributeError:
            pass

        # Copy value, don't get a reference.
        #
        # This isn't very "pythonic", but we need to ensure we don't taint the
        # base command just in case a developer reuses the provisioning class.
        cmd = deepcopy(self._base_cmd)

        cmd.append(self.module_name)
        cmd.extend(list(map(str, self.parameters)))

        self.proc = subprocess.Popen(elevate_cmd(cmd))
Exemple #4
0
    def allow_via_acl(self, user, group, path, mode):
        """
        Set ownership of a given path to a given user/group combination.

        The user and group parameters may be set to None where appropriate. Mode
        should be a string containing character names for filesystem permission
        bits (rwx) which should be set.
        """

        if user:
            acl = "user:{0}:{1}".format(group.group, mode)
            subprocess.Popen(elevate_cmd("setfacl", "-m", acl, path))

        if group:
            acl = "group:{0}:{1}".format(user.account, mode)
            subprocess.Popen(elevate_cmd("setfacl", "-m", acl, path))
Exemple #5
0
    def exec_cmd(self, cmd, expected_statuses=[0], elevate=True):
        """
        Run a command via the package manager.
        """

        if elevate:
            cmd = elevate_cmd(cmd)

        proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
        status = proc.wait()
        (stdout, stderr) = proc.communicate()

        if status not in expected_statuses:
            raise PackageManagerError(status, stderr)

        return (status, stdout, stderr)
Exemple #6
0
    def provision(self):
        """
        Provision the service unit in a subprocess.

        When called from within the agent, handles the initialisation of the
        provisioner utility and hand off the deployment of the unit to it. This
        is the method which should be called from AGENT modules, not
        do_provision().
        """

        # It's busy -- leave it well alone.
        #
        # Under normal circumstances it's impossible for this to happen, since
        # a call to wait() will block the thread. It may catch calls to other
        # threads, but we really need a true semaphore locking solution to
        # ensure robustness under load.
        try:
            if self.proc.poll() is None:
                raise ProvisioningError('attempted to provision whilst another '
                                        + 'operation was in progress')
        except AttributeError:
            pass

        self.cmd = deepcopy(self._base_cmd)
        try:
            self.cmd.append(self.config['provisioner']['binary'])
        except KeyError:
            self.cmd.append(join(dirname(sys.argv[0]), 'provisioner.py'))
        self.cmd.append('site')
        self.cmd.append(self.module_name)
        self.cmd.extend(self.parameters)

        with open(self._get_output_file_base() + ".out.log", "wb") as out:
            with open(self._get_output_file_base() + ".err.log", "wb") as err:
                proc = subprocess.Popen(elevate_cmd(self.cmd), env=environ,
                                        stdin=subprocess.PIPE,
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.PIPE)
                proc.wait()
                (out_buf, err_buf) = proc.communicate()
                out.write(out_buf)
                err.write(err_buf)
Exemple #7
0
 def reload_service(self):
     Popen(elevate_cmd(['service', 'nginx', 'reload']))