blob: f5903592abc595acee359c5e6e33184ee151423f [file] [log] [blame]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +03001import socket
2import logging
koder aka kdanilov70227062016-11-26 23:23:21 +02003from typing import Dict, Any, List, Optional, cast
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +03004
koder aka kdanilov73084622016-11-16 21:51:08 +02005from ..node_interfaces import NodeInfo
6from ..config import ConfigBlock
koder aka kdanilov70227062016-11-26 23:23:21 +02007from ..ssh_utils import ConnCreds
8from ..start_vms import OSConnection, NovaClient
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +03009
10
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030011logger = logging.getLogger("wally.discover")
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030012
13
koder aka kdanilov73084622016-11-16 21:51:08 +020014def get_floating_ip(vm: Any) -> str:
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030015 """Get VM floating IP address"""
16
17 for net_name, ifaces in vm.addresses.items():
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030018 for iface in ifaces:
19 if iface.get('OS-EXT-IPS:type') == "floating":
20 return iface['addr']
21
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030022 raise ValueError("VM {} has no floating ip".format(vm))
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030023
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030024
koder aka kdanilov70227062016-11-26 23:23:21 +020025def discover_vms(client: NovaClient, search_data: str) -> List[NodeInfo]:
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030026 """Discover virtual machines"""
koder aka kdanilov70227062016-11-26 23:23:21 +020027 name, user, key_file = search_data.split(",")
28 servers = client.servers.list(search_opts={"name": name})
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030029 logger.debug("Found %s openstack vms" % len(servers))
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030030
koder aka kdanilov73084622016-11-16 21:51:08 +020031 nodes = [] # type: List[NodeInfo]
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030032 for server in servers:
33 ip = get_floating_ip(server)
koder aka kdanilov70227062016-11-26 23:23:21 +020034 creds = ConnCreds(host=ip, user=user, key_file=key_file)
35 nodes.append(NodeInfo(creds, roles={"test_vm"}))
36
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030037 return nodes
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030038
39
koder aka kdanilov70227062016-11-26 23:23:21 +020040def discover_openstack_nodes(conn: OSConnection, conf: ConfigBlock) -> List[NodeInfo]:
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030041 """Discover openstack services for given cluster"""
koder aka kdanilov70227062016-11-26 23:23:21 +020042 os_nodes_auth = conf['auth'] # type: str
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030043
koder aka kdanilov70227062016-11-26 23:23:21 +020044 if os_nodes_auth.count(":") == 2:
45 user, password, key_file = os_nodes_auth.split(":") # type: str, Optional[str], Optional[str]
46 if not password:
47 password = None
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030048 else:
koder aka kdanilov70227062016-11-26 23:23:21 +020049 user, password = os_nodes_auth.split(":")
50 key_file = None
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030051
koder aka kdanilov70227062016-11-26 23:23:21 +020052 services = conn.nova.services.list() # type: List[Any]
53 host_services_mapping = {} # type: Dict[str, List[str]]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030054
55 for service in services:
koder aka kdanilov70227062016-11-26 23:23:21 +020056 ip = cast(str, socket.gethostbyname(service.host))
koder aka kdanilov73084622016-11-16 21:51:08 +020057 host_services_mapping.get(ip, []).append(service.binary)
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030058
koder aka kdanilov70227062016-11-26 23:23:21 +020059 logger.debug("Found %s openstack service nodes" % len(host_services_mapping))
koder aka kdanilov73084622016-11-16 21:51:08 +020060
61 nodes = [] # type: List[NodeInfo]
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030062 for host, services in host_services_mapping.items():
koder aka kdanilov70227062016-11-26 23:23:21 +020063 creds = ConnCreds(host=host, user=user, passwd=password, key_file=key_file)
64 nodes.append(NodeInfo(creds, set(services)))
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030065
66 return nodes