koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 1 | import socket |
| 2 | import logging |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 3 | from typing import Dict, Any, Tuple, List |
| 4 | from urllib.parse import urlparse |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 5 | |
| 6 | |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 7 | from .. import fuel_rest_api |
| 8 | from ..utils import parse_creds, check_input_param |
| 9 | from ..node import NodeInfo, Node, FuelNodeInfo |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 10 | |
| 11 | |
| 12 | logger = logging.getLogger("wally.discover") |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 13 | |
| 14 | |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 15 | def 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 kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 21 | username, tenant_name, password = parse_creds(fuel_data['creds']) |
| 22 | creds = {"username": username, |
| 23 | "tenant_name": tenant_name, |
| 24 | "password": password} |
| 25 | |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 26 | # connect to FUEL |
| 27 | conn = fuel_rest_api.KeystoneAuth(fuel_data['url'], creds, headers=None) |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 28 | msg = "openstack_env should be provided in fuel config" |
| 29 | check_input_param('openstack_env' in fuel_data, msg) |
| 30 | |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 31 | # 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 kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 35 | |
| 36 | if not discover_nodes: |
| 37 | logger.warning("Skip fuel cluster discovery") |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 38 | return [], FuelNodeInfo(version, None, cluster.get_openrc()) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 39 | |
| 40 | fuel_nodes = list(cluster.get_nodes()) |
| 41 | |
koder aka kdanilov | 416b87a | 2015-05-12 00:26:04 +0300 | [diff] [blame] | 42 | logger.info("Found FUEL {0}".format(".".join(map(str, version)))) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 43 | |
| 44 | network = 'fuelweb_admin' if version >= [6, 0] else 'admin' |
| 45 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 46 | fuel_host = urlparse(fuel_data['url']).hostname |
| 47 | fuel_ip = socket.gethostbyname(fuel_host) |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 48 | fuel_ext_iface = fuel_master_node.get_interface(fuel_ip) |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 49 | |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 50 | # get FUEL master key to connect to cluster nodes via ssh |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 51 | logger.debug("Downloading fuel master key") |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 52 | 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 kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 59 | |
| 60 | nodes = [] |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 61 | for port, fuel_node, ip in zip(port_fw, fuel_nodes, ips): |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 62 | 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 kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 65 | |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 66 | logger.debug("Found {} fuel nodes for env {}".format(len(nodes), fuel_data['openstack_env'])) |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 67 | |
koder aka kdanilov | 3b4da8b | 2016-10-17 00:17:53 +0300 | [diff] [blame] | 68 | return nodes, FuelNodeInfo(version, fuel_ext_iface, cluster.get_openrc()) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 69 | |