Skip to content

Commit

Permalink
Added option to purge remaining devices
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
geektophe committed Feb 8, 2022
1 parent 8a46af1 commit 58775c0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions netbox_agent/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
10 changes: 10 additions & 0 deletions netbox_agent/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 58775c0

Please sign in to comment.