this MR will drop compatibility with versions < 2.9

This commit is contained in:
Solvik Blum 2021-05-12 19:40:36 +02:00
parent 95d7f98389
commit 9eafcbf215
3 changed files with 26 additions and 59 deletions

View file

@ -1,5 +1,5 @@
import netbox_agent.dmidecode as dmidecode import netbox_agent.dmidecode as dmidecode
from netbox_agent.config import config from netbox_agent.config import config, netbox_instance as nb
from netbox_agent.logging import logging # NOQA from netbox_agent.logging import logging # NOQA
from netbox_agent.vendors.dell import DellHost from netbox_agent.vendors.dell import DellHost
from netbox_agent.vendors.generic import GenericHost from netbox_agent.vendors.generic import GenericHost
@ -8,6 +8,8 @@ from netbox_agent.vendors.qct import QCTHost
from netbox_agent.vendors.supermicro import SupermicroHost from netbox_agent.vendors.supermicro import SupermicroHost
from netbox_agent.virtualmachine import VirtualMachine, is_vm from netbox_agent.virtualmachine import VirtualMachine, is_vm
from packaging import version
MANUFACTURERS = { MANUFACTURERS = {
'Dell Inc.': DellHost, 'Dell Inc.': DellHost,
'HP': HPHost, 'HP': HPHost,
@ -32,6 +34,10 @@ def run(config):
except KeyError: except KeyError:
server = GenericHost(dmi=dmi) server = GenericHost(dmi=dmi)
if version.parse(nb.version) < version.parse('2.9'):
print('netbox-agent is not compatible with Netbox prior to verison 2.9')
return False
if config.debug: if config.debug:
server.print_debug() server.print_debug()
if config.register or config.update_all or config.update_network or config.update_location or \ if config.register or config.update_all or config.update_network or config.update_location or \

View file

@ -12,11 +12,8 @@ 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 = nb.version
self.nics = [] self.nics = []
self.server = server self.server = server
@ -91,7 +88,8 @@ class Network(object):
mac = open('/sys/class/net/{}/address'.format(interface), 'r').read().strip() mac = open('/sys/class/net/{}/address'.format(interface), 'r').read().strip()
vlan = None vlan = None
if len(interface.split('.')) > 1: if len(interface.split('.')) > 1:
vlan = int(interface.split('.')[1]) vlan = interface.split('.')[1]
bonding = False bonding = False
bonding_slaves = [] bonding_slaves = []
if os.path.isdir('/sys/class/net/{}/bonding'.format(interface)): if os.path.isdir('/sys/class/net/{}/bonding'.format(interface)):
@ -231,7 +229,7 @@ class Network(object):
type(interface.mode) is not int and ( type(interface.mode) is not int and (
interface.mode.value == self.dcim_choices['interface:mode']['Access'] or interface.mode.value == self.dcim_choices['interface:mode']['Access'] or
len(interface.tagged_vlans) != 1 or len(interface.tagged_vlans) != 1 or
interface.tagged_vlans[0].vid != vlan_id)): int(interface.tagged_vlans[0].vid) != int(vlan_id))):
logging.info('Resetting tagged VLAN(s) on interface {interface}'.format( logging.info('Resetting tagged VLAN(s) on interface {interface}'.format(
interface=interface)) interface=interface))
update = True update = True
@ -327,14 +325,9 @@ class Network(object):
query_params = { query_params = {
'address': ip, 'address': ip,
'status': "active", 'status': "active",
}
if version.parse(self.netbox_version) > version.parse('2.8'):
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
}) }
else:
query_params.update({'interface_id': interface.id})
netbox_ip = nb.ipam.ip_addresses.create( netbox_ip = nb.ipam.ip_addresses.create(
**query_params **query_params
@ -361,14 +354,9 @@ class Network(object):
"status": "active", "status": "active",
"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,
"assigned_object_type": self.assigned_object_type,
"assigned_object_id": interface.id
} }
if version.parse(self.netbox_version) > version.parse('2.8'):
query_params.update({
'assigned_object_type': self.assigned_object_type,
'assigned_object_id': interface.id
})
else:
query_params.update({'interface': interface.id})
netbox_ip = nb.ipam.ip_addresses.create(**query_params) netbox_ip = nb.ipam.ip_addresses.create(**query_params)
return netbox_ip return netbox_ip
else: else:
@ -378,11 +366,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 version.parse(self.netbox_version) > version.parse('2.8'):
old_interface = netbox_ip.assigned_object old_interface = netbox_ip.assigned_object
else:
old_interface = netbox_ip.interface
logging.info( logging.info(
'Detected interface change for ip {ip}: old interface is ' 'Detected interface change for ip {ip}: old interface is '
'{old_interface} (id: {old_id}), new interface is {new_interface} ' '{old_interface} (id: {old_id}), new interface is {new_interface} '
@ -394,11 +378,8 @@ class Network(object):
else: else:
return netbox_ip return netbox_ip
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:
netbox_ip.interface = interface
netbox_ip.save() netbox_ip.save()
return netbox_ip return netbox_ip
@ -420,14 +401,9 @@ 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 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]}
) )
else:
netbox_ips = nb.ipam.ip_addresses.filter(
interface_id=[x.id for x in nb_nics],
)
netbox_ips = list(netbox_ips) netbox_ips = list(netbox_ips)
all_local_ips = list(chain.from_iterable([ all_local_ips = list(chain.from_iterable([
@ -435,11 +411,6 @@ 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 version.parse(self.netbox_version) < version.parse('2.9'):
logging.info('Unassigning IP {ip} from {interface}'.format(
ip=netbox_ip.address, interface=netbox_ip.interface))
netbox_ip.interface = None
else:
logging.info('Unassigning IP {ip} from {interface}'.format( logging.info('Unassigning IP {ip} from {interface}'.format(
ip=netbox_ip.address, interface=netbox_ip.assigned_object)) ip=netbox_ip.address, interface=netbox_ip.assigned_object))
netbox_ip.assigned_object_type = None netbox_ip.assigned_object_type = None
@ -537,10 +508,7 @@ class ServerNetwork(Network):
return nb_server_interface return nb_server_interface
try: try:
if version.parse(self.netbox_version) > version.parse('2.8'):
nb_switch = nb_mgmt_ip.assigned_object.device 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

@ -13,9 +13,6 @@ 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):
if dmi: if dmi:
@ -32,7 +29,6 @@ class ServerBase():
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 []
self.nb_tags = list(create_netbox_tags(self.tags)) self.nb_tags = list(create_netbox_tags(self.tags))
self.netbox_version = nb.version
def get_tenant(self): def get_tenant(self):
tenant = Tenant() tenant = Tenant()
@ -387,10 +383,7 @@ class ServerBase():
if sorted(set([x.name for x in server.tags])) != sorted(set(self.tags)): if sorted(set([x.name for x in server.tags])) != sorted(set(self.tags)):
update += 1 update += 1
if version.parse(self.netbox_version) > version.parse('2.8'):
server.tags = [x.id for x in self.nb_tags] 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)