blob: 1443a9fe1c912073cf9322e2e11895ef718d4240 [file] [log] [blame]
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +03001import socket
2import logging
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +03003from typing import Dict, Any, Tuple, List
4from urllib.parse import urlparse
koder aka kdanilovf86d7af2015-05-06 04:01:54 +03005
6
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +03007from .. import fuel_rest_api
8from ..utils import parse_creds, check_input_param
9from ..node import NodeInfo, Node, FuelNodeInfo
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030010
11
12logger = logging.getLogger("wally.discover")
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030013
14
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030015def discover_fuel_nodes(fuel_master_node: Node,
16 fuel_data: Dict[str, Any],
17 discover_nodes: bool=True) -> Tuple[List[NodeInfo], FuelNodeInfo]:
18 """Discover nodes in fuel cluster, get openrc for selected cluster"""
19
20 # parse FUEL REST credentials
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030021 username, tenant_name, password = parse_creds(fuel_data['creds'])
22 creds = {"username": username,
23 "tenant_name": tenant_name,
24 "password": password}
25
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030026 # connect to FUEL
27 conn = fuel_rest_api.KeystoneAuth(fuel_data['url'], creds, headers=None)
koder aka kdanilovf86d7af2015-05-06 04:01:54 +030028 msg = "openstack_env should be provided in fuel config"
29 check_input_param('openstack_env' in fuel_data, msg)
30
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030031 # get cluster information from REST API
32 cluster_id = fuel_rest_api.get_cluster_id(conn, fuel_data['openstack_env'])
33 cluster = fuel_rest_api.reflect_cluster(conn, cluster_id)
34 version = fuel_rest_api.FuelInfo(conn).get_version()
koder aka kdanilove87ae652015-04-20 02:14:35 +030035
36 if not discover_nodes:
37 logger.warning("Skip fuel cluster discovery")
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030038 return [], FuelNodeInfo(version, None, cluster.get_openrc())
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030039
40 fuel_nodes = list(cluster.get_nodes())
41
koder aka kdanilov416b87a2015-05-12 00:26:04 +030042 logger.info("Found FUEL {0}".format(".".join(map(str, version))))
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030043
44 network = 'fuelweb_admin' if version >= [6, 0] else 'admin'
45
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030046 fuel_host = urlparse(fuel_data['url']).hostname
47 fuel_ip = socket.gethostbyname(fuel_host)
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030048 fuel_ext_iface = fuel_master_node.get_interface(fuel_ip)
koder aka kdanilovf86d7af2015-05-06 04:01:54 +030049
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030050 # get FUEL master key to connect to cluster nodes via ssh
koder aka kdanilovf86d7af2015-05-06 04:01:54 +030051 logger.debug("Downloading fuel master key")
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030052 fuel_key = fuel_master_node.get_file_content('/root/.ssh/id_rsa')
53
54 # forward ports of cluster nodes to FUEL master
55 logger.info("Forwarding ssh ports from FUEL nodes to localhost")
56 ips = [str(fuel_node.get_ip(network)) for fuel_node in fuel_nodes]
57 port_fw = [fuel_master_node.forward_port(ip, 22) for ip in ips]
58 listen_ip = fuel_master_node.get_ip()
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030059
60 nodes = []
koder aka kdanilovf86d7af2015-05-06 04:01:54 +030061 for port, fuel_node, ip in zip(port_fw, fuel_nodes, ips):
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030062 logger.debug("SSH port forwarding {} => {}:{}".format(ip, listen_ip, port))
63 conn_url = "ssh://root@{}:{}".format(listen_ip, port)
64 nodes.append(NodeInfo(conn_url, fuel_node['roles'], listen_ip, fuel_key))
koder aka kdanilovf86d7af2015-05-06 04:01:54 +030065
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030066 logger.debug("Found {} fuel nodes for env {}".format(len(nodes), fuel_data['openstack_env']))
koder aka kdanilovf86d7af2015-05-06 04:01:54 +030067
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030068 return nodes, FuelNodeInfo(version, fuel_ext_iface, cluster.get_openrc())
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030069