Exemplo n.º 1
0
def hosts(hosts, remote_limit=25, remote_kwargs=None):
    """Wraps a deployment function of the form def task1(ctx, *args, **kwargs).
       After task is wrapped it will be called as task1(*args, **kwargs).

       The passed ctx objects will be set with a host from the hosts arg
    """
    hosts = listify(hosts)

    def wrapper(f):
        f = catch_badreturn(f)

        @wraps(f)
        def inner_wrapper(*args, **kwargs):
            logging.info('Running %s' % getattr(f, '__name__', repr(f)))
            t = ThreadPool(remote_limit)
            for host in hosts:
                ctx = Context(remote_kwargs=remote_kwargs)
                ctx.set_host(host)
                t.add_func(f, ctx, *args, **kwargs)
            start = time.time()
            t.run_all()
            end = time.time()
            logging.info('Finished %s (%0.3fs)' %
                         (getattr(f, '__name__', repr(f)), end - start))

        commands[f.__name__] = inner_wrapper
        return inner_wrapper

    return wrapper
Exemplo n.º 2
0
def hostgroups(groups, remote_limit=25, remote_kwargs=None):
    """The same as hosts, except for it accepts a hostgroup or list of
    hostgroups.
    """
    groups = listify(groups)
    hs = reduce(lambda x, y: x + y, [get_systems(group) for group in groups])
    return hosts(hs, remote_limit, remote_kwargs)
Exemplo n.º 3
0
def remote(hosts,
           cmd,
           jumphost=None,
           remote_limit=25,
           ssh_key=None,
           run_threaded=True,
           output=True):

    status = {}

    hosts = listify(hosts)

    if len(hosts) == 1 or remote_limit == 1:
        run_threaded = False

    if run_threaded:
        t = ThreadPool(remote_limit)
        for host in hosts:
            ssh_client = SSHExecClient(host, ssh_key, jumphost)
            t.add_func(_threaded_run, status, ssh_client, cmd, output=output)
        t.run_all()
    else:
        for host in hosts:
            ssh_client = SSHExecClient(host, ssh_key, jumphost)
            status[host] = _run_command(host,
                                        cmd,
                                        ssh_client.run,
                                        output=output)

    return status
Exemplo n.º 4
0
def hosts(hosts, remote_limit=25, remote_kwargs=None):
    """Wraps a deployment function of the form def task1(ctx, *args, **kwargs).
       After task is wrapped it will be called as task1(*args, **kwargs).

       The passed ctx objects will be set with a host from the hosts arg
    """
    hosts = listify(hosts)

    def wrapper(f):
        f = catch_badreturn(f)

        @wraps(f)
        def inner_wrapper(*args, **kwargs):
            logging.info('Running %s' % getattr(f, '__name__', repr(f)))
            t = ThreadPool(remote_limit)
            for host in hosts:
                ctx = Context(remote_kwargs=remote_kwargs)
                ctx.set_host(host)
                t.add_func(f, ctx, *args, **kwargs)
            start = time.time()
            t.run_all()
            end = time.time()
            finished = ('Finished %s (%0.3fs)' %
                        (getattr(f, '__name__', repr(f)), end - start))
            notify(finished)
            logging.info(finished)

        commands[f.__name__] = inner_wrapper
        return inner_wrapper
    return wrapper
Exemplo n.º 5
0
def hostgroups(groups, remote_limit=25, remote_kwargs=None):
    """The same as hosts, except for it accepts a hostgroup or list of
    hostgroups.
    """
    groups = listify(groups)
    hs = reduce(lambda x, y: x + y, [get_systems(group) for group in groups])
    return hosts(hs, remote_limit, remote_kwargs)
Exemplo n.º 6
0
def remote(hosts, cmd, jumphost=None,
           remote_limit=25, ssh_key=None, run_threaded=True, output=True):

    status = {}

    hosts = listify(hosts)

    if len(hosts) == 1 or remote_limit == 1:
        run_threaded = False

    if run_threaded:
        t = ThreadPool(remote_limit)
        for host in hosts:
            ssh_client = SSHExecClient(host, ssh_key, jumphost)
            t.add_func(_threaded_run, status, ssh_client, cmd, output=output)
        t.run_all()
    else:
        for host in hosts:
            ssh_client = SSHExecClient(host, ssh_key, jumphost)
            status[host] = _run_command(host, cmd, ssh_client.run,
                                        output=output)

    return status