X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2Fssh.py;h=1908da41539b5e8ac0b06985f1b2dc9bd8e321d4;hb=a6d233d80c359813cc0a350f07449c03d552d166;hp=e4ac93fb1b3356539b56c3782dbe409f7cfaf30e;hpb=cd635521219e7d7988ccfd9d0a173ba07217cd00;p=csit.git diff --git a/resources/libraries/python/ssh.py b/resources/libraries/python/ssh.py index e4ac93fb1b..1908da4153 100644 --- a/resources/libraries/python/ssh.py +++ b/resources/libraries/python/ssh.py @@ -24,31 +24,13 @@ from robot.api import logger from scp import SCPClient, SCPException from resources.libraries.python.OptionString import OptionString +from resources.libraries.python.PythonThree import raise_from __all__ = ["exec_cmd", "exec_cmd_no_error"] # TODO: load priv key -def raise_from(raising, excepted): - """Function to be replaced by "raise from" in Python 3. - - Neither "six" nor "future" offer good enough implementation right now. - chezsoi.org/lucas/blog/displaying-chained-exceptions-stacktraces-in-python-2 - - Current implementation just logs excepted error, and raises the new one. - - :param raising: The exception to raise. - :param excepted: The exception we excepted and want to log. - :type raising: BaseException - :type excepted: BaseException - :raises: raising - """ - logger.error("Excepted: {exc!r}\nRaising: {rai!r}".format( - exc=excepted, rai=raising)) - raise raising - - class SSHTimeout(Exception): """This exception is raised when a timeout occurs.""" pass @@ -259,9 +241,9 @@ class SSH(object): if isinstance(cmd, (list, tuple)): cmd = OptionString(cmd) if cmd_input is None: - command = 'sudo -S {c}'.format(c=cmd) + command = 'sudo -E -S {c}'.format(c=cmd) else: - command = 'sudo -S {c} <<< "{i}"'.format(c=cmd, i=cmd_input) + command = 'sudo -E -S {c} <<< "{i}"'.format(c=cmd, i=cmd_input) return self.exec_command(command, timeout, log_stdout_err=log_stdout_err) @@ -285,7 +267,7 @@ class SSH(object): .format(p=lxc_params, n=lxc_name, c=lxc_cmd) if sudo: - command = 'sudo -S {c}'.format(c=command) + command = 'sudo -E -S {c}'.format(c=command) return self.exec_command(command, timeout) def interactive_terminal_open(self, time_out=45): @@ -471,8 +453,8 @@ def exec_cmd(node, cmd, timeout=600, sudo=False, disconnect=False): if not sudo: (ret_code, stdout, stderr) = ssh.exec_command(cmd, timeout=timeout) else: - (ret_code, stdout, stderr) = ssh.exec_command_sudo(cmd, - timeout=timeout) + (ret_code, stdout, stderr) = ssh.exec_command_sudo( + cmd, timeout=timeout) except SSHException as err: logger.error(repr(err)) return None, None, None