How to use the pyats.async_.pcall function in pyats

To help you get started, we’ve selected a few pyats examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github CiscoTestAutomation / genielibs / pkgs / sdk-pkg / src / genie / libs / sdk / apis / iosxr / ntp / verify.py View on Github external
target (`obj`): Device object
            max_time (int): Maximum wait time for the trigger,
                            in seconds. Default: 90
            check_interval (int): Wait time between iterations when looping is needed,
                            in seconds. Default: 15
        Returns:
            result (`bool`): Verified result
    """

    def clock(dev):
        return dev.parse("show clock")

    timeout = Timeout(max_time, check_interval)

    while timeout.iterate():
        t1, t2 = pcall(clock, dev=[device, target])
        log.info(
            "Device: {device} Clock: {t1}\n"
            "Device: {target} Clock: {t2}".format(
                device=device.name, target=target.name, t1=t1, t2=t2
            )
        )

        p = re.compile(r"\d+:\d+:\d+")
        for key in t1:
            if "time" == key:
                result = p.findall(t1[key]) == p.findall(t2[key])
            else:
                result = t1[key] == t2[key]
            if not result:
                break
        else:
github CiscoTestAutomation / genielibs / pkgs / clean-pkg / src / genie / libs / clean / stages / iosxe / stages.py View on Github external
with steps.start("Begin TFTP boot of device {}".format(device.name)) as step:

        # Need to instantiate to get the device.start
        # The device.start only works because of a|b
        device.instantiate(connection_timeout=timeout)

        tftp_boot = {'ip_address': ip_address,
                     'subnet_mask': subnet_mask,
                     'gateway': gateway,
                     'tftp_server': tftp_server,
                     'image': image}
        try:
            abstract = Lookup.from_device(device, packages={'clean': clean})
            # Item is needed to be able to know in which parallel child
            # we are
            result = pcall(abstract.clean.stages.recovery.recovery_worker,
                           start=device.start,
                           ikwargs = [{'item': i} for i, _ in enumerate(device.start)],
                           ckwargs = \
                                {'device': device,
                                 'timeout': timeout,
                                 'tftp_boot': tftp_boot,
                                 'break_count': 0,
                                 # Irrelevant as we will not use this pattern anyway
                                 # But needed for the recovery
                                 'console_activity_pattern': '\\.\\.\\.\\.',
                                 'golden_image': None,
                                 'recovery_password': recovery_password})
        except Exception as e:
            log.error(str(e))
            section.failed("Failed to TFTP boot the device '{}'".\
                           format(device.name), goto=['exit'])
github CiscoTestAutomation / genielibs / pkgs / clean-pkg / src / genie / libs / clean / stages / iosxr / stages.py View on Github external
with steps.start("Begin TFTP boot of device {}".format(device.name)) as step:

        # Need to instantiate to get the device.start
        # The device.start only works because of a|b
        device.instantiate(connection_timeout=timeout)

        tftp_boot = {'ip_address': ip_address,
                     'subnet_mask': subnet_mask,
                     'gateway': gateway,
                     'tftp_server': tftp_server,
                     'image': image}
        try:
            abstract = Lookup.from_device(device, packages={'clean': clean})
            # Item is needed to be able to know in which parallel child
            # we are
            result = pcall(abstract.clean.stages.recovery.recovery_worker,
                           start=device.start,
                           ikwargs = [{'item': i} for i, _ in enumerate(device.start)],
                           ckwargs = \
                                {'device': device,
                                 'timeout': timeout,
                                 'tftp_boot': tftp_boot,
                                 'break_count': 0,
                                 # Irrelevant as we will not use this pattern anyway
                                 # But needed for the recovery
                                 'console_activity_pattern': '\\.\\.\\.\\.',
                                 'golden_image': None,
                                 'recovery_username': recovery_username,
                                 'recovery_password': recovery_password})
        except Exception as e:
            log.error(str(e))
            section.failed("Failed to TFTP boot the device '{}'".\
github CiscoTestAutomation / genielibs / pkgs / sdk-pkg / src / genie / libs / sdk / apis / iosxe / ntp / verify.py View on Github external
target (`obj`): Device object
            max_time (int): Maximum wait time for the trigger,
                            in second. Default: 90
            check_interval (int): Wait time between iterations when looping is needed,
                            in second. Default: 15
        Returns:
            result (`bool`): Verified result
    """

    def clock(dev):
        return dev.parse("show clock")

    timeout = Timeout(max_time, check_interval)

    while timeout.iterate():
        t1, t2 = pcall(clock, dev=[device, target])
        log.info(
            "Device: {device} Clock: {t1}\n"
            "Device: {target} Clock: {t2}".format(
                device=device.name, target=target.name, t1=t1, t2=t2
            )
        )

        p = re.compile(r"\d+:\d+:\d+")
        for key in t1:
            if "time" == key:
                a = time_to_int(p.search(t1[key]).group())
                b = time_to_int(p.search(t2[key]).group())
                result = abs(a - b) <= 1
            else:
                result = t1[key] == t2[key]
            if not result:
