Skip to content

Commit a409eba

Browse files
authored
Improvements to "HyperVPreparationTransformer" (#3613)
* Improvements to "HyperVPreparationTransformer" Changed the transformer to use recently added methods to HyperV tool instead of running commands. * Update hyperv_preparation.py * Update hyperv.py
1 parent 7d3e69f commit a409eba

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

lisa/tools/hyperv.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,12 @@ def get_default_switch(self) -> VMSwitch:
232232
raise LisaException("Could not find any Internal or External switch")
233233
return self._default_switch
234234

235-
def exists_switch(self, name: str) -> bool:
235+
def exists_switch(self, name: str, switch_type: str = "") -> bool:
236+
cmd = f"Get-VMSwitch -Name {name}"
237+
if switch_type != "":
238+
cmd += f" -SwitchType '{switch_type}'"
236239
output = self.node.tools[PowerShell].run_cmdlet(
237-
f"Get-VMSwitch -Name {name}",
240+
cmdlet=cmd,
238241
fail_on_error=False,
239242
force_run=True,
240243
)
@@ -248,8 +251,8 @@ def delete_switch(self, name: str) -> None:
248251
)
249252

250253
def create_switch(self, name: str, switch_type: str = "Internal") -> None:
251-
# remove switch if it exists
252-
self.delete_switch(name)
254+
if self.exists_switch(name, switch_type):
255+
return
253256

254257
# create a new switch
255258
self.node.tools[PowerShell].run_cmdlet(
+13-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Any, Dict, List, Type
22

33
from lisa import schema
4-
from lisa.tools import PowerShell
4+
from lisa.tools import HyperV
55
from lisa.transformers.deployment_transformer import (
66
DeploymentTransformer,
77
DeploymentTransformerSchema,
@@ -28,23 +28,16 @@ def _output_names(self) -> List[str]:
2828
def _internal_run(self) -> Dict[str, Any]:
2929
runbook: DeploymentTransformerSchema = self.runbook
3030
assert isinstance(runbook, DeploymentTransformerSchema)
31-
node = self._node
32-
powershell = node.tools[PowerShell]
33-
powershell.run_cmdlet(
34-
"Install-WindowsFeature -Name DHCP,Hyper-V -IncludeManagementTools",
35-
force_run=True,
36-
)
37-
node.reboot()
38-
powershell.run_cmdlet(
39-
"New-VMSwitch -Name 'InternalNAT' -SwitchType Internal",
40-
force_run=True,
41-
)
42-
powershell.run_cmdlet(
43-
"New-NetNat -Name 'InternalNAT' -InternalIPInterfaceAddressPrefix '192.168.0.0/24'", # noqa: E501
44-
force_run=True,
45-
)
46-
powershell.run_cmdlet(
47-
'New-NetIPAddress -IPAddress 192.168.0.1 -InterfaceIndex (Get-NetAdapter | Where-Object { $_.Name -like "*InternalNAT)" } | Select-Object -ExpandProperty ifIndex) -PrefixLength 24', # noqa: E501
48-
force_run=True,
49-
)
31+
switch_name = "InternalNAT"
32+
33+
# Enable Hyper-V
34+
hv = self._node.tools[HyperV]
35+
36+
# Create an internal switch.
37+
hv.create_switch(name=switch_name)
38+
39+
hv.setup_nat_networking(switch_name=switch_name, nat_name=switch_name)
40+
41+
# Configure Internal DHCP
42+
hv.enable_internal_dhcp()
5043
return {}

0 commit comments

Comments
 (0)