| import logging |
| import socket |
| from typing import Dict, Any, Tuple, List, NamedTuple, Union |
| from urllib.parse import urlparse |
| |
| from .. import fuel_rest_api |
| from ..node_interfaces import NodeInfo, IRPCNode |
| from ..ssh_utils import ConnCreds |
| from ..utils import parse_creds, check_input_param |
| |
| logger = logging.getLogger("wally.discover") |
| |
| |
| FuelNodeInfo = NamedTuple("FuelNodeInfo", |
| [("version", List[int]), |
| ("fuel_ext_iface", str), |
| ("openrc", Dict[str, Union[str, bool]])]) |
| |
| |
| def discover_fuel_nodes(fuel_master_node: IRPCNode, |
| fuel_data: Dict[str, Any], |
| discover_nodes: bool = True) -> Tuple[List[NodeInfo], FuelNodeInfo]: |
| """Discover nodes in fuel cluster, get openrc for selected cluster""" |
| |
| # parse FUEL REST credentials |
| username, tenant_name, password = parse_creds(fuel_data['creds']) |
| creds = {"username": username, |
| "tenant_name": tenant_name, |
| "password": password} |
| |
| # connect to FUEL |
| conn = fuel_rest_api.KeystoneAuth(fuel_data['url'], creds, headers=None) |
| msg = "openstack_env should be provided in fuel config" |
| check_input_param('openstack_env' in fuel_data, msg) |
| |
| # get cluster information from REST API |
| cluster_id = fuel_rest_api.get_cluster_id(conn, fuel_data['openstack_env']) |
| cluster = fuel_rest_api.reflect_cluster(conn, cluster_id) |
| version = fuel_rest_api.FuelInfo(conn).get_version() |
| |
| if not discover_nodes: |
| logger.warning("Skip fuel cluster discovery") |
| return [], FuelNodeInfo(version, None, cluster.get_openrc()) |
| |
| fuel_nodes = list(cluster.get_nodes()) |
| |
| logger.info("Found FUEL {0}".format(".".join(map(str, version)))) |
| |
| network = 'fuelweb_admin' if version >= [6, 0] else 'admin' |
| |
| fuel_host = urlparse(fuel_data['url']).hostname |
| fuel_ip = socket.gethostbyname(fuel_host) |
| fuel_ext_iface = fuel_master_node.get_interface(fuel_ip) |
| |
| # get FUEL master key to connect to cluster nodes via ssh |
| logger.debug("Downloading fuel master key") |
| fuel_key = fuel_master_node.get_file_content('/root/.ssh/id_rsa') |
| |
| nodes = [] |
| for fuel_node in fuel_nodes: |
| ip = str(fuel_node.get_ip(network)) |
| nodes.append(NodeInfo(ConnCreds(ip, "root", key=fuel_key), roles=set(fuel_node.get_roles()))) |
| |
| logger.debug("Found {} fuel nodes for env {}".format(len(nodes), fuel_data['openstack_env'])) |
| |
| return nodes, FuelNodeInfo(version, fuel_ext_iface, cluster.get_openrc()) |
| |