Ejemplo n.º 1
0
    def run(self, resource, *args, **kwargs):
        log.debug("RAW SSH: %s", args)

        commands = []
        prefix = []
        if kwargs.get('use_sudo', False):
            prefix.append('sudo')

        if kwargs.get('cwd'):
            cmd = prefix + ['cd', kwargs['cwd']]
            commands.append(' '.join(cmd))

        env = []
        if 'env' in kwargs:
            for key, value in kwargs['env'].items():
                env.append('{}={}'.format(key, value))

        cmd = prefix + env + list(args)
        commands.append(' '.join(cmd))

        remote_cmd = '\"%s\"' % ' && '.join(commands)

        settings = self.settings(resource)
        ssh_cmd = self._ssh_cmd(settings)
        ssh_cmd += (self._ssh_command_host(settings), remote_cmd)

        log.debug("RAW SSH CMD: %r", ssh_cmd)
        # TODO convert it to SolarRunResult

        return execute(' '.join(ssh_cmd), shell=True)
Ejemplo n.º 2
0
    def copy(self, resource, _from, _to, use_sudo=False):
        log.debug("RSYNC: %s -> %s", _from, _to)
        if use_sudo:
            rsync_path = "sudo rsync"
        else:
            rsync_path = "rsync"
        rsync_props = self._rsync_props(resource)
        rsync_cmd = ('rsync -az -e "ssh -i %(ssh_key)s" '
                     '--rsync-path="%(rsync_path)s" %(_from)s '
                     '%(rsync_host)s:%(_to)s') % dict(
                         rsync_path=rsync_path,
                         ssh_key=rsync_props['ssh_key'],
                         rsync_host=rsync_props['host_string'],
                         _from=_from,
                         _to=_to)

        rsync_executor = lambda transport: execute(
            rsync_cmd, shell=True)

        log.debug("RSYNC CMD: %r" % rsync_cmd)

        executor = Executor(resource=resource,
                            executor=rsync_executor,
                            params=(_from, _to, use_sudo))
        self.executors.append(executor)
Ejemplo n.º 3
0
    def copy(self, resource, _from, _to, use_sudo=False):
        log.debug("RSYNC: %s -> %s", _from, _to)
        if use_sudo:
            rsync_path = "sudo rsync"
        else:
            rsync_path = "rsync"

        rsync_props = self._rsync_props(resource)
        ssh_cmd = ' '.join(self._ssh_cmd(rsync_props))
        rsync_cmd = ('rsync -az -e "%(ssh_cmd)s" '
                     '--rsync-path="%(rsync_path)s" %(_from)s '
                     '%(rsync_host)s:%(_to)s') % dict(
                         rsync_path=rsync_path,
                         ssh_cmd=ssh_cmd,
                         rsync_host=rsync_props['host_string'],
                         _from=_from,
                         _to=_to)

        if rsync_props.get('ssh_password'):
            env = os.environ.copy()
            env['SSHPASS'] = rsync_props['ssh_password']
        else:
            env = os.environ

        rsync_executor = lambda transport: execute(
            rsync_cmd, shell=True, env=env)

        log.debug("RSYNC CMD: %r" % rsync_cmd)

        executor = Executor(resource=resource,
                            executor=rsync_executor,
                            params=(_from, _to, use_sudo))
        self.executors.append(executor)
Ejemplo n.º 4
0
 def action(self, resource, action):
     call_args = self.prepare(resource, action)
     log.debug('EXECUTING: %s', ' '.join(call_args))
     ret, out, err = execute(call_args)
     if ret == 0:
         return
     else:
         # ansible returns errors on stdout
         raise errors.SolarError(out)
Ejemplo n.º 5
0
 def action(self, resource, action):
     call_args = self.prepare(resource, action)
     log.debug('EXECUTING: %s', ' '.join(call_args))
     ret, out, err = execute(call_args)
     if ret == 0:
         return
     else:
         # ansible returns errors on stdout
         raise errors.SolarError(out)
Ejemplo n.º 6
0
    def action(self, resource, action_name):
        inventory_file = self._create_inventory(resource)
        playbook_file = self._create_playbook(resource, action_name)
        log.debug('inventory_file: %s', inventory_file)
        log.debug('playbook_file: %s', playbook_file)

        lib_path = self.get_library_path()
        call_args = ['ansible-playbook', '--module-path', lib_path,
                     '-i', inventory_file, playbook_file]
        log.debug('EXECUTING: %s', ' '.join(call_args))

        ret, out, err = execute(call_args)
        if ret == 0:
            return
        else:
            # ansible returns errors on stdout
            raise errors.SolarError(out)
Ejemplo n.º 7
0
    def run(self, resource, *args, **kwargs):
        log.debug("RAW SSH: %s", args)

        # TODO: check for better sanitization
        args = map(lambda x: x.replace('"', '\\"'), args)

        commands = []
        prefix = []
        if kwargs.get('use_sudo', False):
            prefix.append('sudo')

        if kwargs.get('cwd'):
            cmd = prefix + ['cd', kwargs['cwd']]
            commands.append(' '.join(cmd))

        env = []
        if 'env' in kwargs:
            for key, value in kwargs['env'].items():
                env.append('{}={}'.format(key, value))

        cmd = prefix + env + list(args)
        commands.append(' '.join(cmd))

        remote_cmd = '\"%s\"' % ' && '.join(commands)

        settings = self.settings(resource)
        if settings.get('password'):
            env = os.environ.copy()
            env['SSHPASS'] = settings['password']
        else:
            env = os.environ
        ssh_cmd = self._ssh_cmd(settings)
        ssh_cmd += (self._ssh_command_host(settings), remote_cmd)

        log.debug("RAW SSH CMD: %r", ssh_cmd)
        # TODO convert it to SolarRunResult

        res = execute(' '.join(ssh_cmd), shell=True, env=env)
        log.debug("Remote SSH result: %r", res)
        return SolarTransportResult.from_tuple(*res)
Ejemplo n.º 8
0
    def copy(self, resource, _from, _to, use_sudo=False):
        log.debug("RSYNC: %s -> %s", _from, _to)
        if os.path.isdir(_from):
            r_dir_path = _to
        else:
            r_dir_path = _to.rsplit('/', 1)[0]
        if use_sudo:
            rsync_path = "sudo mkdir -p {} && sudo rsync".format(r_dir_path)
        else:
            rsync_path = "mkdir -p {} && rsync".format(r_dir_path)

        rsync_props = self._rsync_props(resource)
        ssh_cmd = ' '.join(self._ssh_cmd(rsync_props))
        rsync_cmd = ('rsync -az -e "%(ssh_cmd)s" '
                     '--rsync-path="%(rsync_path)s" %(_from)s '
                     '%(rsync_host)s:%(_to)s') % dict(
                         rsync_path=rsync_path,
                         ssh_cmd=ssh_cmd,
                         rsync_host=rsync_props['host_string'],
                         _from=_from,
                         _to=_to)

        if rsync_props.get('ssh_password'):
            env = os.environ.copy()
            env['SSHPASS'] = rsync_props['ssh_password']
        else:
            env = os.environ

        rsync_executor = lambda transport: execute(
            rsync_cmd, shell=True, env=env)

        log.debug("RSYNC CMD: %r" % rsync_cmd)

        executor = Executor(resource=resource,
                            executor=rsync_executor,
                            params=(_from, _to, use_sudo))
        self.executors.append(executor)