diff options
author | Ross Brattain <ross.b.brattain@intel.com> | 2016-11-29 13:25:18 -0800 |
---|---|---|
committer | Ross Brattain <ross.b.brattain@intel.com> | 2016-11-30 15:09:12 -0800 |
commit | 6a49981bc5d8f9f6ca1d48aa06295cbc74f975b1 (patch) | |
tree | f295dbcf731e1d06ba8d586c7ea4dc19a37948f3 /yardstick/ssh.py | |
parent | 462f25c8e950110a1624909d4f79ef4219005ba2 (diff) |
import new _put_file_shell method from upstream rally
upstream openstack rally added new _put_file_* methods
we should use these
https://github.com/openstack/rally/blob/0.7.0/rally/common/sshutils.py#L270
Updates:
imported rally test__put_file_shell unittests
quote to prevent word split
use -- guard
only chmod on cat success
Change-Id: I357d1a66b5beddad8042958f4e55d67fc68929f6
Signed-off-by: Ross Brattain <ross.b.brattain@intel.com>
Diffstat (limited to 'yardstick/ssh.py')
-rw-r--r-- | yardstick/ssh.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/yardstick/ssh.py b/yardstick/ssh.py index d287b4dac..71dce8102 100644 --- a/yardstick/ssh.py +++ b/yardstick/ssh.py @@ -282,3 +282,37 @@ class SSH(object): def send_command(self, command): client = self._get_client() client.exec_command(command, get_pty=True) + + def _put_file_sftp(self, localpath, remotepath, mode=None): + client = self._get_client() + + with client.open_sftp() as sftp: + sftp.put(localpath, remotepath) + if mode is None: + mode = 0o777 & os.stat(localpath).st_mode + sftp.chmod(remotepath, mode) + + def _put_file_shell(self, localpath, remotepath, mode=None): + # quote to stop wordpslit + cmd = ['cat > "%s"' % remotepath] + if mode is not None: + # use -- so no options + cmd.append('chmod -- 0%o "%s"' % (mode, remotepath)) + + with open(localpath, "rb") as localfile: + # only chmod on successful cat + cmd = "&& ".join(cmd) + self.run(cmd, stdin=localfile) + + def put_file(self, localpath, remotepath, mode=None): + """Copy specified local file to the server. + + :param localpath: Local filename. + :param remotepath: Remote filename. + :param mode: Permissions to set after upload + """ + import socket + try: + self._put_file_sftp(localpath, remotepath, mode=mode) + except (paramiko.SSHException, socket.error): + self._put_file_shell(localpath, remotepath, mode=mode) |