Example #1
0
def background(endpoints, commands, **kwargs):
    """Similar to batch, but the :class:`Pool` opject is returned before
    reading the results, non-blocking

    :param endpoint: remote host or URI to connect to
    :param commands: command or commands to send
    :param creds: (optional) :class:`Creds <Creds>` object with authentication
                             credentials
    :param protocol: (optional) Protocol name, e.g. 'ssh' or 'eapi'
    :return: :class:`Pool <Pool>` object
    :rtype: arcomm.async.Pool

    Usage:
        >>> with arcomm.background('veos1', 'show version') as bg:
        ...     # do other things...
        ...
        >>> for res in bg:
        ...     print(res.to_yaml())
        ...
        host: vswitch1
        status: ok
        commands:
          - command: show version
            output: |
              Arista vEOS
              Hardware version:
              Serial number:
              System MAC address:  0800.2776.48c5
              [ ...output omitted... ]
    """

    endpoints = to_list(endpoints)
    return Pool(endpoints, commands, **kwargs)
Example #2
0
    def __init__(self, hosts, commands, **kwargs):

        #
        self._hosts = to_list(hosts)

        #
        self._commands = commands

        #
        self._pool_size = kwargs.pop('pool_size', None) or 10


        #
        self.background = kwargs.pop('background', False)

        #
        self._worker_kwargs = kwargs

        #
        self._results = IterQueue()

        #
        #self._async_result = None

        # initialize pool
        self._pool = multiprocessing.Pool(self._pool_size, _initialize_worker)
Example #3
0
def background(endpoints, commands, **kwargs):
    """Similar to batch, but the :class:`Pool` opject is returned before
    reading the results, non-blocking

    :param endpoint: remote host or URI to connect to
    :param commands: command or commands to send
    :param creds: (optional) :class:`Creds <Creds>` object with authentication
                             credentials
    :param protocol: (optional) Protocol name, e.g. 'ssh' or 'eapi'
    :return: :class:`Pool <Pool>` object
    :rtype: arcomm.async.Pool

    Usage:
        >>> with arcomm.background('veos1', 'show version') as bg:
        ...     # do other things...
        ...
        >>> for res in bg:
        ...     print(res.to_yaml())
        ...
        host: vswitch1
        status: ok
        commands:
          - command: show version
            output: |
              Arista vEOS
              Hardware version:
              Serial number:
              System MAC address:  0800.2776.48c5
              [ ...output omitted... ]
    """

    endpoints = to_list(endpoints)
    return Pool(endpoints, commands, **kwargs)
Example #4
0
def configure(uri, commands,  **kwargs):

    commands = to_list(commands)
    commands.insert(0, "configure")
    commands.append("end")

    execute(uri, commands,  **kwargs)
Example #5
0
    def __init__(self, cmd, prompt=None, answer=None):
        if isinstance(cmd, dict):
            prompt = cmd.get("prompt")
            answer = cmd.get("answer", cmd.get('input', ''))
            cmd = cmd.get("cmd", cmd.get('command'))
        assert isinstance(cmd, basestring), "cmd must be a string"

        self._store = dict()
        self.update(dict(cmd=cmd, prompt=to_list(prompt), answer=answer))
Example #6
0
    def __init__(self, cmd, prompt=None, answer=None):
        if isinstance(cmd, dict):
            prompt = cmd.get("prompt")
            answer = cmd.get("answer", cmd.get('input', ''))
            cmd = cmd.get("cmd", cmd.get('command'))
        assert isinstance(cmd, basestring), "cmd must be a string"

        self._store = dict()
        self.update(dict(cmd=cmd, prompt=to_list(prompt), answer=answer))
Example #7
0
def batch(endpoints, commands, **kwargs):
    endpoints = to_list(endpoints)

    #pool_size = kwargs.pop('pool_size', None) or 10
    delay = kwargs.pop('delay', None) or 0

    pool = Pool(endpoints, commands, **kwargs)
    pool.run(delay=delay)

    for item in pool.results:
        yield item
Example #8
0
def commands_from_list(commands):
    """Converts a command or list of commands to a list of Command objects"""
    commands = to_list(commands)
    cmdlist = []
    for cmd in commands:
        if not isinstance(cmd, Command):
            if re.search("^(!|#)", cmd) or re.search("^\s*$", cmd):
                continue
            cmd = Command(cmd.strip())
        cmdlist.append(cmd)
    return cmdlist
Example #9
0
def commands_from_list(commands):
    """Converts a command or list of commands to a list of Command objects"""
    commands = to_list(commands)
    cmdlist = []
    for cmd in commands:
        if not isinstance(cmd, Command):
            if re.search("^(!|#)", cmd) or re.search("^\s*$", cmd):
                continue
            cmd = Command(cmd.strip())
        cmdlist.append(cmd)
    return cmdlist
Example #10
0
def _to_commands(commands):
    """Converts a command or list of commands to a list of Command objects"""
    commands = to_list(commands)
    _loc = []
    for _cmd in commands:
        if not isinstance(_cmd, Command):
            if re.search("^(!|#)", _cmd) or re.search("^\s*$", _cmd):
                continue
            _cmd = Command(_cmd.strip())
        _loc.append(_cmd)
    return _loc
