network update

This commit is contained in:
Solvik Blum 2019-08-09 12:00:48 +02:00
commit f7809eb3be
No known key found for this signature in database
GPG key ID: CC12B3DC262B6C47
2 changed files with 109 additions and 74 deletions

View file

@ -1,6 +1,7 @@
import logging import logging
import subprocess import subprocess
class Ipmi(): class Ipmi():
""" """
Parse IPMI output Parse IPMI output
@ -34,16 +35,15 @@ class Ipmi():
""" """
def __init__(self): def __init__(self):
self.ret, self.output = subprocess.getstatusoutput('ipmitool lan print') self.ret, self.output = subprocess.getstatusoutput('ipmitool lan print')
if not self.ret: if self.ret != 0:
logging.error('Cannot get ipmi info: {}'.format(self.output)) logging.error('Cannot get ipmi info: {}'.format(self.output))
def parse(self): def parse(self):
ret = {} ret = {}
if self.ret != 0: if self.ret != 0:
return ret return ret
for line in self.output.split('\n'): for line in self.output.split('\n'):
key = line.split(':')[0] key = line.split(':')[0].strip()
value = ':'.join(line.split(':')[1:]) value = ':'.join(line.split(':')[1:]).strip()
ret[key] = value ret[key] = value
return ret return ret

View file

