Sean Dague | 556add5 | 2013-07-19 14:28:44 -0400 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 15 | import re |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 16 | import time |
| 17 | |
| 18 | from tempest.common.ssh import Client |
| 19 | from tempest.common import utils |
| 20 | from tempest.config import TempestConfig |
| 21 | from tempest.exceptions import ServerUnreachable |
| 22 | from tempest.exceptions import SSHTimeout |
Daryl Walleck | 98e66dd | 2012-06-21 04:58:39 -0500 | [diff] [blame] | 23 | |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 24 | |
| 25 | class RemoteClient(): |
| 26 | |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 27 | #Note(afazekas): It should always get an address instead of server |
| 28 | def __init__(self, server, username, password=None, pkey=None): |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 29 | ssh_timeout = TempestConfig().compute.ssh_timeout |
| 30 | network = TempestConfig().compute.network_for_ssh |
| 31 | ip_version = TempestConfig().compute.ip_version_for_ssh |
Chris Yeoh | 7691604 | 2013-02-27 16:25:25 +1030 | [diff] [blame] | 32 | ssh_channel_timeout = TempestConfig().compute.ssh_channel_timeout |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 33 | if isinstance(server, basestring): |
| 34 | ip_address = server |
| 35 | else: |
| 36 | addresses = server['addresses'][network] |
| 37 | for address in addresses: |
| 38 | if address['version'] == ip_version: |
| 39 | ip_address = address['addr'] |
| 40 | break |
| 41 | else: |
| 42 | raise ServerUnreachable() |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 43 | |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 44 | self.ssh_client = Client(ip_address, username, password, ssh_timeout, |
Chris Yeoh | 7691604 | 2013-02-27 16:25:25 +1030 | [diff] [blame] | 45 | pkey=pkey, |
| 46 | channel_timeout=ssh_channel_timeout) |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 47 | if not self.ssh_client.test_connection_auth(): |
| 48 | raise SSHTimeout() |
| 49 | |
| 50 | def can_authenticate(self): |
| 51 | # Re-authenticate |
| 52 | return self.ssh_client.test_connection_auth() |
| 53 | |
| 54 | def hostname_equals_servername(self, expected_hostname): |
| 55 | # Get hostname using command "hostname" |
| 56 | actual_hostname = self.ssh_client.exec_command("hostname").rstrip() |
| 57 | return expected_hostname == actual_hostname |
| 58 | |
| 59 | def get_files(self, path): |
| 60 | # Return a list of comma seperated files |
| 61 | command = "ls -m " + path |
| 62 | return self.ssh_client.exec_command(command).rstrip('\n').split(', ') |
| 63 | |
| 64 | def get_ram_size_in_mb(self): |
| 65 | output = self.ssh_client.exec_command('free -m | grep Mem') |
| 66 | if output: |
| 67 | return output.split()[1] |
| 68 | |
| 69 | def get_number_of_vcpus(self): |
| 70 | command = 'cat /proc/cpuinfo | grep processor | wc -l' |
| 71 | output = self.ssh_client.exec_command(command) |
| 72 | return int(output) |
Dan Smith | c18d8c6 | 2012-07-02 08:09:26 -0700 | [diff] [blame] | 73 | |
| 74 | def get_partitions(self): |
| 75 | # Return the contents of /proc/partitions |
| 76 | command = 'cat /proc/partitions' |
| 77 | output = self.ssh_client.exec_command(command) |
| 78 | return output |
Daryl Walleck | 98e66dd | 2012-06-21 04:58:39 -0500 | [diff] [blame] | 79 | |
| 80 | def get_boot_time(self): |
| 81 | cmd = 'date -d "`cut -f1 -d. /proc/uptime` seconds ago" \ |
| 82 | "+%Y-%m-%d %H:%M:%S"' |
| 83 | boot_time_string = self.ssh_client.exec_command(cmd) |
| 84 | boot_time_string = boot_time_string.replace('\n', '') |
| 85 | return time.strptime(boot_time_string, utils.LAST_REBOOT_TIME_FORMAT) |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 86 | |
| 87 | def write_to_console(self, message): |
| 88 | message = re.sub("([$\\`])", "\\\\\\\\\\1", message) |
| 89 | # usually to /dev/ttyS0 |
| 90 | cmd = 'sudo sh -c "echo \\"%s\\" >/dev/console"' % message |
| 91 | return self.ssh_client.exec_command(cmd) |