Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions nodescraper/connection/inband/inbandlocal.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,45 @@ def run_command(

Args:
command (str): shell command string.
sudo (bool, optional): run command with sudo (Linux only). Defaults to False.
sudo (bool, optional): run command with non-interactive sudo -n (Linux only). Defaults to False.
timeout (int, optional): timeout for command in seconds. Defaults to 300.
strip (bool, optional): strip output of command. Defaults to True.

Returns:
CommandArtifact: command result object
"""
if sudo:
command = f"sudo {command}"
command = f"sudo -n {command}"

res = subprocess.run(
command,
encoding="utf-8",
shell=True,
errors="replace",
timeout=timeout,
capture_output=True,
check=False,
)
try:
res = subprocess.run(
command,
encoding="utf-8",
shell=True,
errors="replace",
timeout=timeout,
capture_output=True,
stdin=subprocess.DEVNULL,
check=False,
)
except subprocess.TimeoutExpired as exc:
stdout = exc.stdout or ""
stderr = exc.stderr or ""
if isinstance(stdout, bytes):
stdout = stdout.decode("utf-8", errors="replace")
if isinstance(stderr, bytes):
stderr = stderr.decode("utf-8", errors="replace")
timeout_msg = f"Command timed out after {timeout}s"
stderr = f"{stderr}\n{timeout_msg}".strip() if stderr else timeout_msg
if strip:
stdout = stdout.strip()
stderr = stderr.strip()
return CommandArtifact(
command=command,
stdout=stdout,
stderr=stderr,
exit_code=124,
)

return CommandArtifact(
command=command,
Expand Down
6 changes: 6 additions & 0 deletions nodescraper/connection/redfish/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,18 @@
)
from .redfish_params import RedfishConnectionParams, redfish_params_to_ssh
from .redfish_path import RedfishPath
from .ssh_proxy_connection import SshProxyRedfishConnection
from .ssh_proxy_manager import RedfishSshProxyConnectionManager
from .ssh_proxy_params import RedfishSshProxyConnectionParams

__all__ = [
"RedfishConnection",
"RedfishConnectionError",
"RedfishGetResult",
"RedfishConnectionManager",
"RedfishSshProxyConnectionManager",
"RedfishSshProxyConnectionParams",
"SshProxyRedfishConnection",
"RedfishConnectionParams",
"redfish_params_to_ssh",
"RedfishPath",
Expand Down
30 changes: 25 additions & 5 deletions nodescraper/connection/redfish/redfish_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@

import json
import socket
from typing import Any, Callable, ClassVar, Optional, TypeVar, Union
from typing import Any, Callable, ClassVar, Optional, Protocol, TypeVar, Union
from urllib.parse import urljoin, urlparse

import requests
import urllib3 # type: ignore[import-untyped]
from pydantic import BaseModel
from requests import Response
from requests.auth import HTTPBasicAuth

from .redfish_constants import RF_MEMBERS, RF_MEMBERS_COUNT, RF_MEMBERS_NEXT_LINK
Expand Down Expand Up @@ -71,10 +70,31 @@ def to_html_entry(self) -> dict:
}


class RedfishHttpResponse(Protocol):
"""Status, headers, and body from a Redfish GET or POST."""

status_code: int
headers: Any

@property
def content(self) -> bytes: ...

@property
def ok(self) -> bool: ...

@property
def reason(self) -> str: ...

@property
def text(self) -> str: ...

def json(self) -> Any: ...


class RedfishConnectionError(Exception):
"""Raised when a Redfish API request fails."""

def __init__(self, message: str, response: Optional[Response] = None):
def __init__(self, message: str, response: Optional[RedfishHttpResponse] = None):
super().__init__(message)
self.response = response

Expand Down Expand Up @@ -209,7 +229,7 @@ def get(self, path: RedfishPath) -> dict[str, Any]:
)
return resp.json()

def get_response(self, path: Union[str, "RedfishPath"]) -> Response:
def get_response(self, path: Union[str, "RedfishPath"]) -> RedfishHttpResponse:
"""GET a Redfish path and return the raw Response. path may be a string or RedfishPath."""
path = str(path)
session = self._ensure_session()
Expand All @@ -218,7 +238,7 @@ def get_response(self, path: Union[str, "RedfishPath"]) -> Response:

def post(
self, path: Union[str, "RedfishPath"], json: Optional[dict[str, Any]] = None
) -> Response:
) -> RedfishHttpResponse:
"""POST to a Redfish path and return the raw Response. path may be a string or RedfishPath."""
path = str(path)
session = self._ensure_session()
Expand Down
Loading
Loading