[#307] Add shell command to report

Signed-off-by: Kirill Sosnovskikh <k.sosnovskikh@yadro.com>
This commit is contained in:
k.sosnovskikh 2024-10-22 15:38:13 +03:00
parent 738cfacbb7
commit 17a44bdb9e
2 changed files with 9 additions and 16 deletions

View file

@ -144,3 +144,4 @@ class LocalShell(Shell):
f"Start / End / Elapsed\t {start_time.time()} / {end_time.time()} / {elapsed_time}"
)
reporter.attach(command_attachment, "Command execution.txt")
reporter.attach(command, "Command.txt")

View file

@ -68,8 +68,7 @@ class SshConnectionProvider:
try:
if creds.ssh_key_path:
logger.info(
f"Trying to connect to host {host} as {creds.ssh_login} using SSH key "
f"{creds.ssh_key_path} (attempt {attempt})"
f"Trying to connect to host {host} as {creds.ssh_login} using SSH key " f"{creds.ssh_key_path} (attempt {attempt})"
)
connection.connect(
hostname=host,
@ -79,9 +78,7 @@ class SshConnectionProvider:
timeout=self.CONNECTION_TIMEOUT,
)
else:
logger.info(
f"Trying to connect to host {host} as {creds.ssh_login} using password " f"(attempt {attempt})"
)
logger.info(f"Trying to connect to host {host} as {creds.ssh_login} using password " f"(attempt {attempt})")
connection.connect(
hostname=host,
port=port,
@ -104,9 +101,7 @@ class SshConnectionProvider:
connection.close()
can_retry = attempt + 1 < self.SSH_CONNECTION_ATTEMPTS
if can_retry:
logger.warn(
f"Can't connect to host {host}, will retry after {self.SSH_ATTEMPTS_INTERVAL}s. Error: {exc}"
)
logger.warn(f"Can't connect to host {host}, will retry after {self.SSH_ATTEMPTS_INTERVAL}s. Error: {exc}")
sleep(self.SSH_ATTEMPTS_INTERVAL)
continue
logger.exception(f"Can't connect to host {host}")
@ -145,7 +140,8 @@ def log_command(func):
if not options.no_log:
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 wrapper
@ -185,13 +181,11 @@ class SSHShell(Shell):
private_key_passphrase: Optional[str] = None,
port: str = "22",
command_inspectors: Optional[list[CommandInspector]] = None,
custom_environment: Optional[dict] = None
custom_environment: Optional[dict] = None,
) -> None:
super().__init__()
self.connection_provider = SshConnectionProvider()
self.connection_provider.store_creds(
host, SshCredentials(login, password, private_key_path, private_key_passphrase)
)
self.connection_provider.store_creds(host, SshCredentials(login, password, private_key_path, private_key_passphrase))
self.host = host
self.port = port
@ -220,9 +214,7 @@ class SSHShell(Shell):
result = self._exec_non_interactive(command, options)
if options.check and result.return_code != 0:
raise RuntimeError(
f"Command: {command}\nreturn code: {result.return_code}\nOutput: {result.stdout}\nStderr: {result.stderr}\n"
)
raise RuntimeError(f"Command: {command}\nreturn code: {result.return_code}\nOutput: {result.stdout}\nStderr: {result.stderr}\n")
return result
@log_command