blob: a567c6ad4dbfabf8a843393673215327bb517539 [file] [log] [blame]
Sean Dague556add52013-07-19 14:28:44 -04001# 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 Shileev14113572014-11-21 16:58:02 +030013import netaddr
Attila Fazekasa23f5002012-10-23 19:32:45 +020014import re
Matthew Treinisha83a16e2012-12-07 13:44:02 -050015import time
16
David Kranz968f1b32015-06-18 16:58:18 -040017from oslo_log import log as logging
Matthew Treinish96e9e882014-06-09 18:37:19 -040018import six
Andrey Pavlov64723762015-04-29 06:24:58 +030019from tempest_lib.common import ssh
Matthew Treinish96e9e882014-06-09 18:37:19 -040020
Sean Dague86bd8422013-12-20 09:56:44 -050021from tempest import config
Masayuki Igawa7e9eb542014-02-17 15:03:44 +090022from tempest import exceptions
Daryl Walleck98e66dd2012-06-21 04:58:39 -050023
Sean Dague86bd8422013-12-20 09:56:44 -050024CONF = config.CONF
25
David Kranz968f1b32015-06-18 16:58:18 -040026LOG = logging.getLogger(__name__)
27
Daryl Walleck6b9b2882012-04-08 21:43:39 -050028
Joe Gordon28788b42015-02-25 12:42:37 -080029class RemoteClient(object):
Daryl Walleck6b9b2882012-04-08 21:43:39 -050030
Attila Fazekasc3a095b2013-08-17 09:15:44 +020031 # NOTE(afazekas): It should always get an address instead of server
Attila Fazekasa23f5002012-10-23 19:32:45 +020032 def __init__(self, server, username, password=None, pkey=None):
nithya-ganesan67da2872015-02-08 23:13:48 +000033 ssh_timeout = CONF.validation.ssh_timeout
Sean Dague86bd8422013-12-20 09:56:44 -050034 network = CONF.compute.network_for_ssh
nithya-ganesan67da2872015-02-08 23:13:48 +000035 ip_version = CONF.validation.ip_version_for_ssh
36 connect_timeout = CONF.validation.connect_timeout
llg821243b20502014-02-22 10:32:49 +080037 if isinstance(server, six.string_types):
Attila Fazekasa23f5002012-10-23 19:32:45 +020038 ip_address = server
39 else:
40 addresses = server['addresses'][network]
41 for address in addresses:
42 if address['version'] == ip_version:
43 ip_address = address['addr']
44 break
45 else:
Masayuki Igawa7e9eb542014-02-17 15:03:44 +090046 raise exceptions.ServerUnreachable()
47 self.ssh_client = ssh.Client(ip_address, username, password,
48 ssh_timeout, pkey=pkey,
nithya-ganesan67da2872015-02-08 23:13:48 +000049 channel_timeout=connect_timeout)
Daryl Walleck6b9b2882012-04-08 21:43:39 -050050
Elena Ezhova91db24e2014-02-28 20:47:10 +040051 def exec_command(self, cmd):
Evgeny Antyshevf58ab6d2015-04-15 08:23:05 +000052 # Shell options below add more clearness on failures,
53 # path is extended for some non-cirros guest oses (centos7)
David Kranz968f1b32015-06-18 16:58:18 -040054 cmd = CONF.compute.ssh_shell_prologue + " " + cmd
55 LOG.debug("Remote command: %s" % cmd)
Elena Ezhova91db24e2014-02-28 20:47:10 +040056 return self.ssh_client.exec_command(cmd)
57
Attila Fazekasad7ef7d2013-11-20 10:12:53 +010058 def validate_authentication(self):
59 """Validate ssh connection and authentication
60 This method raises an Exception when the validation fails.
61 """
62 self.ssh_client.test_connection_auth()
Daryl Walleck6b9b2882012-04-08 21:43:39 -050063
64 def hostname_equals_servername(self, expected_hostname):
Chang Bo Guocc1623c2013-09-13 20:11:27 -070065 # Get host name using command "hostname"
Elena Ezhova91db24e2014-02-28 20:47:10 +040066 actual_hostname = self.exec_command("hostname").rstrip()
Daryl Walleck6b9b2882012-04-08 21:43:39 -050067 return expected_hostname == actual_hostname
68
Daryl Walleck6b9b2882012-04-08 21:43:39 -050069 def get_ram_size_in_mb(self):
Elena Ezhova91db24e2014-02-28 20:47:10 +040070 output = self.exec_command('free -m | grep Mem')
Daryl Walleck6b9b2882012-04-08 21:43:39 -050071 if output:
72 return output.split()[1]
73
74 def get_number_of_vcpus(self):
Alexander Gubanov0ff3ffb2015-07-01 15:49:06 +030075 output = self.exec_command('grep -c processor /proc/cpuinfo')
Daryl Walleck6b9b2882012-04-08 21:43:39 -050076 return int(output)
Dan Smithc18d8c62012-07-02 08:09:26 -070077
78 def get_partitions(self):
79 # Return the contents of /proc/partitions
80 command = 'cat /proc/partitions'
Elena Ezhova91db24e2014-02-28 20:47:10 +040081 output = self.exec_command(command)
Dan Smithc18d8c62012-07-02 08:09:26 -070082 return output
Daryl Walleck98e66dd2012-06-21 04:58:39 -050083
84 def get_boot_time(self):
Vincent Untz3c0b5b92014-01-18 10:56:00 +010085 cmd = 'cut -f1 -d. /proc/uptime'
Elena Ezhova91db24e2014-02-28 20:47:10 +040086 boot_secs = self.exec_command(cmd)
Vincent Untz3c0b5b92014-01-18 10:56:00 +010087 boot_time = time.time() - int(boot_secs)
88 return time.localtime(boot_time)
Attila Fazekasa23f5002012-10-23 19:32:45 +020089
90 def write_to_console(self, message):
91 message = re.sub("([$\\`])", "\\\\\\\\\\1", message)
92 # usually to /dev/ttyS0
93 cmd = 'sudo sh -c "echo \\"%s\\" >/dev/console"' % message
Elena Ezhova91db24e2014-02-28 20:47:10 +040094 return self.exec_command(cmd)
Yair Fried5f670ab2013-12-09 09:26:51 +020095
Richard Wintersf87059b2015-02-17 11:46:54 -050096 def ping_host(self, host, count=CONF.compute.ping_count,
97 size=CONF.compute.ping_size):
Kirill Shileev14113572014-11-21 16:58:02 +030098 addr = netaddr.IPAddress(host)
99 cmd = 'ping6' if addr.version == 6 else 'ping'
Richard Wintersf87059b2015-02-17 11:46:54 -0500100 cmd += ' -c{0} -w{0} -s{1} {2}'.format(count, size, host)
Elena Ezhova91db24e2014-02-28 20:47:10 +0400101 return self.exec_command(cmd)
Yair Fried4d7efa62013-11-17 17:12:29 +0200102
103 def get_mac_address(self):
Evgeny Antyshevf58ab6d2015-04-15 08:23:05 +0000104 cmd = "ip addr | awk '/ether/ {print $2}'"
Elena Ezhova91db24e2014-02-28 20:47:10 +0400105 return self.exec_command(cmd)
Yair Fried3097dc12014-01-26 08:46:43 +0200106
Yair Fried413bf2d2014-11-19 17:07:11 +0200107 def get_nic_name(self, address):
Evgeny Antyshevf58ab6d2015-04-15 08:23:05 +0000108 cmd = "ip -o addr | awk '/%s/ {print $2}'" % address
Daniel Mellado9e3e1062015-08-06 18:07:05 +0200109 nic = self.exec_command(cmd)
110 return nic.strip().strip(":").lower()
Yair Fried413bf2d2014-11-19 17:07:11 +0200111
Yair Fried3097dc12014-01-26 08:46:43 +0200112 def get_ip_list(self):
Evgeny Antyshevf58ab6d2015-04-15 08:23:05 +0000113 cmd = "ip address"
Elena Ezhova91db24e2014-02-28 20:47:10 +0400114 return self.exec_command(cmd)
Yair Fried3097dc12014-01-26 08:46:43 +0200115
116 def assign_static_ip(self, nic, addr):
Evgeny Antyshevf58ab6d2015-04-15 08:23:05 +0000117 cmd = "sudo ip addr add {ip}/{mask} dev {nic}".format(
Yair Fried3097dc12014-01-26 08:46:43 +0200118 ip=addr, mask=CONF.network.tenant_network_mask_bits,
119 nic=nic
120 )
Elena Ezhova91db24e2014-02-28 20:47:10 +0400121 return self.exec_command(cmd)
Yair Fried3097dc12014-01-26 08:46:43 +0200122
123 def turn_nic_on(self, nic):
Evgeny Antyshevf58ab6d2015-04-15 08:23:05 +0000124 cmd = "sudo ip link set {nic} up".format(nic=nic)
Elena Ezhova91db24e2014-02-28 20:47:10 +0400125 return self.exec_command(cmd)
Elena Ezhova4a27b462014-04-09 15:25:46 +0400126
127 def get_pids(self, pr_name):
128 # Get pid(s) of a process/program
129 cmd = "ps -ef | grep %s | grep -v 'grep' | awk {'print $1'}" % pr_name
130 return self.exec_command(cmd).split('\n')
Yair Fried413bf2d2014-11-19 17:07:11 +0200131
132 def get_dns_servers(self):
133 cmd = 'cat /etc/resolv.conf'
134 resolve_file = self.exec_command(cmd).strip().split('\n')
135 entries = (l.split() for l in resolve_file)
136 dns_servers = [l[1] for l in entries
137 if len(l) and l[0] == 'nameserver']
138 return dns_servers
139
140 def send_signal(self, pid, signum):
141 cmd = 'sudo /bin/kill -{sig} {pid}'.format(pid=pid, sig=signum)
142 return self.exec_command(cmd)
143
144 def _renew_lease_udhcpc(self, fixed_ip=None):
145 """Renews DHCP lease via udhcpc client. """
146 file_path = '/var/run/udhcpc.'
147 nic_name = self.get_nic_name(fixed_ip)
Yair Fried413bf2d2014-11-19 17:07:11 +0200148 pid = self.exec_command('cat {path}{nic}.pid'.
149 format(path=file_path, nic=nic_name))
150 pid = pid.strip()
151 self.send_signal(pid, 'USR1')
152
153 def _renew_lease_dhclient(self, fixed_ip=None):
154 """Renews DHCP lease via dhclient client. """
Itzik Brownffb14022015-03-23 17:03:55 +0200155 cmd = "sudo /sbin/dhclient -r && sudo /sbin/dhclient"
Yair Fried413bf2d2014-11-19 17:07:11 +0200156 self.exec_command(cmd)
157
158 def renew_lease(self, fixed_ip=None):
159 """Wrapper method for renewing DHCP lease via given client
160
161 Supporting:
162 * udhcpc
163 * dhclient
164 """
165 # TODO(yfried): add support for dhcpcd
166 suported_clients = ['udhcpc', 'dhclient']
167 dhcp_client = CONF.scenario.dhcp_client
168 if dhcp_client not in suported_clients:
169 raise exceptions.InvalidConfiguration('%s DHCP client unsupported'
170 % dhcp_client)
171 if dhcp_client == 'udhcpc' and not fixed_ip:
172 raise ValueError("need to set 'fixed_ip' for udhcpc client")
Joe Gordon28788b42015-02-25 12:42:37 -0800173 return getattr(self, '_renew_lease_' + dhcp_client)(fixed_ip=fixed_ip)