From 58775c0950a3944173afeb1163f132977eaf0fd6 Mon Sep 17 00:00:00 2001 From: Christophe Simon Date: Tue, 8 Feb 2022 18:45:44 +0100 Subject: [PATCH] Added option to purge remaining devices This patch adds an option to clear remaining devices in the context of a new device replacing an old one with the same name but with a different hardware. The serial leading to the bug is described below: - A first server is registered under name `A` with serial `X` - A second server is registered under name `B` with serial `Y` - The server with serial `X` is decomissionned, but not removed - The server with serial `Y` is reinstalled with name `A` In this case, a device with serial `X` is well found, and netbox agent tries to update its name. But this raises an error because of the unique constraint on name, as another device already holds this name. The proposed solution to handle this situation is to delete any device with same `name` but different `serial` before handling a device. As this is not necessarily the expected behavior, it can be enabled by using the `--purge-old-devices` to avoid breaking existing inventory. --- netbox_agent/config.py | 2 ++ netbox_agent/server.py | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/netbox_agent/config.py b/netbox_agent/config.py index cf88f695..44d10190 100644 --- a/netbox_agent/config.py +++ b/netbox_agent/config.py @@ -28,6 +28,8 @@ def get_config(): p.add_argument('--update-inventory', action='store_true', help='Update inventory') p.add_argument('--update-location', action='store_true', help='Update location') p.add_argument('--update-psu', action='store_true', help='Update PSU') + p.add_argument('--purge-old-devices', action='store_true', + help='Purge existing (old ?) devices having same name but different serial') p.add_argument('--log_level', default='debug') p.add_argument('--netbox.url', help='Netbox URL') diff --git a/netbox_agent/server.py b/netbox_agent/server.py index 9cedf625..e052a442 100644 --- a/netbox_agent/server.py +++ b/netbox_agent/server.py @@ -228,6 +228,13 @@ def _netbox_create_blade_expansion(self, chassis, datacenter, tenant, rack): ) return new_blade + def _netbox_deduplicate_server(self): + serial = self.get_service_tag() + hostname = self.get_hostname() + server = nb.dcim.devices.get(name=hostname) + if server and server.serial != serial: + server.delete() + def _netbox_create_server(self, datacenter, tenant, rack): device_role = get_device_role(config.device.server_role) device_type = get_device_type(self.get_product_name()) @@ -336,6 +343,9 @@ def netbox_create_or_update(self, config): rack = self.get_netbox_rack() tenant = self.get_netbox_tenant() + if config.purge_old_devices: + self._netbox_deduplicate_server() + if self.is_blade(): chassis = nb.dcim.devices.get( serial=self.get_chassis_service_tag()