remove useless create_ipmi functions to factor the code

This commit is contained in:
Solvik Blum 2020-04-13 00:35:48 +02:00
parent 7164dd280d
commit 35379e5a1e
6 changed files with 54 additions and 73 deletions

View file

@ -1,12 +1,12 @@
import netbox_agent.dmidecode as dmidecode import netbox_agent.dmidecode as dmidecode
from netbox_agent.config import config from netbox_agent.config import config
from netbox_agent.logging import logging # NOQA from netbox_agent.logging import logging # NOQA
from netbox_agent.virtualmachine import VirtualMachine
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
from netbox_agent.vendors.hp import HPHost from netbox_agent.vendors.hp import HPHost
from netbox_agent.vendors.qct import QCTHost 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
MANUFACTURERS = { MANUFACTURERS = {
'Dell Inc.': DellHost, 'Dell Inc.': DellHost,

View file

@ -1,6 +1,8 @@
import logging import logging
import subprocess import subprocess
from netaddr import IPNetwork
class IPMI(): class IPMI():
""" """
@ -40,11 +42,27 @@ class IPMI():
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 = {} _ipmi = {}
if self.ret != 0: if self.ret != 0:
return ret return _ipmi
for line in self.output.splitlines(): for line in self.output.splitlines():
key = line.split(':')[0].strip() key = line.split(':')[0].strip()
if key not in ['802.1q VLAN ID', 'IP Address', 'Subnet Mask', 'MAC Address']:
continue
value = ':'.join(line.split(':')[1:]).strip() value = ':'.join(line.split(':')[1:]).strip()
ret[key] = value _ipmi[key] = value
ret = {}
ret['name'] = 'IPMI'
ret['bonding'] = False
ret['mac'] = _ipmi['MAC Address']
ret['vlan'] = int(_ipmi['802.1q VLAN ID']) \
if _ipmi['802.1q VLAN ID'] != 'Disabled' else None
ip = _ipmi['IP Address']
netmask = _ipmi['Subnet Mask']
address = str(IPNetwork('{}/{}'.format(ip, netmask)))
ret['ip'] = [address]
ret['ipmi'] = True
return ret return ret

View file

@ -1,15 +1,6 @@
import os
import re
import netifaces
from netaddr import IPAddress, IPNetwork
from shutil import which
import socket import socket
import subprocess
from shutil import which
from netbox_agent.ethtool import Ethtool
from netbox_agent.ipmi import IPMI
from netbox_agent.lldp import LLDP
from netbox_agent.logging import logging # NOQA
def is_tool(name): def is_tool(name):

View file

@ -4,7 +4,7 @@ import re
from itertools import chain from itertools import chain
import netifaces import netifaces
from netaddr import IPAddress, IPNetwork from netaddr import IPAddress
from netbox_agent.config import config from netbox_agent.config import config
from netbox_agent.config import netbox_instance as nb from netbox_agent.config import netbox_instance as nb
@ -18,7 +18,12 @@ class Network(object):
self.nics = [] self.nics = []
self.lldp = LLDP() if config.network.lldp else None self.lldp = LLDP() if config.network.lldp else None
self.scan() self.nics = self.scan()
self.ipmi = None
if self.get_network_type() == 'server':
self.ipmi = self.get_ipmi()
if self.ipmi:
self.nics.append(self.ipmi)
self.dcim_choices = {} self.dcim_choices = {}
dcim_c = nb.dcim.choices() dcim_c = nb.dcim.choices()
@ -59,6 +64,7 @@ class Network(object):
return interface return interface
def scan(self): def scan(self):
nics = []
for interface in os.listdir('/sys/class/net/'): for interface in os.listdir('/sys/class/net/'):
# ignore if it's not a link (ie: bonding_masters etc) # ignore if it's not a link (ie: bonding_masters etc)
if not os.path.islink('/sys/class/net/{}'.format(interface)): if not os.path.islink('/sys/class/net/{}'.format(interface)):
@ -122,7 +128,8 @@ class Network(object):
'bonding': bonding, 'bonding': bonding,
'bonding_slaves': bonding_slaves, 'bonding_slaves': bonding_slaves,
} }
self.nics.append(nic) nics.append(nic)
return nics
def _set_bonding_interfaces(self): def _set_bonding_interfaces(self):
bonding_nics = (x for x in self.nics if x['bonding']) bonding_nics = (x for x in self.nics if x['bonding'])
@ -201,9 +208,10 @@ class Network(object):
# if it's a vlan interface # if it's a vlan interface
elif vlan_id and ( elif vlan_id and (
interface.mode is None or interface.mode is None or
interface.mode.value != self.dcim_choices['interface:mode']['Access'] or type(interface.mode) is not int and (
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): interface.tagged_vlans[0].vid != 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
@ -345,7 +353,10 @@ class Network(object):
interface = self.get_netbox_network_card(nic) interface = self.get_netbox_network_card(nic)
# if network doesn't exist we create it # if network doesn't exist we create it
if not interface: if not interface:
new_interface = self.create_netbox_nic(nic) new_interface = self.create_netbox_nic(
nic,
mgmt=True if 'ipmi' in nic.keys() else False
)
if nic['ip']: if nic['ip']:
# for each ip, we try to find it # for each ip, we try to find it
# assign the device's interface to it # assign the device's interface to it
@ -353,8 +364,6 @@ class Network(object):
for ip in nic['ip']: for ip in nic['ip']:
self.create_or_update_netbox_ip_on_interface(ip, new_interface) self.create_or_update_netbox_ip_on_interface(ip, new_interface)
self._set_bonding_interfaces() self._set_bonding_interfaces()
if self.get_network_type() == 'server':
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):
@ -405,11 +414,11 @@ class Network(object):
ret, interface = self.reset_vlan_on_interface(nic, interface) ret, interface = self.reset_vlan_on_interface(nic, interface)
nic_update += ret nic_update += ret
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 \
type != interface.type.value: _type != interface.type.value:
logging.info('Interface type is wrong, resetting') logging.info('Interface type is wrong, resetting')
interface.type = type interface.type = _type
nic_update += 1 nic_update += 1
if hasattr(interface, 'lag') and interface.lag is not None: if hasattr(interface, 'lag') and interface.lag is not None:
@ -439,18 +448,17 @@ class Network(object):
interface.save() interface.save()
self._set_bonding_interfaces() self._set_bonding_interfaces()
if self.get_network_type() == 'server':
self.create_or_update_ipmi()
logging.debug('Finished updating NIC!') logging.debug('Finished updating NIC!')
class ServerNetwork(Network): class ServerNetwork(Network):
def __init__(self, server, *args, **kwargs): def __init__(self, server, *args, **kwargs):
super(VirtualNetwork, self).__init__(server, args, kwargs) super(ServerNetwork, self).__init__(server, args, kwargs)
self.server = server self.server = server
self.device = self.server.get_netbox_server() self.device = self.server.get_netbox_server()
self.nb_net = nb.dcim self.nb_net = nb.dcim
self.custom_arg = {'device_id': self.device.id} self.custom_arg = {'device': self.device.id}
self.custom_arg_id = {'device_id': self.device.id}
def get_network_type(self): def get_network_type(self):
return 'server' return 'server'
@ -466,42 +474,6 @@ class ServerNetwork(Network):
mac=mac mac=mac
) )
def create_or_update_ipmi(self):
ipmi = self.get_ipmi()
mac = ipmi['MAC Address']
ip = ipmi['IP Address']
netmask = ipmi['Subnet Mask']
vlan = int(ipmi['802.1q VLAN ID']) if ipmi['802.1q VLAN ID'] != 'Disabled' else None
address = str(IPNetwork('{}/{}'.format(ip, netmask)))
interface = self.nb_net.interfaces.get(
device_id=self.device.id,
mgmt_only=True,
)
nic = {
'name': 'IPMI',
'mac': mac,
'vlan': vlan,
'ip': [address],
}
if interface is None:
interface = self.create_netbox_nic(nic, mgmt=True)
self.create_or_update_netbox_ip_on_interface(address, interface)
else:
# let the user chose the name of mgmt ?
# guess it with manufacturer (IDRAC, ILO, ...) ?
update = False
self.create_or_update_netbox_ip_on_interface(address, interface)
update, interface = self.reset_vlan_on_interface(nic, interface)
if mac.upper() != interface.mac_address:
logging.info('IPMI mac changed from {old_mac} to {new_mac}'.format(
old_mac=interface.mac_address, new_mac=mac.upper()))
interface.mac_address = mac
update = True
if update:
interface.save()
return interface
def connect_interface_to_switch(self, switch_ip, switch_interface, nb_server_interface): def connect_interface_to_switch(self, switch_ip, switch_interface, nb_server_interface):
logging.info('Interface {} is not connected to switch, trying to connect..'.format( logging.info('Interface {} is not connected to switch, trying to connect..'.format(
nb_server_interface.name nb_server_interface.name
@ -606,7 +578,6 @@ class ServerNetwork(Network):
return update, nb_server_interface return update, nb_server_interface
class VirtualNetwork(Network): class VirtualNetwork(Network):
def __init__(self, server, *args, **kwargs): def __init__(self, server, *args, **kwargs):
super(VirtualNetwork, self).__init__(server, args, kwargs) super(VirtualNetwork, self).__init__(server, args, kwargs)

View file

@ -8,7 +8,7 @@ from netbox_agent.config import config
from netbox_agent.config import netbox_instance as nb from netbox_agent.config import netbox_instance as nb
from netbox_agent.inventory import Inventory from netbox_agent.inventory import Inventory
from netbox_agent.location import Datacenter, Rack from netbox_agent.location import Datacenter, Rack
from netbox_agent.network import Network from netbox_agent.network import ServerNetwork
from netbox_agent.power import PowerSupply from netbox_agent.power import PowerSupply

View file

@ -5,6 +5,7 @@ from netbox_agent.logging import logging # NOQA
from netbox_agent.misc import get_hostname from netbox_agent.misc import get_hostname
from netbox_agent.network import VirtualNetwork from netbox_agent.network import VirtualNetwork
class VirtualMachine(object): class VirtualMachine(object):
def __init__(self, dmi=None): def __init__(self, dmi=None):
if dmi: if dmi:
@ -37,8 +38,8 @@ class VirtualMachine(object):
vm = nb.virtualization.virtual_machines.create( vm = nb.virtualization.virtual_machines.create(
name=hostname, name=hostname,
cluster=cluster.id, cluster=cluster.id,
vcpu=0, vcpu=0, # FIXME
memory=0, memory=0, # FIXME
) )
self.network = VirtualNetwork(server=self) self.network = VirtualNetwork(server=self)
self.network.update_netbox_network_cards() self.network.update_netbox_network_cards()