Skip to content

Commit

Permalink
support detect vmware esxi
Browse files Browse the repository at this point in the history
  • Loading branch information
LiliDeng committed Aug 29, 2024
1 parent f1bb82e commit b389c3c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lisa/executable.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
if TYPE_CHECKING:
from lisa.node import Node


T = TypeVar("T")


Expand Down Expand Up @@ -168,6 +167,10 @@ def create(cls, node: Node, *args: Any, **kwargs: Any) -> Tool:
freebsd_tool = cls._freebsd_tool()
if freebsd_tool:
tool_cls = freebsd_tool
elif "VMWareESXi" in node.os.name:
vmware_esxi_tool = cls._vmware_esxi_tool()
if vmware_esxi_tool:
tool_cls = vmware_esxi_tool
return tool_cls(node, *args, **kwargs)

@classmethod
Expand All @@ -184,11 +187,21 @@ def _freebsd_tool(cls) -> Optional[Type[Tool]]:
"""
return None

@classmethod
def _vmware_esxi_tool(cls) -> Optional[Type[Tool]]:
"""
return a vmware esxi version tool class, if it's needed
"""
return None

def command_exists(self, command: str) -> Tuple[bool, bool]:
exists = False
use_sudo = False
if self.node.is_posix:
where_command = "command -v"
if "VMWareESXi" in self.node.os.name:
where_command = "which"
else:
where_command = "command -v"
else:
where_command = "where"
where_command = f"{where_command} {command}"
Expand Down
12 changes: 12 additions & 0 deletions lisa/operating_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ class OperatingSystem:
__release_pattern = re.compile(r"^DISTRIB_ID='?([^ \n']+).*$", re.M)
__suse_release_pattern = re.compile(r"^(SUSE).*$", re.M)
__bmc_release_pattern = re.compile(r".*(wcscli).*$", re.M)
# VMware ESXi 8.0.2 build-23305546
# VMware ESXi 8.0 Update 2
__vmware_esxi_release_pattern = re.compile(r"^(VMware ESXi).*$", re.M)

__posix_factory: Optional[Factory[Any]] = None

Expand Down Expand Up @@ -250,6 +253,9 @@ def _get_detect_string(cls, node: Any) -> Iterable[str]:
cmd_result = typed_node.execute(cmd="wcscli", no_error_log=True)
yield get_matched_str(cmd_result.stdout, cls.__bmc_release_pattern)

cmd_result = typed_node.execute(cmd="vmware -lv", no_error_log=True)
yield get_matched_str(cmd_result.stdout, cls.__vmware_esxi_release_pattern)

# try best from distros'family through ID_LIKE
yield get_matched_str(
cmd_result_os_release.stdout, cls.__os_release_pattern_idlike
Expand Down Expand Up @@ -681,6 +687,12 @@ def name_pattern(cls) -> Pattern[str]:
return re.compile("^wcscli$")


class VMWareESXi(Posix):
@classmethod
def name_pattern(cls) -> Pattern[str]:
return re.compile("^VMware ESXi$")


class MacOS(Posix):
@classmethod
def name_pattern(cls) -> Pattern[str]:
Expand Down
41 changes: 41 additions & 0 deletions lisa/tools/lscpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ def command(self) -> str:
def _windows_tool(cls) -> Optional[Type[Tool]]:
return WindowsLscpu

@classmethod
def _vmware_esxi_tool(cls) -> Optional[Type[Tool]]:
return VMWareESXiLscpu

@classmethod
def _freebsd_tool(cls) -> Optional[Type[Tool]]:
return BSDLscpu
Expand Down Expand Up @@ -438,3 +442,40 @@ def calculate_vcpu_count(self, force_run: bool = False) -> int:
* self.get_cluster_count()
* self.get_thread_per_core_count()
)


class VMWareESXiLscpu(Lscpu):
# CPU Threads: 208
__cpu_threads = re.compile(r"CPU Threads:[ ]+([\d]+)?", re.M)
# CPU Packages: 2
__cpu_packages = re.compile(r"CPU Packages:[ ]+([\d]+)?", re.M)
# CPU Cores: 104
__cpu_cores = re.compile(r"CPU Cores:[ ]+([\d]+)?", re.M)

@property
def command(self) -> str:
return "esxcli"

def get_core_count(self, force_run: bool = False) -> int:
result = self.run("hardware cpu global get", force_run)
matched = self.__cpu_threads.findall(result.stdout)
assert_that(
len(matched),
f"cpu thread should have exact one line, but got {matched}",
).is_equal_to(1)
self._core_count = int(matched[0])
return self._core_count

def calculate_vcpu_count(self, force_run: bool = False) -> int:
result = self.run("hardware cpu global get", force_run)
matched_cpu_packages = self.__cpu_packages.findall(result.stdout)
assert_that(
len(matched_cpu_packages),
f"cpu packages should have exact one line, but got {matched_cpu_packages}",
).is_equal_to(1)
matched_cpu_cores = self.__cpu_cores.findall(result.stdout)
assert_that(
len(matched_cpu_cores),
f"cpu cores should have exact one line, but got {matched_cpu_cores}",
).is_equal_to(1)
return int(matched_cpu_packages[0]) * int(matched_cpu_cores[0])

0 comments on commit b389c3c

Please sign in to comment.