示例#1
0
    def exec_command(self,
                     cmd,
                     in_data=None,
                     executable='/bin/sh',
                     sudoable=True):
        ''' run a command in the zone '''
        slpcmd = False

        if '&& sleep 0' in cmd:
            slpcmd = True
            cmd = self._strip_sleep(cmd)

        if 'sudo' in cmd:
            cmd = self._strip_sudo(executable, cmd)

        #cmd = ' '.join([executable, '-c', pipes.quote(cmd)])
        cmd = pipes.quote(cmd)
        if slpcmd:
            cmd = '%s %s %s %s' % (self.get_zlogin(), self.get_zone_name(),
                                   cmd, '&& sleep 0')
        else:
            cmd = '%s %s %s' % (self.get_zlogin(), self.get_zone_name(), cmd)

        if self._play_context.become:
            # display.debug("_low_level_execute_command(): using become for this command")
            # cmd = self._play_context.make_become_cmd(cmd)
            plugin = self.become
            shell = get_shell_plugin(executable=executable)
            cmd = plugin.build_become_command(cmd, shell)

        # display.vvv("ZONE (%s) %s" % (local_cmd), host=self.host)
        return super(Connection, self).exec_command(cmd, in_data, True)
示例#2
0
    def __init__(self, play_context, new_stdin, shell=None, *args, **kwargs):

        super(ConnectionBase, self).__init__()

        # All these hasattrs allow subclasses to override these parameters
        if not hasattr(self, '_play_context'):
            # Backwards compat: self._play_context isn't really needed, using set_options/get_option
            self._play_context = play_context
        if not hasattr(self, '_new_stdin'):
            self._new_stdin = new_stdin
        if not hasattr(self, '_display'):
            # Backwards compat: self._display isn't really needed, just import the global display and use that.
            self._display = display
        if not hasattr(self, '_connected'):
            self._connected = False

        self.success_key = None
        self.prompt = None
        self._connected = False
        self._socket_path = None

        # helper plugins
        self._shell = shell

        # we always must have shell
        if not self._shell:
            self._shell = get_shell_plugin(shell_type=getattr(self, '_shell_type', None), executable=self._play_context.executable)

        self.become = None
示例#3
0
    def _copy_file(self, from_file, to_file, executable='/bin/sh'):
        plugin = self.become
        shell = get_shell_plugin(executable=executable)
        copycmd = plugin.build_become_command(
            ' '.join(['cp', from_file, to_file]), shell)

        display.vvv(u"REMOTE COPY {0} TO {1}".format(from_file, to_file),
                    host=self.inventory_hostname)
        code, stdout, stderr = self._jailhost_command(copycmd)
        if code != 0:
            raise AnsibleError("failed to copy file from %s to %s:\n%s\n%s" %
                               (from_file, to_file, stdout, stderr))
示例#4
0
    def make_become_cmd(self, cmd, executable=None):
        """ helper function to create privilege escalation commands """
        display.deprecated(
            "PlayContext.make_become_cmd should not be used, the calling code should be using become plugins instead",
            version="2.12",
            collection_name='ansible.builtin')

        if not cmd or not self.become:
            return cmd

        become_method = self.become_method

        # load/call become plugins here
        plugin = self._become_plugin

        if plugin:
            options = {
                'become_exe': self.become_exe or become_method,
                'become_flags': self.become_flags or '',
                'become_user': self.become_user,
                'become_pass': self.become_pass
            }
            plugin.set_options(direct=options)

            if not executable:
                executable = self.executable

            shell = get_shell_plugin(executable=executable)
            cmd = plugin.build_become_command(cmd, shell)
            # for backwards compat:
            if self.become_pass:
                self.prompt = plugin.prompt
        else:
            raise AnsibleError("Privilege escalation method not found: %s" %
                               become_method)

        return cmd
示例#5
0
def call_become_plugin(task, var_options, cmd, executable=None):
    """Helper function to call become plugin simiarly on how Ansible itself handles this."""
    plugin = become_loader.get(task['become_method'])
    plugin.set_options(task_keys=task, var_options=var_options)
    shell = get_shell_plugin(executable=executable)
    return plugin.build_become_command(cmd, shell)