DPDK patches and discussions
 help / color / mirror / Atom feed
From: "Juraj Linkeš" <juraj.linkes@pantheon.tech>
To: thomas@monjalon.net, david.marchand@redhat.com,
	Honnappa.Nagarahalli@arm.com, ohilyard@iol.unh.edu,
	lijuan.tu@intel.com, kda@semihalf.com,
	bruce.richardson@intel.com
Cc: dev@dpdk.org, "Juraj Linkeš" <juraj.linkes@pantheon.tech>
Subject: [PATCH v5 05/10] dts: add remote session abstraction
Date: Mon, 26 Sep 2022 14:17:08 +0000	[thread overview]
Message-ID: <20220926141713.2415010-6-juraj.linkes@pantheon.tech> (raw)
In-Reply-To: <20220926141713.2415010-1-juraj.linkes@pantheon.tech>

The abstraction allows for easy switching of implementations of remote
connections (ssh, telnet, etc.). It implements some common features,
such as logging of commands and their outputs and history bookkeeping
and defines methods that must be implemented by derived classes.

Signed-off-by: Owen Hilyard <ohilyard@iol.unh.edu>
Signed-off-by: Juraj Linkeš <juraj.linkes@pantheon.tech>
---
 dts/framework/remote_session/__init__.py      |   5 +
 .../remote_session/remote_session.py          | 100 ++++++++++++++++++
 dts/framework/settings.py                     |  12 +++
 3 files changed, 117 insertions(+)
 create mode 100644 dts/framework/remote_session/__init__.py
 create mode 100644 dts/framework/remote_session/remote_session.py

diff --git a/dts/framework/remote_session/__init__.py b/dts/framework/remote_session/__init__.py
new file mode 100644
index 0000000000..ecf2f5099f
--- /dev/null
+++ b/dts/framework/remote_session/__init__.py
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2022 PANTHEON.tech s.r.o.
+#
+
+from .session_factory import RemoteSession, create_remote_session
diff --git a/dts/framework/remote_session/remote_session.py b/dts/framework/remote_session/remote_session.py
new file mode 100644
index 0000000000..8b55c04a95
--- /dev/null
+++ b/dts/framework/remote_session/remote_session.py
@@ -0,0 +1,100 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2010-2014 Intel Corporation
+# Copyright(c) 2022 PANTHEON.tech s.r.o.
+# Copyright(c) 2022 University of New Hampshire
+#
+
+import dataclasses
+from abc import ABC, abstractmethod
+from typing import Optional
+
+from framework.config import NodeConfiguration
+from framework.logger import DTSLOG
+from framework.settings import SETTINGS
+
+
+@dataclasses.dataclass(slots=True, frozen=True)
+class HistoryRecord:
+    command: str
+    name: str
+    output: str | int
+
+
+class RemoteSession(ABC):
+    _node_config: NodeConfiguration
+    hostname: str
+    username: str
+    password: str
+    ip: str
+    port: Optional[int]
+    name: str
+    logger: DTSLOG
+    history: list[HistoryRecord]
+
+    def __init__(
+        self,
+        node_config: NodeConfiguration,
+        session_name: str,
+        logger: DTSLOG,
+    ):
+        self._node_config = node_config
+        self.logger = logger
+        self.name = session_name
+
+        self.hostname = node_config.hostname
+        self.ip = self.hostname
+        self.port = None
+        if ":" in self.hostname:
+            self.ip, port = self.hostname.split(":")
+            self.port = int(port)
+
+        self.username = node_config.user
+        self.password = node_config.password or ""
+        self.logger.info(f"Remote {self.username}@{self.hostname}")
+        self.history = []
+
+        self._connect()
+
+    def _history_add(self, command: str, output: str) -> None:
+        self.history.append(
+            HistoryRecord(command=command, name=self.name, output=output)
+        )
+
+    def send_command(self, command: str, timeout: float = SETTINGS.timeout) -> str:
+        self.logger.info(f"Sending: {command}")
+        out = self._send_command(command, timeout)
+        self.logger.debug(f"Received from {command}: {out}")
+        self._history_add(command=command, output=out)
+        return out
+
+    def close(self, force: bool = False) -> None:
+        self.logger.logger_exit()
+        self._close(force)
+
+    @abstractmethod
+    def _connect(self) -> None:
+        """
+        Create connection to assigned node.
+        """
+        pass
+
+    @abstractmethod
+    def _send_command(self, command: str, timeout: float) -> str:
+        """
+        Send a command and return the output.
+        """
+        pass
+
+    @abstractmethod
+    def _close(self, force: bool = False) -> None:
+        """
+        Close the remote session, freeing all used resources.
+        """
+        pass
+
+    @abstractmethod
+    def is_alive(self) -> bool:
+        """
+        Check whether the session is still responding.
+        """
+        pass
diff --git a/dts/framework/settings.py b/dts/framework/settings.py
index ed43ba33a0..39be3808ff 100644
--- a/dts/framework/settings.py
+++ b/dts/framework/settings.py
@@ -58,6 +58,7 @@ def __call__(
 class _Settings:
     config_file_path: str
     verbose: bool
+    timeout: float
 
 
 def _get_parser() -> argparse.ArgumentParser:
@@ -82,6 +83,16 @@ def _get_parser() -> argparse.ArgumentParser:
         "to the console.",
     )
 
