PAPI: Reduce the amount of logged information
[csit.git] / resources / libraries / python / ssh.py
index 966d1b0..e89daf2 100644 (file)
@@ -140,17 +140,21 @@ class SSH(object):
         logger.debug('Reconnecting peer done: {host}, {port}'.
                      format(host=node['host'], port=node['port']))
 
-    def exec_command(self, cmd, timeout=10):
+    def exec_command(self, cmd, timeout=10, log_stdout_err=True):
         """Execute SSH command on a new channel on the connected Node.
 
         :param cmd: Command to run on the Node.
         :param timeout: Maximal time in seconds to wait until the command is
-        done. If set to None then wait forever.
+            done. If set to None then wait forever.
+        :param log_stdout_err: If True, stdout and stderr are logged. stdout
+            and stderr are logged also if the return code is not zero
+            independently of the value of log_stdout_err.
         :type cmd: str or OptionString
         :type timeout: int
-        :return return_code, stdout, stderr
+        :type log_stdout_err: bool
+        :returns: return_code, stdout, stderr
         :rtype: tuple(int, str, str)
-        :raise SSHTimeout: If command is not finished in timeout time.
+        :raises SSHTimeout: If command is not finished in timeout time.
         """
         cmd = str(cmd)
         stdout = StringIO.StringIO()
@@ -203,17 +207,27 @@ class SSH(object):
                      format(peer=peer, total=end-start))
 
         logger.trace('return RC {rc}'.format(rc=return_code))
-        logger.trace('return STDOUT {stdout}'.format(stdout=stdout.getvalue()))
-        logger.trace('return STDERR {stderr}'.format(stderr=stderr.getvalue()))
+        if log_stdout_err or int(return_code):
+            logger.trace('return STDOUT {stdout}'.
+                         format(stdout=stdout.getvalue()))
+            logger.trace('return STDERR {stderr}'.
+                         format(stderr=stderr.getvalue()))
         return return_code, stdout.getvalue(), stderr.getvalue()
 
-    def exec_command_sudo(self, cmd, cmd_input=None, timeout=30):
+    def exec_command_sudo(self, cmd, cmd_input=None, timeout=30,
+                          log_stdout_err=True):
         """Execute SSH command with sudo on a new channel on the connected Node.
 
         :param cmd: Command to be executed.
         :param cmd_input: Input redirected to the command.
         :param timeout: Timeout.
+        :param log_stdout_err: If True, stdout and stderr are logged.
+        :type cmd: str
+        :type cmd_input: str
+        :type timeout: int
+        :type log_stdout_err: bool
         :returns: return_code, stdout, stderr
+        :rtype: tuple(int, str, str)
 
         :Example:
 
@@ -229,7 +243,8 @@ class SSH(object):
             command = 'sudo -S {c}'.format(c=cmd)
         else:
             command = 'sudo -S {c} <<< "{i}"'.format(c=cmd, i=cmd_input)
-        return self.exec_command(command, timeout)
+        return self.exec_command(command, timeout,
+                                 log_stdout_err=log_stdout_err)
 
     def exec_command_lxc(self, lxc_cmd, lxc_name, lxc_params='', sudo=True,
                          timeout=30):
@@ -452,10 +467,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 +482,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