Add option to skip log output to logger

Signed-off-by: Andrey Berezin <a.berezin@yadro.com>
This commit is contained in:
Andrey Berezin 2022-11-03 12:18:57 +03:00 committed by abereziny
parent dc0f115a73
commit c6603dbf84
2 changed files with 9 additions and 3 deletions

View file

@ -44,12 +44,14 @@ class CommandOptions:
timeout: Timeout for command execution (in seconds). timeout: Timeout for command execution (in seconds).
check: Controls whether to check return code of the command. Set to False to check: Controls whether to check return code of the command. Set to False to
ignore non-zero return codes. ignore non-zero return codes.
no_log: Do not print output to logger if True.
""" """
interactive_inputs: Optional[list[InteractiveInput]] = None interactive_inputs: Optional[list[InteractiveInput]] = None
close_stdin: bool = False close_stdin: bool = False
timeout: int = 30 timeout: int = 30
check: bool = True check: bool = True
no_log: bool = False
@dataclass @dataclass

View file

@ -36,13 +36,15 @@ class HostIsNotAvailable(Exception):
def log_command(func): def log_command(func):
@wraps(func) @wraps(func)
def wrapper(shell: "SSHShell", command: str, *args, **kwargs) -> CommandResult: def wrapper(
shell: "SSHShell", command: str, options: CommandOptions, *args, **kwargs
) -> CommandResult:
command_info = command.removeprefix("$ProgressPreference='SilentlyContinue'\n") command_info = command.removeprefix("$ProgressPreference='SilentlyContinue'\n")
with reporter.step(command_info): with reporter.step(command_info):
logger.info(f'Execute command "{command}" on "{shell.host}"') logger.info(f'Execute command "{command}" on "{shell.host}"')
start_time = datetime.utcnow() start_time = datetime.utcnow()
result = func(shell, command, *args, **kwargs) result = func(shell, command, options, *args, **kwargs)
end_time = datetime.utcnow() end_time = datetime.utcnow()
elapsed_time = end_time - start_time elapsed_time = end_time - start_time
@ -55,7 +57,9 @@ def log_command(func):
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}"
) )
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.txt")
return result return result