Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Copyright 2012 OpenStack, LLC |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 18 | |
Monty Taylor | b2ca5ca | 2013-04-28 18:00:21 -0700 | [diff] [blame] | 19 | import cStringIO |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 20 | import select |
| 21 | import socket |
| 22 | import time |
| 23 | import warnings |
| 24 | |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 25 | from tempest import exceptions |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 26 | |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 27 | |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 28 | with warnings.catch_warnings(): |
| 29 | warnings.simplefilter("ignore") |
| 30 | import paramiko |
| 31 | |
| 32 | |
| 33 | class Client(object): |
| 34 | |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 35 | def __init__(self, host, username, password=None, timeout=300, pkey=None, |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 36 | channel_timeout=10, look_for_keys=False, key_filename=None): |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 37 | self.host = host |
| 38 | self.username = username |
| 39 | self.password = password |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 40 | if isinstance(pkey, basestring): |
Monty Taylor | b2ca5ca | 2013-04-28 18:00:21 -0700 | [diff] [blame] | 41 | pkey = paramiko.RSAKey.from_private_key( |
| 42 | cStringIO.StringIO(str(pkey))) |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 43 | self.pkey = pkey |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 44 | self.look_for_keys = look_for_keys |
| 45 | self.key_filename = key_filename |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 46 | self.timeout = int(timeout) |
Jaroslav Henner | ab32784 | 2012-09-11 15:44:29 +0200 | [diff] [blame] | 47 | self.channel_timeout = float(channel_timeout) |
| 48 | self.buf_size = 1024 |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 49 | |
Andrea Frittoli | 334f1fd | 2013-05-15 06:57:43 +0100 | [diff] [blame] | 50 | def _get_ssh_connection(self, sleep=1.5, backoff=1.01): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 51 | """Returns an ssh connection to the specified host.""" |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 52 | _timeout = True |
Andrea Frittoli | 334f1fd | 2013-05-15 06:57:43 +0100 | [diff] [blame] | 53 | bsleep = sleep |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 54 | ssh = paramiko.SSHClient() |
| 55 | ssh.set_missing_host_key_policy( |
| 56 | paramiko.AutoAddPolicy()) |
| 57 | _start_time = time.time() |
| 58 | |
Mate Lakat | c3f8cd6 | 2013-08-23 12:00:42 +0100 | [diff] [blame^] | 59 | while not self._is_timed_out(_start_time): |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 60 | try: |
| 61 | ssh.connect(self.host, username=self.username, |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 62 | password=self.password, |
| 63 | look_for_keys=self.look_for_keys, |
| 64 | key_filename=self.key_filename, |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 65 | timeout=self.timeout, pkey=self.pkey) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 66 | _timeout = False |
| 67 | break |
Andrea Frittoli | 334f1fd | 2013-05-15 06:57:43 +0100 | [diff] [blame] | 68 | except (socket.error, |
| 69 | paramiko.AuthenticationException): |
| 70 | time.sleep(bsleep) |
| 71 | bsleep *= backoff |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 72 | continue |
| 73 | if _timeout: |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 74 | raise exceptions.SSHTimeout(host=self.host, |
| 75 | user=self.username, |
| 76 | password=self.password) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 77 | return ssh |
| 78 | |
Mate Lakat | c3f8cd6 | 2013-08-23 12:00:42 +0100 | [diff] [blame^] | 79 | def _is_timed_out(self, start_time): |
| 80 | return (time.time() - self.timeout) > start_time |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 81 | |
| 82 | def connect_until_closed(self): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 83 | """Connect to the server and wait until connection is lost.""" |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 84 | try: |
| 85 | ssh = self._get_ssh_connection() |
| 86 | _transport = ssh.get_transport() |
| 87 | _start_time = time.time() |
Mate Lakat | c3f8cd6 | 2013-08-23 12:00:42 +0100 | [diff] [blame^] | 88 | _timed_out = self._is_timed_out(_start_time) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 89 | while _transport.is_active() and not _timed_out: |
| 90 | time.sleep(5) |
Mate Lakat | c3f8cd6 | 2013-08-23 12:00:42 +0100 | [diff] [blame^] | 91 | _timed_out = self._is_timed_out(_start_time) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 92 | ssh.close() |
| 93 | except (EOFError, paramiko.AuthenticationException, socket.error): |
| 94 | return |
| 95 | |
| 96 | def exec_command(self, cmd): |
Jaroslav Henner | ab32784 | 2012-09-11 15:44:29 +0200 | [diff] [blame] | 97 | """ |
| 98 | Execute the specified command on the server. |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 99 | |
Jaroslav Henner | ab32784 | 2012-09-11 15:44:29 +0200 | [diff] [blame] | 100 | Note that this method is reading whole command outputs to memory, thus |
| 101 | shouldn't be used for large outputs. |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 102 | |
Jaroslav Henner | ab32784 | 2012-09-11 15:44:29 +0200 | [diff] [blame] | 103 | :returns: data read from standard output of the command. |
| 104 | :raises: SSHExecCommandFailed if command returns nonzero |
| 105 | status. The exception contains command status stderr content. |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 106 | """ |
| 107 | ssh = self._get_ssh_connection() |
Jaroslav Henner | ab32784 | 2012-09-11 15:44:29 +0200 | [diff] [blame] | 108 | transport = ssh.get_transport() |
| 109 | channel = transport.open_session() |
Attila Fazekas | e14e5a4 | 2013-03-06 07:52:51 +0100 | [diff] [blame] | 110 | channel.fileno() # Register event pipe |
Jaroslav Henner | ab32784 | 2012-09-11 15:44:29 +0200 | [diff] [blame] | 111 | channel.exec_command(cmd) |
| 112 | channel.shutdown_write() |
| 113 | out_data = [] |
| 114 | err_data = [] |
Matthew Treinish | dcaa2b4 | 2013-08-12 19:16:16 +0000 | [diff] [blame] | 115 | poll = select.poll() |
| 116 | poll.register(channel, select.POLLIN) |
Mate Lakat | 99f1663 | 2013-08-23 08:50:32 +0100 | [diff] [blame] | 117 | start_time = time.time() |
| 118 | |
Jaroslav Henner | ab32784 | 2012-09-11 15:44:29 +0200 | [diff] [blame] | 119 | while True: |
Matthew Treinish | dcaa2b4 | 2013-08-12 19:16:16 +0000 | [diff] [blame] | 120 | ready = poll.poll(self.channel_timeout) |
Jaroslav Henner | ab32784 | 2012-09-11 15:44:29 +0200 | [diff] [blame] | 121 | if not any(ready): |
Mate Lakat | c3f8cd6 | 2013-08-23 12:00:42 +0100 | [diff] [blame^] | 122 | if not self._is_timed_out(start_time): |
Mate Lakat | 99f1663 | 2013-08-23 08:50:32 +0100 | [diff] [blame] | 123 | continue |
Jaroslav Henner | ab32784 | 2012-09-11 15:44:29 +0200 | [diff] [blame] | 124 | raise exceptions.TimeoutException( |
Sean Dague | 14c6818 | 2013-04-14 15:34:30 -0400 | [diff] [blame] | 125 | "Command: '{0}' executed on host '{1}'.".format( |
| 126 | cmd, self.host)) |
Jaroslav Henner | ab32784 | 2012-09-11 15:44:29 +0200 | [diff] [blame] | 127 | if not ready[0]: # If there is nothing to read. |
| 128 | continue |
| 129 | out_chunk = err_chunk = None |
| 130 | if channel.recv_ready(): |
| 131 | out_chunk = channel.recv(self.buf_size) |
| 132 | out_data += out_chunk, |
| 133 | if channel.recv_stderr_ready(): |
| 134 | err_chunk = channel.recv_stderr(self.buf_size) |
| 135 | err_data += err_chunk, |
| 136 | if channel.closed and not err_chunk and not out_chunk: |
| 137 | break |
| 138 | exit_status = channel.recv_exit_status() |
| 139 | if 0 != exit_status: |
| 140 | raise exceptions.SSHExecCommandFailed( |
Sean Dague | 14c6818 | 2013-04-14 15:34:30 -0400 | [diff] [blame] | 141 | command=cmd, exit_status=exit_status, |
| 142 | strerror=''.join(err_data)) |
Jaroslav Henner | ab32784 | 2012-09-11 15:44:29 +0200 | [diff] [blame] | 143 | return ''.join(out_data) |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 144 | |
| 145 | def test_connection_auth(self): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 146 | """Returns true if ssh can connect to server.""" |
Daryl Walleck | 1465d61 | 2011-11-02 02:22:15 -0500 | [diff] [blame] | 147 | try: |
| 148 | connection = self._get_ssh_connection() |
| 149 | connection.close() |
| 150 | except paramiko.AuthenticationException: |
| 151 | return False |
| 152 | |
| 153 | return True |