lib/oeqa/utils/sshcontrol.py: fix passing command to subprocess

Don't use shlex.split in subprocess call and also prepend
. /etc/profile, because PATH over ssh is always
/usr/bin:/bin which isn't what many tests expect.

Changed in v2:
 We now need to use a separate call for scp command.

(From OE-Core rev: 1988de2fad86e8e34070ed6573a7be09fff5c0a2)

Signed-off-by: Stefan Stanacar <stefanx.stanacar@intel.com>
Signed-off-by: Saul Wold <sgw@linux.intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Stefan Stanacar 2013-07-09 17:52:55 +03:00 committed by Richard Purdie
parent 46d3bb502f
commit 1552f33a3e
1 changed files with 17 additions and 23 deletions

View File

@ -1,7 +1,6 @@
import subprocess
import time
import os
import shlex
class SSHControl(object):
@ -18,8 +17,12 @@ class SSHControl(object):
f.write("%s\n" % msg)
def _internal_run(self, cmd):
# We need this for a proper PATH
cmd = ". /etc/profile; " + cmd
command = ['ssh', '-o', 'UserKnownHostsFile=/dev/null', '-o', 'StrictHostKeyChecking=no', '-l', 'root', self.host, cmd ]
self.log("[Running]$ %s" % " ".join(command))
# ssh hangs without os.setsid
proc = subprocess.Popen(shlex.split(cmd), shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, preexec_fn=os.setsid)
proc = subprocess.Popen(command, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, preexec_fn=os.setsid)
return proc
def run(self, cmd, timeout=None):
@ -30,15 +33,12 @@ class SSHControl(object):
actualcmd = "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -l root %s '%s'" % (self.host, cmd)
if self.host:
sshconn = self._internal_run(actualcmd)
sshconn = self._internal_run(cmd)
else:
raise Exception("Remote IP hasn't been set: '%s'" % actualcmd)
if timeout == 0:
self.log("[SSH run without timeout]$ %s" % actualcmd)
self.log(" # %s" % cmd)
self._out = sshconn.communicate()[0]
self._ret = sshconn.poll()
else:
@ -48,8 +48,6 @@ class SSHControl(object):
endtime = time.time() + timeout
while sshconn.poll() is None and time.time() < endtime:
time.sleep(1)
self.log("[SSH run with timeout]$ %s" % actualcmd)
self.log(" # %s" % cmd)
# process hasn't returned yet
if sshconn.poll() is None:
self._ret = 255
@ -70,27 +68,23 @@ class SSHControl(object):
return (self._ret, self._out)
def _internal_scp(self, cmd):
self.log("[SCP]$ %s" % cmd)
scpconn = self._internal_run(cmd)
try:
self._out = scpconn.communicate()[0]
self._ret = scpconn.poll()
if self._ret != 0:
self.log("%s" % self._out)
raise Exception("Error copying file")
except Exception as e:
print("Execution failed: %s :" % cmd)
print e
self.log("%s" % self._out)
return (self._ret, self._out)
self.log("[Running SCP]$ %s" % " ".join(cmd))
scpconn = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, preexec_fn=os.setsid)
out = scpconn.communicate()[0]
ret = scpconn.poll()
self.log("%s" % out)
if ret != 0:
# we raise an exception so that tests fail in setUp and setUpClass without a need for an assert
raise Exception("Error running %s, output: %s" % ( " ".join(cmd), out))
return (ret, out)
def copy_to(self, localpath, remotepath):
actualcmd = "scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no %s root@%s:%s" % (localpath, self.host, remotepath)
actualcmd = ['scp', '-o', 'UserKnownHostsFile=/dev/null', '-o', 'StrictHostKeyChecking=no', localpath, 'root@%s:%s' % (self.host, remotepath)]
return self._internal_scp(actualcmd)
def copy_from(self, remotepath, localpath):
actualcmd = "scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@%s:%s %s" % (self.host, remotepath, localpath)
actualcmd = ['scp', '-o', 'UserKnownHostsFile=/dev/null', '-o', 'StrictHostKeyChecking=no', 'root@%s:%s' % (self.host, remotepath), localpath]
return self._internal_scp(actualcmd)
def get_status(self):