Revert "fix(IPsecUtil): Delete keywords no longer used"
[csit.git] / resources / libraries / python / ssh.py
index fad0048..437b1ad 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2021 Cisco and/or its affiliates.
+# Copyright (c) 2022 Cisco and/or its affiliates.
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at:
@@ -17,7 +17,7 @@
 import socket
 
 from io import StringIO
-from time import time, sleep
+from time import monotonic, sleep
 
 from paramiko import RSAKey, SSHClient, AutoAddPolicy
 from paramiko.ssh_exception import SSHException, NoValidConnectionsError
@@ -82,7 +82,7 @@ class SSH:
                     raise IOError(f"Cannot connect to {node['host']}")
         else:
             try:
-                start = time()
+                start = monotonic()
                 pkey = None
                 if u"priv_key" in node:
                     pkey = RSAKey.from_private_key(StringIO(node[u"priv_key"]))
@@ -101,7 +101,7 @@ class SSH:
                 SSH.__existing_connections[node_hash] = self._ssh
                 logger.debug(
                     f"New SSH to {self._ssh.get_transport().getpeername()} "
-                    f"took {time() - start} seconds: {self._ssh}"
+                    f"took {monotonic() - start} seconds: {self._ssh}"
                 )
             except SSHException as exc:
                 raise IOError(f"Cannot connect to {node[u'host']}") from exc
@@ -151,6 +151,7 @@ class SSH:
         :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.
+            Needed for calls outside Robot (e.g. from reservation script).
         :type cmd: str or OptionString
         :type timeout: int
         :type log_stdout_err: bool
@@ -174,7 +175,7 @@ class SSH:
 
         logger.trace(f"exec_command on {peer} with timeout {timeout}: {cmd}")
 
-        start = time()
+        start = monotonic()
         chan.exec_command(cmd)
         while not chan.exit_status_ready() and timeout is not None:
             if chan.recv_ready():
@@ -187,7 +188,8 @@ class SSH:
                 stderr += s_err.decode(encoding=u'utf-8', errors=u'ignore') \
                     if isinstance(s_err, bytes) else s_err
 
-            if time() - start > timeout:
+            duration = monotonic() - start
+            if duration > timeout:
                 raise SSHTimeout(
                     f"Timeout exception during execution of command: {cmd}\n"
                     f"Current contents of stdout buffer: "
@@ -209,8 +211,8 @@ class SSH:
             stderr += s_err.decode(encoding=u'utf-8', errors=u'ignore') \
                 if isinstance(s_err, bytes) else s_err
 
-        end = time()
-        logger.trace(f"exec_command on {peer} took {end-start} seconds")
+        duration = monotonic() - start
+        logger.trace(f"exec_command on {peer} took {duration} seconds")
 
         logger.trace(f"return RC {return_code}")
         if log_stdout_err or int(return_code):
@@ -230,6 +232,7 @@ class SSH:
         :param cmd_input: Input redirected to the command.
         :param timeout: Timeout.
         :param log_stdout_err: If True, stdout and stderr are logged.
+            Needed for calls outside Robot (e.g. from reservation script).
         :type cmd: str
         :type cmd_input: str
         :type timeout: int
@@ -400,19 +403,20 @@ class SSH:
                 self._ssh.get_transport(), sanitize=lambda x: x,
                 socket_timeout=timeout
             )
-        start = time()
+        start = monotonic()
         if not get:
             scp.put(local_path, remote_path)
         else:
             scp.get(remote_path, local_path)
         scp.close()
-        end = time()
-        logger.trace(f"SCP took {end-start} seconds")
+        duration = monotonic() - start
+        logger.trace(f"SCP took {duration} seconds")
 
 
 def exec_cmd(
         node, cmd, timeout=600, sudo=False, disconnect=False,
-        log_stdout_err=True):
+        log_stdout_err=True
+    ):
     """Convenience function to ssh/exec/return rc, out & err.
 
     Returns (rc, stdout, stderr).
@@ -425,6 +429,7 @@ def exec_cmd(
     :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.
+        Needed for calls outside Robot (e.g. from reservation script).
     :type node: dict
     :type cmd: str or OptionString
     :type timeout: int
@@ -432,7 +437,7 @@ def exec_cmd(
     :type disconnect: bool
     :type log_stdout_err: bool
     :returns: RC, Stdout, Stderr.
-    :rtype: tuple(int, str, str)
+    :rtype: Tuple[int, str, str]
     """
     if node is None:
         raise TypeError(u"Node parameter is None")
@@ -470,7 +475,8 @@ def exec_cmd(
 
 def exec_cmd_no_error(
         node, cmd, timeout=600, sudo=False, message=None, disconnect=False,
-        retries=0, include_reason=False, log_stdout_err=True):
+        retries=0, include_reason=False, log_stdout_err=True
+    ):
     """Convenience function to ssh/exec/return out & err.
 
     Verifies that return code is zero.
@@ -489,6 +495,7 @@ def exec_cmd_no_error(
     :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.
+        Needed for calls outside Robot thread (e.g. parallel framework setup).
     :type node: dict
     :type cmd: str or OptionString
     :type timeout: int