From 2d974b1e46332afbdb3d16ee7078ad0f3ed2c681 Mon Sep 17 00:00:00 2001 From: Artem Kotik Date: Sat, 9 Nov 2024 18:58:20 +0100 Subject: [PATCH] Handle exceptions when calculating missing/extra commands in Configurator and improve error logging --- netbox_config_diff/configurator/base.py | 15 +++++++++------ netbox_config_diff/configurator/platforms.py | 6 +++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/netbox_config_diff/configurator/base.py b/netbox_config_diff/configurator/base.py index 0106ffe..5eda8ad 100644 --- a/netbox_config_diff/configurator/base.py +++ b/netbox_config_diff/configurator/base.py @@ -137,12 +137,15 @@ async def _collect_one_diff(self, device: ConfiguratorDeviceDataClass) -> None: device.diff = get_unified_diff(device.rendered_config, device.actual_config, device.name) self.logger.add_diff(device.name, diff=device.diff) - device.missing = diff_network_config( - device.rendered_config, device.actual_config, PLATFORM_MAPPING[device.platform] - ) - device.extra = diff_network_config( - device.actual_config, device.rendered_config, PLATFORM_MAPPING[device.platform] - ) + try: + device.missing = diff_network_config( + device.rendered_config, device.actual_config, PLATFORM_MAPPING[device.platform] + ) + device.extra = diff_network_config( + device.actual_config, device.rendered_config, PLATFORM_MAPPING[device.platform] + ) + except Exception as e: + self.logger.log_warning(f"Unable to get missing/extra commands for {device.name}: {e}") device.patch = get_remediation_commands( device.name, device.platform, device.actual_config, device.rendered_config ) diff --git a/netbox_config_diff/configurator/platforms.py b/netbox_config_diff/configurator/platforms.py index 45eb5a2..994faee 100644 --- a/netbox_config_diff/configurator/platforms.py +++ b/netbox_config_diff/configurator/platforms.py @@ -46,15 +46,15 @@ def _render_substituted_config( replace_sections = [(name, re.search(pattern=pattern, string=source_config)) for name, pattern in substitutes] - rendered_config = "" + rendered_config = config_template for name, replace_section in replace_sections: if not replace_section: - msg = f"substitution pattern {name} was unable to find a match in the target config" " source" + msg = f"substitution pattern {name} was unable to find a match in the target configsource" self.logger.critical(msg) raise TemplateError(msg) replace_group = replace_section.group() - rendered_config = config_template.replace(f"{{{{ {name} }}}}", replace_group) + rendered_config = rendered_config.replace(f"{{{{ {name} }}}}", replace_group) # remove any totally empty lines (from bad regex, or just device spitting out lines w/ # nothing on it