add more compatibility, now to netbox 2.10

This commit is contained in:
Solvik Blum 2021-05-11 20:42:13 +02:00
parent 3639662961
commit 58c18fc2da
4 changed files with 28 additions and 18 deletions

View file

@ -61,6 +61,7 @@ def get_hostname(config):
def create_netbox_tags(tags): def create_netbox_tags(tags):
ret = []
for tag in tags: for tag in tags:
nb_tag = nb.extras.tags.get( nb_tag = nb.extras.tags.get(
name=tag name=tag
@ -70,3 +71,5 @@ def create_netbox_tags(tags):
name=tag, name=tag,
slug=slugify(tag), slug=slugify(tag),
) )
ret.append(nb_tag)
return ret

View file

@ -12,10 +12,11 @@ from netbox_agent.ethtool import Ethtool
from netbox_agent.ipmi import IPMI from netbox_agent.ipmi import IPMI
from netbox_agent.lldp import LLDP from netbox_agent.lldp import LLDP
from packaging import version
class Network(object): class Network(object):
def __init__(self, server, *args, **kwargs): def __init__(self, server, *args, **kwargs):
self.netbox_version = float(nb.version) self.netbox_version = nb.version
self.nics = [] self.nics = []
self.server = server self.server = server
@ -323,7 +324,7 @@ class Network(object):
'address': ip, 'address': ip,
'status': "active", 'status': "active",
} }
if self.netbox_version > 2.8: if version.parse(self.netbox_version) > version.parse('2.8'):
query_params.update({ query_params.update({
'assigned_object_type': self.assigned_object_type, 'assigned_object_type': self.assigned_object_type,
'assigned_object_id': interface.id 'assigned_object_id': interface.id
@ -335,7 +336,7 @@ class Network(object):
**query_params **query_params
) )
else: else:
netbox_ip = netbox_ips[0] netbox_ip = list(netbox_ips)[0]
# If IP exists in anycast # If IP exists in anycast
if netbox_ip.role and netbox_ip.role.label == 'Anycast': if netbox_ip.role and netbox_ip.role.label == 'Anycast':
logging.debug('IP {} is Anycast..'.format(ip)) logging.debug('IP {} is Anycast..'.format(ip))
@ -357,7 +358,7 @@ class Network(object):
"role": self.ipam_choices['ip-address:role']['Anycast'], "role": self.ipam_choices['ip-address:role']['Anycast'],
"tenant": self.tenant.id if self.tenant else None, "tenant": self.tenant.id if self.tenant else None,
} }
if self.netbox_version > 2.8: if version.parse(self.netbox_version) > version.parse('2.8'):
query_params.update({ query_params.update({
'assigned_object_type': self.assigned_object_type, 'assigned_object_type': self.assigned_object_type,
'assigned_object_id': interface.id 'assigned_object_id': interface.id
@ -373,7 +374,7 @@ class Network(object):
ip=ip, interface=interface)) ip=ip, interface=interface))
elif hasattr(netbox_ip, 'interface') and netbox_ip.interface.id != interface.id or \ elif hasattr(netbox_ip, 'interface') and netbox_ip.interface.id != interface.id or \
hasattr(netbox_ip, 'assigned_object') and netbox_ip.assigned_object_id != interface.id: hasattr(netbox_ip, 'assigned_object') and netbox_ip.assigned_object_id != interface.id:
if self.netbox_version > 2.8: if version.parse(self.netbox_version) > version.parse('2.8'):
old_interface = netbox_ip.assigned_object old_interface = netbox_ip.assigned_object
else: else:
old_interface = netbox_ip.interface old_interface = netbox_ip.interface
@ -389,7 +390,7 @@ class Network(object):
else: else:
return netbox_ip return netbox_ip
if self.netbox_version > 2.8: if version.parse(self.netbox_version) > version.parse('2.8'):
netbox_ip.assigned_object_type = self.assigned_object_type netbox_ip.assigned_object_type = self.assigned_object_type
netbox_ip.assigned_object_id = interface.id netbox_ip.assigned_object_id = interface.id
else: else:
@ -403,9 +404,9 @@ class Network(object):
logging.debug('Creating/Updating NIC...') logging.debug('Creating/Updating NIC...')
# delete unknown interface # delete unknown interface
nb_nics = self.get_netbox_network_cards() nb_nics = list(self.get_netbox_network_cards())
local_nics = [x['name'] for x in self.nics] local_nics = [x['name'] for x in self.nics]
for nic in nb_nics[:]: for nic in nb_nics:
if nic.name not in local_nics: if nic.name not in local_nics:
logging.info('Deleting netbox interface {name} because not present locally'.format( logging.info('Deleting netbox interface {name} because not present locally'.format(
name=nic.name name=nic.name
@ -415,7 +416,7 @@ class Network(object):
# delete IP on netbox that are not known on this server # delete IP on netbox that are not known on this server
if len(nb_nics): if len(nb_nics):
if self.netbox_version > 2.8: if version.parse(self.netbox_version) > version.parse('2.8'):
netbox_ips = nb.ipam.ip_addresses.filter( netbox_ips = nb.ipam.ip_addresses.filter(
**{self.intf_type: [x.id for x in nb_nics]} **{self.intf_type: [x.id for x in nb_nics]}
) )
@ -429,7 +430,7 @@ class Network(object):
])) ]))
for netbox_ip in netbox_ips: for netbox_ip in netbox_ips:
if netbox_ip.address not in all_local_ips: if netbox_ip.address not in all_local_ips:
if self.netbox_version < 2.9: if version.parse(self.netbox_version) < version.parse('2.9'):
logging.info('Unassigning IP {ip} from {interface}'.format( logging.info('Unassigning IP {ip} from {interface}'.format(
ip=netbox_ip.address, interface=netbox_ip.interface)) ip=netbox_ip.address, interface=netbox_ip.interface))
netbox_ip.interface = None netbox_ip.interface = None
@ -531,7 +532,10 @@ class ServerNetwork(Network):
return nb_server_interface return nb_server_interface
try: try:
nb_switch = nb_mgmt_ip.interface.device if version.parse(self.netbox_version) > version.parse('2.8'):
nb_switch = nb_mgmt_ip.assigned_object.device
else:
nb_switch = nb_mgmt_ip.interface.device
logging.info('Found a switch in Netbox based on LLDP infos: {} (id: {})'.format( logging.info('Found a switch in Netbox based on LLDP infos: {} (id: {})'.format(
switch_ip, switch_ip,
nb_switch.id nb_switch.id

View file

@ -51,7 +51,7 @@ class PowerSupply():
) )
def create_or_update_power_supply(self): def create_or_update_power_supply(self):
nb_psus = self.get_netbox_power_supply() nb_psus = list(self.get_netbox_power_supply())
psus = self.get_power_supply() psus = self.get_power_supply()
# Delete unknown PSU # Delete unknown PSU

View file

@ -13,6 +13,8 @@ from netbox_agent.misc import create_netbox_tags, get_device_role, get_device_ty
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 packaging import version
class ServerBase(): class ServerBase():
def __init__(self, dmi=None): def __init__(self, dmi=None):
@ -29,8 +31,8 @@ class ServerBase():
self.network = None self.network = None
self.tags = list(set([x.strip() for x in config.device.tags.split(',') if x.strip()])) if config.device.tags else [] self.tags = list(set([x.strip() for x in config.device.tags.split(',') if x.strip()])) if config.device.tags else []
if self.tags and len(self.tags): self.nb_tags = list(create_netbox_tags(self.tags))
create_netbox_tags(self.tags) self.netbox_version = nb.version
def get_tenant(self): def get_tenant(self):
tenant = Tenant() tenant = Tenant()
@ -383,11 +385,12 @@ class ServerBase():
update += 1 update += 1
server.name = self.get_hostname() server.name = self.get_hostname()
if float(nb.version) < 2.8 and sorted(set(server.tags)) != sorted(set(self.tags)): if sorted(set([x.name for x in server.tags])) != sorted(set(self.tags)):
server.tags = self.tags
update += 1 update += 1
else: if version.parse(self.netbox_version) > version.parse('2.8'):
logging.warning("netbox-agent doesn't support tag updates for Netbox version >=2.8") server.tags = [x.id for x in self.nb_tags]
else:
server.tags = self.tags
if config.update_all or config.update_location: if config.update_all or config.update_location:
ret, server = self.update_netbox_location(server) ret, server = self.update_netbox_location(server)