Truncate Serials Over 50 characters in inventory.py #291
4 changed files with 118 additions and 97 deletions
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"python.analysis.typeCheckingMode": "basic"
|
||||
}
|
|
@ -172,7 +172,7 @@ class Inventory():
|
|||
discovered=True,
|
||||
tags=[{'name': INVENTORY_TAG['interface']['name']}],
|
||||
name="{}".format(iface['product']),
|
||||
serial='{}'.format(iface['serial']),
|
||||
serial='{}'.format(iface['serial'][:50]),
|
||||
description='{} {}'.format(iface['description'], iface['name'])
|
||||
)
|
||||
|
||||
|
@ -256,7 +256,7 @@ class Inventory():
|
|||
)
|
||||
|
||||
name = raid_card.get_product_name()
|
||||
serial = raid_card.get_serial_number()
|
||||
serial = raid_card.get_serial_number()[:50]
|
||||
nb_raid_card = nb.dcim.inventory_items.create(
|
||||
device=self.device_id,
|
||||
discovered=True,
|
||||
|
@ -371,7 +371,7 @@ class Inventory():
|
|||
desc = disk.get('description')
|
||||
name = '{} ({})'.format(disk['Model'], disk['Size'])
|
||||
description = disk['Type']
|
||||
sn = disk.get('SN', 'unknown')
|
||||
sn = disk.get('SN', 'unknown')[:50]
|
||||
|
||||
parms = {
|
||||
'device': self.device_id,
|
||||
|
@ -455,7 +455,7 @@ class Inventory():
|
|||
tags=[{'name': INVENTORY_TAG['memory']['name']}],
|
||||
name=name,
|
||||
part_id=memory['product'],
|
||||
serial=memory['serial'],
|
||||
serial=memory['serial'][:50],
|
||||
description=memory['description'],
|
||||
)
|
||||
|
||||
|
|
|
@ -54,6 +54,11 @@ class Network(object):
|
|||
logging.debug('Ignore interface {interface}'.format(interface=interface))
|
||||
continue
|
||||
|
||||
# Skip if the interface is ib0
|
||||
if interface in ('ib0', 'ib1'):
|
||||
logging.debug('Skipping ib interface')
|
||||
continue
|
||||
|
||||
ip_addr = netifaces.ifaddresses(interface).get(netifaces.AF_INET, [])
|
||||
ip6_addr = netifaces.ifaddresses(interface).get(netifaces.AF_INET6, [])
|
||||
if config.network.ignore_ips:
|
||||
|
@ -314,87 +319,98 @@ class Network(object):
|
|||
return interface
|
||||
|
||||
def create_or_update_netbox_ip_on_interface(self, ip, interface):
|
||||
'''
|
||||
Two behaviors:
|
||||
- Anycast IP
|
||||
* If IP exists and is in Anycast, create a new Anycast one
|
||||
* If IP exists and isn't assigned, take it
|
||||
* If server is decomissioned, then free IP will be taken
|
||||
'''
|
||||
Two behaviors:
|
||||
- Anycast IP
|
||||
* If IP exists and is in Anycast, create a new Anycast one
|
||||
* If IP exists and isn't assigned, take it
|
||||
* If server is decomissioned, then free IP will be taken
|
||||
|
||||
- Normal IP (can be associated only once)
|
||||
* If IP doesn't exist, create it
|
||||
* If IP exists and isn't assigned, take it
|
||||
* If IP exists and interface is wrong, change interface
|
||||
'''
|
||||
netbox_ips = nb.ipam.ip_addresses.filter(
|
||||
address=ip,
|
||||
)
|
||||
if not netbox_ips:
|
||||
logging.info('Create new IP {ip} on {interface}'.format(
|
||||
ip=ip, interface=interface))
|
||||
query_params = {
|
||||
'address': ip,
|
||||
'status': "active",
|
||||
'assigned_object_type': self.assigned_object_type,
|
||||
'assigned_object_id': interface.id
|
||||
}
|
||||
|
||||
netbox_ip = nb.ipam.ip_addresses.create(
|
||||
**query_params
|
||||
)
|
||||
return netbox_ip
|
||||
|
||||
netbox_ip = list(netbox_ips)[0]
|
||||
# If IP exists in anycast
|
||||
if netbox_ip.role and netbox_ip.role.label == 'Anycast':
|
||||
logging.debug('IP {} is Anycast..'.format(ip))
|
||||
unassigned_anycast_ip = [x for x in netbox_ips if x.interface is None]
|
||||
assigned_anycast_ip = [x for x in netbox_ips if
|
||||
x.interface and x.interface.id == interface.id]
|
||||
# use the first available anycast ip
|
||||
if len(unassigned_anycast_ip):
|
||||
logging.info('Assigning existing Anycast IP {} to interface'.format(ip))
|
||||
netbox_ip = unassigned_anycast_ip[0]
|
||||
netbox_ip.interface = interface
|
||||
netbox_ip.save()
|
||||
# or if everything is assigned to other servers
|
||||
elif not len(assigned_anycast_ip):
|
||||
logging.info('Creating Anycast IP {} and assigning it to interface'.format(ip))
|
||||
query_params = {
|
||||
"address": ip,
|
||||
"status": "active",
|
||||
"role": self.ipam_choices['ip-address:role']['Anycast'],
|
||||
"tenant": self.tenant.id if self.tenant else None,
|
||||
"assigned_object_type": self.assigned_object_type,
|
||||
"assigned_object_id": interface.id
|
||||
}
|
||||
netbox_ip = nb.ipam.ip_addresses.create(**query_params)
|
||||
return netbox_ip
|
||||
else:
|
||||
ip_interface = getattr(netbox_ip, 'interface', None)
|
||||
assigned_object = getattr(netbox_ip, 'assigned_object', None)
|
||||
if not ip_interface or not assigned_object:
|
||||
logging.info('Assigning existing IP {ip} to {interface}'.format(
|
||||
- Normal IP (can be associated only once)
|
||||
* If IP doesn't exist, create it
|
||||
* If IP exists and isn't assigned, take it
|
||||
* If IP exists and interface is wrong, change interface
|
||||
'''
|
||||
netbox_ip = None
|
||||
# Check if the IP is not 0.0.0.0/0
|
||||
if ip != '0.0.0.0/0':
|
||||
netbox_ips = nb.ipam.ip_addresses.filter(
|
||||
address=ip,
|
||||
)
|
||||
if not netbox_ips:
|
||||
logging.info('Create new IP {ip} on {interface}'.format(
|
||||
ip=ip, interface=interface))
|
||||
elif (ip_interface and ip_interface.id != interface.id) or \
|
||||
(assigned_object and assigned_object_id != interface.id):
|
||||
|
||||
old_interface = getattr(netbox_ip, "assigned_object", "n/a")
|
||||
logging.info(
|
||||
'Detected interface change for ip {ip}: old interface is '
|
||||
'{old_interface} (id: {old_id}), new interface is {new_interface} '
|
||||
' (id: {new_id})'
|
||||
.format(
|
||||
old_interface=old_interface, new_interface=interface,
|
||||
old_id=netbox_ip.id, new_id=interface.id, ip=netbox_ip.address
|
||||
))
|
||||
query_params = {
|
||||
'address': ip,
|
||||
'status': "active",
|
||||
'assigned_object_type': self.assigned_object_type,
|
||||
'assigned_object_id': interface.id
|
||||
}
|
||||
netbox_ip = nb.ipam.ip_addresses.create(
|
||||
**query_params
|
||||
)
|
||||
return netbox_ip
|
||||
else:
|
||||
# IP already exists, log a warning and return None
|
||||
logging.warning('IP {ip} already exists in NetBox'.format(ip=ip))
|
||||
return None
|
||||
else:
|
||||
return netbox_ip
|
||||
# Log that this IP is not being added due to being 0.0.0.0/0
|
||||
logging.info('Skipping IP {ip} on {interface} as it is 0.0.0.0/0'.format(
|
||||
ip=ip, interface=interface))
|
||||
|
||||
netbox_ip.assigned_object_type = self.assigned_object_type
|
||||
netbox_ip.assigned_object_id = interface.id
|
||||
netbox_ip.save()
|
||||
# Check if netbox_ip is not None before accessing its properties
|
||||
if netbox_ip is not None:
|
||||
# Proceed with further processing only if netbox_ip is created
|
||||
if netbox_ip.role and netbox_ip.role.label == 'Anycast':
|
||||
logging.debug('IP {} is Anycast..'.format(ip))
|
||||
unassigned_anycast_ip = [x for x in netbox_ips if x.interface is None]
|
||||
assigned_anycast_ip = [x for x in netbox_ips if
|
||||
x.interface and x.interface.id == interface.id]
|
||||
# use the first available anycast ip
|
||||
if len(unassigned_anycast_ip):
|
||||
logging.info('Assigning existing Anycast IP {} to interface'.format(ip))
|
||||
netbox_ip = unassigned_anycast_ip[0]
|
||||
netbox_ip.interface = interface
|
||||
netbox_ip.save()
|
||||
# or if everything is assigned to other servers
|
||||
elif not len(assigned_anycast_ip):
|
||||
logging.info('Creating Anycast IP {} and assigning it to interface'.format(ip))
|
||||
query_params = {
|
||||
"address": ip,
|
||||
"status": "active",
|
||||
"role": self.ipam_choices['ip-address:role']['Anycast'],
|
||||
"tenant": self.tenant.id if self.tenant else None,
|
||||
"assigned_object_type": self.assigned_object_type,
|
||||
"assigned_object_id": interface.id
|
||||
}
|
||||
netbox_ip = nb.ipam.ip_addresses.create(**query_params)
|
||||
return netbox_ip
|
||||
else:
|
||||
ip_interface = getattr(netbox_ip, 'interface', None)
|
||||
assigned_object = getattr(netbox_ip, 'assigned_object', None)
|
||||
if not ip_interface or not assigned_object:
|
||||
logging.info('Assigning existing IP {ip} to {interface}'.format(
|
||||
ip=ip, interface=interface))
|
||||
elif (ip_interface and ip_interface.id != interface.id) or \
|
||||
(assigned_object and assigned_object.id != interface.id):
|
||||
|
||||
old_interface = getattr(netbox_ip, "assigned_object", "n/a")
|
||||
logging.info(
|
||||
'Detected interface change for ip {ip}: old interface is '
|
||||
'{old_interface} (id: {old_id}), new interface is {new_interface} '
|
||||
' (id: {new_id})'
|
||||
.format(
|
||||
old_interface=old_interface, new_interface=interface,
|
||||
old_id=netbox_ip.id, new_id=interface.id, ip=netbox_ip.address
|
||||
))
|
||||
else:
|
||||
return netbox_ip
|
||||
|
||||
netbox_ip.assigned_object_type = self.assigned_object_type
|
||||
netbox_ip.assigned_object_id = interface.id
|
||||
netbox_ip.save()
|
||||
|
||||
def create_or_update_netbox_network_cards(self):
|
||||
if config.update_all is None or config.update_network is None:
|
||||
return None
|
||||
|
@ -495,7 +511,7 @@ class Network(object):
|
|||
|
||||
class ServerNetwork(Network):
|
||||
def __init__(self, server, *args, **kwargs):
|
||||
super(ServerNetwork, self).__init__(server, args, kwargs)
|
||||
super(ServerNetwork, self).__init__(server, *args, **kwargs)
|
||||
|
||||
if config.network.ipmi:
|
||||
self.ipmi = self.get_ipmi()
|
||||
|
@ -524,22 +540,24 @@ class ServerNetwork(Network):
|
|||
nb_mgmt_ip = nb.ipam.ip_addresses.get(
|
||||
address=switch_ip,
|
||||
)
|
||||
if not nb_mgmt_ip:
|
||||
logging.error('Switch IP {} cannot be found in Netbox'.format(switch_ip))
|
||||
return nb_server_interface
|
||||
|
||||
try:
|
||||
nb_switch = nb_mgmt_ip.assigned_object.device
|
||||
logging.info('Found a switch in Netbox based on LLDP infos: {} (id: {})'.format(
|
||||
switch_ip,
|
||||
nb_switch.id
|
||||
))
|
||||
except KeyError:
|
||||
logging.error(
|
||||
'Switch IP {} is found but not associated to a Netbox Switch Device'.format(
|
||||
switch_ip
|
||||
|
||||
# Add the check here
|
||||
if nb_mgmt_ip is not None:
|
||||
try:
|
||||
nb_switch = nb_mgmt_ip.assigned_object.device
|
||||
logging.info('Found a switch in Netbox based on LLDP infos: {} (id: {})'.format(
|
||||
switch_ip,
|
||||
nb_switch.id
|
||||
))
|
||||
except KeyError:
|
||||
logging.error(
|
||||
'Switch IP {} is found but not associated to a Netbox Switch Device'.format(
|
||||
switch_ip
|
||||
)
|
||||
)
|
||||
)
|
||||
return nb_server_interface
|
||||
else:
|
||||
logging.error('No NetBox IP found for switch IP {}'.format(switch_ip))
|
||||
return nb_server_interface
|
||||
|
||||
switch_interface = self.lldp.get_switch_port(nb_server_interface.name)
|
||||
|
|
|
@ -517,4 +517,4 @@ class ServerBase():
|
|||
"""
|
||||
Indicates if the device hosts a drive expansion bay
|
||||
"""
|
||||
return False
|
||||
return False
|
Loading…
Reference in a new issue