def fetch_interfaces(device_id=None): device = Device.objects.get(id=device_id) if device.environment == "LAB": return { "interfaces_fetched": False, "reason": "can only fetch production interfaces" } try: options = { "style": device.os_type, "sectional_overwrite": [], "sectional_overwrite_no_negate": [], "indent_adjust": [], "parent_allows_duplicate_child": [], "sectional_exiting": [], "full_text_sub": [], "per_line_sub": [], "idempotent_commands_blacklist": [], "idempotent_commands": [], "negation_default_when": [], "negation_negate_with": [], "ordering": [] } device_config = RouteSwitchConfig.objects.get(device=device) host = Host(hostname=device.name, os=device.os_type, hconfig_options=options) host.load_config_from(name=device_config.text, config_type="running", load_file=False) interfaces = host.running_config.get_children('startswith', 'interface') for item in interfaces: try: DeviceInterface.objects.get( device=device, name=f"{item.text.strip('interface ')}") except DeviceInterface.DoesNotExist: DeviceInterface.objects.create( device=device, name=f"{item.text.strip('interface ')}") return {"interfaces_fetched": True, "reason": "interfaces fetched"} except RouteSwitchConfig.DoesNotExist: return { "interfaces_fetched": False, "reason": "must fetch production config first" }
def hier_host( task: Task, include_tags: list = None, exclude_tags: list = None, running_config: str = None, compiled_config: str = None, config_path: str = None, load_file: bool = False, **kwargs: Any, ) -> Result: """ hier_config task for nornir :param task: type object :param incude_tags: type list :param exclude_tags: type list :param running_config: type str :param compiled_config: type str :param config_path: type str :param load_file: type bool :returns: hier remediation object """ from netnir.constants import HIER_DIR from hier_config import Host import logging import os import yaml operating_system = task.host.data["os"] hier_options_file = "/".join([HIER_DIR, operating_system, "options.yml"]) hier_tags_file = "/".join([HIER_DIR, operating_system, "tags.yml"]) running_config = "/".join([config_path, task.host.name, running_config]) compiled_config = "/".join([config_path, task.host.name, compiled_config]) logging = logging.getLogger("nornir") if os.path.isfile(hier_options_file): hier_options = yaml.load(open(hier_options_file), Loader=yaml.SafeLoader) else: return Result( host=task.host, result=f"{hier_options_file} does not exist", failed=True, severity_level=logging.WARN, ) host = Host(hostname=task.host.name, os=operating_system, hconfig_options=hier_options) host.load_config_from(config_type="running", name=running_config, load_file=load_file) host.load_config_from(config_type="compiled", name=compiled_config, load_file=load_file) if os.path.isfile(hier_tags_file): host.load_tags(hier_tags_file) else: return Result( host=task.host, result=f"{hier_tags_file} does not exist", failed=True, severity_level=logging.WARN, ) host.load_remediation() results = host.filter_remediation(include_tags=include_tags, exclude_tags=exclude_tags) return Result(host=task.host, result=results)
def fetch_lab_config(device_id): device = Device.objects.get(id=device_id) device_pair = DevicePair.objects.get(lab_device=device) other_device = device_pair.prod_device interfaces = DeviceInterface.objects.filter(device=device) interface_maps = list() for interface in interfaces: interface_maps.append( InterfaceMapper.objects.get(lab_device=interface)) interface_replace_maps = list() lab_interface_names = list() for item in interface_maps: interface_replace_maps.append({ "search": f"{item.prod_device.name}$", "replace": item.lab_device.name }) lab_interface_names.append(item.lab_device.name) replace_maps = interface_replace_maps options = { "style": device.os_type, "sectional_overwrite": [], "sectional_overwrite_no_negate": [], "indent_adjust": [], "parent_allows_duplicate_child": [], "sectional_exiting": [], "full_text_sub": [], "per_line_sub": replace_maps, "idempotent_commands_blacklist": [], "idempotent_commands": [], "negation_default_when": [], "negation_negate_with": [], "ordering": [] } prod_config = RouteSwitchConfig.objects.get(device=other_device) host = Host(device.name, os=device.os_type, hconfig_options=options) host.load_config_from(name="", config_type="running", load_file=False) host.load_config_from(name=prod_config.text, config_type="compiled", load_file=False) tags = HierSerializer(os=device.os_type) for item in host.compiled_config.get_children("startswith", "interface"): if item.text.startswith("interface Loopback"): pass elif item.text.strip("interface ") in lab_interface_names: pass else: tags.add_lineage({ "lineage": [{ "startswith": [item.text] }], "add_tags": "ignore" }) host.load_tags(tags.fetch_lineage(), load_file=False) host.load_remediation() result = host.filter_remediation(exclude_tags="ignore") fetch_or_update(device, result) return tags