Skip to content

Commit c34ef71

Browse files
committed
[vbox] Enhance ensure_hostonlyif_exists
Enhance `ensure_hostonlyif_exists` by: - Introducing `get_hostonlyif_name` to avoid duplication. - Parsing the VBoxManage output with a regular expression for consistency and to improve code readability. - Removing noisy output and making the output consistent with other vboxcommon functions to make easier to call them from other function displaying a nice output. - Removing the unneeded use of exceptions re-raising. - Use concrete exception type (`RuntimeError`).
1 parent f1bc666 commit c34ef71

File tree

1 file changed

+21
-35
lines changed

1 file changed

+21
-35
lines changed

virtualbox/vboxcommon.py

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -48,45 +48,31 @@ def run_vboxmanage(cmd):
4848
return result.stdout
4949

5050

51+
def get_hostonlyif_name():
52+
"""Get the name of the host-only interface. Return None if there is no host-only interface"""
53+
# Example of `VBoxManage list hostonlyifs` relevant output:
54+
# Name: vboxnet0
55+
hostonlyifs_info = run_vboxmanage(["list", "hostonlyifs"])
56+
57+
match = re.search(f"^Name: *(?P<hostonlyif_name>\S+)", hostonlyifs_info, flags=re.M)
58+
if match:
59+
return match["hostonlyif_name"]
60+
5161
def ensure_hostonlyif_exists():
52-
"""Gets the name of, or creates a new hostonlyif"""
53-
try:
54-
# Name: vboxnet0
55-
# GUID: f0000000-dae8-4abf-8000-0a0027000000
56-
# DHCP: Disabled
57-
# IPAddress: 192.168.56.1
58-
# NetworkMask: 255.255.255.0
59-
# IPV6Address: fe80::800:27ff:fe00:0
60-
# IPV6NetworkMaskPrefixLength: 64
61-
# HardwareAddress: 0a:00:27:00:00:00
62-
# MediumType: Ethernet
63-
# Wireless: No
64-
# Status: Up
65-
# VBoxNetworkName: HostInterfaceNetworking-vboxnet0
66-
67-
# Find existing hostonlyif
68-
hostonlyifs_output = run_vboxmanage(["list", "hostonlyifs"])
69-
for line in hostonlyifs_output.splitlines():
70-
if line.startswith("Name:"):
71-
hostonlyif_name = line.split(":")[1].strip()
72-
print(f"Found existing hostonlyif {hostonlyif_name}")
73-
return hostonlyif_name
62+
"""Get the name of the host-only interface. Create the interface if it doesn't exist."""
63+
hostonlyif_name = get_hostonlyif_name()
7464

65+
if not hostonlyif_name:
7566
# No host-only interface found, create one
76-
print("No host-only interface found. Creating one...")
7767
run_vboxmanage(["hostonlyif", "create"])
78-
hostonlyifs_output = run_vboxmanage(
79-
["list", "hostonlyifs"]
80-
) # Get the updated list
81-
for line in hostonlyifs_output.splitlines():
82-
if line.startswith("Name:"):
83-
hostonlyif_name = line.split(":")[1].strip()
84-
print(f"Created hostonlyif {hostonlyif_name}")
85-
return hostonlyif_name
86-
print("Failed to create new hostonlyif. Exiting...")
87-
raise Exception("Failed to create new hostonlyif.")
88-
except Exception as e:
89-
raise Exception("Failed to verify host-only interface exists") from e
68+
69+
hostonlyif_name = get_hostonlyif_name()
70+
if not hostonlyif_name:
71+
raise RuntimeError("Failed to create new hostonly interface.")
72+
73+
print(f"VM {vm_uuid} Created hostonly interface: {hostonlyif_name}")
74+
75+
return hostonlyif_name
9076

9177

9278
def get_vm_state(vm_uuid):

0 commit comments

Comments
 (0)