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