From 8a5b78d931be58b5e5617765c234bffb9e54a0fd Mon Sep 17 00:00:00 2001 From: Alexsander de Souza <61709370+alexsander-souza@users.noreply.github.com> Date: Tue, 9 May 2023 23:55:59 +0200 Subject: [PATCH] Fix ESXi disk serial parsing (#112) fixes #33 --- vmware-esxi/maas/storage-esxi | 14 +++++++++++--- vmware-esxi/requirements-dev.txt | 7 ++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/vmware-esxi/maas/storage-esxi b/vmware-esxi/maas/storage-esxi index 061d8973..1d9712ce 100755 --- a/vmware-esxi/maas/storage-esxi +++ b/vmware-esxi/maas/storage-esxi @@ -93,6 +93,10 @@ def get_disks(): r"Revis:\s+(?P.*)\s*$" ) + serial_regex = re.compile( + r"^(?P\S+?)__+(?P\S+?)__+(?P\S+?)__+$" + ) + for line in output.splitlines(): if not line.startswith(" " * 3): # Each section starts with the device name, all fields are defined @@ -101,15 +105,17 @@ def get_disks(): disks.append(disk) other_names = False name = line.strip() + m = serial_regex.search(name) disk = { "name": name, "other_names": [], "blocksize": disk_block_sizes.get(name, 0), + "serial": m.group("serial") if m else "", } elif disk: if other_names and not line.startswith(" " * 6): - # Other names is a list which contains the serial. Entries must - # start with 6 spaces. + # Other names is a list of alias. + # Entries must start with 6 spaces. other_names = False line = line.strip() if line.startswith("Size"): @@ -139,6 +145,8 @@ def get_disk(disks, model, serial): # udev will sometimes replace a space in the model name with an # underscore. if disk["model"] == model or disk["model"].replace(" ", "_") == model: + if serial == disk["serial"]: + return disk for other_name in disk["other_names"]: # VMware doesn't provide the exact serial number. It provides # a list of other names, one of which contains the serial @@ -255,7 +263,7 @@ def get_ending_sector(blocksize, starting_sector, size): if size.endswith("B"): size = size[:-1] - mpliers = {"B": 1, "K": 2 ** 10, "M": 2 ** 20, "G": 2 ** 30, "T": 2 ** 40} + mpliers = {"B": 1, "K": 2**10, "M": 2**20, "G": 2**30, "T": 2**40} mplier = "B" for m in mpliers: diff --git a/vmware-esxi/requirements-dev.txt b/vmware-esxi/requirements-dev.txt index 066a26f3..2986f496 100644 --- a/vmware-esxi/requirements-dev.txt +++ b/vmware-esxi/requirements-dev.txt @@ -1,3 +1,4 @@ -black==21.5b1 -isort==5.8.0 -flake8==3.9.2 +black==23.1.0 +isort==5.12.0 +flake8==6.0.0 +click==8.1.3