From: "Juraj Linkeš" <juraj.linkes@pantheon.tech>
To: thomas@monjalon.net, 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 v6 06/10] dts: add ssh session module
Date: Thu, 13 Oct 2022 10:35:13 +0000 [thread overview]
Message-ID: <20221013103517.3443997-7-juraj.linkes@pantheon.tech> (raw)
In-Reply-To: <20221013103517.3443997-1-juraj.linkes@pantheon.tech>
The module uses the pexpect python library and implements connection to
a node and two ways to interact with the node:
1. Send a string with specified prompt which will be matched after
the string has been sent to the node.
2. Send a command to be executed. No prompt is specified here.
Signed-off-by: Owen Hilyard <ohilyard@iol.unh.edu>
Signed-off-by: Juraj Linkeš <juraj.linkes@pantheon.tech>
---
dts/framework/exception.py | 57 ++++++
dts/framework/remote_session/__init__.py | 10 +
.../remote_session/remote_session.py | 40 ++--
dts/framework/remote_session/ssh_session.py | 185 ++++++++++++++++++
dts/framework/utils.py | 13 ++
5 files changed, 285 insertions(+), 20 deletions(-)
create mode 100644 dts/framework/exception.py
create mode 100644 dts/framework/remote_session/ssh_session.py
create mode 100644 dts/framework/utils.py
diff --git a/dts/framework/exception.py b/dts/framework/exception.py
new file mode 100644
index 0000000000..8bff9cf9f6
--- /dev/null
+++ b/dts/framework/exception.py
@@ -0,0 +1,57 @@
+# 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
+#
+
+"""
+User-defined exceptions used across the framework.
+"""
+
+
+class SSHTimeoutError(Exception):
+ """
+ Command execution timeout.
+ """
+
+ command: str
+ output: str
+
+ def __init__(self, command: str, output: str):
+ self.command = command
+ self.output = output
+
+ def __str__(self) -> str:
+ return f"TIMEOUT on {self.command}"
+
+ def get_output(self) -> str:
+ return self.output
+
+
+class SSHConnectionError(Exception):
+ """
+ SSH connection error.
+ """
+
+ host: str
+
+ def __init__(self, host: str):
+ self.host = host
+
+ def __str__(self) -> str:
+ return f"Error trying to connect with {self.host}"
+
+
+class SSHSessionDeadError(Exception):
+ """
+ SSH session is not alive.
+ It can no longer be used.
+ """
+
+ host: str
+
+ def __init__(self, host: str):
+ self.host = host
+
+ def __str__(self) -> str:
+ return f"SSH session with {self.host} has died"
diff --git a/dts/framework/remote_session/__init__.py b/dts/framework/remote_session/__init__.py
index d924d8aaa9..d7478e6800 100644
--- a/dts/framework/remote_session/__init__.py
+++ b/dts/framework/remote_session/__init__.py
@@ -2,4 +2,14 @@
# Copyright(c) 2022 PANTHEON.tech s.r.o.
#
+from framework.config import NodeConfiguration
+from framework.logger import DTSLOG
+
from .remote_session import RemoteSession
+from .ssh_session import SSHSession
+
+
+def create_remote_session(
+ node_config: NodeConfiguration, name: str, logger: DTSLOG
+) -> RemoteSession:
+ return SSHSession(node_config, name, logger)
diff --git a/dts/framework/remote_session/remote_session.py b/dts/framework/remote_session/remote_session.py
index 7c499c32e3..eaa4fa7a42 100644
--- a/dts/framework/remote_session/remote_session.py
+++ b/dts/framework/remote_session/remote_session.py
@@ -55,6 +55,13 @@ def __init__(
self._connect()
self.logger.info(f"Connection to {self.username}@{self.hostname} successful.")
+ @abstractmethod
+ def _connect(self) -> None:
+ """
+ Create connection to assigned node.
+ """
+ pass
+
def send_command(self, command: str, timeout: float = SETTINGS.timeout) -> str:
self.logger.info(f"Sending: {command}")
out = self._send_command(command, timeout)
@@ -62,39 +69,32 @@ def send_command(self, command: str, timeout: float = SETTINGS.timeout) -> str:
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 _send_command(self, command: str, timeout: float) -> str:
+ """
+ Send a command and return the output.
+ """
+ pass
def _history_add(self, command: str, output: str) -> None:
self.history.append(
HistoryRecord(name=self.name, command=command, output=output)
)
- @abstractmethod
- def is_alive(self) -> bool:
- """
- Check whether the session is still responding.
- """
- pass
-
- @abstractmethod
- def _connect(self) -> None:
- """
- Create connection to assigned node.
- """
- pass
+ def close(self, force: bool = False) -> None:
+ self.logger.logger_exit()
+ self._close(force)
@abstractmethod
- def _send_command(self, command: str, timeout: float) -> str:
+ def _close(self, force: bool = False) -> None:
"""
- Send a command and return the output.
+ Close the remote session, freeing all used resources.
"""
pass
@abstractmethod
- def _close(self, force: bool = False) -> None:
+ def is_alive(self) -> bool:
"""
- Close the remote session, freeing all used resources.
+ Check whether the session is still responding.
"""
pass
diff --git a/dts/framework/remote_session/ssh_session.py b/dts/framework/remote_session/ssh_session.py
new file mode 100644
index 0000000000..f71acfb1ca
--- /dev/null
+++ b/dts/framework/remote_session/ssh_session.py
@@ -0,0 +1,185 @@
+# 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 time
+
+from pexpect import pxssh
+
+from framework.config import NodeConfiguration
+from framework.exception import SSHConnectionError, SSHSessionDeadError, SSHTimeoutError
+from framework.logger import DTSLOG
+from framework.utils import GREEN, RED
+
+from .remote_session import RemoteSession
+
+
+class SSHSession(RemoteSession):
+ """
+ Module for creating Pexpect SSH sessions to a node.
+ """
+
+ session: pxssh.pxssh
+ magic_prompt: str
+
+ def __init__(
+ self,
+ node_config: NodeConfiguration,
+ session_name: str,
+ logger: DTSLOG,
+ ):
+ self.magic_prompt = "MAGIC PROMPT"
+ super(SSHSession, self).__init__(node_config, session_name, logger)
+
+ def _connect(self) -> None:
+ """
+ Create connection to assigned node.
+ """
+ retry_attempts = 10
+ login_timeout = 20 if self.port else 10
+ password_regex = (
+ r"(?i)(?:password:)|(?:passphrase for key)|(?i)(password for .+:)"
+ )
+ try:
+ for retry_attempt in range(retry_attempts):
+ self.session = pxssh.pxssh(encoding="utf-8")
+ try:
+ self.session.login(
+ self.ip,
+ self.username,
+ self.password,
+ original_prompt="[$#>]",
+ port=self.port,
+ login_timeout=login_timeout,
+ password_regex=password_regex,
+ )
+ break
+ except Exception as e:
+ self.logger.warning(e)
+ time.sleep(2)
+ self.logger.info(
+ f"Retrying connection: retry number {retry_attempt + 1}."
+ )
+ else:
+ raise Exception(f"Connection to {self.hostname} failed")
+
+ self.send_expect("stty -echo", "#")
+ self.send_expect("stty columns 1000", "#")
+ except Exception as e:
+ self.logger.error(RED(str(e)))
+ if getattr(self, "port", None):
+ suggestion = (
+ f"\nSuggestion: Check if the firewall on {self.hostname} is "
+ f"stopped.\n"
+ )
+ self.logger.info(GREEN(suggestion))
+
+ raise SSHConnectionError(self.hostname)
+
+ def send_expect(
+ self, command: str, prompt: str, timeout: float = 15, verify: bool = False
+ ) -> str | int:
+ try:
+ ret = self.send_expect_base(command, prompt, timeout)
+ if verify:
+ ret_status = self.send_expect_base("echo $?", prompt, timeout)
+ try:
+ retval = int(ret_status)
+ if retval:
+ self.logger.error(f"Command: {command} failure!")
+ self.logger.error(ret)
+ return retval
+ else:
+ return ret
+ except ValueError:
+ return ret
+ else:
+ return ret
+ except Exception as e:
+ self.logger.error(
+ f"Exception happened in [{command}] and output is "
+ f"[{self._get_output()}]"
+ )
+ raise e
+
+ def send_expect_base(self, command: str, prompt: str, timeout: float) -> str:
+ self._clean_session()
+ original_prompt = self.session.PROMPT
+ self.session.PROMPT = prompt
+ self._send_line(command)
+ self._prompt(command, timeout)
+
+ before = self._get_output()
+ self.session.PROMPT = original_prompt
+ return before
+
+ def _clean_session(self) -> None:
+ self.get_output(timeout=0.01)
+
+ def _send_line(self, command: str) -> None:
+ if not self.is_alive():
+ raise SSHSessionDeadError(self.hostname)
+ if len(command) == 2 and command.startswith("^"):
+ self.session.sendcontrol(command[1])
+ else:
+ self.session.sendline(command)
+
+ def _prompt(self, command: str, timeout: float) -> None:
+ if not self.session.prompt(timeout):
+ raise SSHTimeoutError(command, self._get_output()) from None
+
+ def get_output(self, timeout: float = 15) -> str:
+ """
+ Get all output before timeout
+ """
+ self.session.PROMPT = self.magic_prompt
+ try:
+ self.session.prompt(timeout)
+ except Exception:
+ pass
+
+ before = self._get_output()
+ self._flush()
+
+ self.logger.debug(before)
+ return before
+
+ def _get_output(self) -> str:
+ if not self.is_alive():
+ raise SSHSessionDeadError(self.hostname)
+ before = self.session.before.rsplit("\r\n", 1)[0]
+ if before == "[PEXPECT]":
+ return ""
+ return before
+
+ def _flush(self) -> None:
+ """
+ Clear all session buffer
+ """
+ self.session.buffer = ""
+ self.session.before = ""
+
+ def is_alive(self) -> bool:
+ return self.session.isalive()
+
+ def _send_command(self, command: str, timeout: float) -> str:
+ try:
+ self._clean_session()
+ self._send_line(command)
+ except Exception as e:
+ raise e
+
+ output = self.get_output(timeout=timeout)
+ self.session.PROMPT = self.session.UNIQUE_PROMPT
+ self.session.prompt(0.1)
+
+ return output
+
+ def _close(self, force: bool = False) -> None:
+ if force is True:
+ self.session.close()
+ else:
+ if self.is_alive():
+ self.session.logout()
diff --git a/dts/framework/utils.py b/dts/framework/utils.py
new file mode 100644
index 0000000000..fe13ae5e77
--- /dev/null
+++ b/dts/framework/utils.py
@@ -0,0 +1,13 @@
+# 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
+#
+
+
+def GREEN(text: str) -> str:
+ return f"\u001B[32;1m{str(text)}\u001B[0m"
+
+
+def RED(text: str) -> str:
+ return f"\u001B[31;1m{str(text)}\u001B[0m"
--
2.30.2
next prev parent reply other threads:[~2022-10-13 10:36 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-13 10:35 [PATCH v6 00/10] dts: ssh connection to a node Juraj Linkeš
2022-10-13 10:35 ` [PATCH v6 01/10] dts: add project tools config Juraj Linkeš
2022-10-13 10:35 ` [PATCH v6 02/10] dts: add developer tools Juraj Linkeš
2022-10-13 10:35 ` [PATCH v6 03/10] dts: add config parser module Juraj Linkeš
2022-10-13 10:35 ` [PATCH v6 04/10] dts: add basic logging facility Juraj Linkeš
2022-10-13 10:35 ` [PATCH v6 05/10] dts: add remote session abstraction Juraj Linkeš
2022-10-13 10:35 ` Juraj Linkeš [this message]
2022-10-13 10:35 ` [PATCH v6 07/10] dts: add node base class Juraj Linkeš
2022-10-13 10:35 ` [PATCH v6 08/10] dts: add dts workflow module Juraj Linkeš
2022-10-13 10:35 ` [PATCH v6 09/10] dts: add dts executable script Juraj Linkeš
2022-10-13 10:35 ` [PATCH v6 10/10] maintainers: add dts maintainers Juraj Linkeš
2022-10-13 10:45 ` [PATCH v6 00/10] dts: ssh connection to a node Bruce Richardson
2022-10-31 19:01 ` Thomas Monjalon
2022-11-02 12:58 ` Owen Hilyard
2022-11-02 13:15 ` Thomas Monjalon
2022-11-03 15:19 ` [PATCH v7 0/9] " Juraj Linkeš
2022-11-03 15:19 ` [PATCH v7 1/9] dts: add project tools config Juraj Linkeš
2022-11-03 15:19 ` [PATCH v7 2/9] dts: add developer tools Juraj Linkeš
2022-11-03 15:19 ` [PATCH v7 3/9] dts: add config parser module Juraj Linkeš
2022-11-03 15:19 ` [PATCH v7 4/9] dts: add basic logging facility Juraj Linkeš
2022-11-03 15:19 ` [PATCH v7 5/9] dts: add remote session abstraction Juraj Linkeš
2022-11-03 15:19 ` [PATCH v7 6/9] dts: add ssh session module Juraj Linkeš
2022-11-03 15:19 ` [PATCH v7 7/9] dts: add node base class Juraj Linkeš
2022-11-03 15:19 ` [PATCH v7 8/9] dts: add dts workflow module Juraj Linkeš
2022-11-03 15:19 ` [PATCH v7 9/9] dts: add dts executable script Juraj Linkeš
2022-11-04 11:05 ` [PATCH v8 0/9] dts: ssh connection to a node Juraj Linkeš
2022-11-04 11:05 ` [PATCH v8 1/9] dts: add project tools config Juraj Linkeš
2022-11-04 11:05 ` [PATCH v8 2/9] dts: add developer tools Juraj Linkeš
2022-11-04 11:05 ` [PATCH v8 3/9] dts: add config parser module Juraj Linkeš
2022-11-04 11:05 ` [PATCH v8 4/9] dts: add basic logging facility Juraj Linkeš
2022-11-04 11:05 ` [PATCH v8 5/9] dts: add remote session abstraction Juraj Linkeš
2022-11-04 11:05 ` [PATCH v8 6/9] dts: add ssh session module Juraj Linkeš
2022-11-04 11:05 ` [PATCH v8 7/9] dts: add node base class Juraj Linkeš
2022-11-04 11:05 ` [PATCH v8 8/9] dts: add dts workflow module Juraj Linkeš
2022-11-04 11:05 ` [PATCH v8 9/9] dts: add dts executable script Juraj Linkeš
2022-11-09 16:11 ` [PATCH v8 0/9] dts: ssh connection to a node Thomas Monjalon
2022-11-09 16:23 ` Honnappa Nagarahalli
2022-11-09 17:05 ` 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=20221013103517.3443997-7-juraj.linkes@pantheon.tech \
--to=juraj.linkes@pantheon.tech \
--cc=Honnappa.Nagarahalli@arm.com \
--cc=bruce.richardson@intel.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).