Ken'ichi Ohmichi | d25a1a3 | 2017-03-01 13:40:35 -0800 | [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 | |
lkuchlan | d13acd1 | 2017-12-27 15:24:47 +0200 | [diff] [blame] | 13 | import functools |
Ken'ichi Ohmichi | d25a1a3 | 2017-03-01 13:40:35 -0800 | [diff] [blame] | 14 | import sys |
| 15 | |
| 16 | import netaddr |
| 17 | from oslo_log import log as logging |
Ken'ichi Ohmichi | d25a1a3 | 2017-03-01 13:40:35 -0800 | [diff] [blame] | 18 | |
| 19 | from tempest.lib.common import ssh |
| 20 | from tempest.lib.common.utils import test_utils |
| 21 | import tempest.lib.exceptions |
| 22 | |
| 23 | LOG = logging.getLogger(__name__) |
| 24 | |
| 25 | |
| 26 | def debug_ssh(function): |
| 27 | """Decorator to generate extra debug info in case off SSH failure""" |
lkuchlan | d13acd1 | 2017-12-27 15:24:47 +0200 | [diff] [blame] | 28 | @functools.wraps(function) |
Ken'ichi Ohmichi | d25a1a3 | 2017-03-01 13:40:35 -0800 | [diff] [blame] | 29 | def wrapper(self, *args, **kwargs): |
| 30 | try: |
| 31 | return function(self, *args, **kwargs) |
zhufl | f52c759 | 2017-05-25 13:55:24 +0800 | [diff] [blame] | 32 | except Exception as e: |
| 33 | caller = test_utils.find_test_caller() or "not found" |
| 34 | if not isinstance(e, tempest.lib.exceptions.SSHTimeout): |
zhufl | 35422ac | 2017-11-13 16:54:55 +0800 | [diff] [blame] | 35 | message = ('Executing command on %(ip)s failed. ' |
zhufl | f52c759 | 2017-05-25 13:55:24 +0800 | [diff] [blame] | 36 | 'Error: %(error)s' % {'ip': self.ip_address, |
| 37 | 'error': e}) |
| 38 | message = '(%s) %s' % (caller, message) |
| 39 | LOG.error(message) |
| 40 | raise |
| 41 | else: |
| 42 | try: |
| 43 | original_exception = sys.exc_info() |
| 44 | if self.server: |
| 45 | msg = 'Caller: %s. Timeout trying to ssh to server %s' |
| 46 | LOG.debug(msg, caller, self.server) |
| 47 | if self.console_output_enabled and self.servers_client: |
| 48 | try: |
| 49 | msg = 'Console log for server %s: %s' |
| 50 | console_log = ( |
| 51 | self.servers_client.get_console_output( |
| 52 | self.server['id'])['output']) |
| 53 | LOG.debug(msg, self.server['id'], console_log) |
| 54 | except Exception: |
| 55 | msg = 'Could not get console_log for server %s' |
| 56 | LOG.debug(msg, self.server['id']) |
songwenping | e662307 | 2021-02-22 14:47:34 +0800 | [diff] [blame] | 57 | # raise the original ssh timeout exception |
| 58 | raise |
zhufl | f52c759 | 2017-05-25 13:55:24 +0800 | [diff] [blame] | 59 | finally: |
| 60 | # Delete the traceback to avoid circular references |
| 61 | _, _, trace = original_exception |
| 62 | del trace |
Ken'ichi Ohmichi | d25a1a3 | 2017-03-01 13:40:35 -0800 | [diff] [blame] | 63 | return wrapper |
| 64 | |
| 65 | |
| 66 | class RemoteClient(object): |
| 67 | |
| 68 | def __init__(self, ip_address, username, password=None, pkey=None, |
| 69 | server=None, servers_client=None, ssh_timeout=300, |
| 70 | connect_timeout=60, console_output_enabled=True, |
Kris Stercxk | d8152de | 2017-07-30 09:44:12 +0200 | [diff] [blame] | 71 | ssh_shell_prologue="set -eu -o pipefail; PATH=$PATH:/sbin;", |
Ade Lee | 6ded070 | 2021-09-04 15:56:34 -0400 | [diff] [blame] | 72 | ping_count=1, ping_size=56, ssh_key_type='rsa'): |
Ken'ichi Ohmichi | d25a1a3 | 2017-03-01 13:40:35 -0800 | [diff] [blame] | 73 | """Executes commands in a VM over ssh |
| 74 | |
| 75 | :param ip_address: IP address to ssh to |
| 76 | :param username: Ssh username |
| 77 | :param password: Ssh password |
| 78 | :param pkey: Ssh public key |
| 79 | :param server: Server dict, used for debugging purposes |
| 80 | :param servers_client: Servers client, used for debugging purposes |
| 81 | :param ssh_timeout: Timeout in seconds to wait for the ssh banner |
| 82 | :param connect_timeout: Timeout in seconds to wait for TCP connection |
| 83 | :param console_output_enabled: Support serial console output? |
| 84 | :param ssh_shell_prologue: Shell fragments to use before command |
| 85 | :param ping_count: Number of ping packets |
| 86 | :param ping_size: Packet size for ping packets |
Ade Lee | 6ded070 | 2021-09-04 15:56:34 -0400 | [diff] [blame] | 87 | :param ssh_key_type: ssh key type (rsa, ecdsa) |
Ken'ichi Ohmichi | d25a1a3 | 2017-03-01 13:40:35 -0800 | [diff] [blame] | 88 | """ |
| 89 | self.server = server |
| 90 | self.servers_client = servers_client |
zhufl | f52c759 | 2017-05-25 13:55:24 +0800 | [diff] [blame] | 91 | self.ip_address = ip_address |
Ken'ichi Ohmichi | d25a1a3 | 2017-03-01 13:40:35 -0800 | [diff] [blame] | 92 | self.console_output_enabled = console_output_enabled |
| 93 | self.ssh_shell_prologue = ssh_shell_prologue |
| 94 | self.ping_count = ping_count |
| 95 | self.ping_size = ping_size |
Ade Lee | 6ded070 | 2021-09-04 15:56:34 -0400 | [diff] [blame] | 96 | self.ssh_key_type = ssh_key_type |
Ken'ichi Ohmichi | d25a1a3 | 2017-03-01 13:40:35 -0800 | [diff] [blame] | 97 | |
| 98 | self.ssh_client = ssh.Client(ip_address, username, password, |
| 99 | ssh_timeout, pkey=pkey, |
Ade Lee | 6ded070 | 2021-09-04 15:56:34 -0400 | [diff] [blame] | 100 | channel_timeout=connect_timeout, |
| 101 | ssh_key_type=ssh_key_type) |
Ken'ichi Ohmichi | d25a1a3 | 2017-03-01 13:40:35 -0800 | [diff] [blame] | 102 | |
| 103 | @debug_ssh |
| 104 | def exec_command(self, cmd): |
| 105 | # Shell options below add more clearness on failures, |
| 106 | # path is extended for some non-cirros guest oses (centos7) |
| 107 | cmd = self.ssh_shell_prologue + " " + cmd |
| 108 | LOG.debug("Remote command: %s", cmd) |
| 109 | return self.ssh_client.exec_command(cmd) |
| 110 | |
| 111 | @debug_ssh |
| 112 | def validate_authentication(self): |
| 113 | """Validate ssh connection and authentication |
| 114 | |
| 115 | This method raises an Exception when the validation fails. |
| 116 | """ |
| 117 | self.ssh_client.test_connection_auth() |
| 118 | |
| 119 | def ping_host(self, host, count=None, size=None, nic=None): |
| 120 | if count is None: |
| 121 | count = self.ping_count |
| 122 | if size is None: |
| 123 | size = self.ping_size |
| 124 | |
| 125 | addr = netaddr.IPAddress(host) |
| 126 | cmd = 'ping6' if addr.version == 6 else 'ping' |
| 127 | if nic: |
| 128 | cmd = 'sudo {cmd} -I {nic}'.format(cmd=cmd, nic=nic) |
| 129 | cmd += ' -c{0} -w{0} -s{1} {2}'.format(count, size, host) |
| 130 | return self.exec_command(cmd) |
Mohammed Naser | b6c6d2a | 2017-12-29 18:13:29 -0500 | [diff] [blame] | 131 | |
| 132 | def mount_config_drive(self): |
| 133 | """Mount the config drive inside a virtual machine |
| 134 | |
| 135 | This method will not unmount the config drive, so unmount_config_drive |
| 136 | must be used for cleanup. |
| 137 | """ |
Lee Yarwood | 99b7c11 | 2020-03-02 20:04:49 +0000 | [diff] [blame] | 138 | cmd_blkid = 'blkid -L config-2 -o device' |
| 139 | dev_name = self.exec_command(cmd_blkid).strip() |
Mohammed Naser | b6c6d2a | 2017-12-29 18:13:29 -0500 | [diff] [blame] | 140 | |
| 141 | try: |
| 142 | self.exec_command('sudo mount %s /mnt' % dev_name) |
| 143 | except tempest.lib.exceptions.SSHExecCommandFailed: |
| 144 | # So the command failed, let's try to know why and print some |
| 145 | # useful information. |
| 146 | lsblk = self.exec_command('sudo lsblk --fs --ascii') |
| 147 | LOG.error("Mounting %s on /mnt failed. Right after the " |
| 148 | "failure 'lsblk' in the guest reported:\n%s", |
| 149 | dev_name, lsblk) |
| 150 | raise |
| 151 | |
| 152 | def unmount_config_drive(self): |
| 153 | self.exec_command('sudo umount /mnt') |