@@ -58,13 +58,7 @@ def __init__(self, netns, ssh_key: Path, host, user, *, on_error=None):
5858 # _init_connection loops until it can connect to the guest
5959 # dumping debug state on every iteration is not useful or wanted, so
6060 # only dump it once if _all_ iterations fail.
61- try :
62- self ._init_connection ()
63- except Exception as exc :
64- if on_error :
65- on_error (exc )
66-
67- raise
61+ self ._init_connection ()
6862
6963 self ._on_error = on_error
7064
@@ -79,7 +73,7 @@ def remote_path(self, path):
7973
8074 def _scp (self , path1 , path2 , options ):
8175 """Copy files to/from the VM using scp."""
82- self ._exec (["scp" , * options , path1 , path2 ], check = True )
76+ self ._exec (["scp" , * options , path1 , path2 ])
8377
8478 def scp_put (self , local_path , remote_path , recursive = False ):
8579 """Copy files to the VM using scp."""
@@ -96,7 +90,7 @@ def scp_get(self, remote_path, local_path, recursive=False):
9690 self ._scp (self .remote_path (remote_path ), local_path , opts )
9791
9892 @retry (
99- retry = retry_if_exception_type (ChildProcessError ),
93+ retry = retry_if_exception_type (AssertionError ),
10094 wait = wait_fixed (0.5 ),
10195 stop = stop_after_attempt (20 ),
10296 reraise = True ,
@@ -109,11 +103,12 @@ def _init_connection(self):
109103 We'll keep trying to execute a remote command that can't fail
110104 (`/bin/true`), until we get a successful (0) exit code.
111105 """
112- self .check_output ("true" , timeout = 100 , debug = True )
106+ self .run ("true" , timeout = 100 , debug = True )
113107
114- def run (self , cmd_string , timeout = None , * , check = False , debug = False ):
108+ def run (self , cmd_string , timeout = None , * , check = True , debug = False ):
115109 """
116110 Execute the command passed as a string in the ssh context.
111+ By default raises an exception on non-zero return code of remote command.
117112
118113 If `debug` is set, pass `-vvv` to `ssh`. Note that this will clobber stderr.
119114 """
@@ -124,22 +119,29 @@ def run(self, cmd_string, timeout=None, *, check=False, debug=False):
124119
125120 return self ._exec (command , timeout , check = check )
126121
127- def check_output (self , cmd_string , timeout = None , * , debug = False ):
128- """Same as `run`, but raises an exception on non-zero return code of remote command"""
129- return self .run (cmd_string , timeout , check = True , debug = debug )
130-
131- def _exec (self , cmd , timeout = None , check = False ):
122+ def _exec (self , cmd , timeout = None , check = True ):
132123 """Private function that handles the ssh client invocation."""
133124 if self .netns is not None :
134125 cmd = ["ip" , "netns" , "exec" , self .netns ] + cmd
135126
136- try :
137- return utils .run_cmd (cmd , check = check , timeout = timeout )
138- except Exception as exc :
127+ rc , stdout , stderr = utils .run_cmd (cmd , timeout = timeout )
128+
129+ if not check :
130+ return rc , stdout , stderr
131+
132+ if rc != 0 :
133+ print (
134+ f"SSH command { cmd } exited with non zero error code: { rc } \n "
135+ f"stdout: { stdout } \n "
136+ f"stderr: { stderr } \n "
137+ )
138+
139139 if self ._on_error :
140- self ._on_error (exc )
140+ self ._on_error ()
141+
142+ assert False
141143
142- raise
144+ return rc , stdout , stderr
143145
144146 # pylint:disable=invalid-name
145147 def Popen (
0 commit comments