Added platform to the config

This commit is contained in:
Fredrik Bergström 2022-03-30 11:57:31 +02:00
parent 7bf955d601
commit a7b965a8b5
4 changed files with 31 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='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,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',

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
@ -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()

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
@ -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()