Exemplo n.º 1
0
def device_recovery(start, device, console_activity_pattern, golden_image=None,
                           break_count=10, timeout=600, recovery_password=None,
                           tftp_boot=None, item=None):

    ''' A method for starting Spawns and handling the device statements during recovery
        Args:
            device ('obj'): Device object
            start ('obj'): Start method under device object
            console_activity_pattern ('str'): Pattern to send the break at
            golden_image ('str'): Image to load the device with
            break_count ('int'): Number of sending break times
            timeout ('int'): Recovery process timeout
            recovery_password ('str'): Device password after recovery
        Returns:
            None
    '''


    break_dialog = BreakBootDialog()
    break_dialog.add_statement(Statement(pattern=console_activity_pattern,
                                         action=sendbrk_handler,
                                         args={'break_count':break_count},
                                         loop_continue=True,
                                         continue_timer=False), pos=0)

    # Set a target for each recovery session
    # so it's easier to distinguish expect debug logs on the console.
    device.instantiate(connection_timeout=timeout)

    # Get device console port information
    last_word_in_start_match = re.match('.*\s(\S+)$', start)
    last_word_in_start = last_word_in_start_match.group(1) \
        if last_word_in_start_match else ""

    # Set target
    target = "{}_{}".format(device.hostname, last_word_in_start)

    if len(log.handlers) >=2:
        logfile= log.handlers[1].logfile
    else:
        logfile = None

    spawn = Spawn(start,
                  settings=device.cli.settings,
                  target=target,
                  log=log,
                  logfile=logfile)

    if 'kickstart' not in golden_image or 'system' not in golden_image:
        raise Exception("Either Kickstart or System image has not been provided "
                        "in the 'device_recovery' section of clean YAML")

    dialog = RommonDialog()
    dialog.dialog.process(spawn, context={'kick':golden_image.get('kickstart'),
                                          'sys':golden_image.get('system'),
                                          'password':recovery_password},
                          timeout=timeout)
    spawn.close()
Exemplo n.º 2
0
def tftp_recovery_worker(start,
                         device,
                         console_activity_pattern,
                         tftp_boot=None,
                         break_count=10,
                         timeout=600,
                         recovery_password=None,
                         golden_image=None,
                         item=None,
                         **kwargs):
    ''' A method for starting Spawns and handling the device statements during recovery
        Args:
            device ('obj'): Device object
            start ('obj'): Start method under device object
            console_activity_pattern ('str'): Pattern to send the break at
            tftp_boot ('dict'): Tftp boot information
            break_count ('int'): Number of sending break times
            timeout ('int'): Recovery process timeout
            recovery_password ('str'): Device password after recovery
        Returns:
            None
    '''

    log.info('Set the device in rommon and load the device with tftp boot')
    break_dialog = BreakBootDialog()
    break_dialog.add_statement(Statement(pattern=console_activity_pattern,
                                         action=sendbrk_handler,
                                         args={'break_count': break_count},
                                         loop_continue=True,
                                         continue_timer=False),
                               pos=0)

    # Set a target for each recovery session
    # so it's easier to distinguish expect debug logs on the console.
    device.instantiate(connection_timeout=timeout)

    # Get device console port information
    last_word_in_start_match = re.match(r'.*\s(\S+)$', start)
    last_word_in_start = last_word_in_start_match.group(1) \
        if last_word_in_start_match else ""

    # Set target
    target = "{}_{}".format(device.hostname, last_word_in_start)

    if len(log.handlers) >= 2:
        logfile = log.handlers[1].logfile
    else:
        logfile = None

    spawn = Spawn(spawn_command=start,
                  settings=device.cli.settings,
                  target=target,
                  log=log,
                  logfile=logfile)

    rommon_dialog = TftpRommonDialog()
    rommon_dialog.hostname_statement(device.hostname)
    rommon_dialog.dialog.process(spawn,
                                 timeout=timeout,
                                 context={
                                     'device_name': device.name,
                                     'ip': tftp_boot['ip_address'][item],
                                     'password': recovery_password,
                                     'subnet_mask': tftp_boot['subnet_mask'],
                                     'gateway': tftp_boot['gateway'],
                                     'image': tftp_boot['image'],
                                     'tftp_server': tftp_boot['tftp_server'],
                                     'hostname': device.hostname
                                 })

    spawn.close()