Skip to content

Commit

Permalink
[vbox] Enhance get_vm_state
Browse files Browse the repository at this point in the history
Parse the VBoxManage output to get the state with a regular expression
for consistency and to improve code readability. Implement other small
enhancements in `get_vm_state` for consistency with other functions.
  • Loading branch information
Ana06 committed Jan 29, 2025
1 parent c34ef71 commit 1e82ac5
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions virtualbox/vboxcommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,16 @@ def ensure_hostonlyif_exists():


def get_vm_state(vm_uuid):
"""Gets the VM state using 'VBoxManage showvminfo'."""
"""Get the VM state using 'VBoxManage showvminfo'."""
# Example of `VBoxManage showvminfo <VM_UUID> --machinereadable` relevant output:
# VMState="poweroff"
# VMStateChangeTime="2025-01-02T16:31:51.000000000"
vm_info = run_vboxmanage(["showvminfo", vm_uuid, "--machinereadable"])

vminfo = run_vboxmanage(["showvminfo", vm_uuid, "--machinereadable"])
for line in vminfo.splitlines():
if line.startswith("VMState"):
return line.split("=")[1].strip('"')
raise Exception(f"Could not start VM '{vm_uuid}'")
match = re.search(f'^VMState="(?P<state>\S+)"', vm_info, flags=re.M)
if match:
return match["state"]

raise Exception(f"Unable to get state of VM {vm_uuid}")


def wait_until_vm_state(vm_uuid, target_state):
Expand Down

0 comments on commit 1e82ac5

Please sign in to comment.