fix: probe correct value

This commit is contained in:
eric
2026-03-18 03:21:10 +01:00
parent 9e0eb5b583
commit f150afec0a
7 changed files with 29 additions and 101 deletions

View File

@@ -584,7 +584,7 @@ def validate_host_name(name: str) -> None:
def probe_host(ip: str, user: str) -> ProbeFacts:
lsblk_cmd = "lsblk -o NAME,SIZE,TYPE,MODEL,FSTYPE,PTTYPE,MOUNTPOINTS"
lsblk_cmd = "lsblk -P -o NAME,SIZE,TYPE,MODEL,FSTYPE,PTTYPE,MOUNTPOINTS"
boot_cmd = "test -d /sys/firmware/efi && echo UEFI || echo BIOS"
root_cmd = "findmnt -no SOURCE /"
swap_cmd = "cat /proc/swaps"
@@ -635,28 +635,25 @@ def probe_host(ip: str, user: str) -> ProbeFacts:
def parse_lsblk_output(output: str) -> list[dict[str, str]]:
lines = [line.rstrip("\n") for line in output.splitlines() if line.strip()]
if len(lines) < 2:
lines = [line.strip() for line in output.splitlines() if line.strip()]
if not lines:
raise NodeiwestError("Unexpected lsblk output: not enough lines to parse.")
header = lines[0]
columns = ["NAME", "SIZE", "TYPE", "MODEL", "FSTYPE", "PTTYPE", "MOUNTPOINTS"]
starts = []
for column in columns:
index = header.find(column)
if index == -1:
raise NodeiwestError(f"Unexpected lsblk output: missing {column} column.")
starts.append(index)
starts.append(len(header) + 20)
rows: list[dict[str, str]] = []
for line in lines[1:]:
row: dict[str, str] = {}
for idx, column in enumerate(columns):
start = starts[idx]
end = starts[idx + 1] if idx + 1 < len(columns) else len(line)
row[column] = line[start:end].strip()
row["NAME"] = re.sub(r"^[^0-9A-Za-z]+", "", row["NAME"])
for line in lines:
tokens = shlex.split(line)
row = {}
for token in tokens:
if "=" not in token:
continue
key, value = token.split("=", 1)
row[key] = value
missing = [column for column in columns if column not in row]
if missing:
raise NodeiwestError(
f"Unexpected lsblk output: missing columns {', '.join(missing)} in line {line!r}."
)
rows.append(row)
return rows

View File

@@ -46,6 +46,19 @@ class HelperCliTests(unittest.TestCase):
self.assertIn('device = lib.mkDefault "/dev/vda";', rendered)
self.assertIn('size = "8GiB";', rendered)
def test_parse_lsblk_output_reads_pairs_without_smearing_columns(self) -> None:
output = (
'NAME="sda" SIZE="11G" TYPE="disk" MODEL="QEMU HARDDISK" FSTYPE="" PTTYPE="gpt" MOUNTPOINTS=""\n'
'NAME="sda1" SIZE="512M" TYPE="part" MODEL="" FSTYPE="vfat" PTTYPE="" MOUNTPOINTS="/boot"\n'
)
rows = cli.parse_lsblk_output(output)
self.assertEqual(rows[0]["NAME"], "sda")
self.assertEqual(rows[0]["SIZE"], "11G")
self.assertEqual(rows[0]["MODEL"], "QEMU HARDDISK")
self.assertEqual(rows[1]["NAME"], "sda1")
self.assertEqual(rows[1]["MOUNTPOINTS"], "/boot")
def test_bao_kv_get_uses_explicit_kv_mount(self) -> None:
completed = mock.Mock()
completed.stdout = '{"data": {"data": {"CLIENT_ID": "x"}}}'