github CiscoTestAutomation / genielibs / pkgs / clean-pkg / src / genie / libs / clean / stages / nxos / stages.py View on Github external
time.sleep(reboot_delay)

    # Need to instantiate to get the device.start
    # The device.start only works because of a|b
    device.instantiate(connection_timeout=timeout)

    tftp_boot = {'ip_address': ip_address,
                 'subnet_mask': subnet_mask,
                 'gateway': gateway,
                 'tftp_server': tftp_server,
                 'image': image}
    try:
        abstract = Lookup.from_device(device, packages={'clean': clean})
        # Item is needed to be able to know in which parallel child
        # we are
        result = pcall(abstract.clean.stages.recovery.recovery_worker,
                       start=device.start,
                       ikwargs = [{'item': i} for i, _ in enumerate(device.start)],
                       ckwargs = \
                            {'device': device,
                             'timeout': timeout,
                             'tftp_boot': tftp_boot,
                             # Irrelevant as we will not use this pattern anyway
                             # But needed for the recovery
                             'break_count': 0,
                             'console_activity_pattern': '\\.\\.\\.\\.',
                             'golden_image': None,
                             'recovery_password': None})
    except Exception as e:
        log.error(str(e))
        section.failed("Failed to recover the device '{}'".\
                        format(device.name))
github CiscoTestAutomation / genielibs / pkgs / clean-pkg / src / genie / libs / clean / stages / recovery.py View on Github external
else:
        # This case is for the simple boot
        # Not yet supported
        raise Exception('Global recovery only support golden image and tftp '
                         'boot recovery and neither was provided')

    # Need to instantiate to get the device.start
    # The device.start only works because of a|b
    device.instantiate(connection_timeout=timeout)

    # For each default connection, start a fork to try to recover the device
    try:
        abstract = Lookup.from_device(device, packages={'clean': clean})
        # Item is needed to be able to know in which parallel child
        # we are
        result = pcall(abstract.clean.stages.recovery.recovery_worker,
                       start=device.start,
                       ikwargs = [{'item': i} for i, _ in enumerate(device.start)],
                       ckwargs = \
                            {'device': device,
                             'console_activity_pattern': console_activity_pattern,
                             'break_count': break_count,
                             'timeout': timeout,
                             'golden_image': golden_image,
                             'tftp_boot': tftp_boot,
                             'recovery_password': recovery_password})
    except Exception as e:
        log.error(str(e))
        raise Exception("Failed to recover the device '{}'".\
                        format(device.name))
    else:
        log.info("Successfully recovered the device '{}'".\
github CiscoTestAutomation / genielibs / pkgs / sdk-pkg / src / genie / libs / sdk / apis / iosxe / snmp / verify.py View on Github external
id_slot_map (`dict`): Id-slot mapping
                ex: {"1": "sip1", "7": "rp0", "9": "esp0"}
            snmp_map (`dict`): SNMP-CLI mapping
                ex: {'12': 'used', '13': 'free', '24': '1_min',
                     '25': '5_min', '26': '15_min', '27': 'committed'}
            version (`str`): SNMP version
            load_tolerance (`int`): Tolerance for load information
            memory_tolerance (`int`): Tolerance for memory information
        Returns:
            result (`bool`): Verified result
        Raises:
            None
    """
    result = True
    try:
        cli_dict, snmp_dict = pcall(
            [get_snmp_cli_dict, get_snmp_dict],
            iargs=[
                [device],
                [
                    snmp_device,
                    community,
                    ip_address,
                    oid,
                    id_slot_map,
                    snmp_map,
                    version,
                ],
            ],
        )
    except Exception as e:
        log.error(
github CiscoTestAutomation / genielibs / pkgs / sdk-pkg / src / genie / libs / sdk / triggers / blitz / actions.py View on Github external
def action_parallel(self, steps, testbed, section, data):
    # When called run all the actions
    # below the keyword parallel concurently
    pcall_payloads = []
    with steps.start('Executing actions in parallel', continue_=True) as steps:

        for action_item in data:
            for action, action_kwargs in action_item.items():
                # for future use - Enhancement needed in pyATS
                # with steps.start("Implementing action '{a}' in parallel".format(a=actions)) as step:
                # on parallel it is not possible to set continue to False and benefit from that feature
                step = Steps()
                kwargs = {'steps': step, 'testbed': testbed, 'section': section, 'data': [{action:action_kwargs}]}
                pcall_payloads.append(kwargs)
        pcall_returns = pcall(self.dispatcher, ikwargs=pcall_payloads)
        # Each action return is a dictionary containing the action name, possible saved_variable
        # Action results, and device name that action is being implemented on
        # These value would be lost when the child processor that executes the action end the process.
        # It is being implemented this way in order to add these values to the main processor.
        for each_return in pcall_returns:

            if each_return.get('saved_vars'):
                for saved_var_name, saved_var_data in each_return.get('saved_vars').items():

                    if each_return.get('filters'):
                        log.info('Applied filter: {} to the action {} output'.format(each_return['filters'], action))

                    save_variable(self, saved_var_data, saved_var_name)

            if each_return['device']:
                msg = 'Executed action {action} on {device} in parallel'.format(