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