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 | |
David Kranz | 968f1b3 | 2015-06-18 16:58:18 -0400 | [diff] [blame] | 17 | from oslo_log import log as logging |
Andrey Pavlov | 6472376 | 2015-04-29 06:24:58 +0300 | [diff] [blame] | 18 | from tempest_lib.common import ssh |
Sean Dague | 57c6655 | 2016-02-08 08:51:13 -0500 | [diff] [blame] | 19 | import tempest_lib.exceptions |
Matthew Treinish | 96e9e88 | 2014-06-09 18:37:19 -0400 | [diff] [blame] | 20 | |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 21 | from tempest import config |
Masayuki Igawa | 7e9eb54 | 2014-02-17 15:03:44 +0900 | [diff] [blame] | 22 | from tempest import exceptions |
Daryl Walleck | 98e66dd | 2012-06-21 04:58:39 -0500 | [diff] [blame] | 23 | |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 24 | CONF = config.CONF |
| 25 | |
David Kranz | 968f1b3 | 2015-06-18 16:58:18 -0400 | [diff] [blame] | 26 | LOG = logging.getLogger(__name__) |
| 27 | |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 28 | |
Joe Gordon | 28788b4 | 2015-02-25 12:42:37 -0800 | [diff] [blame] | 29 | class RemoteClient(object): |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 30 | |
Sean Dague | 20e9861 | 2016-01-06 14:33:28 -0500 | [diff] [blame] | 31 | def __init__(self, ip_address, username, password=None, pkey=None): |
nithya-ganesan | 67da287 | 2015-02-08 23:13:48 +0000 | [diff] [blame] | 32 | ssh_timeout = CONF.validation.ssh_timeout |
nithya-ganesan | 67da287 | 2015-02-08 23:13:48 +0000 | [diff] [blame] | 33 | connect_timeout = CONF.validation.connect_timeout |
Sean Dague | 20e9861 | 2016-01-06 14:33:28 -0500 | [diff] [blame] | 34 | |
Masayuki Igawa | 7e9eb54 | 2014-02-17 15:03:44 +0900 | [diff] [blame] | 35 | self.ssh_client = ssh.Client(ip_address, username, password, |
| 36 | ssh_timeout, pkey=pkey, |
nithya-ganesan | 67da287 | 2015-02-08 23:13:48 +0000 | [diff] [blame] | 37 | channel_timeout=connect_timeout) |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 38 | |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 39 | def exec_command(self, cmd): |
Evgeny Antyshev | f58ab6d | 2015-04-15 08:23:05 +0000 | [diff] [blame] | 40 | # Shell options below add more clearness on failures, |
| 41 | # path is extended for some non-cirros guest oses (centos7) |
lanoux | 283273b | 2015-12-04 03:01:54 -0800 | [diff] [blame] | 42 | cmd = CONF.validation.ssh_shell_prologue + " " + cmd |
David Kranz | 968f1b3 | 2015-06-18 16:58:18 -0400 | [diff] [blame] | 43 | LOG.debug("Remote command: %s" % cmd) |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 44 | return self.ssh_client.exec_command(cmd) |
| 45 | |
Attila Fazekas | ad7ef7d | 2013-11-20 10:12:53 +0100 | [diff] [blame] | 46 | def validate_authentication(self): |
| 47 | """Validate ssh connection and authentication |
Ken'ichi Ohmichi | cb67d2d | 2015-11-19 08:23:22 +0000 | [diff] [blame] | 48 | |
Attila Fazekas | ad7ef7d | 2013-11-20 10:12:53 +0100 | [diff] [blame] | 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" |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 55 | actual_hostname = self.exec_command("hostname").rstrip() |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 56 | return expected_hostname == actual_hostname |
| 57 | |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 58 | def get_ram_size_in_mb(self): |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 59 | output = self.exec_command('free -m | grep Mem') |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 60 | if output: |
| 61 | return output.split()[1] |
| 62 | |
| 63 | def get_number_of_vcpus(self): |
Mathieu Gagné | 2020d6e | 2015-12-10 19:52:12 -0500 | [diff] [blame] | 64 | output = self.exec_command('grep -c ^processor /proc/cpuinfo') |
Daryl Walleck | 6b9b288 | 2012-04-08 21:43:39 -0500 | [diff] [blame] | 65 | return int(output) |
Dan Smith | c18d8c6 | 2012-07-02 08:09:26 -0700 | [diff] [blame] | 66 | |
| 67 | def get_partitions(self): |
| 68 | # Return the contents of /proc/partitions |
| 69 | command = 'cat /proc/partitions' |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 70 | output = self.exec_command(command) |
Dan Smith | c18d8c6 | 2012-07-02 08:09:26 -0700 | [diff] [blame] | 71 | return output |
Daryl Walleck | 98e66dd | 2012-06-21 04:58:39 -0500 | [diff] [blame] | 72 | |
| 73 | def get_boot_time(self): |
Vincent Untz | 3c0b5b9 | 2014-01-18 10:56:00 +0100 | [diff] [blame] | 74 | cmd = 'cut -f1 -d. /proc/uptime' |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 75 | boot_secs = self.exec_command(cmd) |
Vincent Untz | 3c0b5b9 | 2014-01-18 10:56:00 +0100 | [diff] [blame] | 76 | boot_time = time.time() - int(boot_secs) |
| 77 | return time.localtime(boot_time) |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 78 | |
| 79 | def write_to_console(self, message): |
| 80 | message = re.sub("([$\\`])", "\\\\\\\\\\1", message) |
| 81 | # usually to /dev/ttyS0 |
| 82 | cmd = 'sudo sh -c "echo \\"%s\\" >/dev/console"' % message |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 83 | return self.exec_command(cmd) |
Yair Fried | 5f670ab | 2013-12-09 09:26:51 +0200 | [diff] [blame] | 84 | |
lanoux | 283273b | 2015-12-04 03:01:54 -0800 | [diff] [blame] | 85 | def ping_host(self, host, count=CONF.validation.ping_count, |
| 86 | size=CONF.validation.ping_size, nic=None): |
Kirill Shileev | 1411357 | 2014-11-21 16:58:02 +0300 | [diff] [blame] | 87 | addr = netaddr.IPAddress(host) |
| 88 | cmd = 'ping6' if addr.version == 6 else 'ping' |
Yair Fried | bc46f59 | 2015-11-18 16:29:34 +0200 | [diff] [blame] | 89 | if nic: |
| 90 | cmd = 'sudo {cmd} -I {nic}'.format(cmd=cmd, nic=nic) |
Richard Winters | f87059b | 2015-02-17 11:46:54 -0500 | [diff] [blame] | 91 | cmd += ' -c{0} -w{0} -s{1} {2}'.format(count, size, host) |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 92 | return self.exec_command(cmd) |
Yair Fried | 4d7efa6 | 2013-11-17 17:12:29 +0200 | [diff] [blame] | 93 | |
Yair Fried | bc46f59 | 2015-11-18 16:29:34 +0200 | [diff] [blame] | 94 | def set_mac_address(self, nic, address): |
| 95 | self.set_nic_state(nic=nic, state="down") |
| 96 | cmd = "sudo ip link set dev {0} address {1}".format(nic, address) |
| 97 | self.exec_command(cmd) |
| 98 | self.set_nic_state(nic=nic, state="up") |
| 99 | |
| 100 | def get_mac_address(self, nic=""): |
| 101 | show_nic = "show {nic} ".format(nic=nic) if nic else "" |
| 102 | cmd = "ip addr %s| awk '/ether/ {print $2}'" % show_nic |
| 103 | return self.exec_command(cmd).strip().lower() |
Yair Fried | 3097dc1 | 2014-01-26 08:46:43 +0200 | [diff] [blame] | 104 | |
Yair Fried | 413bf2d | 2014-11-19 17:07:11 +0200 | [diff] [blame] | 105 | def get_nic_name(self, address): |
Evgeny Antyshev | f58ab6d | 2015-04-15 08:23:05 +0000 | [diff] [blame] | 106 | cmd = "ip -o addr | awk '/%s/ {print $2}'" % address |
Daniel Mellado | 9e3e106 | 2015-08-06 18:07:05 +0200 | [diff] [blame] | 107 | nic = self.exec_command(cmd) |
| 108 | return nic.strip().strip(":").lower() |
Yair Fried | 413bf2d | 2014-11-19 17:07:11 +0200 | [diff] [blame] | 109 | |
Yair Fried | 3097dc1 | 2014-01-26 08:46:43 +0200 | [diff] [blame] | 110 | def get_ip_list(self): |
Evgeny Antyshev | f58ab6d | 2015-04-15 08:23:05 +0000 | [diff] [blame] | 111 | cmd = "ip address" |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 112 | return self.exec_command(cmd) |
Yair Fried | 3097dc1 | 2014-01-26 08:46:43 +0200 | [diff] [blame] | 113 | |
| 114 | def assign_static_ip(self, nic, addr): |
Evgeny Antyshev | f58ab6d | 2015-04-15 08:23:05 +0000 | [diff] [blame] | 115 | cmd = "sudo ip addr add {ip}/{mask} dev {nic}".format( |
Yair Fried | 3097dc1 | 2014-01-26 08:46:43 +0200 | [diff] [blame] | 116 | ip=addr, mask=CONF.network.tenant_network_mask_bits, |
| 117 | nic=nic |
| 118 | ) |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 119 | return self.exec_command(cmd) |
Yair Fried | 3097dc1 | 2014-01-26 08:46:43 +0200 | [diff] [blame] | 120 | |
Yair Fried | bc46f59 | 2015-11-18 16:29:34 +0200 | [diff] [blame] | 121 | def set_nic_state(self, nic, state="up"): |
| 122 | cmd = "sudo ip link set {nic} {state}".format(nic=nic, state=state) |
Elena Ezhova | 91db24e | 2014-02-28 20:47:10 +0400 | [diff] [blame] | 123 | return self.exec_command(cmd) |
Elena Ezhova | 4a27b46 | 2014-04-09 15:25:46 +0400 | [diff] [blame] | 124 | |
| 125 | def get_pids(self, pr_name): |
| 126 | # Get pid(s) of a process/program |
| 127 | cmd = "ps -ef | grep %s | grep -v 'grep' | awk {'print $1'}" % pr_name |
| 128 | return self.exec_command(cmd).split('\n') |
Yair Fried | 413bf2d | 2014-11-19 17:07:11 +0200 | [diff] [blame] | 129 | |
| 130 | def get_dns_servers(self): |
| 131 | cmd = 'cat /etc/resolv.conf' |
| 132 | resolve_file = self.exec_command(cmd).strip().split('\n') |
| 133 | entries = (l.split() for l in resolve_file) |
| 134 | dns_servers = [l[1] for l in entries |
| 135 | if len(l) and l[0] == 'nameserver'] |
| 136 | return dns_servers |
| 137 | |
| 138 | def send_signal(self, pid, signum): |
| 139 | cmd = 'sudo /bin/kill -{sig} {pid}'.format(pid=pid, sig=signum) |
| 140 | return self.exec_command(cmd) |
| 141 | |
| 142 | def _renew_lease_udhcpc(self, fixed_ip=None): |
| 143 | """Renews DHCP lease via udhcpc client. """ |
| 144 | file_path = '/var/run/udhcpc.' |
| 145 | nic_name = self.get_nic_name(fixed_ip) |
Yair Fried | 413bf2d | 2014-11-19 17:07:11 +0200 | [diff] [blame] | 146 | pid = self.exec_command('cat {path}{nic}.pid'. |
| 147 | format(path=file_path, nic=nic_name)) |
| 148 | pid = pid.strip() |
| 149 | self.send_signal(pid, 'USR1') |
| 150 | |
| 151 | def _renew_lease_dhclient(self, fixed_ip=None): |
| 152 | """Renews DHCP lease via dhclient client. """ |
Itzik Brown | ffb1402 | 2015-03-23 17:03:55 +0200 | [diff] [blame] | 153 | cmd = "sudo /sbin/dhclient -r && sudo /sbin/dhclient" |
Yair Fried | 413bf2d | 2014-11-19 17:07:11 +0200 | [diff] [blame] | 154 | self.exec_command(cmd) |
| 155 | |
| 156 | def renew_lease(self, fixed_ip=None): |
| 157 | """Wrapper method for renewing DHCP lease via given client |
| 158 | |
| 159 | Supporting: |
| 160 | * udhcpc |
| 161 | * dhclient |
| 162 | """ |
| 163 | # TODO(yfried): add support for dhcpcd |
Takashi NATSUME | 6d5a2b4 | 2015-09-08 11:27:49 +0900 | [diff] [blame] | 164 | supported_clients = ['udhcpc', 'dhclient'] |
Yair Fried | 413bf2d | 2014-11-19 17:07:11 +0200 | [diff] [blame] | 165 | dhcp_client = CONF.scenario.dhcp_client |
Takashi NATSUME | 6d5a2b4 | 2015-09-08 11:27:49 +0900 | [diff] [blame] | 166 | if dhcp_client not in supported_clients: |
Yair Fried | 413bf2d | 2014-11-19 17:07:11 +0200 | [diff] [blame] | 167 | raise exceptions.InvalidConfiguration('%s DHCP client unsupported' |
| 168 | % dhcp_client) |
| 169 | if dhcp_client == 'udhcpc' and not fixed_ip: |
| 170 | raise ValueError("need to set 'fixed_ip' for udhcpc client") |
Joe Gordon | 28788b4 | 2015-02-25 12:42:37 -0800 | [diff] [blame] | 171 | return getattr(self, '_renew_lease_' + dhcp_client)(fixed_ip=fixed_ip) |
Alexander Gubanov | abd154c | 2015-09-23 23:24:06 +0300 | [diff] [blame] | 172 | |
| 173 | def mount(self, dev_name, mount_path='/mnt'): |
| 174 | cmd_mount = 'sudo mount /dev/%s %s' % (dev_name, mount_path) |
| 175 | self.exec_command(cmd_mount) |
| 176 | |
| 177 | def umount(self, mount_path='/mnt'): |
| 178 | self.exec_command('sudo umount %s' % mount_path) |
| 179 | |
| 180 | def make_fs(self, dev_name, fs='ext4'): |
| 181 | cmd_mkfs = 'sudo /usr/sbin/mke2fs -t %s /dev/%s' % (fs, dev_name) |
Sean Dague | 57c6655 | 2016-02-08 08:51:13 -0500 | [diff] [blame] | 182 | try: |
| 183 | self.exec_command(cmd_mkfs) |
| 184 | except tempest_lib.exceptions.SSHExecCommandFailed: |
| 185 | LOG.error("Couldn't mke2fs") |
| 186 | cmd_why = 'sudo ls -lR /dev' |
| 187 | LOG.info("Contents of /dev: %s" % self.exec_command(cmd_why)) |
| 188 | raise |