d6ecffcb9458602b6170f3c05977c5efefcf7d59
[yardstick.git] / yardstick / ssh.py
1 # Copyright 2013: Mirantis Inc.
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
16 # yardstick comment: this is a modified copy of rally/rally/common/sshutils.py
17
18 """High level ssh library.
19
20 Usage examples:
21
22 Execute command and get output:
23
24     ssh = sshclient.SSH("root", "example.com", port=33)
25     status, stdout, stderr = ssh.execute("ps ax")
26     if status:
27         raise Exception("Command failed with non-zero status.")
28     print stdout.splitlines()
29
30 Execute command with huge output:
31
32     class PseudoFile(io.RawIOBase):
33         def write(chunk):
34             if "error" in chunk:
35                 email_admin(chunk)
36
37     ssh = SSH("root", "example.com")
38     with PseudoFile() as p:
39         ssh.run("tail -f /var/log/syslog", stdout=p, timeout=False)
40
41 Execute local script on remote side:
42
43     ssh = sshclient.SSH("user", "example.com")
44
45     with open("~/myscript.sh", "r") as stdin_file:
46         status, out, err = ssh.execute('/bin/sh -s "arg1" "arg2"',
47                                        stdin=stdin_file)
48
49 Upload file:
50
51     ssh = SSH("user", "example.com")
52     # use rb for binary files
53     with open("/store/file.gz", "rb") as stdin_file:
54         ssh.run("cat > ~/upload/file.gz", stdin=stdin_file)
55
56 Eventlet:
57
58     eventlet.monkey_patch(select=True, time=True)
59     or
60     eventlet.monkey_patch()
61     or
62     sshclient = eventlet.import_patched("yardstick.ssh")
63
64 """
65
66 import select
67 import socket
68 import time
69
70 import paramiko
71 from scp import SCPClient
72 import six
73 import logging
74
75 LOG = logging.getLogger(__name__)
76
77 DEFAULT_PORT = 22
78
79
80 class SSHError(Exception):
81     pass
82
83
84 class SSHTimeout(SSHError):
85     pass
86
87
88 class SSH(object):
89     """Represent ssh connection."""
90
91     def __init__(self, user, host, port=DEFAULT_PORT, pkey=None,
92                  key_filename=None, password=None):
93         """Initialize SSH client.
94
95         :param user: ssh username
96         :param host: hostname or ip address of remote ssh server
97         :param port: remote ssh port
98         :param pkey: RSA or DSS private key string or file object
99         :param key_filename: private key filename
100         :param password: password
101         """
102
103         self.user = user
104         self.host = host
105         # we may get text port from YAML, convert to int
106         self.port = int(port)
107         self.pkey = self._get_pkey(pkey) if pkey else None
108         self.password = password
109         self.key_filename = key_filename
110         self._client = False
111
112     def _get_pkey(self, key):
113         if isinstance(key, six.string_types):
114             key = six.moves.StringIO(key)
115         errors = []
116         for key_class in (paramiko.rsakey.RSAKey, paramiko.dsskey.DSSKey):
117             try:
118                 return key_class.from_private_key(key)
119             except paramiko.SSHException as e:
120                 errors.append(e)
121         raise SSHError("Invalid pkey: %s" % (errors))
122
123     def _get_client(self):
124         if self._client:
125             return self._client
126         try:
127             self._client = paramiko.SSHClient()
128             self._client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
129             self._client.connect(self.host, username=self.user,
130                                  port=self.port, pkey=self.pkey,
131                                  key_filename=self.key_filename,
132                                  password=self.password,
133                                  allow_agent=False, look_for_keys=False,
134                                  timeout=1)
135             return self._client
136         except Exception as e:
137             message = ("Exception %(exception_type)s was raised "
138                        "during connect. Exception value is: %(exception)r")
139             self._client = False
140             raise SSHError(message % {"exception": e,
141                                       "exception_type": type(e)})
142
143     def close(self):
144         self._client.close()
145         self._client = False
146
147     def run(self, cmd, stdin=None, stdout=None, stderr=None,
148             raise_on_error=True, timeout=3600):
149         """Execute specified command on the server.
150
151         :param cmd:             Command to be executed.
152         :param stdin:           Open file or string to pass to stdin.
153         :param stdout:          Open file to connect to stdout.
154         :param stderr:          Open file to connect to stderr.
155         :param raise_on_error:  If False then exit code will be return. If True
156                                 then exception will be raized if non-zero code.
157         :param timeout:         Timeout in seconds for command execution.
158                                 Default 1 hour. No timeout if set to 0.
159         """
160
161         client = self._get_client()
162
163         if isinstance(stdin, six.string_types):
164             stdin = six.moves.StringIO(stdin)
165
166         return self._run(client, cmd, stdin=stdin, stdout=stdout,
167                          stderr=stderr, raise_on_error=raise_on_error,
168                          timeout=timeout)
169
170     def _run(self, client, cmd, stdin=None, stdout=None, stderr=None,
171              raise_on_error=True, timeout=3600):
172
173         transport = client.get_transport()
174         session = transport.open_session()
175         session.exec_command(cmd)
176         start_time = time.time()
177
178         data_to_send = ""
179         stderr_data = None
180
181         # If we have data to be sent to stdin then `select' should also
182         # check for stdin availability.
183         if stdin and not stdin.closed:
184             writes = [session]
185         else:
186             writes = []
187
188         while True:
189             # Block until data can be read/write.
190             r, w, e = select.select([session], writes, [session], 1)
191
192             if session.recv_ready():
193                 data = session.recv(4096)
194                 LOG.debug("stdout: %r" % data)
195                 if stdout is not None:
196                     stdout.write(data)
197                 continue
198
199             if session.recv_stderr_ready():
200                 stderr_data = session.recv_stderr(4096)
201                 LOG.debug("stderr: %r" % stderr_data)
202                 if stderr is not None:
203                     stderr.write(stderr_data)
204                 continue
205
206             if session.send_ready():
207                 if stdin is not None and not stdin.closed:
208                     if not data_to_send:
209                         data_to_send = stdin.read(4096)
210                         if not data_to_send:
211                             stdin.close()
212                             session.shutdown_write()
213                             writes = []
214                             continue
215                     sent_bytes = session.send(data_to_send)
216                     # LOG.debug("sent: %s" % data_to_send[:sent_bytes])
217                     data_to_send = data_to_send[sent_bytes:]
218
219             if session.exit_status_ready():
220                 break
221
222             if timeout and (time.time() - timeout) > start_time:
223                 args = {"cmd": cmd, "host": self.host}
224                 raise SSHTimeout("Timeout executing command "
225                                  "'%(cmd)s' on host %(host)s" % args)
226             if e:
227                 raise SSHError("Socket error.")
228
229         exit_status = session.recv_exit_status()
230         if 0 != exit_status and raise_on_error:
231             fmt = "Command '%(cmd)s' failed with exit_status %(status)d."
232             details = fmt % {"cmd": cmd, "status": exit_status}
233             if stderr_data:
234                 details += " Last stderr data: '%s'." % stderr_data
235             raise SSHError(details)
236         return exit_status
237
238     def execute(self, cmd, stdin=None, timeout=3600):
239         """Execute the specified command on the server.
240
241         :param cmd:     Command to be executed.
242         :param stdin:   Open file to be sent on process stdin.
243         :param timeout: Timeout for execution of the command.
244
245         :returns: tuple (exit_status, stdout, stderr)
246         """
247         stdout = six.moves.StringIO()
248         stderr = six.moves.StringIO()
249
250         exit_status = self.run(cmd, stderr=stderr,
251                                stdout=stdout, stdin=stdin,
252                                timeout=timeout, raise_on_error=False)
253         stdout.seek(0)
254         stderr.seek(0)
255         return (exit_status, stdout.read(), stderr.read())
256
257     def wait(self, timeout=120, interval=1):
258         """Wait for the host will be available via ssh."""
259         start_time = time.time()
260         while True:
261             try:
262                 return self.execute("uname")
263             except (socket.error, SSHError) as e:
264                 LOG.debug("Ssh is still unavailable: %r" % e)
265                 time.sleep(interval)
266             if time.time() > (start_time + timeout):
267                 raise SSHTimeout("Timeout waiting for '%s'" % self.host)
268
269     def put(self, files, remote_path=b'.', recursive=False):
270         client = self._get_client()
271
272         with SCPClient(client.get_transport()) as scp:
273             scp.put(files, remote_path, recursive)
274
275     # keep shell running in the background, e.g. screen
276     def send_command(self, command):
277         client = self._get_client()
278         client.exec_command(command, get_pty=True)