better tag management
This commit is contained in:
parent
753956e7de
commit
ae87351be6
3 changed files with 78 additions and 45 deletions
|
|
@ -2,12 +2,32 @@ import socket
|
|||
import subprocess
|
||||
from shutil import which
|
||||
|
||||
from netbox_agent.config import netbox_instance as nb
|
||||
|
||||
|
||||
def is_tool(name):
|
||||
'''Check whether `name` is on PATH and marked as executable.'''
|
||||
return which(name) is not None
|
||||
|
||||
|
||||
def get_device_role(role):
|
||||
device_role = nb.dcim.device_roles.get(
|
||||
name=role
|
||||
)
|
||||
if device_role is None:
|
||||
raise Exception('DeviceRole "{}" does not exist, please create it'.format(role))
|
||||
return device_role
|
||||
|
||||
|
||||
def get_device_type(type):
|
||||
device_type = nb.dcim.device_types.get(
|
||||
model=type
|
||||
)
|
||||
if device_type is None:
|
||||
raise Exception('DeviceType "{}" does not exist, please create it'.format(type))
|
||||
return device_type
|
||||
|
||||
|
||||
def get_vendor(name):
|
||||
vendors = {
|
||||
'PERC': 'Dell',
|
||||
|
|
@ -37,3 +57,15 @@ def get_hostname(config):
|
|||
if config.hostname_cmd is None:
|
||||
return '{}'.format(socket.gethostname())
|
||||
return subprocess.getoutput(config.hostname_cmd)
|
||||
|
||||
|
||||
def create_netbox_tags(tags):
|
||||
for tag in tags:
|
||||
nb_tag = nb.extras.tags.get(
|
||||
name=tag
|
||||
)
|
||||
if not nb_tag:
|
||||
nb_tag = nb.extras.tags.create(
|
||||
name=tag,
|
||||
slug=tag,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -8,28 +8,11 @@ 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_type, get_device_role
|
||||
from netbox_agent.network import ServerNetwork
|
||||
from netbox_agent.power import PowerSupply
|
||||
|
||||
|
||||
def get_device_role(role):
|
||||
device_role = nb.dcim.device_roles.get(
|
||||
name=role
|
||||
)
|
||||
if device_role is None:
|
||||
raise Exception('DeviceRole "{}" does not exist, please create it'.format(role))
|
||||
return device_role
|
||||
|
||||
|
||||
def get_device_type(type):
|
||||
device_type = nb.dcim.device_types.get(
|
||||
model=type
|
||||
)
|
||||
if device_type is None:
|
||||
raise Exception('DeviceType "{}" does not exist, please create it'.format(type))
|
||||
return device_type
|
||||
|
||||
|
||||
class ServerBase():
|
||||
def __init__(self, dmi=None):
|
||||
if dmi:
|
||||
|
|
@ -44,17 +27,9 @@ class ServerBase():
|
|||
|
||||
self.network = None
|
||||
|
||||
def create_netbox_tags(self, tags):
|
||||
for tag in tags:
|
||||
nb_tag = nb.extras.tags.get(
|
||||
name=tag
|
||||
)
|
||||
|
||||
if not nb_tag:
|
||||
nb_tag = nb.extras.tags.create(
|
||||
name=tag,
|
||||
slug=tag,
|
||||
)
|
||||
self.tags = list(set(config.device.tags.split(','))) if config.device.tags else []
|
||||
if self.tags and len(self.tags):
|
||||
create_netbox_tags(self.tags)
|
||||
|
||||
def get_tenant(self):
|
||||
tenant = Tenant()
|
||||
|
|
@ -172,14 +147,11 @@ class ServerBase():
|
|||
site=datacenter.id if datacenter else None,
|
||||
tenant=tenant.id if tenant else None,
|
||||
rack=rack.id if rack else None,
|
||||
tags=tags,
|
||||
tags=self.tags,
|
||||
)
|
||||
return new_chassis
|
||||
|
||||
def _netbox_create_blade(self, chassis, datacenter, tenant, rack):
|
||||
tags = config.device.tags.split(',')
|
||||
self.create_netbox_tags(tags)
|
||||
|
||||
device_role = get_device_role(config.device.blade_role)
|
||||
device_type = get_device_type(self.get_product_name())
|
||||
serial = self.get_service_tag()
|
||||
|
|
@ -197,14 +169,11 @@ class ServerBase():
|
|||
site=datacenter.id if datacenter else None,
|
||||
tenant=tenant.id if tenant else None,
|
||||
rack=rack.id if rack else None,
|
||||
tags=tags,
|
||||
tags=self.tags,
|
||||
)
|
||||
return new_blade
|
||||
|
||||
def _netbox_create_server(self, datacenter, tenant, rack):
|
||||
tags = config.device.tags.split(",")
|
||||
self.create_netbox_tags(tags)
|
||||
|
||||
device_role = get_device_role(config.device.server_role)
|
||||
device_type = get_device_type(self.get_product_name())
|
||||
if not device_type:
|
||||
|
|
@ -221,7 +190,7 @@ class ServerBase():
|
|||
site=datacenter.id if datacenter else None,
|
||||
tenant=tenant.id if tenant else None,
|
||||
rack=rack.id if rack else None,
|
||||
tags=tags,
|
||||
tags=self.tags,
|
||||
)
|
||||
return new_server
|
||||
|
||||
|
|
@ -318,6 +287,10 @@ class ServerBase():
|
|||
update += 1
|
||||
server.name = self.get_hostname()
|
||||
|
||||
if sorted(set(server.tags)) != sorted(set(self.tags)):
|
||||
server.tags = self.tags
|
||||
update += 1
|
||||
|
||||
if config.update_all or config.update_location:
|
||||
ret, server = self.update_netbox_location(server)
|
||||
update += ret
|
||||
|
|
|
|||
|
|
@ -3,8 +3,9 @@ import os
|
|||
import netbox_agent.dmidecode as dmidecode
|
||||
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 get_hostname
|
||||
from netbox_agent.misc import create_netbox_tags, get_hostname
|
||||
from netbox_agent.network import VirtualNetwork
|
||||
|
||||
|
||||
|
|
@ -29,6 +30,10 @@ class VirtualMachine(object):
|
|||
self.dmi = dmidecode.parse()
|
||||
self.network = None
|
||||
|
||||
self.tags = list(set(config.device.tags.split(','))) if config.device.tags else []
|
||||
if self.tags and len(self.tags):
|
||||
create_netbox_tags(self.tags)
|
||||
|
||||
def get_memory(self):
|
||||
mem_bytes = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') # e.g. 4015976448
|
||||
mem_gib = mem_bytes / (1024.**2) # e.g. 3.74
|
||||
|
|
@ -50,6 +55,22 @@ class VirtualMachine(object):
|
|||
)
|
||||
return cluster
|
||||
|
||||
def get_netbox_datacenter(self, name):
|
||||
cluster = self.get_netbox_cluster()
|
||||
if cluster.datacenter:
|
||||
return cluster.datacenter
|
||||
return None
|
||||
|
||||
def get_tenant(self):
|
||||
tenant = Tenant()
|
||||
return tenant.get()
|
||||
|
||||
def get_netbox_tenant(self):
|
||||
tenant = nb.tenancy.tenants.get(
|
||||
slug=self.get_tenant()
|
||||
)
|
||||
return tenant
|
||||
|
||||
def netbox_create_or_update(self, config):
|
||||
logging.debug('It\'s a virtual machine')
|
||||
created = False
|
||||
|
|
@ -60,6 +81,7 @@ class VirtualMachine(object):
|
|||
|
||||
vcpus = self.get_vcpus()
|
||||
memory = self.get_memory()
|
||||
tenant = self.get_netbox_tenant()
|
||||
if not vm:
|
||||
logging.debug('Creating Virtual machine..')
|
||||
cluster = self.get_netbox_cluster(config.virtual.cluster_name)
|
||||
|
|
@ -69,18 +91,24 @@ class VirtualMachine(object):
|
|||
cluster=cluster.id,
|
||||
vcpus=vcpus,
|
||||
memory=memory,
|
||||
tenant=tenant.id if tenant else None,
|
||||
tags=self.tags,
|
||||
)
|
||||
created = True
|
||||
|
||||
self.network = VirtualNetwork(server=self)
|
||||
self.network.create_or_update_netbox_network_cards()
|
||||
|
||||
if not created and vm.vcpus != vcpus:
|
||||
vm.vcpus = vcpus
|
||||
updated += 1
|
||||
elif not created and vm.memory != memory:
|
||||
vm.memory = memory
|
||||
updated += 1
|
||||
if not created:
|
||||
if vm.vcpus != vcpus:
|
||||
vm.vcpus = vcpus
|
||||
updated += 1
|
||||
if vm.memory != memory:
|
||||
vm.memory = memory
|
||||
updated += 1
|
||||
if sorted(set(vm.tags)) != sorted(set(self.tags)):
|
||||
vm.tags = self.tags
|
||||
updated += 1
|
||||
|
||||
if updated:
|
||||
vm.save()
|
||||
|
|
|
|||
Loading…
Add table
editor.link_modal.header
Reference in a new issue