2019-08-04 14:37:51 +02:00
|
|
|
from pprint import pprint
|
2019-08-04 15:14:36 +02:00
|
|
|
import socket
|
2019-08-04 00:00:22 +02:00
|
|
|
|
2019-08-03 15:46:21 +02:00
|
|
|
from netbox_agent.config import netbox_instance as nb
|
2019-08-04 01:11:53 +02:00
|
|
|
from netbox_agent.datacenter import Datacenter
|
2019-08-04 00:00:22 +02:00
|
|
|
import netbox_agent.dmidecode as dmidecode
|
2019-08-04 14:37:51 +02:00
|
|
|
from netbox_agent.network import Network
|
2019-08-04 00:00:22 +02:00
|
|
|
|
2019-08-04 15:14:36 +02:00
|
|
|
|
2019-08-02 18:39:05 +02:00
|
|
|
class ServerBase():
|
|
|
|
def __init__(self, dmi=None):
|
|
|
|
if dmi:
|
|
|
|
self.dmi = dmi
|
|
|
|
else:
|
2019-08-03 15:46:21 +02:00
|
|
|
self.dmi = dmidecode.parse()
|
|
|
|
self.system = self.dmi.get_by_type('System')
|
|
|
|
self.bios = self.dmi.get_by_type('BIOS')
|
2019-08-02 18:39:05 +02:00
|
|
|
|
2019-08-04 15:14:36 +02:00
|
|
|
self.network = Network(server=self)
|
2019-08-02 18:39:05 +02:00
|
|
|
|
2019-08-04 01:11:53 +02:00
|
|
|
def get_datacenter(self):
|
|
|
|
dc = Datacenter()
|
2019-08-04 19:33:09 +02:00
|
|
|
return dc.get()
|
2019-08-04 01:11:53 +02:00
|
|
|
|
|
|
|
def get_netbox_datacenter(self):
|
|
|
|
datacenter = nb.dcim.sites.get(
|
2019-08-04 19:33:09 +02:00
|
|
|
slug=self.get_datacenter()
|
2019-08-04 01:11:53 +02:00
|
|
|
)
|
|
|
|
return datacenter
|
|
|
|
|
2019-08-02 18:39:05 +02:00
|
|
|
def get_product_name(self):
|
2019-08-04 00:00:22 +02:00
|
|
|
"""
|
2019-08-02 18:39:05 +02:00
|
|
|
Return the Chassis Name from dmidecode info
|
2019-08-04 00:00:22 +02:00
|
|
|
"""
|
2019-08-02 18:39:05 +02:00
|
|
|
return self.system[0]['Product Name']
|
|
|
|
|
|
|
|
def get_service_tag(self):
|
2019-08-04 00:00:22 +02:00
|
|
|
"""
|
2019-08-02 18:39:05 +02:00
|
|
|
Return the Service Tag from dmidecode info
|
2019-08-04 00:00:22 +02:00
|
|
|
"""
|
2019-08-02 18:39:05 +02:00
|
|
|
return self.system[0]['Serial Number']
|
|
|
|
|
|
|
|
def is_blade(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_blade_slot(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_chassis(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_chassis_service_tag(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_bios_version(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_bios_version_attr(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_bios_release_date(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2019-08-04 21:48:06 +02:00
|
|
|
def _netbox_create_blade_chassis(self, datacenter):
|
2019-08-03 15:46:21 +02:00
|
|
|
device_type = nb.dcim.device_types.get(
|
|
|
|
model=self.get_chassis(),
|
|
|
|
)
|
|
|
|
if not device_type:
|
|
|
|
raise Exception('Chassis "{}" doesn\'t exist'.format(self.get_chassis()))
|
|
|
|
device_role = nb.dcim.device_roles.get(
|
|
|
|
name='Server Chassis',
|
|
|
|
)
|
|
|
|
new_chassis = nb.dcim.devices.create(
|
|
|
|
name=''.format(),
|
|
|
|
device_type=device_type.id,
|
|
|
|
serial=self.get_chassis_service_tag(),
|
|
|
|
device_role=device_role.id,
|
2019-08-04 01:11:53 +02:00
|
|
|
site=datacenter.id if datacenter else None,
|
2019-08-03 15:46:21 +02:00
|
|
|
)
|
|
|
|
return new_chassis
|
|
|
|
|
2019-08-04 01:11:53 +02:00
|
|
|
def _netbox_create_blade(self, chassis, datacenter):
|
2019-08-03 15:46:21 +02:00
|
|
|
device_role = nb.dcim.device_roles.get(
|
|
|
|
name='Blade',
|
|
|
|
)
|
|
|
|
device_type = nb.dcim.device_types.get(
|
|
|
|
model=self.get_product_name(),
|
|
|
|
)
|
|
|
|
new_blade = nb.dcim.devices.create(
|
|
|
|
name='{}'.format(socket.gethostname()),
|
|
|
|
serial=self.get_service_tag(),
|
|
|
|
device_role=device_role.id,
|
|
|
|
device_type=device_type.id,
|
|
|
|
parent_device=chassis.id,
|
2019-08-04 01:11:53 +02:00
|
|
|
site=datacenter.id if datacenter else None,
|
2019-08-03 15:46:21 +02:00
|
|
|
)
|
|
|
|
return new_blade
|
|
|
|
|
2019-08-04 01:11:53 +02:00
|
|
|
def _netbox_create_server(self, datacenter):
|
2019-08-03 15:57:06 +02:00
|
|
|
device_role = nb.dcim.device_roles.get(
|
|
|
|
name='Server',
|
|
|
|
)
|
|
|
|
device_type = nb.dcim.device_types.get(
|
|
|
|
model=self.get_product_name(),
|
|
|
|
)
|
|
|
|
if not device_type:
|
|
|
|
raise Exception('Chassis "{}" doesn\'t exist'.format(self.get_chassis()))
|
|
|
|
new_server = nb.dcim.devices.create(
|
|
|
|
name='{}'.format(socket.gethostname()),
|
|
|
|
serial=self.get_service_tag(),
|
|
|
|
device_role=device_role.id,
|
|
|
|
device_type=device_type.id,
|
2019-08-04 01:11:53 +02:00
|
|
|
site=datacenter.id if datacenter else None,
|
2019-08-03 15:57:06 +02:00
|
|
|
)
|
|
|
|
return new_server
|
|
|
|
|
2019-08-04 15:14:36 +02:00
|
|
|
def get_netbox_server(self):
|
|
|
|
return nb.dcim.devices.get(serial=self.get_service_tag())
|
|
|
|
|
2019-08-03 15:46:21 +02:00
|
|
|
def netbox_create(self):
|
2019-08-04 01:11:53 +02:00
|
|
|
datacenter = self.get_netbox_datacenter()
|
2019-08-03 15:46:21 +02:00
|
|
|
if self.is_blade():
|
|
|
|
# let's find the blade
|
|
|
|
blade = nb.dcim.devices.get(serial=self.get_service_tag())
|
|
|
|
chassis = nb.dcim.devices.get(serial=self.get_chassis_service_tag())
|
|
|
|
# if it doesn't exist, create it
|
|
|
|
if not blade:
|
|
|
|
# check if the chassis exist before
|
|
|
|
# if it doesn't exist, create it
|
|
|
|
if not chassis:
|
2019-08-04 01:11:53 +02:00
|
|
|
chassis = self._netbox_create_blade_chassis(datacenter)
|
2019-08-03 15:46:21 +02:00
|
|
|
|
2019-08-04 01:11:53 +02:00
|
|
|
blade = self._netbox_create_blade(chassis, datacenter)
|
2019-08-03 15:46:21 +02:00
|
|
|
|
|
|
|
# Find the slot and update it with our blade
|
|
|
|
device_bays = nb.dcim.device_bays.filter(
|
|
|
|
device_id=chassis.id,
|
|
|
|
name='Blade {}'.format(self.get_blade_slot()),
|
|
|
|
)
|
|
|
|
if len(device_bays) > 0:
|
|
|
|
device_bay = device_bays[0]
|
|
|
|
device_bay.installed_device = blade
|
|
|
|
device_bay.save()
|
|
|
|
else:
|
2019-08-03 15:57:06 +02:00
|
|
|
server = nb.dcim.devices.get(serial=self.get_service_tag())
|
|
|
|
if not server:
|
|
|
|
self._netbox_create_server()
|
2019-08-04 00:00:22 +02:00
|
|
|
|
2019-08-04 15:14:36 +02:00
|
|
|
self.network.update_netbox_network_cards()
|
|
|
|
|
2019-08-05 12:02:42 +02:00
|
|
|
def netbox_update(self):
|
|
|
|
server = nb.dcim.devices.get(serial=self.get_service_tag())
|
|
|
|
if self.is_blade():
|
|
|
|
# check if the slot/chassis is the same
|
|
|
|
# for every other specs
|
|
|
|
# check hostname
|
|
|
|
# check network cards
|
|
|
|
|
2019-08-04 00:00:22 +02:00
|
|
|
def print_debug(self):
|
|
|
|
# FIXME: do something more generic by looping on every get_* methods
|
|
|
|
print('Datacenter:', self.get_datacenter())
|
|
|
|
print('Netbox Datacenter:', self.get_netbox_datacenter())
|
|
|
|
print('Is blade:', self.is_blade())
|
|
|
|
print('Product Name:', self.get_product_name())
|
|
|
|
print('Chassis:', self.get_chassis())
|
|
|
|
print('Chassis service tag:', self.get_chassis_service_tag())
|
|
|
|
print('Service tag:', self.get_service_tag())
|
2019-08-04 14:37:51 +02:00
|
|
|
print('NIC:',)
|
|
|
|
pprint(self.network.get_network_cards())
|
|
|
|
pass
|