blob: fa59e14c0e8363dc3f27af799d42e9badf8fb00f [file] [log] [blame]
Sean Dague556add52013-07-19 14:28:44 -04001# 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 Fazekasa23f5002012-10-23 19:32:45 +020015import re
Matthew Treinisha83a16e2012-12-07 13:44:02 -050016import time
17
18from tempest.common.ssh import Client
19from tempest.common import utils
Sean Dague86bd8422013-12-20 09:56:44 -050020from tempest import config
Matthew Treinisha83a16e2012-12-07 13:44:02 -050021from tempest.exceptions import ServerUnreachable
Daryl Walleck98e66dd2012-06-21 04:58:39 -050022
Sean Dague86bd8422013-12-20 09:56:44 -050023CONF = config.CONF
24
Daryl Walleck6b9b2882012-04-08 21:43:39 -050025
26class RemoteClient():
27
Attila Fazekasc3a095b2013-08-17 09:15:44 +020028 # NOTE(afazekas): It should always get an address instead of server
Attila Fazekasa23f5002012-10-23 19:32:45 +020029 def __init__(self, server, username, password=None, pkey=None):
Sean Dague86bd8422013-12-20 09:56:44 -050030 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
Attila Fazekasa23f5002012-10-23 19:32:45 +020034 if isinstance(server, basestring):
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:
43 raise ServerUnreachable()
Attila Fazekasa23f5002012-10-23 19:32:45 +020044 self.ssh_client = Client(ip_address, username, password, ssh_timeout,
Chris Yeoh76916042013-02-27 16:25:25 +103045 pkey=pkey,
46 channel_timeout=ssh_channel_timeout)
Daryl Walleck6b9b2882012-04-08 21:43:39 -050047
Attila Fazekasad7ef7d2013-11-20 10:12:53 +010048 def validate_authentication(self):
49 """Validate ssh connection and authentication
50 This method raises an Exception when the validation fails.
51 """
52 self.ssh_client.test_connection_auth()
Daryl Walleck6b9b2882012-04-08 21:43:39 -050053
54 def hostname_equals_servername(self, expected_hostname):
Chang Bo Guocc1623c2013-09-13 20:11:27 -070055 # Get host name using command "hostname"
Daryl Walleck6b9b2882012-04-08 21:43:39 -050056 actual_hostname = self.ssh_client.exec_command("hostname").rstrip()
57 return expected_hostname == actual_hostname
58
59 def get_files(self, path):
Chang Bo Guocc1623c2013-09-13 20:11:27 -070060 # Return a list of comma separated files
Daryl Walleck6b9b2882012-04-08 21:43:39 -050061 command = "ls -m " + path
62 return self.ssh_client.exec_command(command).rstrip('\n').split(', ')
63
64 def get_ram_size_in_mb(self):
65 output = self.ssh_client.exec_command('free -m | grep Mem')
66 if output:
67 return output.split()[1]
68
69 def get_number_of_vcpus(self):
70 command = 'cat /proc/cpuinfo | grep processor | wc -l'
71 output = self.ssh_client.exec_command(command)
72 return int(output)
Dan Smithc18d8c62012-07-02 08:09:26 -070073
74 def get_partitions(self):
75 # Return the contents of /proc/partitions
76 command = 'cat /proc/partitions'
77 output = self.ssh_client.exec_command(command)
78 return output
Daryl Walleck98e66dd2012-06-21 04:58:39 -050079
80 def get_boot_time(self):
81 cmd = 'date -d "`cut -f1 -d. /proc/uptime` seconds ago" \
82 "+%Y-%m-%d %H:%M:%S"'
83 boot_time_string = self.ssh_client.exec_command(cmd)
84 boot_time_string = boot_time_string.replace('\n', '')
85 return time.strptime(boot_time_string, utils.LAST_REBOOT_TIME_FORMAT)
Attila Fazekasa23f5002012-10-23 19:32:45 +020086
87 def write_to_console(self, message):
88 message = re.sub("([$\\`])", "\\\\\\\\\\1", message)
89 # usually to /dev/ttyS0
90 cmd = 'sudo sh -c "echo \\"%s\\" >/dev/console"' % message
91 return self.ssh_client.exec_command(cmd)
Yair Fried5f670ab2013-12-09 09:26:51 +020092
93 def ping_host(self, host):
94 cmd = 'ping -c1 -w1 %s' % host
95 return self.ssh_client.exec_command(cmd)
Yair Fried4d7efa62013-11-17 17:12:29 +020096
97 def get_mac_address(self):
98 cmd = "/sbin/ifconfig | awk '/HWaddr/ {print $5}'"
99 return self.ssh_client.exec_command(cmd)