feat: add guest IP retrieval functions and enhance VM preparation logic
Lint / pssa (push) Successful in 9s
Lint / python (push) Successful in 21s

This commit is contained in:
2026-05-27 23:34:37 +02:00
parent 3eb29b404f
commit 4c80ff2528
2 changed files with 135 additions and 4 deletions
+44 -1
View File
@@ -827,9 +827,34 @@ def _build_seed_iso(staging_dir: Path, iso_path: Path) -> None:
)
def _read_guestinfo_ip(vmrun: str, vmx: str) -> str | None:
"""Read guestinfo.ci-ip written by ci-report-ip service inside the guest."""
try:
result = subprocess.run(
[vmrun, "readVariable", vmx, "runtimeConfig", "guestinfo.ci-ip"],
capture_output=True,
text=True,
timeout=10,
check=False,
)
ip = result.stdout.strip()
if re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", ip):
return ip
except subprocess.TimeoutExpired:
pass
return None
def _poll_guest_ip_deploy(vmrun: str, vmx: str, timeout: float) -> str | None:
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
# Prefer guestinfo.ci-ip written by ci-report-ip service (faster, no
# VMware Tools DHCP heuristic); fall back to getGuestIPAddress for
# first-boot VMs that don't have ci-report-ip installed yet.
ip = _read_guestinfo_ip(vmrun, vmx)
if ip:
click.echo(f" ... guest IP from guestinfo.ci-ip: {ip}")
return ip
try:
result = subprocess.run(
[vmrun, "getGuestIPAddress", vmx],
@@ -1218,7 +1243,7 @@ def template_deploy_linux(
f'numvcpus = "{vcpu_count}"',
f'cpuid.coresPerSocket = "{cores_per_socket}"',
'scsi0.present = "TRUE"',
'scsi0.virtualDev = "lsilogic"',
'scsi0.virtualDev = "pvscsi"',
'scsi0:0.present = "TRUE"',
'scsi0:0.fileName = "LinuxBuild2404.vmdk"',
'scsi0:0.deviceType = "scsi-hardDisk"',
@@ -1247,8 +1272,14 @@ def template_deploy_linux(
'floppy0.present = "FALSE"',
'usb.present = "FALSE"',
'sound.present = "FALSE"',
'mks.enable3d = "FALSE"',
'svga.vramSize = "4194304"',
'tools.syncTime = "TRUE"',
'uuid.action = "create"',
'sched.mem.pshare.enable = "FALSE"',
'MemAllowAutoScaleDown = "FALSE"',
'mainMem.useNamedFile = "FALSE"',
'isolation.tools.hgfs.disable = "TRUE"',
]
if guest_user_data_b64 and guest_meta_data_b64:
vmx_lines += [
@@ -1572,6 +1603,18 @@ def template_prepare_linux(
backend.start(handle, headless=True)
else:
click.echo(f"[prepare-linux] VM already running: {vmx_path.name}")
# VM process alive but guest may be powered off (e.g. after a
# previous prepare run that shut down the guest). Do a quick IP
# probe; if nothing comes back, stop and cold-boot the VM.
if not guest_ip:
click.echo("[prepare-linux] Quick IP probe (30s) ...")
quick_ip = _poll_guest_ip_deploy(vmrun, str(vmx_path), timeout=30.0)
if not quick_ip:
click.echo(
"[prepare-linux] Guest not responding — stopping and restarting VM ..."
)
backend.stop(handle)
backend.start(handle, headless=True)
if not guest_ip:
click.echo("[prepare-linux] Detecting guest IP (vmrun getGuestIPAddress) ...")