Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
testbed = section.parameters.get('testbed', {})
uut = testbed.devices['uut']
if os.path.isfile(configs):
configs = yaml.safe_load(open(configs))
elif isinstance(configs, str):
module = Lookup.from_device(uut)
path = configs.split('.')
for item in path:
module = getattr(module, item)
configs = module
else:
section.skipped('The configs type {} is not supported'
.format(type(configs)))
lookup = Lookup.from_device(uut)
# learn the lldp neighbors
if not hasattr(uut, 'lldp_mapping'):
log.info(banner('Learn LLDP Neighbors'))
# inital lldp ops object
lldp_ops = lookup.ops.lldp.lldp.Lldp(
uut, attributes=['info[interfaces][(.*)][neighbors][(.*)][port_id]'])
# learn the lldp ops
try:
lldp_ops.learn()
except Exception as e:
section.passx('Cannot learn lldp information',
from_exception=e)
# store the lldp information
uut.lldp_mapping = lldp_ops.info['interfaces']
goto=['exit'])
# Begin TFTP boot of device
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})
def _find_parser_cls(device, data):
lookup = Lookup.from_device(device, packages={'parser':importlib.import_module(data['package'])})
return getattr(getattr(lookup.parser, data['module_name']), data['class'])
def genie_prep(dev):
"""
Connects and looks up platform parsers for device
Returns an abstract object
"""
# Device must be connected so that a lookup can be performed
if not dev.is_connected():
dev.connect()
# Load the approprate platform parsers dynamically
abstract = Lookup.from_device(dev)
return abstract
def update_clean_section(device, order, images):
'''Updates given section with images provided'''
# Get abstracted ImageHandler class
abstract = Lookup.from_device(device, packages={'clean': clean})
ImageHandler = abstract.clean.stages.image_handler.ImageHandler
# Image handler
image_handler = ImageHandler(device, images)
# Update section with image information if needed
for section in order:
if section not in SECTIONS_WITH_IMAGE:
continue
elif not device.clean[section]:
device.clean[section] = {}
# Section: tftp_boot
if section == 'tftp_boot':
image_handler.update_tftp_boot()
def clear_logging(section, devices=None):
'''Trigger Pre-Processor:
* Clear logging on device
'''
# Init
log.info(banner("processor: 'clear_logging'"))
# Execute on section devices if devices list not specified
if not devices:
devices = [section.parameters['uut'].name]
for dev in devices:
device = section.parameters['testbed'].devices[dev]
# Abstract
lookup = Lookup.from_device(device, packages={'sdk': sdk})
clear_log = lookup.sdk.libs.abstracted_libs.clear_logging.ClearLogging()
# Clear logging on device
try:
log.info("Clear logging on device {}".format(dev))
clear_log.clear_logging(device)
except Exception as e:
log.error(e)
section.failed("Unable to clear logging on device")
else:
log.info("Cleared logging successfully on device")
log.info(banner("Booting device '{}' with the Tftp images".\
format(device.name)))
log.info("Tftp boot information found:\n{}".format(tftp_boot))
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 '{}'".\
Args:
Mandatory:
device (`obj`) : Device object.
Returns:
default_dir (`str`): Default directory of the system
Raises:
Exception
Example:
>>> get_default_dir(device=device)
"""
try:
lookup = Lookup.from_device(device)
parsed_dict = lookup.parser.show_platform.Dir(device=device).parse()
if ":" in parsed_dict['dir']['dir_name']:
default_dir = parsed_dict['dir']['dir_name']
else:
default_dir = ''
except SchemaEmptyParserError as e:
raise Exception("No output when executing 'dir' command") from e
except Exception as e:
raise Exception("Unable to execute 'dir' command") from e
# Return default_dir to caller
log.info("Default directory on '{d}' is '{dir}'".format(d=device.name,
dir=default_dir))
return default_dir
Arguments
---------
device (`obj`): Device object
obj (`obj`): Genie Trigger Object
update_ver_list (`list`): List of verifications names
update_feature_list(`list`): List of pts features names
"""
self.device = device
self.obj = obj
# update verifications list
self.update_ver_list = update_ver_list
# update pts features list
self.update_feature_list = update_feature_list
# build up abstract object based on the os of device
self.abstract = Lookup.from_device(device)