forked from TrueCloudLab/frostfs-testlib
[#307] Add shell command to report
Signed-off-by: Kirill Sosnovskikh <k.sosnovskikh@yadro.com>
This commit is contained in:
parent
738cfacbb7
commit
17a44bdb9e
2 changed files with 9 additions and 16 deletions
|
@ -144,3 +144,4 @@ class LocalShell(Shell):
|
||||||
f"Start / End / Elapsed\t {start_time.time()} / {end_time.time()} / {elapsed_time}"
|
f"Start / End / Elapsed\t {start_time.time()} / {end_time.time()} / {elapsed_time}"
|
||||||
)
|
)
|
||||||
reporter.attach(command_attachment, "Command execution.txt")
|
reporter.attach(command_attachment, "Command execution.txt")
|
||||||
|
reporter.attach(command, "Command.txt")
|
||||||
|
|
|
@ -68,8 +68,7 @@ class SshConnectionProvider:
|
||||||
try:
|
try:
|
||||||
if creds.ssh_key_path:
|
if creds.ssh_key_path:
|
||||||
logger.info(
|
logger.info(
|
||||||
f"Trying to connect to host {host} as {creds.ssh_login} using SSH key "
|
f"Trying to connect to host {host} as {creds.ssh_login} using SSH key " f"{creds.ssh_key_path} (attempt {attempt})"
|
||||||
f"{creds.ssh_key_path} (attempt {attempt})"
|
|
||||||
)
|
)
|
||||||
connection.connect(
|
connection.connect(
|
||||||
hostname=host,
|
hostname=host,
|
||||||
|
@ -79,9 +78,7 @@ class SshConnectionProvider:
|
||||||
timeout=self.CONNECTION_TIMEOUT,
|
timeout=self.CONNECTION_TIMEOUT,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
logger.info(
|
logger.info(f"Trying to connect to host {host} as {creds.ssh_login} using password " f"(attempt {attempt})")
|
||||||
f"Trying to connect to host {host} as {creds.ssh_login} using password " f"(attempt {attempt})"
|
|
||||||
)
|
|
||||||
connection.connect(
|
connection.connect(
|
||||||
hostname=host,
|
hostname=host,
|
||||||
port=port,
|
port=port,
|
||||||
|
@ -104,9 +101,7 @@ class SshConnectionProvider:
|
||||||
connection.close()
|
connection.close()
|
||||||
can_retry = attempt + 1 < self.SSH_CONNECTION_ATTEMPTS
|
can_retry = attempt + 1 < self.SSH_CONNECTION_ATTEMPTS
|
||||||
if can_retry:
|
if can_retry:
|
||||||
logger.warn(
|
logger.warn(f"Can't connect to host {host}, will retry after {self.SSH_ATTEMPTS_INTERVAL}s. Error: {exc}")
|
||||||
f"Can't connect to host {host}, will retry after {self.SSH_ATTEMPTS_INTERVAL}s. Error: {exc}"
|
|
||||||
)
|
|
||||||
sleep(self.SSH_ATTEMPTS_INTERVAL)
|
sleep(self.SSH_ATTEMPTS_INTERVAL)
|
||||||
continue
|
continue
|
||||||
logger.exception(f"Can't connect to host {host}")
|
logger.exception(f"Can't connect to host {host}")
|
||||||
|
@ -145,7 +140,8 @@ def log_command(func):
|
||||||
if not options.no_log:
|
if not options.no_log:
|
||||||
logger.info(log_message)
|
logger.info(log_message)
|
||||||
|
|
||||||
reporter.attach(log_message, "SSH command.txt")
|
reporter.attach(log_message, "SSH Command execution.txt")
|
||||||
|
reporter.attach(command, "SSH Command.txt")
|
||||||
return result
|
return result
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
@ -185,13 +181,11 @@ class SSHShell(Shell):
|
||||||
private_key_passphrase: Optional[str] = None,
|
private_key_passphrase: Optional[str] = None,
|
||||||
port: str = "22",
|
port: str = "22",
|
||||||
command_inspectors: Optional[list[CommandInspector]] = None,
|
command_inspectors: Optional[list[CommandInspector]] = None,
|
||||||
custom_environment: Optional[dict] = None
|
custom_environment: Optional[dict] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.connection_provider = SshConnectionProvider()
|
self.connection_provider = SshConnectionProvider()
|
||||||
self.connection_provider.store_creds(
|
self.connection_provider.store_creds(host, SshCredentials(login, password, private_key_path, private_key_passphrase))
|
||||||
host, SshCredentials(login, password, private_key_path, private_key_passphrase)
|
|
||||||
)
|
|
||||||
self.host = host
|
self.host = host
|
||||||
self.port = port
|
self.port = port
|
||||||
|
|
||||||
|
@ -220,9 +214,7 @@ class SSHShell(Shell):
|
||||||
result = self._exec_non_interactive(command, options)
|
result = self._exec_non_interactive(command, options)
|
||||||
|
|
||||||
if options.check and result.return_code != 0:
|
if options.check and result.return_code != 0:
|
||||||
raise RuntimeError(
|
raise RuntimeError(f"Command: {command}\nreturn code: {result.return_code}\nOutput: {result.stdout}\nStderr: {result.stderr}\n")
|
||||||
f"Command: {command}\nreturn code: {result.return_code}\nOutput: {result.stdout}\nStderr: {result.stderr}\n"
|
|
||||||
)
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@log_command
|
@log_command
|
||||||
|
|
Loading…
Reference in a new issue