X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2Fssh.py;h=9c7adc44e14093a4043398d54e02244cb3d96585;hb=226758f550b645b7044aa5ecbcef14bb65e80ee5;hp=966d1b0448ac729c9a5112202d866ad19ce433b7;hpb=c481185ca0662f4e7800af2ae6a8f3f81c753470;p=csit.git diff --git a/resources/libraries/python/ssh.py b/resources/libraries/python/ssh.py index 966d1b0448..9c7adc44e1 100644 --- a/resources/libraries/python/ssh.py +++ b/resources/libraries/python/ssh.py @@ -452,10 +452,14 @@ def exec_cmd(node, cmd, timeout=600, sudo=False, disconnect=False): def exec_cmd_no_error( - node, cmd, timeout=600, sudo=False, message=None, disconnect=False): + node, cmd, timeout=600, sudo=False, message=None, disconnect=False, + retries=0): """Convenience function to ssh/exec/return out & err. Verifies that return code is zero. + Supports retries, timeout is related to each try separately then. There is + sleep(1) before each retry. + Disconnect (if enabled) is applied after each try. :param node: DUT node. :param cmd: Command to be executed. @@ -463,21 +467,27 @@ def exec_cmd_no_error( :param sudo: Sudo privilege execution flag. Default: False. :param message: Error message in case of failure. Default: None. :param disconnect: Close the opened SSH connection if True. + :param retries: How many times to retry on failure. :type node: dict :type cmd: str or OptionString :type timeout: int :type sudo: bool :type message: str :type disconnect: bool + :type retries: int :returns: Stdout, Stderr. :rtype: tuple(str, str) :raises RuntimeError: If bash return code is not 0. """ - ret_code, stdout, stderr = exec_cmd( - node, cmd, timeout=timeout, sudo=sudo, disconnect=disconnect) - msg = ('Command execution failed: "{cmd}"\n{stderr}'. - format(cmd=cmd, stderr=stderr) if message is None else message) - if ret_code != 0: + for _ in range(retries + 1): + ret_code, stdout, stderr = exec_cmd( + node, cmd, timeout=timeout, sudo=sudo, disconnect=disconnect) + if ret_code == 0: + break + sleep(1) + else: + msg = ('Command execution failed: "{cmd}"\n{stderr}'. + format(cmd=cmd, stderr=stderr) if message is None else message) raise RuntimeError(msg) return stdout, stderr