@ -91,8 +91,11 @@ class Network():
self.nics.append(nic) self.nics.append(nic)
def _set_bonding_interfaces(self): def _set_bonding_interfaces(self):
bonding_nics = [x for x in self.nics if x['bonding']]
if not len(bonding_nics):
return False
logging.debug('Setting bonding interfaces..') logging.debug('Setting bonding interfaces..')
for nic in [x for x in self.nics if x['bonding']]: for nic in bonding_nics:
bond_int = self.get_netbox_network_card(nic) bond_int = self.get_netbox_network_card(nic)
logging.debug('Setting slave interface for {name}'.format( logging.debug('Setting slave interface for {name}'.format(
name=bond_int.name name=bond_int.name
@ -105,6 +108,7 @@ class Network():
)) ))
slave_int.lag = bond_int slave_int.lag = bond_int
slave_int.save() slave_int.save()
return True
def get_network_cards(self): def get_network_cards(self):
return self.nics return self.nics
@ -126,10 +130,11 @@ class Network():
def get_netbox_network_cards(self): def get_netbox_network_cards(self):
return nb.dcim.interfaces.filter( return nb.dcim.interfaces.filter(
device_id=self.device.id, device_id=self.device.id,
mgmt_only=False,
) )
def get_netbox_type_for_nic(self, nic): def get_netbox_type_for_nic(self, nic):
if nic['bonding']: if nic.get('bonding'):
return IFACE_TYPE_LAG return IFACE_TYPE_LAG
if nic.get('ethtool') is None: if nic.get('ethtool') is None:
return IFACE_TYPE_OTHER return IFACE_TYPE_OTHER
@ -154,59 +159,124 @@ class Network():
mac=mac mac=mac
) )
def get_or_create_vlan(self, vlan_id):
# FIXME: we may need to specify the datacenter
# since users may have same vlan id in multiple dc
vlan = nb.ipam.vlans.get(
vid=vlan_id,
)
if vlan is None:
vlan = nb.ipam.vlans.create(
name='VLAN {}'.format(vlan_id),
vid=vlan_id,
)
return vlan
def reset_vlan_on_interface(self, vlan_id, interface):
update = False
if vlan_id is None and \
(interface.mode is not None or len(interface.tagged_vlans) > 0):
logging.info('Interface {interface} is not tagged, reseting mode'.format(
interface=interface))
update = True
interface.mode = None
interface.tagged_vlans = []
elif vlan_id and (
interface.mode is None or
len(interface.tagged_vlans) != 1 or
interface.tagged_vlans[0].vid != vlan_id):
logging.info('Resetting VLAN on interface {interface}'.format(
interface=interface))
update = True
nb_vlan = self.get_or_create_vlan(vlan_id)
interface.mode = 200
interface.tagged_vlans = [nb_vlan] if nb_vlan else []
return update, interface
def create_or_update_ipmi(self): def create_or_update_ipmi(self):
ipmi = self.get_ipmi() ipmi = self.get_ipmi()
mac = ipmi['MAC Address'] mac = ipmi['MAC Address']
ip = ipmi['IP Address'] ip = ipmi['IP Address']
netmask = ipmi['Subnet Mask'] netmask = ipmi['Subnet Mask']
vlan = ipmi['802.1q VLAN ID'] if ipmi['802.1q VLAN ID'] != 'Disabled' else None vlan = int(ipmi['802.1q VLAN ID']) if ipmi['802.1q VLAN ID'] != 'Disabled' else None
address = IPNetwork('{}/{}'.format(ip, netmask)).__str__() address = IPNetwork('{}/{}'.format(ip, netmask)).__str__()
device = nb.dcim.interfaces.get( interface = nb.dcim.interfaces.get(
device_id=self.device.id, device_id=self.device.id,
mgmt_only=True, mgmt_only=True,
) )
if device is None: nic = {
return nb.dcim.interfaces.create( 'name': 'IPMI',
device=self.device.id, 'mac': mac,
name='IPMI', 'vlan': vlan,
mac_address=ipmi['MAC Address'], 'ip': [address],
address=address, }
mode=200 if vlan else None, if interface is None:
type=IFACE_TYPE_1GE_FIXED, interface = self.create_netbox_nic(nic, mgmt=True)
mgmt_only=True, self.create_or_update_netbox_ip_on_interface(address, interface)
)
else: else:
# let the user chose the name of mgmt ? # let the user chose the name of mgmt ?
# guess it with manufacturer (IDRAC, ILO, ...) ? # guess it with manufacturer (IDRAC, ILO, ...) ?
update = False update = False
if device.address != address: self.create_or_update_netbox_ip_on_interface(address, interface)
device.address = address update, interface = self.reset_vlan_on_interface(nic['vlan'], interface)
update = True if mac != interface.mac_address:
if vlan and device.mode != 200: interface.mac_address = mac
device.mode = 200
update = True
if mac != device.mac_address:
device.mac_address = mac
update = True update = True
if update: if update:
device.save() interface.save()
return device return interface
def create_netbox_nic(self, nic): def create_netbox_nic(self, nic, mgmt=False):
# TODO: add Optic Vendor, PN and Serial # TODO: add Optic Vendor, PN and Serial
type = self.get_netbox_type_for_nic(nic) type = self.get_netbox_type_for_nic(nic)
logging.info('Creating NIC {name} ({mac}) on {device}'.format( logging.info('Creating NIC {name} ({mac}) on {device}'.format(
name=nic['name'], mac=nic['mac'], device=self.device.name)) name=nic['name'], mac=nic['mac'], device=self.device.name))
nb_vlan = None
if nic['vlan']:
nb_vlan = self.get_or_create_vlan(nic['vlan'])
return nb.dcim.interfaces.create( return nb.dcim.interfaces.create(
device=self.device.id, device=self.device.id,
name=nic['name'], name=nic['name'],
mac_address=nic['mac'], mac_address=nic['mac'],
type=type, type=type,
mode=200 if nic['vlan'] else None, mode=200 if nic['vlan'] else None,
tagged_vlans=[nb_vlan.id] if nb_vlan is not None else [],
mgmt_only=mgmt,
) )
def create_or_update_netbox_ip_on_interface(self, ip, interface):
netbox_ip = nb.ipam.ip_addresses.get(
address=ip,
)
if netbox_ip:
if netbox_ip.interface is None:
logging.info('Assigning existing IP {ip} to {interface}'.format(
ip=ip, interface=interface))
elif netbox_ip.interface.id != interface.id:
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=netbox_ip.interface, new_interface=interface,
old_id=netbox_ip.id, new_id=interface.id, ip=netbox_ip.address
))
else:
return netbox_ip
netbox_ip.interface = interface
netbox_ip.save()
else:
logging.info('Create new IP {ip} on {interface}'.format(
ip=ip, interface=interface))
netbox_ip = nb.ipam.ip_addresses.create(
address=ip,
interface=interface.id,
status=1,
)
return netbox_ip
def create_netbox_network_cards(self): def create_netbox_network_cards(self):
logging.debug('Creating NIC...') logging.debug('Creating NIC...')
for nic in self.nics: for nic in self.nics:
@ -219,23 +289,9 @@ class Network():
# assign the device's interface to it # assign the device's interface to it
# or simply create it # or simply create it
for ip in nic['ip']: for ip in nic['ip']:
netbox_ip = nb.ipam.ip_addresses.get( self.create_or_update_netbox_ip_on_interface(ip, new_interface)
address=ip,
)
if netbox_ip:
logging.info('Assigning existing IP {ip} to {interface}'.format(
ip=ip, interface=new_interface))
netbox_ip.interface = new_interface
netbox_ip.save()
else:
logging.info('Create new IP {ip} on {interface}'.format(
ip=ip, interface=new_interface))
netbox_ip = nb.ipam.ip_addresses.create(
address=ip,
interface=new_interface.id,
status=1,
)
self._set_bonding_interfaces() self._set_bonding_interfaces()
self.create_or_update_ipmi()
logging.debug('Finished creating NIC!') logging.debug('Finished creating NIC!')
def update_netbox_network_cards(self): def update_netbox_network_cards(self):
@ -253,7 +309,8 @@ class Network():
# delete IP on netbox that are not known on this server # delete IP on netbox that are not known on this server
netbox_ips = nb.ipam.ip_addresses.filter( netbox_ips = nb.ipam.ip_addresses.filter(
device_id=self.device.id device_id=self.device.id,
interface_id=[x.id for x in nb_nics],
) )
all_local_ips = list(chain.from_iterable([ all_local_ips = list(chain.from_iterable([
x['ip'] for x in self.nics if x['ip'] is not None x['ip'] for x in self.nics if x['ip'] is not None
@ -281,10 +338,7 @@ class Network():
interface=interface, name=nic['name'])) interface=interface, name=nic['name']))
interface.name = nic['name'] interface.name = nic['name']
if nic['vlan'] is None and interface.mode is not None: nic_update, interface = self.reset_vlan_on_interface(nic['vlan'], interface)
logging.info('Interface is not tagged, reseting mode')
nic_update = True
interface.mode = None
type = self.get_netbox_type_for_nic(nic) type = self.get_netbox_type_for_nic(nic)
if not interface.type or \ if not interface.type or \
@ -294,7 +348,9 @@ class Network():
interface.type = type interface.type = type
if interface.lag is not None: if interface.lag is not None:
local_lag_int = next(item for item in self.nics if item['name'] == interface.lag.name) local_lag_int = next(
item for item in self.nics if item['name'] == interface.lag.name
)
if nic['name'] not in local_lag_int['bonding_slaves']: if nic['name'] not in local_lag_int['bonding_slaves']:
logging.info('Interface has no LAG, resetting') logging.info('Interface has no LAG, resetting')
nic_update = True nic_update = True
@ -306,28 +362,7 @@ class Network():
netbox_ip = nb.ipam.ip_addresses.get( netbox_ip = nb.ipam.ip_addresses.get(
address=ip, address=ip,
) )
if not netbox_ip: self.create_or_update_netbox_ip_on_interface(ip, interface)
# create netbox_ip on device
netbox_ip = nb.ipam.ip_addresses.create(
address=ip,
interface=interface.id,
status=1,
)
logging.info('Created new IP {ip} on {interface}'.format(
ip=ip, interface=interface))
else:
if not netbox_ip.interface or \
netbox_ip.interface.id != interface.id:
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=netbox_ip.interface, new_interface=interface,
old_id=netbox_ip.id, new_id=interface.id, ip=netbox_ip.address
))
netbox_ip.interface = interface
netbox_ip.save()
if nic_update: if nic_update:
interface.save() interface.save()