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 | |
Kirill Shileev | 1411357 | 2014-11-21 16:58:02 +0300 | [diff] [blame] | 13 | import netaddr |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 14 | import re |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 15 | import time |
| 16 | |
Matthew Treinish | 96e9e88 | 2014-06-09 18:37:19 -0400 | [diff] [blame] | 17 | import six |
| 18 | |
Masayuki Igawa | 7e9eb54 | 2014-02-17 15:03:44 +0900 | [diff] [blame] | 19 | from tempest.common import ssh |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 20 | from tempest import config |
Masayuki Igawa | 7e9eb54 | 2014-02-17 15:03:44 +0900 | [diff] [blame] | 21 | from tempest import exceptions |
Daryl Walleck | 98e66dd | 2012-06-21 04:58:39 -0500 | [diff] [blame] | 22 | |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 23 | CONF = config.CONF |
| 24 | |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 25 | |
| 26 | class RemoteClient(): |
| 27 | |
Attila Fazekas | c3a095b | 2013-08-17 09:15:44 +0200 | [diff] [blame] | 28 | # NOTE(afazekas): It should always get an address instead of server |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 29 | def __init__(self, server, username, password=None, pkey=None): |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 30 | ssh_timeout = CONF.compute.ssh_timeout |
| 31 | network = CONF.compute.network_for_ssh |
| 32 | ip_version = CONF.compute.ip_version_for_ssh |
| 33 | ssh_channel_timeout = CONF.compute.ssh_channel_timeout |
llg8212 | 43b2050 | 2014-02-22 10:32:49 +0800 | [diff] [blame] | 34 | if isinstance(server, six.string_types): |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 35 | ip_address = server |
| 36 | else: |
| 37 | addresses = server['addresses'][network] |
| 38 | for address in addresses: |
| 39 | if address['version'] == ip_version: |
| 40 | ip_address = address['addr'] |
| 41 | break |
| 42 | else: |
Masayuki Igawa | 7e9eb54 | 2014-02-17 15:03:44 +0900 | [diff] [blame] | 43 | raise exceptions.ServerUnreachable() |
| 44 | self.ssh_client = ssh.Client(ip_address, username, password, |
| 45 | ssh_timeout, pkey=pkey, |
| 46 | channel_timeout=ssh_channel_timeout) |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 47 | |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 48 | def exec_command(self, cmd): |
| 49 | return self.ssh_client.exec_command(cmd) |
| 50 | |
Attila Fazekas | ad7ef7d | 2013-11-20 10:12:53 +0100 | [diff] [blame] | 51 | def validate_authentication(self): |
| 52 | """Validate ssh connection and authentication |
| 53 | This method raises an Exception when the validation fails. |
| 54 | """ |
| 55 | self.ssh_client.test_connection_auth() |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 56 | |
| 57 | def hostname_equals_servername(self, expected_hostname): |
Chang Bo Guo | cc1623c | 2013-09-13 20:11:27 -0700 | [diff] [blame] | 58 | # Get host name using command "hostname" |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 59 | actual_hostname = self.exec_command("hostname").rstrip() |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 60 | return expected_hostname == actual_hostname |
| 61 | |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 62 | def get_ram_size_in_mb(self): |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 63 | output = self.exec_command('free -m | grep Mem') |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 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' |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 69 | output = self.exec_command(command) |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 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' |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 75 | output = self.exec_command(command) |
Dan Smith | c18d8c6 | 2012-07-02 08:09:26 -0700 | [diff] [blame] | 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' |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 80 | boot_secs = self.exec_command(cmd) |
Vincent Untz | 3c0b5b9 | 2014-01-18 10:56:00 +0100 | [diff] [blame] | 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 |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 88 | return self.exec_command(cmd) |
Yair Fried | 5f670ab | 2013-12-09 09:26:51 +0200 | [diff] [blame] | 89 | |
| 90 | def ping_host(self, host): |
Kirill Shileev | 1411357 | 2014-11-21 16:58:02 +0300 | [diff] [blame] | 91 | addr = netaddr.IPAddress(host) |
| 92 | cmd = 'ping6' if addr.version == 6 else 'ping' |
| 93 | cmd += ' -c1 -w1 {0}'.format(host) |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 94 | return self.exec_command(cmd) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 95 | |
| 96 | def get_mac_address(self): |
Shang Yong | 1b95cf7 | 2014-10-15 17:19:48 +0800 | [diff] [blame] | 97 | cmd = "/bin/ip addr | awk '/ether/ {print $2}'" |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 98 | return self.exec_command(cmd) |
Yair Fried | 3097dc1 | 2014-01-26 08:46:43 +0200 | [diff] [blame] | 99 | |
Yair Fried | 413bf2d | 2014-11-19 17:07:11 +0200 | [diff] [blame] | 100 | def get_nic_name(self, address): |
| 101 | cmd = "/bin/ip -o addr | awk '/%s/ {print $2}'" % address |
| 102 | return self.exec_command(cmd) |
| 103 | |
Yair Fried | 3097dc1 | 2014-01-26 08:46:43 +0200 | [diff] [blame] | 104 | def get_ip_list(self): |
| 105 | cmd = "/bin/ip address" |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 106 | return self.exec_command(cmd) |
Yair Fried | 3097dc1 | 2014-01-26 08:46:43 +0200 | [diff] [blame] | 107 | |
| 108 | def assign_static_ip(self, nic, addr): |
| 109 | cmd = "sudo /bin/ip addr add {ip}/{mask} dev {nic}".format( |
| 110 | ip=addr, mask=CONF.network.tenant_network_mask_bits, |
| 111 | nic=nic |
| 112 | ) |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 113 | return self.exec_command(cmd) |
Yair Fried | 3097dc1 | 2014-01-26 08:46:43 +0200 | [diff] [blame] | 114 | |
| 115 | def turn_nic_on(self, nic): |
| 116 | cmd = "sudo /bin/ip link set {nic} up".format(nic=nic) |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 117 | return self.exec_command(cmd) |
Elena Ezhova | 4a27b46 | 2014-04-09 15:25:46 +0400 | [diff] [blame] | 118 | |
| 119 | def get_pids(self, pr_name): |
| 120 | # Get pid(s) of a process/program |
| 121 | cmd = "ps -ef | grep %s | grep -v 'grep' | awk {'print $1'}" % pr_name |
| 122 | return self.exec_command(cmd).split('\n') |
Yair Fried | 413bf2d | 2014-11-19 17:07:11 +0200 | [diff] [blame] | 123 | |
| 124 | def get_dns_servers(self): |
| 125 | cmd = 'cat /etc/resolv.conf' |
| 126 | resolve_file = self.exec_command(cmd).strip().split('\n') |
| 127 | entries = (l.split() for l in resolve_file) |
| 128 | dns_servers = [l[1] for l in entries |
| 129 | if len(l) and l[0] == 'nameserver'] |
| 130 | return dns_servers |
| 131 | |
| 132 | def send_signal(self, pid, signum): |
| 133 | cmd = 'sudo /bin/kill -{sig} {pid}'.format(pid=pid, sig=signum) |
| 134 | return self.exec_command(cmd) |
| 135 | |
| 136 | def _renew_lease_udhcpc(self, fixed_ip=None): |
| 137 | """Renews DHCP lease via udhcpc client. """ |
| 138 | file_path = '/var/run/udhcpc.' |
| 139 | nic_name = self.get_nic_name(fixed_ip) |
| 140 | nic_name = nic_name.strip().lower() |
| 141 | pid = self.exec_command('cat {path}{nic}.pid'. |
| 142 | format(path=file_path, nic=nic_name)) |
| 143 | pid = pid.strip() |
| 144 | self.send_signal(pid, 'USR1') |
| 145 | |
| 146 | def _renew_lease_dhclient(self, fixed_ip=None): |
| 147 | """Renews DHCP lease via dhclient client. """ |
| 148 | cmd = "sudo /sbin/dhclient -r && /sbin/dhclient" |
| 149 | self.exec_command(cmd) |
| 150 | |
| 151 | def renew_lease(self, fixed_ip=None): |
| 152 | """Wrapper method for renewing DHCP lease via given client |
| 153 | |
| 154 | Supporting: |
| 155 | * udhcpc |
| 156 | * dhclient |
| 157 | """ |
| 158 | # TODO(yfried): add support for dhcpcd |
| 159 | suported_clients = ['udhcpc', 'dhclient'] |
| 160 | dhcp_client = CONF.scenario.dhcp_client |
| 161 | if dhcp_client not in suported_clients: |
| 162 | raise exceptions.InvalidConfiguration('%s DHCP client unsupported' |
| 163 | % dhcp_client) |
| 164 | if dhcp_client == 'udhcpc' and not fixed_ip: |
| 165 | raise ValueError("need to set 'fixed_ip' for udhcpc client") |
| 166 | return getattr(self, '_renew_lease_' + dhcp_client)(fixed_ip=fixed_ip) |