Skip to content

Commit

Permalink
Improvements to "HyperVPreparationTransformer" (#3613)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
SRIKKANTH authored Jan 25, 2025
1 parent 7d3e69f commit a409eba
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
11 changes: 7 additions & 4 deletions lisa/tools/hyperv.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,12 @@ def get_default_switch(self) -> VMSwitch:
raise LisaException("Could not find any Internal or External switch")
return self._default_switch

def exists_switch(self, name: str) -> bool:
def exists_switch(self, name: str, switch_type: str = "") -> bool:
cmd = f"Get-VMSwitch -Name {name}"
if switch_type != "":
cmd += f" -SwitchType '{switch_type}'"
output = self.node.tools[PowerShell].run_cmdlet(
f"Get-VMSwitch -Name {name}",
cmdlet=cmd,
fail_on_error=False,
force_run=True,
)
Expand All @@ -248,8 +251,8 @@ def delete_switch(self, name: str) -> None:
)

def create_switch(self, name: str, switch_type: str = "Internal") -> None:
# remove switch if it exists
self.delete_switch(name)
if self.exists_switch(name, switch_type):
return

# create a new switch
self.node.tools[PowerShell].run_cmdlet(
Expand Down
33 changes: 13 additions & 20 deletions lisa/transformers/hyperv_preparation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Any, Dict, List, Type

from lisa import schema
from lisa.tools import PowerShell
from lisa.tools import HyperV
from lisa.transformers.deployment_transformer import (
DeploymentTransformer,
DeploymentTransformerSchema,
Expand All @@ -28,23 +28,16 @@ def _output_names(self) -> List[str]:
def _internal_run(self) -> Dict[str, Any]:
runbook: DeploymentTransformerSchema = self.runbook
assert isinstance(runbook, DeploymentTransformerSchema)
node = self._node
powershell = node.tools[PowerShell]
powershell.run_cmdlet(
"Install-WindowsFeature -Name DHCP,Hyper-V -IncludeManagementTools",
force_run=True,
)
node.reboot()
powershell.run_cmdlet(
"New-VMSwitch -Name 'InternalNAT' -SwitchType Internal",
force_run=True,
)
powershell.run_cmdlet(
"New-NetNat -Name 'InternalNAT' -InternalIPInterfaceAddressPrefix '192.168.0.0/24'", # noqa: E501
force_run=True,
)
powershell.run_cmdlet(
'New-NetIPAddress -IPAddress 192.168.0.1 -InterfaceIndex (Get-NetAdapter | Where-Object { $_.Name -like "*InternalNAT)" } | Select-Object -ExpandProperty ifIndex) -PrefixLength 24', # noqa: E501
force_run=True,
)
switch_name = "InternalNAT"

# Enable Hyper-V
hv = self._node.tools[HyperV]

# Create an internal switch.
hv.create_switch(name=switch_name)

hv.setup_nat_networking(switch_name=switch_name, nat_name=switch_name)

# Configure Internal DHCP
hv.enable_internal_dhcp()
return {}

0 comments on commit a409eba

Please sign in to comment.