+    parser.add_argument(
+        "-t",
+        "--timeout",
+        action=_env_arg("DTS_TIMEOUT"),
+        default=15,
+        required=False,
+        help="[DTS_TIMEOUT] The default timeout for all DTS operations except for "
+        "compiling DPDK.",
+    )
+
     return parser
 
 
@@ -90,6 +101,7 @@ def _get_settings() -> _Settings:
     return _Settings(
         config_file_path=parsed_args.config_file,
         verbose=(parsed_args.verbose == "Y"),
+        timeout=float(parsed_args.timeout),
     )
 
 
-- 
2.30.2


  parent reply	other threads:[~2022-09-26 14:17 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-26 14:17 [PATCH v5 00/10] dts: ssh connection to a node Juraj Linkeš
2022-09-26 14:17 ` [PATCH v5 01/10] dts: add project tools config Juraj Linkeš
2022-09-26 14:17 ` [PATCH v5 02/10] dts: add developer tools Juraj Linkeš
2022-09-26 14:17 ` [PATCH v5 03/10] dts: add config parser module Juraj Linkeš
2022-09-26 14:17 ` [PATCH v5 04/10] dts: add basic logging facility Juraj Linkeš
2022-09-26 14:17 ` Juraj Linkeš [this message]
2022-09-26 14:17 ` [PATCH v5 06/10] dts: add ssh connection module Juraj Linkeš
2022-09-27 10:12   ` Stanislaw Kardach
2022-09-30  8:10     ` Juraj Linkeš
2022-09-26 14:17 ` [PATCH v5 07/10] dts: add node base class Juraj Linkeš
2022-09-26 14:17 ` [PATCH v5 08/10] dts: add dts workflow module Juraj Linkeš
2022-09-26 14:17 ` [PATCH v5 09/10] dts: add dts executable script Juraj Linkeš
2022-09-26 14:17 ` [PATCH v5 10/10] maintainers: add dts maintainers Juraj Linkeš
2022-10-11 15:40 ` [PATCH v5 00/10] dts: ssh connection to a node Owen Hilyard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220926141713.2415010-6-juraj.linkes@pantheon.tech \
    --to=juraj.linkes@pantheon.tech \
    --cc=Honnappa.Nagarahalli@arm.com \
    --cc=bruce.richardson@intel.com \
    --cc=david.marchand@redhat.com \
    --cc=dev@dpdk.org \
    --cc=kda@semihalf.com \
    --cc=lijuan.tu@intel.com \
    --cc=ohilyard@iol.unh.edu \
    --cc=thomas@monjalon.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).