예제 #1
0
    def _low_level_execute_command(self,
                                   cmd,
                                   sudoable=True,
                                   in_data=None,
                                   executable=None,
                                   encoding_errors='surrogate_then_replace',
                                   chdir=None):
        """
        Replace the mad rat's nest of logic in the base implementation by
        simply calling helpers.exec_command() in the target context.
        """
        if executable is None:  # executable defaults to False
            executable = self._play_context.executable
        if executable:
            cmd = executable + ' -c ' + commands.mkarg(cmd)

        rc, stdout, stderr = self.call(
            ansible_mitogen.helpers.exec_command,
            cast(cmd),
            cast(in_data),
            chdir=cast(chdir),
        )
        stdout_text = to_text(stdout, errors=encoding_errors)

        return {
            'rc': rc,
            'stdout': stdout_text,
            'stdout_lines': stdout_text.splitlines(),
            'stderr': stderr,
        }
예제 #2
0
    def put_data(self, out_path, data):
        """
        Implement put_file() by caling the corresponding
        ansible_mitogen.helpers function in the target.

        :param str in_path:
            Local filesystem path to read.
        :param str out_path:
            Remote filesystem path to write.
        """
        self.call(ansible_mitogen.helpers.write_path,
                  cast(out_path), cast(data))
예제 #3
0
    def exec_command(self, cmd, in_data='', sudoable=True):
        """
        Implement exec_command() by calling the corresponding
        ansible_mitogen.helpers function in the target.

        :param str cmd:
            Shell command to execute.
        :param bytes in_data:
            Data to supply on ``stdin`` of the process.
        :returns:
            (return code, stdout bytes, stderr bytes)
        """
        return self.call(ansible_mitogen.helpers.exec_command,
                         cast(cmd), cast(in_data))
예제 #4
0
    def _execute_module(self,
                        module_name=None,
                        module_args=None,
                        tmp=None,
                        task_vars=None,
                        persist_files=False,
                        delete_remote_tmp=True,
                        wrap_async=False):
        """
        Collect up a module's execution environment then use it to invoke
        helpers.run_module() or helpers.run_module_async() in the target
        context.
        """
        if task_vars is None:
            task_vars = {}
        if module_name is None:
            module_name = self._task.action
        if module_args is None:
            module_args = self._task.args

        self._update_module_args(module_name, module_args, task_vars)
        if wrap_async:
            helper = ansible_mitogen.helpers.run_module_async
        else:
            helper = ansible_mitogen.helpers.run_module

        env = {}
        self._compute_environment_string(env)

        js = self.call(
            helper,
            get_command_module_name(module_name),
            args=cast(module_args),
            env=cast(env),
        )

        data = self._parse_returned_data({
            'rc': 0,
            'stdout': js,
            'stdout_lines': [js],
            'stderr': ''
        })

        # Cutpasted from the base implementation.
        if 'stdout' in data and 'stdout_lines' not in data:
            data['stdout_lines'] = (data['stdout'] or u'').splitlines()
        if 'stderr' in data and 'stderr_lines' not in data:
            data['stderr_lines'] = (data['stderr'] or u'').splitlines()

        return data
예제 #5
0
    def _connect_sudo(self, via=None, python_path=None):
        """
        Fetch a reference to a sudo Context matching the play context from
        ContextService in the master process.

        :param via:
            Parent Context of the sudo Context. For Ansible, this should always
            be a Context returned by _connect_ssh().
        """
        return mitogen.service.call(
            self.parent,
            ContextService.handle,
            cast({
                'method': 'sudo',
                'username': self._play_context.become_user,
                'password': self._play_context.password,
                'python_path': python_path or self.python_path,
                'sudo_path': self.sudo_path,
                'connect_timeout': self._play_context.timeout,
                'via': via,
                'sudo_args': shlex.split(
                    self._play_context.sudo_flags or
                    self._play_context.become_flags or ''
                ),
            })
        )
예제 #6
0
 def _connect_ssh(self):
     """
     Fetch a reference to an SSH Context matching the play context from
     ContextService in the master process.
     """
     return mitogen.service.call(
         self.parent,
         ContextService.handle,
         cast({
             'method': 'ssh',
             'check_host_keys': False,  # TODO
             'hostname': self._play_context.remote_addr,
             'discriminator': self.mitogen_ssh_discriminator,
             'username': self._play_context.remote_user,
             'password': self._play_context.password,
             'port': self._play_context.port,
             'python_path': self.python_path,
             'identity_file': self._play_context.private_key_file,
             'ssh_path': self._play_context.ssh_executable,
             'connect_timeout': self.ansible_ssh_timeout,
             'ssh_args': [
                 term
                 for s in (
                     getattr(self._play_context, 'ssh_args', ''),
                     getattr(self._play_context, 'ssh_common_args', ''),
                     getattr(self._play_context, 'ssh_extra_args', '')
                 )
                 for term in shlex.split(s or '')
             ]
         })
     )
예제 #7
0
 def _connect_local(self):
     """
     Fetch a reference to the local() Context from ContextService in the
     master process.
     """
     return mitogen.service.call(self.parent, ContextService.handle, cast({
         'method': 'local',
     }))
예제 #8
0
 def _connect_docker(self):
     return mitogen.service.call(
         self.parent, ContextService.handle,
         cast({
             'method': 'docker',
             'container': self._play_context.remote_addr,
             'python_path': self.python_path,
             'connect_timeout': self._play_context.timeout,
         }))
예제 #9
0
    def fetch_file(self, in_path, out_path):
        """
        Implement fetch_file() by calling the corresponding
        ansible_mitogen.helpers function in the target.

        :param str in_path:
            Remote filesystem path to read.
        :param str out_path:
            Local filesystem path to write.
        """
        output = self.call(ansible_mitogen.helpers.read_path, cast(in_path))
        ansible_mitogen.helpers.write_path(out_path, output)
예제 #10
0
 def run(self, tmp=None, task_vars=None):
     job_id = self._task.args['jid']
     try:
         result = self._connection.call(
             ansible_mitogen.helpers.get_async_result,
             cast(job_id),
         )
     except mitogen.core.CallError, e:
         return {
             'ansible_job_id': job_id,
             'started': 1,
             'failed': 1,
             'finished': 1,
             'msg': str(e),
         }