Merge pull request #216 from cyrinux/feature/platform

Added platform to the config with auto-detect as fallback
This commit is contained in:
Cyril Levis 2022-04-01 08:57:24 +02:00 committed by GitHub
commit d06931852f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 2 deletions

View file

@ -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='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')

View file

@ -29,6 +29,26 @@ def get_device_type(type):
return device_type
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, NameError):
return None
else:
linux_distribution = device_platform
device_platform = nb.dcim.platforms.get(name=linux_distribution)
if device_platform is None:
device_platform = nb.dcim.platforms.create(
name=linux_distribution, slug=slugify(linux_distribution)
)
return device_platform
def get_vendor(name):
vendors = {
'PERC': 'Dell',

View file

@ -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
@ -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
@ -273,6 +274,7 @@ class ServerBase():
serial=serial,
device_role=device_role.id,
device_type=device_type.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,
@ -463,6 +465,10 @@ class ServerBase():
ret, server = self.update_netbox_location(server)
update += ret
if server.platform != self.device_platform:
server.platform = self.device_platform
update += 1
if update:
server.save()
@ -487,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())

View file

@ -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
@ -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):
@ -94,6 +95,8 @@ class VirtualMachine(object):
vm = nb.virtualization.virtual_machines.create(
name=hostname,
cluster=cluster.id,
platform=self.device_platform,
device_platform=self.device_platform,
vcpus=vcpus,
memory=memory,
tenant=tenant.id if tenant else None,
@ -114,6 +117,9 @@ class VirtualMachine(object):
if sorted(set(vm.tags)) != sorted(set(self.tags)):
vm.tags = self.tags
updated += 1
if vm.platform != self.device_platform:
vm.platform = self.device_platform
updated += 1
if updated:
vm.save()