From a7b965a8b5f74e58533b01bd710ae5360dcc521f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Bergstr=C3=B6m?= Date: Wed, 30 Mar 2022 11:57:31 +0200 Subject: [PATCH 1/7] Added platform to the config --- netbox_agent/config.py | 2 ++ netbox_agent/misc.py | 12 ++++++++++++ netbox_agent/server.py | 9 ++++++++- netbox_agent/virtualmachine.py | 10 +++++++++- 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/netbox_agent/config.py b/netbox_agent/config.py index 7bb67d7..5a03117 100644 --- a/netbox_agent/config.py +++ b/netbox_agent/config.py @@ -43,6 +43,8 @@ def get_config(): p.add_argument('--virtual.cluster_name', help='Cluster name of VM') p.add_argument('--hostname_cmd', default=None, help="Command to output hostname, used as Device's name in netbox") + p.add_argument('--device.platform', default=None, + help='Device platform. Here we use OS distribution.') p.add_argument('--device.tags', default=r'', help='tags to use for a host') p.add_argument('--preserve-tags', action='store_true', help='Append new unique tags, preserve those already present') diff --git a/netbox_agent/misc.py b/netbox_agent/misc.py index df4638c..8084a84 100644 --- a/netbox_agent/misc.py +++ b/netbox_agent/misc.py @@ -29,6 +29,18 @@ def get_device_type(type): return device_type +def get_device_platform(config): + device_platform = nb.dcim.platforms.get( + name=config.device.platform + ) + if device_platform is None: + device_platform = nb.dcim.platforms.create( + name=config.device.platform, + slug=slugify(config.device.platform) + ) + return device_platform + + def get_vendor(name): vendors = { 'PERC': 'Dell', diff --git a/netbox_agent/server.py b/netbox_agent/server.py index 5904a60..2a9619e 100644 --- a/netbox_agent/server.py +++ b/netbox_agent/server.py @@ -3,7 +3,7 @@ from netbox_agent.config import config from netbox_agent.config import netbox_instance as nb from netbox_agent.inventory import Inventory from netbox_agent.location import Datacenter, Rack, Tenant -from netbox_agent.misc import create_netbox_tags, get_device_role, get_device_type +from netbox_agent.misc import create_netbox_tags, get_device_role, get_device_type, get_device_platform from netbox_agent.network import ServerNetwork from netbox_agent.power import PowerSupply from pprint import pprint @@ -262,6 +262,7 @@ class ServerBase(): 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()) + device_platform = get_device_platform(config) if not device_type: raise Exception('Chassis "{}" doesn\'t exist'.format(self.get_chassis())) serial = self.get_service_tag() @@ -273,6 +274,7 @@ class ServerBase(): serial=serial, device_role=device_role.id, device_type=device_type.id, + platform=device_platform.id, site=datacenter.id if datacenter else None, tenant=tenant.id if tenant else None, rack=rack.id if rack else None, @@ -463,6 +465,11 @@ class ServerBase(): ret, server = self.update_netbox_location(server) update += ret + if get_device_platform(config) is not None: + if server.platform != get_device_platform(config).name: + update += 1 + server.platform = get_device_platform(config).id + if update: server.save() diff --git a/netbox_agent/virtualmachine.py b/netbox_agent/virtualmachine.py index 6669b3b..f9b779a 100644 --- a/netbox_agent/virtualmachine.py +++ b/netbox_agent/virtualmachine.py @@ -5,7 +5,7 @@ from netbox_agent.config import config from netbox_agent.config import netbox_instance as nb from netbox_agent.location import Tenant from netbox_agent.logging import logging # NOQA -from netbox_agent.misc import create_netbox_tags, get_hostname +from netbox_agent.misc import create_netbox_tags, get_hostname, get_device_platform from netbox_agent.network import VirtualNetwork @@ -90,10 +90,13 @@ class VirtualMachine(object): if not vm: logging.debug('Creating Virtual machine..') cluster = self.get_netbox_cluster(config.virtual.cluster_name) + device_platform = get_device_platform(config) vm = nb.virtualization.virtual_machines.create( name=hostname, cluster=cluster.id, + platform=device_platform.id, + device_platform = get_device_platform(config), vcpus=vcpus, memory=memory, tenant=tenant.id if tenant else None, @@ -114,6 +117,11 @@ class VirtualMachine(object): if sorted(set(vm.tags)) != sorted(set(self.tags)): vm.tags = self.tags updated += 1 + if get_device_platform(config) is not None: + if vm.platform != get_device_platform(config).name: + updated += 1 + vm.platform = get_device_platform(config).id + logging.debug('Finished updating Platform!') if updated: vm.save() From 2a4f24f00a24a995e00ae1c703c7fc8b85ddb002 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Bergstr=C3=B6m?= Date: Thu, 31 Mar 2022 11:14:26 +0200 Subject: [PATCH 2/7] not failing if platform is not set --- netbox_agent/misc.py | 3 +++ netbox_agent/server.py | 7 ++++--- netbox_agent/virtualmachine.py | 4 ++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/netbox_agent/misc.py b/netbox_agent/misc.py index 8084a84..6f45d86 100644 --- a/netbox_agent/misc.py +++ b/netbox_agent/misc.py @@ -30,6 +30,9 @@ def get_device_type(type): def get_device_platform(config): + if config.device.platform is None: + return None + device_platform = nb.dcim.platforms.get( name=config.device.platform ) diff --git a/netbox_agent/server.py b/netbox_agent/server.py index 2a9619e..c902e2f 100644 --- a/netbox_agent/server.py +++ b/netbox_agent/server.py @@ -465,10 +465,11 @@ class ServerBase(): ret, server = self.update_netbox_location(server) update += ret - if get_device_platform(config) is not None: - if server.platform != get_device_platform(config).name: + if config.device.platform is not None: + platform = get_device_platform(config) + if server.platform != platform.name: update += 1 - server.platform = get_device_platform(config).id + server.platform = platform.id if update: server.save() diff --git a/netbox_agent/virtualmachine.py b/netbox_agent/virtualmachine.py index f9b779a..ae1afe4 100644 --- a/netbox_agent/virtualmachine.py +++ b/netbox_agent/virtualmachine.py @@ -95,8 +95,8 @@ class VirtualMachine(object): vm = nb.virtualization.virtual_machines.create( name=hostname, cluster=cluster.id, - platform=device_platform.id, - device_platform = get_device_platform(config), + platform=device_platform.id if device_platform is not None else None, + device_platform=device_platform, vcpus=vcpus, memory=memory, tenant=tenant.id if tenant else None, From cf2cc54da40daabc9f7838cb24443ce3da45b68b Mon Sep 17 00:00:00 2001 From: Cyril Levis Date: Thu, 31 Mar 2022 17:08:16 +0200 Subject: [PATCH 3/7] feat: auto-detect device platform this platform module still works for python3.6 we use. Signed-off-by: Cyril Levis --- netbox_agent/misc.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/netbox_agent/misc.py b/netbox_agent/misc.py index 6f45d86..0c73522 100644 --- a/netbox_agent/misc.py +++ b/netbox_agent/misc.py @@ -31,19 +31,24 @@ def get_device_type(type): def get_device_platform(config): if config.device.platform is None: - return None + try: + import platform - device_platform = nb.dcim.platforms.get( - name=config.device.platform - ) + linux_distribution = " ".join(platform.linux_distribution()) + if not linux_distribution: + return None + except ModuleNotFoundError: + return None + else: + linux_distribution = config.device.platform + + device_platform = nb.dcim.platforms.get(name=linux_distribution) if device_platform is None: device_platform = nb.dcim.platforms.create( - name=config.device.platform, - slug=slugify(config.device.platform) + name=linux_distribution, slug=slugify(linux_distribution) ) return device_platform - def get_vendor(name): vendors = { 'PERC': 'Dell', From 9e797c376ed19c5f9b2c648c54f549233d7e7137 Mon Sep 17 00:00:00 2001 From: Cyril Levis Date: Thu, 31 Mar 2022 19:54:34 +0200 Subject: [PATCH 4/7] fix: always get_device_platform Signed-off-by: Cyril Levis --- netbox_agent/server.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/netbox_agent/server.py b/netbox_agent/server.py index c902e2f..f0a7e5e 100644 --- a/netbox_agent/server.py +++ b/netbox_agent/server.py @@ -465,11 +465,10 @@ class ServerBase(): ret, server = self.update_netbox_location(server) update += ret - if config.device.platform is not None: - platform = get_device_platform(config) - if server.platform != platform.name: - update += 1 - server.platform = platform.id + platform = get_device_platform(config) + if server.platform != platform.name: + server.platform = platform.id + update += 1 if update: server.save() From dfb6b234ba3c63db69b4e4100aec28c6efe551c4 Mon Sep 17 00:00:00 2001 From: Cyril Levis Date: Thu, 31 Mar 2022 20:58:23 +0200 Subject: [PATCH 5/7] chore: cleanup, add device_platform in summary Signed-off-by: Cyril Levis --- netbox_agent/misc.py | 8 ++++---- netbox_agent/server.py | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/netbox_agent/misc.py b/netbox_agent/misc.py index 0c73522..6e7e92d 100644 --- a/netbox_agent/misc.py +++ b/netbox_agent/misc.py @@ -29,18 +29,18 @@ def get_device_type(type): return device_type -def get_device_platform(config): - if config.device.platform is None: +def get_device_platform(device_platform): + if device_platform is None: try: import platform linux_distribution = " ".join(platform.linux_distribution()) if not linux_distribution: return None - except ModuleNotFoundError: + except (ModuleNotFoundError, NameError): return None else: - linux_distribution = config.device.platform + linux_distribution = device_platform device_platform = nb.dcim.platforms.get(name=linux_distribution) if device_platform is None: diff --git a/netbox_agent/server.py b/netbox_agent/server.py index f0a7e5e..f7008bf 100644 --- a/netbox_agent/server.py +++ b/netbox_agent/server.py @@ -24,6 +24,7 @@ class ServerBase(): self.bios = dmidecode.get_by_type(self.dmi, 'BIOS') self.chassis = dmidecode.get_by_type(self.dmi, 'Chassis') self.system = dmidecode.get_by_type(self.dmi, 'System') + self.device_platform = get_device_platform(config.device.platform) self.network = None @@ -262,7 +263,6 @@ class ServerBase(): 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()) - device_platform = get_device_platform(config) if not device_type: raise Exception('Chassis "{}" doesn\'t exist'.format(self.get_chassis())) serial = self.get_service_tag() @@ -274,7 +274,7 @@ class ServerBase(): serial=serial, device_role=device_role.id, device_type=device_type.id, - platform=device_platform.id, + platform=self.device_platform, site=datacenter.id if datacenter else None, tenant=tenant.id if tenant else None, rack=rack.id if rack else None, @@ -465,9 +465,8 @@ class ServerBase(): ret, server = self.update_netbox_location(server) update += ret - platform = get_device_platform(config) - if server.platform != platform.name: - server.platform = platform.id + if server.platform != self.device_platform: + server.platform = self.device_platform update += 1 if update: @@ -494,6 +493,7 @@ class ServerBase(): print('Is blade:', self.is_blade()) print('Got expansion:', self.own_expansion_slot()) print('Product Name:', self.get_product_name()) + print('Platform:', self.device_platform) print('Chassis:', self.get_chassis()) print('Chassis service tag:', self.get_chassis_service_tag()) print('Service tag:', self.get_service_tag()) From 3dbeb5b9de5bcd74788d99eafaf686760659cc54 Mon Sep 17 00:00:00 2001 From: Cyril Levis Date: Thu, 31 Mar 2022 21:04:32 +0200 Subject: [PATCH 6/7] chore: cleanup virtual_machines get_device_platform Signed-off-by: Cyril Levis --- netbox_agent/virtualmachine.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/netbox_agent/virtualmachine.py b/netbox_agent/virtualmachine.py index ae1afe4..f86c082 100644 --- a/netbox_agent/virtualmachine.py +++ b/netbox_agent/virtualmachine.py @@ -31,6 +31,7 @@ class VirtualMachine(object): else: self.dmi = dmidecode.parse() self.network = None + self.device_platform = get_device_platform(config.device.platform) self.tags = list(set(config.device.tags.split(','))) if config.device.tags else [] if self.tags and len(self.tags): @@ -90,13 +91,12 @@ class VirtualMachine(object): if not vm: logging.debug('Creating Virtual machine..') cluster = self.get_netbox_cluster(config.virtual.cluster_name) - device_platform = get_device_platform(config) vm = nb.virtualization.virtual_machines.create( name=hostname, cluster=cluster.id, - platform=device_platform.id if device_platform is not None else None, - device_platform=device_platform, + platform=self.device_platform, + device_platform=self.device_platform, vcpus=vcpus, memory=memory, tenant=tenant.id if tenant else None, @@ -117,11 +117,9 @@ class VirtualMachine(object): if sorted(set(vm.tags)) != sorted(set(self.tags)): vm.tags = self.tags updated += 1 - if get_device_platform(config) is not None: - if vm.platform != get_device_platform(config).name: - updated += 1 - vm.platform = get_device_platform(config).id - logging.debug('Finished updating Platform!') + if vm.platform != self.device_platform: + vm.platform = self.device_platform + updated += 1 if updated: vm.save() From 46354865d31a2555fe148e6a765a7b7b72d27eb8 Mon Sep 17 00:00:00 2001 From: Cyril Levis Date: Thu, 31 Mar 2022 21:21:19 +0200 Subject: [PATCH 7/7] chore: update Help Signed-off-by: Cyril Levis --- netbox_agent/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netbox_agent/config.py b/netbox_agent/config.py index 5a03117..702c9b3 100644 --- a/netbox_agent/config.py +++ b/netbox_agent/config.py @@ -44,7 +44,7 @@ def get_config(): p.add_argument('--hostname_cmd', default=None, help="Command to output hostname, used as Device's name in netbox") p.add_argument('--device.platform', default=None, - help='Device platform. Here we use OS distribution.') + help='Override device platform. Here we use OS distribution.') p.add_argument('--device.tags', default=r'', help='tags to use for a host') p.add_argument('--preserve-tags', action='store_true', help='Append new unique tags, preserve those already present')