Merge pull request #216 from cyrinux/feature/platform
Added platform to the config with auto-detect as fallback
This commit is contained in:
commit
d06931852f
4 changed files with 37 additions and 2 deletions
|
@ -43,6 +43,8 @@ def get_config():
|
||||||
p.add_argument('--virtual.cluster_name', help='Cluster name of VM')
|
p.add_argument('--virtual.cluster_name', help='Cluster name of VM')
|
||||||
p.add_argument('--hostname_cmd', default=None,
|
p.add_argument('--hostname_cmd', default=None,
|
||||||
help="Command to output hostname, used as Device's name in netbox")
|
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'',
|
p.add_argument('--device.tags', default=r'',
|
||||||
help='tags to use for a host')
|
help='tags to use for a host')
|
||||||
p.add_argument('--preserve-tags', action='store_true', help='Append new unique tags, preserve those already present')
|
p.add_argument('--preserve-tags', action='store_true', help='Append new unique tags, preserve those already present')
|
||||||
|
|
|
@ -29,6 +29,26 @@ def get_device_type(type):
|
||||||
return device_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):
|
def get_vendor(name):
|
||||||
vendors = {
|
vendors = {
|
||||||
'PERC': 'Dell',
|
'PERC': 'Dell',
|
||||||
|
|
|
@ -3,7 +3,7 @@ from netbox_agent.config import config
|
||||||
from netbox_agent.config import netbox_instance as nb
|
from netbox_agent.config import netbox_instance as nb
|
||||||
from netbox_agent.inventory import Inventory
|
from netbox_agent.inventory import Inventory
|
||||||
from netbox_agent.location import Datacenter, Rack, Tenant
|
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.network import ServerNetwork
|
||||||
from netbox_agent.power import PowerSupply
|
from netbox_agent.power import PowerSupply
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
|
@ -24,6 +24,7 @@ class ServerBase():
|
||||||
self.bios = dmidecode.get_by_type(self.dmi, 'BIOS')
|
self.bios = dmidecode.get_by_type(self.dmi, 'BIOS')
|
||||||
self.chassis = dmidecode.get_by_type(self.dmi, 'Chassis')
|
self.chassis = dmidecode.get_by_type(self.dmi, 'Chassis')
|
||||||
self.system = dmidecode.get_by_type(self.dmi, 'System')
|
self.system = dmidecode.get_by_type(self.dmi, 'System')
|
||||||
|
self.device_platform = get_device_platform(config.device.platform)
|
||||||
|
|
||||||
self.network = None
|
self.network = None
|
||||||
|
|
||||||
|
@ -273,6 +274,7 @@ class ServerBase():
|
||||||
serial=serial,
|
serial=serial,
|
||||||
device_role=device_role.id,
|
device_role=device_role.id,
|
||||||
device_type=device_type.id,
|
device_type=device_type.id,
|
||||||
|
platform=self.device_platform,
|
||||||
site=datacenter.id if datacenter else None,
|
site=datacenter.id if datacenter else None,
|
||||||
tenant=tenant.id if tenant else None,
|
tenant=tenant.id if tenant else None,
|
||||||
rack=rack.id if rack else None,
|
rack=rack.id if rack else None,
|
||||||
|
@ -463,6 +465,10 @@ class ServerBase():
|
||||||
ret, server = self.update_netbox_location(server)
|
ret, server = self.update_netbox_location(server)
|
||||||
update += ret
|
update += ret
|
||||||
|
|
||||||
|
if server.platform != self.device_platform:
|
||||||
|
server.platform = self.device_platform
|
||||||
|
update += 1
|
||||||
|
|
||||||
if update:
|
if update:
|
||||||
server.save()
|
server.save()
|
||||||
|
|
||||||
|
@ -487,6 +493,7 @@ class ServerBase():
|
||||||
print('Is blade:', self.is_blade())
|
print('Is blade:', self.is_blade())
|
||||||
print('Got expansion:', self.own_expansion_slot())
|
print('Got expansion:', self.own_expansion_slot())
|
||||||
print('Product Name:', self.get_product_name())
|
print('Product Name:', self.get_product_name())
|
||||||
|
print('Platform:', self.device_platform)
|
||||||
print('Chassis:', self.get_chassis())
|
print('Chassis:', self.get_chassis())
|
||||||
print('Chassis service tag:', self.get_chassis_service_tag())
|
print('Chassis service tag:', self.get_chassis_service_tag())
|
||||||
print('Service tag:', self.get_service_tag())
|
print('Service tag:', self.get_service_tag())
|
||||||
|
|
|
@ -5,7 +5,7 @@ from netbox_agent.config import config
|
||||||
from netbox_agent.config import netbox_instance as nb
|
from netbox_agent.config import netbox_instance as nb
|
||||||
from netbox_agent.location import Tenant
|
from netbox_agent.location import Tenant
|
||||||
from netbox_agent.logging import logging # NOQA
|
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
|
from netbox_agent.network import VirtualNetwork
|
||||||
|
|
||||||
|
|
||||||
|
@ -31,6 +31,7 @@ class VirtualMachine(object):
|
||||||
else:
|
else:
|
||||||
self.dmi = dmidecode.parse()
|
self.dmi = dmidecode.parse()
|
||||||
self.network = None
|
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 []
|
self.tags = list(set(config.device.tags.split(','))) if config.device.tags else []
|
||||||
if self.tags and len(self.tags):
|
if self.tags and len(self.tags):
|
||||||
|
@ -94,6 +95,8 @@ class VirtualMachine(object):
|
||||||
vm = nb.virtualization.virtual_machines.create(
|
vm = nb.virtualization.virtual_machines.create(
|
||||||
name=hostname,
|
name=hostname,
|
||||||
cluster=cluster.id,
|
cluster=cluster.id,
|
||||||
|
platform=self.device_platform,
|
||||||
|
device_platform=self.device_platform,
|
||||||
vcpus=vcpus,
|
vcpus=vcpus,
|
||||||
memory=memory,
|
memory=memory,
|
||||||
tenant=tenant.id if tenant else None,
|
tenant=tenant.id if tenant else None,
|
||||||
|
@ -114,6 +117,9 @@ class VirtualMachine(object):
|
||||||
if sorted(set(vm.tags)) != sorted(set(self.tags)):
|
if sorted(set(vm.tags)) != sorted(set(self.tags)):
|
||||||
vm.tags = self.tags
|
vm.tags = self.tags
|
||||||
updated += 1
|
updated += 1
|
||||||
|
if vm.platform != self.device_platform:
|
||||||
|
vm.platform = self.device_platform
|
||||||
|
updated += 1
|
||||||
|
|
||||||
if updated:
|
if updated:
|
||||||
vm.save()
|
vm.save()
|
||||||
|
|
Loading…
Reference in a new issue