|
| 1 | +# Copyright (C) 2025 Intel Corporation |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +"""RShell Connection Class.""" |
| 4 | + |
| 5 | +import logging |
| 6 | +import sys |
| 7 | +import time |
| 8 | +import typing |
| 9 | +from ipaddress import IPv4Address, IPv6Address |
| 10 | +from subprocess import CalledProcessError |
| 11 | + |
| 12 | +import requests |
| 13 | +from mfd_common_libs import add_logging_level, log_levels, TimeoutCounter |
| 14 | +from mfd_typing.cpu_values import CPUArchitecture |
| 15 | +from mfd_typing.os_values import OSBitness, OSName, OSType |
| 16 | + |
| 17 | +from mfd_connect.local import LocalConnection |
| 18 | +from mfd_connect.pathlib.path import CustomPath, custom_path_factory |
| 19 | +from mfd_connect.process.base import RemoteProcess |
| 20 | + |
| 21 | +from .base import Connection, ConnectionCompletedProcess |
| 22 | + |
| 23 | +if typing.TYPE_CHECKING: |
| 24 | + from pydantic import ( |
| 25 | + BaseModel, # from pytest_mfd_config.models.topology import ConnectionModel |
| 26 | + ) |
| 27 | + |
| 28 | + |
| 29 | +logger = logging.getLogger(__name__) |
| 30 | +add_logging_level(level_name="MODULE_DEBUG", level_value=log_levels.MODULE_DEBUG) |
| 31 | +add_logging_level(level_name="CMD", level_value=log_levels.CMD) |
| 32 | +add_logging_level(level_name="OUT", level_value=log_levels.OUT) |
| 33 | + |
| 34 | + |
| 35 | +class RShellConnection(Connection): |
| 36 | + """RShell Connection Class.""" |
| 37 | + |
| 38 | + def __init__( |
| 39 | + self, |
| 40 | + ip: str | IPv4Address | IPv6Address, |
| 41 | + server_ip: str | IPv4Address | IPv6Address | None = "127.0.0.1", |
| 42 | + model: "BaseModel | None" = None, |
| 43 | + cache_system_data: bool = True, |
| 44 | + connection_timeout: int = 60, |
| 45 | + ): |
| 46 | + """ |
| 47 | + Initialize RShellConnection. |
| 48 | +
|
| 49 | + :param ip: The IP address of the RShell server. |
| 50 | + :param server_ip: The IP address of the server to connect to (optional). |
| 51 | + :param model: The Pydantic model to use for the connection (optional). |
| 52 | + :param cache_system_data: Whether to cache system data (default: True). |
| 53 | + """ |
| 54 | + super().__init__(model=model, cache_system_data=cache_system_data) |
| 55 | + self._ip = ip |
| 56 | + self.server_ip = server_ip if server_ip else "127.0.0.1" |
| 57 | + self.server_process = None |
| 58 | + if server_ip == "127.0.0.1": |
| 59 | + # start Rshell server |
| 60 | + self.server_process = self._run_server() |
| 61 | + time.sleep(5) |
| 62 | + timeout = TimeoutCounter(connection_timeout) |
| 63 | + while not timeout: |
| 64 | + logger.log(level=log_levels.MODULE_DEBUG, msg="Checking RShell server health") |
| 65 | + status_code = requests.get( |
| 66 | + f"http://{self.server_ip}/health/{self._ip}", proxies={"no_proxy": "*"} |
| 67 | + ).status_code |
| 68 | + if status_code == 200: |
| 69 | + logger.log(level=log_levels.MODULE_DEBUG, msg="RShell server is healthy") |
| 70 | + break |
| 71 | + time.sleep(5) |
| 72 | + else: |
| 73 | + raise TimeoutError("Connection of Client to RShell server timed out") |
| 74 | + |
| 75 | + def disconnect(self, stop_client: bool = False) -> None: |
| 76 | + """ |
| 77 | + Disconnect connection. |
| 78 | +
|
| 79 | + Stop local RShell server if established. |
| 80 | +
|
| 81 | + :param stop_client: Whether to stop the RShell client (default: False). |
| 82 | + """ |
| 83 | + if stop_client: |
| 84 | + logger.log(level=log_levels.MODULE_DEBUG, msg="Stopping RShell client") |
| 85 | + self.execute_command("end") |
| 86 | + if self.server_process: |
| 87 | + logger.log(level=log_levels.MODULE_DEBUG, msg="Stopping RShell server") |
| 88 | + self.server_process.kill() |
| 89 | + logger.log(level=log_levels.MODULE_DEBUG, msg="RShell server stopped") |
| 90 | + logger.log(level=log_levels.MODULE_DEBUG, msg=self.server_process.stdout_text) |
| 91 | + |
| 92 | + def _run_server(self) -> RemoteProcess: |
| 93 | + """Run RShell server locally.""" |
| 94 | + conn = LocalConnection() |
| 95 | + server_file = conn.path(__file__).parent / "rshell_server.py" |
| 96 | + return conn.start_process(f"{conn.modules().sys.executable} {server_file}") |
| 97 | + |
| 98 | + def execute_command( |
| 99 | + self, |
| 100 | + command: str, |
| 101 | + *, |
| 102 | + input_data: str | None = None, |
| 103 | + cwd: str | None = None, |
| 104 | + timeout: int | None = None, |
| 105 | + env: dict | None = None, |
| 106 | + stderr_to_stdout: bool = False, |
| 107 | + discard_stdout: bool = False, |
| 108 | + discard_stderr: bool = False, |
| 109 | + skip_logging: bool = False, |
| 110 | + expected_return_codes: list[int] | None = None, |
| 111 | + shell: bool = False, |
| 112 | + custom_exception: type[CalledProcessError] | None = None, |
| 113 | + ) -> ConnectionCompletedProcess: |
| 114 | + """ |
| 115 | + Execute a command on the remote server. |
| 116 | +
|
| 117 | + :param command: The command to execute. |
| 118 | + :param timeout: The timeout for the command execution (optional). |
| 119 | + :return: The result of the command execution. |
| 120 | + """ |
| 121 | + if input_data is not None: |
| 122 | + logger.log( |
| 123 | + level=log_levels.MODULE_DEBUG, |
| 124 | + msg="Input data is not supported for RShellConnection and will be ignored.", |
| 125 | + ) |
| 126 | + |
| 127 | + if cwd is not None: |
| 128 | + logger.log( |
| 129 | + level=log_levels.MODULE_DEBUG, |
| 130 | + msg="CWD is not supported for RShellConnection and will be ignored.", |
| 131 | + ) |
| 132 | + |
| 133 | + if env is not None: |
| 134 | + logger.log( |
| 135 | + level=log_levels.MODULE_DEBUG, |
| 136 | + msg="Environment variables are not supported for RShellConnection and will be ignored.", |
| 137 | + ) |
| 138 | + |
| 139 | + if stderr_to_stdout: |
| 140 | + logger.log( |
| 141 | + level=log_levels.MODULE_DEBUG, |
| 142 | + msg="Redirecting stderr to stdout is not supported for RShellConnection and will be ignored.", |
| 143 | + ) |
| 144 | + |
| 145 | + if discard_stdout: |
| 146 | + logger.log( |
| 147 | + level=log_levels.MODULE_DEBUG, |
| 148 | + msg="Discarding stdout is not supported for RShellConnection and will be ignored.", |
| 149 | + ) |
| 150 | + |
| 151 | + if discard_stderr: |
| 152 | + logger.log( |
| 153 | + level=log_levels.MODULE_DEBUG, |
| 154 | + msg="Discarding stderr is not supported for RShellConnection and will be ignored.", |
| 155 | + ) |
| 156 | + |
| 157 | + if skip_logging: |
| 158 | + logger.log( |
| 159 | + level=log_levels.MODULE_DEBUG, |
| 160 | + msg="Skipping logging is not supported for RShellConnection and will be ignored.", |
| 161 | + ) |
| 162 | + |
| 163 | + if expected_return_codes is not None: |
| 164 | + logger.log( |
| 165 | + level=log_levels.MODULE_DEBUG, |
| 166 | + msg="Expected return codes are not supported for RShellConnection and will be ignored.", |
| 167 | + ) |
| 168 | + |
| 169 | + if shell: |
| 170 | + logger.log( |
| 171 | + level=log_levels.MODULE_DEBUG, |
| 172 | + msg="Shell execution is not supported for RShellConnection and will be ignored.", |
| 173 | + ) |
| 174 | + |
| 175 | + if custom_exception: |
| 176 | + logger.log( |
| 177 | + level=log_levels.MODULE_DEBUG, |
| 178 | + msg="Custom exceptions are not supported for RShellConnection and will be ignored.", |
| 179 | + ) |
| 180 | + timeout_string = f" with timeout {timeout} seconds" if timeout is not None else "" |
| 181 | + logger.log(level=log_levels.CMD, msg=f"Executing >{self._ip}> '{command}',{timeout_string}") |
| 182 | + |
| 183 | + response = requests.post( |
| 184 | + f"http://{self.server_ip}/execute_command", |
| 185 | + data={"command": command, "timeout": timeout, "ip": self._ip}, |
| 186 | + proxies={"no_proxy": "*"}, |
| 187 | + ) |
| 188 | + completed_process = ConnectionCompletedProcess( |
| 189 | + args=command, |
| 190 | + stdout=response.text, |
| 191 | + return_code=int(response.headers.get("rc", -1)), |
| 192 | + ) |
| 193 | + logger.log( |
| 194 | + level=log_levels.MODULE_DEBUG, |
| 195 | + msg=f"Finished executing '{command}', rc={completed_process.return_code}", |
| 196 | + ) |
| 197 | + if skip_logging: |
| 198 | + return completed_process |
| 199 | + |
| 200 | + stdout = completed_process.stdout |
| 201 | + if stdout: |
| 202 | + logger.log(level=log_levels.OUT, msg=f"stdout>>\n{stdout}") |
| 203 | + |
| 204 | + return completed_process |
| 205 | + |
| 206 | + def path(self, *args, **kwargs) -> CustomPath: |
| 207 | + """Path represents a filesystem path.""" |
| 208 | + if sys.version_info >= (3, 12): |
| 209 | + kwargs["owner"] = self |
| 210 | + return custom_path_factory(*args, **kwargs) |
| 211 | + |
| 212 | + return CustomPath(*args, owner=self, **kwargs) |
| 213 | + |
| 214 | + def get_os_name(self) -> OSName: # noqa: D102 |
| 215 | + raise NotImplementedError |
| 216 | + |
| 217 | + def get_os_type(self) -> OSType: # noqa: D102 |
| 218 | + raise NotImplementedError |
| 219 | + |
| 220 | + def get_os_bitness(self) -> OSBitness: # noqa: D102 |
| 221 | + raise NotImplementedError |
| 222 | + |
| 223 | + def get_cpu_architecture(self) -> CPUArchitecture: # noqa: D102 |
| 224 | + raise NotImplementedError |
| 225 | + |
| 226 | + def restart_platform(self) -> None: # noqa: D102 |
| 227 | + raise NotImplementedError |
| 228 | + |
| 229 | + def shutdown_platform(self) -> None: # noqa: D102 |
| 230 | + raise NotImplementedError |
| 231 | + |
| 232 | + def wait_for_host(self, timeout: int = 60) -> None: # noqa: D102 |
| 233 | + raise NotImplementedError |
0 commit comments