fix logic order while using all nvme disk, round disk size to 2 digit after point

This commit is contained in:
root 2022-08-02 13:32:36 +07:00
commit 814b931410
3 changed files with 35 additions and 23 deletions

View file

@ -314,7 +314,6 @@ class Inventory():
non_raid_disks = [ non_raid_disks = [
'MR9361-8i', 'MR9361-8i',
] ]
if logicalname in raid_devices or \ if logicalname in raid_devices or \
disk_type is None or \ disk_type is None or \
product in non_raid_disks or \ product in non_raid_disks or \
@ -339,9 +338,10 @@ class Inventory():
] ]
for disk in self.lshw.get_hw_linux("storage"): for disk in self.lshw.get_hw_linux("storage"):
logging.debug(f"disk: {disk}")
if self.is_virtual_disk(disk, raid_devices): if self.is_virtual_disk(disk, raid_devices):
continue continue
size =int(disk.get('size', 0)) / 1073741824 size = round(int(disk.get('size', 0)) / 1073741824, 2)
d = { d = {
"name": "", "name": "",
'Size': '{} GB'.format(size), 'Size': '{} GB'.format(size),

View file

@ -14,6 +14,7 @@ class LSHW():
data = subprocess.getoutput( data = subprocess.getoutput(
'lshw -quiet -json' 'lshw -quiet -json'
) )
json_data = json.loads(data) json_data = json.loads(data)
# Starting from version 02.18, `lshw -json` wraps its result in a list # Starting from version 02.18, `lshw -json` wraps its result in a list
# rather than returning directly a dictionary # rather than returning directly a dictionary
@ -72,8 +73,9 @@ class LSHW():
# Some interfaces do not have device (logical) name (eth0, for # Some interfaces do not have device (logical) name (eth0, for
# instance), such as not connected network mezzanine cards in blade # instance), such as not connected network mezzanine cards in blade
# servers. In such situations, the card will be named `unknown[0-9]`. # servers. In such situations, the card will be named `unknown[0-9]`.
#DEBUG:root:lshw interfaces is: [{'name': ['enp68s0f0', '/dev/fb0'], 'macaddress': 'xx:ec:xx:fb:xx:xx', 'serial': 'xx:ec:xx:fb:xx:xx', 'product': 'Ethernet Controller X710 for 10GBASE-T', 'vendor': 'Intel Corporation', 'description': 'Ethernet interface'}]
unkn_intfs = [ unkn_intfs = [
i for i in self.interfaces if i["name"].startswith("unknown") i for i in self.interfaces if i["name"][0].startswith("unknown")
] ]
unkn_name = "unknown{}".format(len(unkn_intfs)) unkn_name = "unknown{}".format(len(unkn_intfs))
self.interfaces.append({ self.interfaces.append({
@ -86,18 +88,8 @@ class LSHW():
}) })
def find_storage(self, obj): def find_storage(self, obj):
if "children" in obj: if "nvme" in obj["configuration"]["driver"]:
for device in obj["children"]: logging.debug("go to nvme")
self.disks.append({
"logicalname": device.get("logicalname"),
"product": device.get("product"),
"serial": device.get("serial"),
"version": device.get("version"),
"size": device.get("size"),
"description": device.get("description"),
"type": device.get("description"),
})
elif "nvme" in obj["configuration"]["driver"]:
if not is_tool('nvme'): if not is_tool('nvme'):
logging.error('nvme-cli >= 1.0 does not seem to be installed') logging.error('nvme-cli >= 1.0 does not seem to be installed')
return return
@ -123,7 +115,25 @@ class LSHW():
self.disks.append(d) self.disks.append(d)
except Exception: except Exception:
pass pass
elif "children" in obj:
logging.debug("go to children")
for device in obj["children"]:
logging.debug(device)
# if device.get("logicalname") is None or device.get("product") is None or \
# device.get("serial") is None or device.get("version") is None or \
# device.get("size") is None:
# continue
if device.get("size") is None:
continue
self.disks.append({
"logicalname": device.get("logicalname"),
"product": device.get("product"),
"serial": device.get("serial"),
"version": device.get("version"),
"size": device.get("size"),
"description": device.get("description"),
"type": device.get("description"),
})
def find_cpus(self, obj): def find_cpus(self, obj):
if "product" in obj: if "product" in obj:
self.cpus.append({ self.cpus.append({

View file

@ -198,6 +198,7 @@ class Network(object):
def get_or_create_vlan(self, vlan_id): def get_or_create_vlan(self, vlan_id):
# FIXME: we may need to specify the datacenter # FIXME: we may need to specify the datacenter
# since users may have same vlan id in multiple dc # since users may have same vlan id in multiple dc
logging.debug(f"VLAN ID is: {vlan_id}")
vlan = nb.ipam.vlans.get( vlan = nb.ipam.vlans.get(
vid=vlan_id, vid=vlan_id,
) )
@ -305,6 +306,7 @@ class Network(object):
# if pvid is not present, it'll be processed as a vlan tagged interface # if pvid is not present, it'll be processed as a vlan tagged interface
vlans = self.lldp.get_switch_vlan(nic['name']) vlans = self.lldp.get_switch_vlan(nic['name'])
for vid, vlan_infos in vlans.items(): for vid, vlan_infos in vlans.items():
logging.debug(f"vid: {vid}, infos: {vlan_infos}")
nb_vlan = self.get_or_create_vlan(vid) nb_vlan = self.get_or_create_vlan(vid)
if vlan_infos.get('vid'): if vlan_infos.get('vid'):
interface.mode = self.dcim_choices['interface:mode']['Access'] interface.mode = self.dcim_choices['interface:mode']['Access']