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 |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 19 | from tempest import config |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 20 | from tempest.exceptions import ServerUnreachable |
Daryl Walleck | 98e66dd | 2012-06-21 04:58:39 -0500 | [diff] [blame] | 21 | |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 22 | CONF = config.CONF |
| 23 | |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 24 | |
| 25 | class RemoteClient(): |
| 26 | |
Attila Fazekas | c3a095b | 2013-08-17 09:15:44 +0200 | [diff] [blame] | 27 | # NOTE(afazekas): It should always get an address instead of server |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 28 | def __init__(self, server, username, password=None, pkey=None): |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 29 | ssh_timeout = CONF.compute.ssh_timeout |
| 30 | network = CONF.compute.network_for_ssh |
| 31 | ip_version = CONF.compute.ip_version_for_ssh |
| 32 | ssh_channel_timeout = CONF.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() |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 43 | self.ssh_client = Client(ip_address, username, password, ssh_timeout, |
Chris Yeoh | 7691604 | 2013-02-27 16:25:25 +1030 | [diff] [blame] | 44 | pkey=pkey, |
| 45 | channel_timeout=ssh_channel_timeout) |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 46 | |
Attila Fazekas | ad7ef7d | 2013-11-20 10:12:53 +0100 | [diff] [blame] | 47 | def validate_authentication(self): |
| 48 | """Validate ssh connection and authentication |
| 49 | This method raises an Exception when the validation fails. |
| 50 | """ |
| 51 | self.ssh_client.test_connection_auth() |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 52 | |
| 53 | def hostname_equals_servername(self, expected_hostname): |
Chang Bo Guo | cc1623c | 2013-09-13 20:11:27 -0700 | [diff] [blame] | 54 | # Get host name using command "hostname" |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 55 | actual_hostname = self.ssh_client.exec_command("hostname").rstrip() |
| 56 | return expected_hostname == actual_hostname |
| 57 | |
| 58 | def get_files(self, path): |
Chang Bo Guo | cc1623c | 2013-09-13 20:11:27 -0700 | [diff] [blame] | 59 | # Return a list of comma separated files |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 60 | command = "ls -m " + path |
| 61 | return self.ssh_client.exec_command(command).rstrip('\n').split(', ') |
| 62 | |
| 63 | def get_ram_size_in_mb(self): |
| 64 | output = self.ssh_client.exec_command('free -m | grep Mem') |
| 65 | if output: |
| 66 | return output.split()[1] |
| 67 | |
| 68 | def get_number_of_vcpus(self): |
| 69 | command = 'cat /proc/cpuinfo | grep processor | wc -l' |
| 70 | output = self.ssh_client.exec_command(command) |
| 71 | return int(output) |
Dan Smith | c18d8c6 | 2012-07-02 08:09:26 -0700 | [diff] [blame] | 72 | |
| 73 | def get_partitions(self): |
| 74 | # Return the contents of /proc/partitions |
| 75 | command = 'cat /proc/partitions' |
| 76 | output = self.ssh_client.exec_command(command) |
| 77 | return output |
Daryl Walleck | 98e66dd | 2012-06-21 04:58:39 -0500 | [diff] [blame] | 78 | |
| 79 | def get_boot_time(self): |
Vincent Untz | 3c0b5b9 | 2014-01-18 10:56:00 +0100 | [diff] [blame^] | 80 | cmd = 'cut -f1 -d. /proc/uptime' |
| 81 | boot_secs = self.ssh_client.exec_command(cmd) |
| 82 | boot_time = time.time() - int(boot_secs) |
| 83 | return time.localtime(boot_time) |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 84 | |
| 85 | def write_to_console(self, message): |
| 86 | message = re.sub("([$\\`])", "\\\\\\\\\\1", message) |
| 87 | # usually to /dev/ttyS0 |
| 88 | cmd = 'sudo sh -c "echo \\"%s\\" >/dev/console"' % message |
| 89 | return self.ssh_client.exec_command(cmd) |
Yair Fried | 5f670ab | 2013-12-09 09:26:51 +0200 | [diff] [blame] | 90 | |
| 91 | def ping_host(self, host): |
| 92 | cmd = 'ping -c1 -w1 %s' % host |
| 93 | return self.ssh_client.exec_command(cmd) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 94 | |
| 95 | def get_mac_address(self): |
| 96 | cmd = "/sbin/ifconfig | awk '/HWaddr/ {print $5}'" |
| 97 | return self.ssh_client.exec_command(cmd) |