Merge "Update Nova client"

This commit is contained in:
Zuul 2019-11-12 21:38:20 +00:00 committed by Gerrit Code Review
commit 1640aaff02
3 changed files with 115 additions and 32 deletions

View File

@ -19,17 +19,19 @@ from tobiko.openstack.nova import _server
CLIENT_CLASSES = _client.CLIENT_CLASSES CLIENT_CLASSES = _client.CLIENT_CLASSES
get_console_output = _client.get_console_output
get_nova_client = _client.get_nova_client get_nova_client = _client.get_nova_client
get_server = _client.get_server
find_hypervisor = _client.find_hypervisor
find_server = _client.find_server
HasNovaClientMixin = _client.HasNovaClientMixin
list_hypervisors = _client.list_hypervisors list_hypervisors = _client.list_hypervisors
list_servers = _client.list_servers list_servers = _client.list_servers
find_server = _client.find_server
nova_client = _client.nova_client nova_client = _client.nova_client
NovaClientFixture = _client.NovaClientFixture NovaClientFixture = _client.NovaClientFixture
find_hypervisor = _client.find_hypervisor
get_console_output = _client.get_console_output
get_server = _client.get_server
skip_if_missing_hypervisors = _hypervisor.skip_if_missing_hypervisors skip_if_missing_hypervisors = _hypervisor.skip_if_missing_hypervisors
find_server_ip_address = _server.find_server_ip_address find_server_ip_address = _server.find_server_ip_address
HasServerMixin = _server.HasServerMixin
list_server_ip_addresses = _server.list_server_ip_addresses list_server_ip_addresses = _server.list_server_ip_addresses

View File

@ -94,14 +94,18 @@ def find_server(client=None, unique=False, **params):
return servers.first return servers.first
def get_server(server, client=None): def get_server(server, client=None, **params):
return nova_client(client).servers.get(server) return nova_client(client).servers.get(server, **params)
MAX_SERVER_CONSOLE_OUTPUT_LENGTH = 1024 * 256
def get_console_output(server, timeout=None, interval=1., length=None, def get_console_output(server, timeout=None, interval=1., length=None,
client=None): client=None):
client = nova_client(client) client = nova_client(client)
start_time = time.time() start_time = time.time()
length = length or MAX_SERVER_CONSOLE_OUTPUT_LENGTH
while True: while True:
try: try:
output = client.servers.get_console_output(server=server, output = client.servers.get_console_output(server=server,
@ -124,3 +128,15 @@ def get_console_output(server, timeout=None, interval=1., length=None,
time.sleep(interval) time.sleep(interval)
return output return output
class HasNovaClientMixin(object):
nova_client = None
def get_server(self, server, **params):
return get_server(server=server, client=self.nova_client, **params)
def get_server_console_output(self, server, **params):
return get_console_output(server=server, client=self.nova_client,
**params)

View File

@ -17,6 +17,8 @@ from oslo_log import log
import netaddr import netaddr
import tobiko import tobiko
from tobiko.openstack.nova import _client
from tobiko.shell import ssh
from tobiko.shell import ping from tobiko.shell import ping
@ -25,43 +27,106 @@ LOG = log.getLogger(__name__)
def list_server_ip_addresses(server, network_name=None, ip_version=None, def list_server_ip_addresses(server, network_name=None, ip_version=None,
address_type=None, check_connectivity=False, address_type=None, check_connectivity=False,
ssh_client=None): ssh_client=None, count=None):
selected_addresses = [] ips = tobiko.Selection()
for _network_name, addresses in server.addresses.items(): for _network_name, addresses in server.addresses.items():
if count and len(ips) == count:
break
# check network name # check network name
if network_name and network_name != _network_name: if network_name and network_name != _network_name:
continue continue
for address in addresses: for address in addresses:
# check IP address version if check_server_ip_address(address,
_ip_version = address['version'] ip_version=ip_version,
if ip_version and ip_version != _ip_version: address_type=address_type):
continue ips.append(netaddr.IPAddress(address['addr'],
version=address['version']))
# check IP address type # check ICMP connectivity
_address_type = address.get('OS-EXT-IPS:type') if check_connectivity:
if address_type and address_type != _address_type: ips = ping.list_reachable_hosts(ips, ssh_client=ssh_client)
if _address_type is None:
LOG.warning('Unable to get address type of address %r',
address)
continue
ip_address = netaddr.IPAddress(address['addr'], return ips
version=_ip_version)
# check ICMP connectivity
if check_connectivity and not ping.ping(
host=ip_address, ssh_client=ssh_client).received:
continue
selected_addresses.append(ip_address) def check_server_ip_address(address, ip_version=None, address_type=None):
if ip_version and ip_version != address['version']:
return False
return tobiko.Selection(selected_addresses) # check IP address type
if address_type:
try:
if address_type != address['OS-EXT-IPS:type']:
return False
except KeyError:
message = ("Unable to get IP type from server address {!r}"
).format(address)
raise ValueError(message)
return True
def find_server_ip_address(server, unique=False, **kwargs): def find_server_ip_address(server, unique=False, **kwargs):
addresses = list_server_ip_addresses(server=server, **kwargs) count = unique and 2 or 1
if unique: addresses = list_server_ip_addresses(server=server, count=count, **kwargs)
return addresses.unique return unique and addresses.unique or addresses.first
else:
return addresses.first
class HasServerMixin(_client.HasNovaClientMixin):
@property
def server_id(self):
raise NotImplementedError
@property
def server(self):
return _client.get_server(self.server_id)
@property
def server_name(self):
return self.server.name
@property
def server_ips(self):
return self.list_server_ips()
def list_server_ips(self, **kwargs):
return list_server_ip_addresses(server=self.server, **kwargs)
@property
def server_fixed_ips(self):
return self.list_server_ips(address_type='fixed', check=True)
@property
def server_floating_ip(self):
floating_ips = self.list_server_ips(address_type='floating', count=1)
if floating_ips:
return floating_ips.first
else:
return None
@property
def has_floating_ip(self):
return bool(self.server_floating_ip)
@property
def server_console_output(self):
return super(HasServerMixin, self).get_server_console_output(
server=self.server_id)
@property
def server_public_ip(self):
return self.server_floating_ip or self.server_fixed_ips.first
@property
def ssh_client(self):
return ssh.ssh_client(host=self.server_public_ip,
username=self.ssh_username,
password=self.ssh_password,
proxy_client=self.ssh_proxy_client)
ssh_proxy_client = None
ssh_username = None
ssh_password = None