diff options
author | Rex Lee <limingjiang@huawei.com> | 2016-12-05 06:37:08 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2016-12-05 06:37:08 +0000 |
commit | cee61dec722722c76d71d32fe56ad877c08e1d36 (patch) | |
tree | edd3514e21d4709f0cf5ce3fa48aa789fa2adf20 /yardstick | |
parent | 310eec58b65fdea6ded88f4271d93a8eaf7a599a (diff) | |
parent | 6a49981bc5d8f9f6ca1d48aa06295cbc74f975b1 (diff) |
Merge "import new _put_file_shell method from upstream rally"
Diffstat (limited to 'yardstick')
-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 8485dccd0..b9d9262cf 100644 --- a/yardstick/ssh.py +++ b/yardstick/ssh.py @@ -290,3 +290,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) |