Skip to content

Commit

Permalink
Merge pull request #1 from dailymotion/fix/chriss/purge_remaining_dev…
Browse files Browse the repository at this point in the history
…ices

Added option to purge remaining devices
  • Loading branch information
geektophe authored Feb 15, 2022
2 parents 71c6036 + 5d96024 commit 448f0f2
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('--expansion-as-device', action='store_true',
help='Manage blade expansions as external devices')

Expand Down
10 changes: 10 additions & 0 deletions netbox_agent/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,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 @@ -351,6 +358,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 448f0f2

Please sign in to comment.