blob: 5eb73361169ed8ad787af2b64eb42b71321b2b59 [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
Jay Pipes051075a2012-04-28 17:39:37 -04002# 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 Pipes051075a2012-04-28 17:39:37 -040016
Monty Taylorb2ca5ca2013-04-28 18:00:21 -070017import cStringIO
Matthew Treinisha83a16e2012-12-07 13:44:02 -050018import select
19import socket
20import time
21import warnings
22
Daryl Walleck6b9b2882012-04-08 21:43:39 -050023from tempest import exceptions
Attila Fazekasad7ef7d2013-11-20 10:12:53 +010024from tempest.openstack.common import log as logging
Daryl Walleck1465d612011-11-02 02:22:15 -050025
Jay Pipes051075a2012-04-28 17:39:37 -040026
Daryl Walleck1465d612011-11-02 02:22:15 -050027with warnings.catch_warnings():
28 warnings.simplefilter("ignore")
29 import paramiko
30
31
Attila Fazekasad7ef7d2013-11-20 10:12:53 +010032LOG = logging.getLogger(__name__)
33
34
Daryl Walleck1465d612011-11-02 02:22:15 -050035class Client(object):
36
Attila Fazekasa23f5002012-10-23 19:32:45 +020037 def __init__(self, host, username, password=None, timeout=300, pkey=None,
Jay Pipes051075a2012-04-28 17:39:37 -040038 channel_timeout=10, look_for_keys=False, key_filename=None):
Daryl Walleck1465d612011-11-02 02:22:15 -050039 self.host = host
40 self.username = username
41 self.password = password
Attila Fazekasa23f5002012-10-23 19:32:45 +020042 if isinstance(pkey, basestring):
Monty Taylorb2ca5ca2013-04-28 18:00:21 -070043 pkey = paramiko.RSAKey.from_private_key(
44 cStringIO.StringIO(str(pkey)))
Attila Fazekasa23f5002012-10-23 19:32:45 +020045 self.pkey = pkey
Jay Pipes051075a2012-04-28 17:39:37 -040046 self.look_for_keys = look_for_keys
47 self.key_filename = key_filename
Daryl Walleck1465d612011-11-02 02:22:15 -050048 self.timeout = int(timeout)
Jaroslav Hennerab327842012-09-11 15:44:29 +020049 self.channel_timeout = float(channel_timeout)
50 self.buf_size = 1024
Daryl Walleck1465d612011-11-02 02:22:15 -050051
Andrea Frittoli334f1fd2013-05-15 06:57:43 +010052 def _get_ssh_connection(self, sleep=1.5, backoff=1.01):
Sean Daguef237ccb2013-01-04 15:19:14 -050053 """Returns an ssh connection to the specified host."""
Andrea Frittoli334f1fd2013-05-15 06:57:43 +010054 bsleep = sleep
Daryl Walleck1465d612011-11-02 02:22:15 -050055 ssh = paramiko.SSHClient()
56 ssh.set_missing_host_key_policy(
57 paramiko.AutoAddPolicy())
58 _start_time = time.time()
Attila Fazekasad7ef7d2013-11-20 10:12:53 +010059 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 Walleck1465d612011-11-02 02:22:15 -050069 try:
70 ssh.connect(self.host, username=self.username,
Jay Pipes051075a2012-04-28 17:39:37 -040071 password=self.password,
72 look_for_keys=self.look_for_keys,
73 key_filename=self.key_filename,
Soren Hansenb20cf3a2013-11-27 14:39:28 +010074 timeout=self.channel_timeout, pkey=self.pkey)
Attila Fazekasad7ef7d2013-11-20 10:12:53 +010075 LOG.info("ssh connection to %s@%s sucessfuly created",
76 self.username, self.host)
77 return ssh
Andrea Frittoli334f1fd2013-05-15 06:57:43 +010078 except (socket.error,
Joe Gordon31a91a62013-11-22 14:44:13 -080079 paramiko.SSHException):
Attila Fazekasad7ef7d2013-11-20 10:12:53 +010080 attempts += 1
Andrea Frittoli334f1fd2013-05-15 06:57:43 +010081 time.sleep(bsleep)
82 bsleep *= backoff
Attila Fazekasad7ef7d2013-11-20 10:12:53 +010083 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 Walleck1465d612011-11-02 02:22:15 -050092
Mate Lakatc3f8cd62013-08-23 12:00:42 +010093 def _is_timed_out(self, start_time):
94 return (time.time() - self.timeout) > start_time
Daryl Walleck1465d612011-11-02 02:22:15 -050095
96 def connect_until_closed(self):
Sean Daguef237ccb2013-01-04 15:19:14 -050097 """Connect to the server and wait until connection is lost."""
Daryl Walleck1465d612011-11-02 02:22:15 -050098 try:
99 ssh = self._get_ssh_connection()
100 _transport = ssh.get_transport()
101 _start_time = time.time()
Mate Lakatc3f8cd62013-08-23 12:00:42 +0100102 _timed_out = self._is_timed_out(_start_time)
Daryl Walleck1465d612011-11-02 02:22:15 -0500103 while _transport.is_active() and not _timed_out:
104 time.sleep(5)
Mate Lakatc3f8cd62013-08-23 12:00:42 +0100105 _timed_out = self._is_timed_out(_start_time)
Daryl Walleck1465d612011-11-02 02:22:15 -0500106 ssh.close()
107 except (EOFError, paramiko.AuthenticationException, socket.error):
108 return
109
110 def exec_command(self, cmd):
Jaroslav Hennerab327842012-09-11 15:44:29 +0200111 """
112 Execute the specified command on the server.
Daryl Walleck1465d612011-11-02 02:22:15 -0500113
Jaroslav Hennerab327842012-09-11 15:44:29 +0200114 Note that this method is reading whole command outputs to memory, thus
115 shouldn't be used for large outputs.
Daryl Walleck1465d612011-11-02 02:22:15 -0500116
Jaroslav Hennerab327842012-09-11 15:44:29 +0200117 :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 Walleck1465d612011-11-02 02:22:15 -0500120 """
121 ssh = self._get_ssh_connection()
Jaroslav Hennerab327842012-09-11 15:44:29 +0200122 transport = ssh.get_transport()
123 channel = transport.open_session()
Attila Fazekase14e5a42013-03-06 07:52:51 +0100124 channel.fileno() # Register event pipe
Jaroslav Hennerab327842012-09-11 15:44:29 +0200125 channel.exec_command(cmd)
126 channel.shutdown_write()
127 out_data = []
128 err_data = []
Matthew Treinishdcaa2b42013-08-12 19:16:16 +0000129 poll = select.poll()
130 poll.register(channel, select.POLLIN)
Mate Lakat99f16632013-08-23 08:50:32 +0100131 start_time = time.time()
132
Jaroslav Hennerab327842012-09-11 15:44:29 +0200133 while True:
Matthew Treinishdcaa2b42013-08-12 19:16:16 +0000134 ready = poll.poll(self.channel_timeout)
Jaroslav Hennerab327842012-09-11 15:44:29 +0200135 if not any(ready):
Mate Lakatc3f8cd62013-08-23 12:00:42 +0100136 if not self._is_timed_out(start_time):
Mate Lakat99f16632013-08-23 08:50:32 +0100137 continue
Jaroslav Hennerab327842012-09-11 15:44:29 +0200138 raise exceptions.TimeoutException(
Sean Dague14c68182013-04-14 15:34:30 -0400139 "Command: '{0}' executed on host '{1}'.".format(
140 cmd, self.host))
Jaroslav Hennerab327842012-09-11 15:44:29 +0200141 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 Dague14c68182013-04-14 15:34:30 -0400155 command=cmd, exit_status=exit_status,
156 strerror=''.join(err_data))
Jaroslav Hennerab327842012-09-11 15:44:29 +0200157 return ''.join(out_data)
Daryl Walleck1465d612011-11-02 02:22:15 -0500158
159 def test_connection_auth(self):
Attila Fazekasad7ef7d2013-11-20 10:12:53 +0100160 """Raises an exception when we can not connect to server via ssh."""
161 connection = self._get_ssh_connection()
162 connection.close()