flake8 fixes and code cleanup
This commit is contained in:
parent
ef7d318c8c
commit
435df24698
6 changed files with 36 additions and 35 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
from pkg_resources import get_distribution, DistributionNotFound
|
from pkg_resources import DistributionNotFound, get_distribution
|
||||||
|
|
||||||
try:
|
try:
|
||||||
__version__ = get_distribution(__name__).version
|
__version__ = get_distribution(__name__).version
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,7 @@
|
||||||
import argparse
|
import argparse
|
||||||
import sys
|
|
||||||
from pprint import pprint
|
|
||||||
import netbox_agent.dmidecode as dmidecode
|
|
||||||
from netbox_agent.dell.dell import DellHost
|
from netbox_agent.dell.dell import DellHost
|
||||||
|
import netbox_agent.dmidecode as dmidecode
|
||||||
from netbox_agent.hp.hp import HPHost
|
from netbox_agent.hp.hp import HPHost
|
||||||
|
|
||||||
MANUFACTURERS = {
|
MANUFACTURERS = {
|
||||||
|
|
@ -11,6 +10,7 @@ MANUFACTURERS = {
|
||||||
'HPE': HPHost,
|
'HPE': HPHost,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def run(args):
|
def run(args):
|
||||||
manufacturer = dmidecode.get_by_type('Chassis')[0].get('Manufacturer')
|
manufacturer = dmidecode.get_by_type('Chassis')[0].get('Manufacturer')
|
||||||
server = MANUFACTURERS[manufacturer](dmidecode)
|
server = MANUFACTURERS[manufacturer](dmidecode)
|
||||||
|
|
@ -20,8 +20,9 @@ def run(args):
|
||||||
server.netbox_create()
|
server.netbox_create()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description='Process some integers.')
|
parser = argparse.ArgumentParser(description='Netbox agent command line')
|
||||||
parser.add_argument('--register', action='store_true',
|
parser.add_argument('--register', action='store_true',
|
||||||
help='Register server in Netbox')
|
help='Register server in Netbox')
|
||||||
parser.add_argument('--debug', action='store_true',
|
parser.add_argument('--debug', action='store_true',
|
||||||
|
|
@ -30,5 +31,6 @@ def main():
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
return run(args)
|
return run(args)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
import socket
|
|
||||||
from pprint import pprint
|
|
||||||
|
|
||||||
from netbox_agent.server import ServerBase
|
from netbox_agent.server import ServerBase
|
||||||
|
|
||||||
|
|
||||||
class DellHost(ServerBase):
|
class DellHost(ServerBase):
|
||||||
def is_blade(self):
|
def is_blade(self):
|
||||||
return self.get_product_name().startswith('PowerEdge M')
|
return self.get_product_name().startswith('PowerEdge M')
|
||||||
|
|
@ -26,4 +24,3 @@ class DellHost(ServerBase):
|
||||||
if self.is_blade():
|
if self.is_blade():
|
||||||
return self.dmi.get_by_type('chassis')[0]['Serial Number']
|
return self.dmi.get_by_type('chassis')[0]['Serial Number']
|
||||||
return self.get_service_tag()
|
return self.get_service_tag()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,10 @@ import re as _re
|
||||||
import subprocess as _subprocess
|
import subprocess as _subprocess
|
||||||
|
|
||||||
|
|
||||||
_handle_re = _re.compile("^Handle\\s+(.+),\\s+DMI\\s+type\\s+(\\d+),\\s+(\\d+)\\s+bytes$")
|
_handle_re = _re.compile('^Handle\\s+(.+),\\s+DMI\\s+type\\s+(\\d+),\\s+(\\d+)\\s+bytes$')
|
||||||
_in_block_re = _re.compile("^\\t\\t(.+)$")
|
_in_block_re = _re.compile('^\\t\\t(.+)$')
|
||||||
_record_re = _re.compile("\\t(.+):\\s+(.+)$")
|
_record_re = _re.compile('\\t(.+):\\s+(.+)$')
|
||||||
_record2_re = _re.compile("\\t(.+):$")
|
_record2_re = _re.compile('\\t(.+):$')
|
||||||
|
|
||||||
_type2str = {
|
_type2str = {
|
||||||
0: 'BIOS',
|
0: 'BIOS',
|
||||||
|
|
@ -58,10 +58,10 @@ for type_id, type_str in _type2str.items():
|
||||||
|
|
||||||
|
|
||||||
def parse():
|
def parse():
|
||||||
"""
|
'''
|
||||||
parse the full output of the dmidecode
|
parse the full output of the dmidecode
|
||||||
command and return a dic containing the parsed information
|
command and return a dic containing the parsed information
|
||||||
"""
|
'''
|
||||||
buffer = _execute_cmd()
|
buffer = _execute_cmd()
|
||||||
if isinstance(buffer, bytes):
|
if isinstance(buffer, bytes):
|
||||||
buffer = buffer.decode('utf-8')
|
buffer = buffer.decode('utf-8')
|
||||||
|
|
@ -70,7 +70,7 @@ def parse():
|
||||||
|
|
||||||
|
|
||||||
def get_by_type(type_id):
|
def get_by_type(type_id):
|
||||||
"""
|
'''
|
||||||
filter the output of dmidecode per type
|
filter the output of dmidecode per type
|
||||||
0 BIOS
|
0 BIOS
|
||||||
1 System
|
1 System
|
||||||
|
|
@ -115,7 +115,7 @@ def get_by_type(type_id):
|
||||||
40 Additional Information
|
40 Additional Information
|
||||||
41 Onboard Devices Extended Information
|
41 Onboard Devices Extended Information
|
||||||
42 Management Controller Host Interface
|
42 Management Controller Host Interface
|
||||||
"""
|
'''
|
||||||
if isinstance(type_id, str):
|
if isinstance(type_id, str):
|
||||||
type_id = _str2type[type_id]
|
type_id = _str2type[type_id]
|
||||||
|
|
||||||
|
|
@ -129,7 +129,7 @@ def get_by_type(type_id):
|
||||||
|
|
||||||
|
|
||||||
def _execute_cmd():
|
def _execute_cmd():
|
||||||
return _subprocess.check_output("dmidecode", stderr=_subprocess.PIPE)
|
return _subprocess.check_output('dmidecode', stderr=_subprocess.PIPE)
|
||||||
|
|
||||||
|
|
||||||
def _parse(buffer):
|
def _parse(buffer):
|
||||||
|
|
@ -153,21 +153,21 @@ def _parse(buffer):
|
||||||
dmi_handle = handle_data[0]
|
dmi_handle = handle_data[0]
|
||||||
|
|
||||||
output_data[dmi_handle] = {}
|
output_data[dmi_handle] = {}
|
||||||
output_data[dmi_handle]["DMIType"] = int(handle_data[1])
|
output_data[dmi_handle]['DMIType'] = int(handle_data[1])
|
||||||
output_data[dmi_handle]["DMISize"] = int(handle_data[2])
|
output_data[dmi_handle]['DMISize'] = int(handle_data[2])
|
||||||
|
|
||||||
# Okay, we know 2nd line == name
|
# Okay, we know 2nd line == name
|
||||||
output_data[dmi_handle]["DMIName"] = record_element[1]
|
output_data[dmi_handle]['DMIName'] = record_element[1]
|
||||||
|
|
||||||
in_block_elemet = ""
|
in_block_elemet = ''
|
||||||
in_block_list = ""
|
in_block_list = ''
|
||||||
|
|
||||||
# Loop over the rest of the record, gathering values
|
# Loop over the rest of the record, gathering values
|
||||||
for i in range(2, len(record_element), 1):
|
for i in range(2, len(record_element), 1):
|
||||||
if i >= len(record_element):
|
if i >= len(record_element):
|
||||||
break
|
break
|
||||||
# Check whether we are inside a \t\t block
|
# Check whether we are inside a \t\t block
|
||||||
if in_block_elemet != "":
|
if in_block_elemet != '':
|
||||||
|
|
||||||
in_block_data = _in_block_re.findall(record_element[1])
|
in_block_data = _in_block_re.findall(record_element[1])
|
||||||
|
|
||||||
|
|
@ -175,14 +175,14 @@ def _parse(buffer):
|
||||||
if not in_block_list:
|
if not in_block_list:
|
||||||
in_block_list = in_block_data[0][0]
|
in_block_list = in_block_data[0][0]
|
||||||
else:
|
else:
|
||||||
in_block_list = in_block_list + "\t\t" + in_block_data[0][1]
|
in_block_list = in_block_list + '\t\t' + in_block_data[0][1]
|
||||||
|
|
||||||
output_data[dmi_handle][in_block_elemet] = in_block_list
|
output_data[dmi_handle][in_block_elemet] = in_block_list
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
# We are out of the \t\t block; reset it again, and let
|
# We are out of the \t\t block; reset it again, and let
|
||||||
# the parsing continue
|
# the parsing continue
|
||||||
in_block_elemet = ""
|
in_block_elemet = ''
|
||||||
|
|
||||||
record_data = _record_re.findall(record_element[i])
|
record_data = _record_re.findall(record_element[i])
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
from netbox_agent.server import ServerBase
|
from netbox_agent.server import ServerBase
|
||||||
|
|
||||||
|
|
||||||
class HPHost(ServerBase):
|
class HPHost(ServerBase):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(HPHost, self).__init__(*args, **kwargs)
|
super(HPHost, self).__init__(*args, **kwargs)
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,15 @@
|
||||||
import re
|
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import socket
|
import socket
|
||||||
import netbox_agent.dmidecode as dmidecode
|
|
||||||
from netbox_agent.config import netbox_instance as nb
|
from netbox_agent.config import netbox_instance as nb
|
||||||
|
import netbox_agent.dmidecode as dmidecode
|
||||||
|
|
||||||
# Regex to match base interface name
|
# Regex to match base interface name
|
||||||
# Doesn't match vlan interfaces and other loopback etc
|
# Doesn't match vlan interfaces and other loopback etc
|
||||||
INTERFACE_REGEX = re.compile('^(eth[0-9]+|ens[0-9]+|enp[0-9]+s[0-9]f[0-9])$')
|
INTERFACE_REGEX = re.compile('^(eth[0-9]+|ens[0-9]+|enp[0-9]+s[0-9]f[0-9])$')
|
||||||
|
|
||||||
|
|
||||||
class ServerBase():
|
class ServerBase():
|
||||||
def __init__(self, dmi=None):
|
def __init__(self, dmi=None):
|
||||||
if dmi:
|
if dmi:
|
||||||
|
|
@ -74,7 +76,7 @@ class ServerBase():
|
||||||
name='Server Chassis',
|
name='Server Chassis',
|
||||||
)
|
)
|
||||||
datacenter = nb.dcim.sites.get(
|
datacenter = nb.dcim.sites.get(
|
||||||
name='DC3' # FIXME: datacenter support
|
name='DC3', # FIXME: datacenter support
|
||||||
)
|
)
|
||||||
new_chassis = nb.dcim.devices.create(
|
new_chassis = nb.dcim.devices.create(
|
||||||
name=''.format(),
|
name=''.format(),
|
||||||
|
|
@ -93,7 +95,7 @@ class ServerBase():
|
||||||
model=self.get_product_name(),
|
model=self.get_product_name(),
|
||||||
)
|
)
|
||||||
datacenter = nb.dcim.sites.get(
|
datacenter = nb.dcim.sites.get(
|
||||||
name='DC3' # FIXME: datacenter support
|
name='DC3', # FIXME: datacenter support
|
||||||
)
|
)
|
||||||
new_blade = nb.dcim.devices.create(
|
new_blade = nb.dcim.devices.create(
|
||||||
name='{}'.format(socket.gethostname()),
|
name='{}'.format(socket.gethostname()),
|
||||||
|
|
@ -126,7 +128,6 @@ class ServerBase():
|
||||||
)
|
)
|
||||||
return new_server
|
return new_server
|
||||||
|
|
||||||
|
|
||||||
def netbox_create(self):
|
def netbox_create(self):
|
||||||
if self.is_blade():
|
if self.is_blade():
|
||||||
# let's find the blade
|
# let's find the blade
|
||||||
|
|
|
||||||
Loading…
Add table
editor.link_modal.header
Reference in a new issue