Sean Dague | 556add5 | 2013-07-19 14:28:44 -0400 | [diff] [blame] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | # not use this file except in compliance with the License. You may obtain |
| 3 | # a copy of the License at |
| 4 | # |
| 5 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | # |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | # License for the specific language governing permissions and limitations |
| 11 | # under the License. |
| 12 | |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 13 | import re |
llg8212 | 43b2050 | 2014-02-22 10:32:49 +0800 | [diff] [blame] | 14 | import six |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 15 | import time |
| 16 | |
Masayuki Igawa | 7e9eb54 | 2014-02-17 15:03:44 +0900 | [diff] [blame] | 17 | from tempest.common import ssh |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 18 | from tempest import config |
Masayuki Igawa | 7e9eb54 | 2014-02-17 15:03:44 +0900 | [diff] [blame] | 19 | from tempest import exceptions |
Daryl Walleck | 98e66dd | 2012-06-21 04:58:39 -0500 | [diff] [blame] | 20 | |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 21 | CONF = config.CONF |
| 22 | |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 23 | |
| 24 | class RemoteClient(): |
| 25 | |
Attila Fazekas | c3a095b | 2013-08-17 09:15:44 +0200 | [diff] [blame] | 26 | # NOTE(afazekas): It should always get an address instead of server |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 27 | def __init__(self, server, username, password=None, pkey=None): |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 28 | ssh_timeout = CONF.compute.ssh_timeout |
| 29 | network = CONF.compute.network_for_ssh |
| 30 | ip_version = CONF.compute.ip_version_for_ssh |
| 31 | ssh_channel_timeout = CONF.compute.ssh_channel_timeout |
llg8212 | 43b2050 | 2014-02-22 10:32:49 +0800 | [diff] [blame] | 32 | if isinstance(server, six.string_types): |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 33 | ip_address = server |
| 34 | else: |
| 35 | addresses = server['addresses'][network] |
| 36 | for address in addresses: |
| 37 | if address['version'] == ip_version: |
| 38 | ip_address = address['addr'] |
| 39 | break |
| 40 | else: |
Masayuki Igawa | 7e9eb54 | 2014-02-17 15:03:44 +0900 | [diff] [blame] | 41 | raise exceptions.ServerUnreachable() |
| 42 | self.ssh_client = ssh.Client(ip_address, username, password, |
| 43 | ssh_timeout, pkey=pkey, |
| 44 | channel_timeout=ssh_channel_timeout) |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 45 | |
Attila Fazekas | ad7ef7d | 2013-11-20 10:12:53 +0100 | [diff] [blame] | 46 | def validate_authentication(self): |
| 47 | """Validate ssh connection and authentication |
| 48 | This method raises an Exception when the validation fails. |
| 49 | """ |
| 50 | self.ssh_client.test_connection_auth() |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 51 | |
| 52 | def hostname_equals_servername(self, expected_hostname): |
Chang Bo Guo | cc1623c | 2013-09-13 20:11:27 -0700 | [diff] [blame] | 53 | # Get host name using command "hostname" |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 54 | actual_hostname = self.ssh_client.exec_command("hostname").rstrip() |
| 55 | return expected_hostname == actual_hostname |
| 56 | |
| 57 | def get_files(self, path): |
Chang Bo Guo | cc1623c | 2013-09-13 20:11:27 -0700 | [diff] [blame] | 58 | # Return a list of comma separated files |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 59 | command = "ls -m " + path |
| 60 | return self.ssh_client.exec_command(command).rstrip('\n').split(', ') |
| 61 | |
| 62 | def get_ram_size_in_mb(self): |
| 63 | output = self.ssh_client.exec_command('free -m | grep Mem') |
| 64 | if output: |
| 65 | return output.split()[1] |
| 66 | |
| 67 | def get_number_of_vcpus(self): |
| 68 | command = 'cat /proc/cpuinfo | grep processor | wc -l' |
| 69 | output = self.ssh_client.exec_command(command) |
| 70 | return int(output) |
Dan Smith | c18d8c6 | 2012-07-02 08:09:26 -0700 | [diff] [blame] | 71 | |
| 72 | def get_partitions(self): |
| 73 | # Return the contents of /proc/partitions |
| 74 | command = 'cat /proc/partitions' |
| 75 | output = self.ssh_client.exec_command(command) |
| 76 | return output |
Daryl Walleck | 98e66dd | 2012-06-21 04:58:39 -0500 | [diff] [blame] | 77 | |
| 78 | def get_boot_time(self): |
Vincent Untz | 3c0b5b9 | 2014-01-18 10:56:00 +0100 | [diff] [blame] | 79 | cmd = 'cut -f1 -d. /proc/uptime' |
| 80 | boot_secs = self.ssh_client.exec_command(cmd) |
| 81 | boot_time = time.time() - int(boot_secs) |
| 82 | return time.localtime(boot_time) |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 83 | |
| 84 | def write_to_console(self, message): |
| 85 | message = re.sub("([$\\`])", "\\\\\\\\\\1", message) |
| 86 | # usually to /dev/ttyS0 |
| 87 | cmd = 'sudo sh -c "echo \\"%s\\" >/dev/console"' % message |
| 88 | return self.ssh_client.exec_command(cmd) |
Yair Fried | 5f670ab | 2013-12-09 09:26:51 +0200 | [diff] [blame] | 89 | |
| 90 | def ping_host(self, host): |
| 91 | cmd = 'ping -c1 -w1 %s' % host |
| 92 | return self.ssh_client.exec_command(cmd) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 93 | |
| 94 | def get_mac_address(self): |
| 95 | cmd = "/sbin/ifconfig | awk '/HWaddr/ {print $5}'" |
| 96 | return self.ssh_client.exec_command(cmd) |
Yair Fried | 3097dc1 | 2014-01-26 08:46:43 +0200 | [diff] [blame] | 97 | |
| 98 | def get_ip_list(self): |
| 99 | cmd = "/bin/ip address" |
| 100 | return self.ssh_client.exec_command(cmd) |
| 101 | |
| 102 | def assign_static_ip(self, nic, addr): |
| 103 | cmd = "sudo /bin/ip addr add {ip}/{mask} dev {nic}".format( |
| 104 | ip=addr, mask=CONF.network.tenant_network_mask_bits, |
| 105 | nic=nic |
| 106 | ) |
| 107 | return self.ssh_client.exec_command(cmd) |
| 108 | |
| 109 | def turn_nic_on(self, nic): |
| 110 | cmd = "sudo /bin/ip link set {nic} up".format(nic=nic) |
| 111 | return self.ssh_client.exec_command(cmd) |