Example #11
0
    def _handle_input(self, response, prompt, answer):
        """look for interactive prompts and send answer"""
        if prompt is None or answer is None:
            return

        re_type = type(re.compile(r'^$'))

        prompt = to_list(prompt)
        answer = to_list(answer)

        if len(prompt) != len(answer):
            raise ValueError(("Lists of prompts and answers have different "
                              "lengths"))

        for _prompt, _answer in zip(prompt, answer):
            if not isinstance(_prompt, re_type):
                _prompt = re.compile(_prompt)

            match = _prompt.search(response)
            if match:
                self._channel.send(_answer + '\r')
Example #12
0
    def _handle_input(self, response, prompt, answer):
        """look for interactive prompts and send answer"""
        if prompt is None or answer is None:
            return

        re_type = type(re.compile(r'^$'))

        prompt = to_list(prompt)
        answer = to_list(answer)

        if len(prompt) != len(answer):
            raise ValueError(("Lists of prompts and answers have different "
                              "lengths"))

        for _prompt, _answer in zip(prompt, answer):
            if not isinstance(_prompt, re_type):
                _prompt = re.compile(_prompt)

            match = _prompt.search(response)
            if match:
                self._channel.send(_answer + '\r')
Example #13
0
def configure(endpoint, commands, **kwargs):
    """Make configuration changes to the switch

    :param endpoint: remote host or URI to connect to
    :param commands: configuration mode command or commands to send
    :param creds: (optional) :class:`Creds <Creds>` object with authentication
                             credentials
    :param protocol: (optional) Protocol name, e.g. 'ssh' or 'eapi'

    Usage:
        >>> arcomm.configure('eapi://veos',
        ...                  ['interface Ethernet1', 'no shutdown'])
        <ResponseStore [ok]>
    """
    commands = to_list(commands)
    commands.insert(0, "configure")
    commands.append("end")

    execute(endpoint, commands, **kwargs)
Example #14
0
def configure(endpoint, commands,  **kwargs):
    """Make configuration changes to the switch

    :param endpoint: remote host or URI to connect to
    :param commands: configuration mode command or commands to send
    :param creds: (optional) :class:`Creds <Creds>` object with authentication
                             credentials
    :param protocol: (optional) Protocol name, e.g. 'ssh' or 'eapi'

    Usage:
        >>> arcomm.configure('eapi://veos',
        ...                  ['interface Ethernet1', 'no shutdown'])
        <ResponseStore [ok]>
    """
    commands = to_list(commands)
    commands.insert(0, "configure")
    commands.append("end")

    execute(endpoint, commands,  **kwargs)
Example #15
0
    def __init__(self, endpoints, commands, size=10, delay=0, **kwargs):

        #
        self._endpoints = to_list(endpoints)

        #
        self._commands = commands

        #
        self._pool_size = size

        #
        self._delay = delay

        #
        self._worker_kwargs = kwargs

        #
        self._results = []

        # initialize pool
        self._pool = multiprocessing.Pool(self._pool_size, _prep_worker)
Example #16
0
def batch(endpoints, commands, **kwargs):
    """Send commands to multiple endpoints

    :param endpoint: remote host or URI to connect to
    :param commands: command or commands to send
    :param creds: (optional) :class:`Creds <Creds>` object with authentication
                             credentials
    :param protocol: (optional) Protocol name, e.g. 'ssh' or 'eapi'

    Usage:
        >>> pool = arcomm.batch(['veos1', 'veos2'], ['show version'])
        >>> for res in pool:
        ...     print(res.to_yaml())
    """

    endpoints = to_list(endpoints)
    with Pool(endpoints, commands, **kwargs) as pool:
        try:
            for item in pool.results:
                yield item.get()
        except KeyboardInterrupt:
            print('Caught interrupt')
            pool.kill()
            raise
Example #17
0
def batch(endpoints, commands, **kwargs):
    """Send commands to multiple endpoints

    :param endpoint: remote host or URI to connect to
    :param commands: command or commands to send
    :param creds: (optional) :class:`Creds <Creds>` object with authentication
                             credentials
    :param protocol: (optional) Protocol name, e.g. 'ssh' or 'eapi'

    Usage:
        >>> pool = arcomm.batch(['veos1', 'veos2'], ['show version'])
        >>> for res in pool:
        ...     print(res.to_yaml())
    """

    endpoints = to_list(endpoints)
    with Pool(endpoints, commands, **kwargs) as pool:
        try:
            for item in pool.results:
                yield item.get()
        except KeyboardInterrupt:
            print('Caught interrupt')
            pool.kill()
            raise
Example #18
0
def unsubscribe(callback):
    callback = to_list(callback)
    global _SUBSCRIBERS
    _SUBSCRIBERS = [cb for cb in _SUBSCRIBERS if cb not in callback]
Example #19
0
 def errors(self):
     return to_list(self.error)
Example #20
0
def configure(connection, commands, *args, **kwargs):
    """Similar to execute, but wraps the commands in a configure/end block"""
    commands = to_list(commands)
    commands.insert(0, "configure")
    commands.append("end")
    return connection.send(commands, **kwargs)
Example #21
0
def configure(connection, commands, *args, **kwargs):
    """Similar to execute, but wraps the commands in a configure/end block"""
    commands = to_list(commands)
    commands.insert(0, "configure")
    commands.append("end")
    return connection.send(commands, **kwargs)
Example #22
0
def unsubscribe(callback):
    callback = to_list(callback)
    global _SUBSCRIBERS
    _SUBSCRIBERS = [cb for cb in _SUBSCRIBERS if cb not in callback]