koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 1 | import logging |
| 2 | from typing import Dict, List, NamedTuple, Union, cast |
| 3 | |
| 4 | from paramiko.ssh_exception import AuthenticationException |
| 5 | |
| 6 | from .fuel_rest_api import get_cluster_id, reflect_cluster, FuelInfo, KeystoneAuth |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame] | 7 | from .ssh_utils import ConnCreds |
| 8 | from .utils import StopTestError, parse_creds, to_ip |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 9 | from .stage import Stage, StepOrder |
| 10 | from .test_run_class import TestRun |
| 11 | from .node import connect, setup_rpc |
| 12 | from .config import ConfigBlock |
| 13 | from .openstack_api import OSCreds |
| 14 | |
| 15 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 16 | logger = logging.getLogger("wally") |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 17 | |
| 18 | |
| 19 | FuelNodeInfo = NamedTuple("FuelNodeInfo", |
| 20 | [("version", List[int]), |
| 21 | ("fuel_ext_iface", str), |
| 22 | ("openrc", Dict[str, Union[str, bool]])]) |
| 23 | |
| 24 | |
| 25 | |
| 26 | class DiscoverFuelStage(Stage): |
| 27 | """"Fuel nodes discovery, also can get openstack openrc""" |
| 28 | |
| 29 | priority = StepOrder.DISCOVER |
| 30 | config_block = 'fuel' |
| 31 | |
| 32 | @classmethod |
| 33 | def validate(cls, cfg: ConfigBlock) -> None: |
| 34 | # msg = "openstack_env should be provided in fuel config" |
| 35 | # check_input_param('openstack_env' in fuel_data, msg) |
| 36 | # fuel.openstack_env |
| 37 | pass |
| 38 | |
| 39 | def run(self, ctx: TestRun) -> None: |
kdanylov aka koder | cdfcdaf | 2017-04-29 10:03:39 +0300 | [diff] [blame] | 40 | full_discovery = 'fuel' in ctx.config.discover |
| 41 | metadata_only = (not full_discovery) and ('metadata' in ctx.config.discover) |
| 42 | ignore_errors = 'ignore_errors' in ctx.config.discover |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame] | 43 | |
| 44 | if not (metadata_only or full_discovery): |
| 45 | logger.debug("Skip ceph discovery due to config setting") |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 46 | return |
| 47 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 48 | if "fuel_os_creds" in ctx.storage and 'fuel_version' in ctx.storage: |
| 49 | logger.debug("Skip FUEL credentials discovery, use previously discovered info") |
| 50 | ctx.fuel_openstack_creds = OSCreds(*cast(List, ctx.storage.get('fuel_os_creds'))) |
| 51 | ctx.fuel_version = ctx.storage.get('fuel_version') |
| 52 | if 'all_nodes' in ctx.storage: |
| 53 | logger.debug("Skip FUEL nodes discovery, use data from DB") |
| 54 | return |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame] | 55 | elif metadata_only: |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 56 | logger.debug("Skip FUEL nodes discovery due to discovery settings") |
| 57 | return |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 58 | |
| 59 | fuel = ctx.config.fuel |
| 60 | fuel_node_info = ctx.merge_node(fuel.ssh_creds, {'fuel_master'}) |
| 61 | creds = dict(zip(("user", "passwd", "tenant"), parse_creds(fuel.creds))) |
| 62 | fuel_conn = KeystoneAuth(fuel.url, creds) |
| 63 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 64 | cluster_id = get_cluster_id(fuel_conn, fuel.openstack_env) |
| 65 | cluster = reflect_cluster(fuel_conn, cluster_id) |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 66 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 67 | if ctx.fuel_version is None: |
| 68 | ctx.fuel_version = FuelInfo(fuel_conn).get_version() |
| 69 | ctx.storage.put(ctx.fuel_version, "fuel_version") |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 70 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 71 | logger.info("Found FUEL {0}".format(".".join(map(str, ctx.fuel_version)))) |
| 72 | openrc = cluster.get_openrc() |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 73 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 74 | if openrc: |
| 75 | auth_url = cast(str, openrc['os_auth_url']) |
| 76 | if ctx.fuel_version >= [8, 0] and auth_url.startswith("https://"): |
| 77 | logger.warning("Fixing FUEL 8.0 AUTH url - replace https://->http://") |
| 78 | auth_url = auth_url.replace("https", "http", 1) |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 79 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 80 | os_creds = OSCreds(name=cast(str, openrc['username']), |
| 81 | passwd=cast(str, openrc['password']), |
| 82 | tenant=cast(str, openrc['tenant_name']), |
| 83 | auth_url=cast(str, auth_url), |
| 84 | insecure=cast(bool, openrc['insecure'])) |
| 85 | |
| 86 | ctx.fuel_openstack_creds = os_creds |
| 87 | else: |
| 88 | ctx.fuel_openstack_creds = None |
| 89 | |
| 90 | ctx.storage.put(list(ctx.fuel_openstack_creds), "fuel_os_creds") |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 91 | |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame] | 92 | if metadata_only: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 93 | logger.debug("Skip FUEL nodes discovery due to discovery settings") |
| 94 | return |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 95 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 96 | try: |
| 97 | fuel_rpc = setup_rpc(connect(fuel_node_info), |
| 98 | ctx.rpc_code, |
| 99 | ctx.default_rpc_plugins, |
| 100 | log_level=ctx.config.rpc_log_level) |
| 101 | except AuthenticationException: |
| 102 | msg = "FUEL nodes discovery failed - wrong FUEL master SSH credentials" |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame] | 103 | if ignore_errors: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 104 | raise StopTestError(msg) |
| 105 | logger.warning(msg) |
| 106 | return |
| 107 | except Exception as exc: |
kdanylov aka koder | 150b219 | 2017-04-01 16:53:01 +0300 | [diff] [blame] | 108 | if ignore_errors: |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 109 | logger.exception("While connection to FUEL") |
| 110 | raise StopTestError("Failed to connect to FUEL") |
| 111 | logger.warning("Failed to connect to FUEL - %s", exc) |
| 112 | return |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 113 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 114 | logger.debug("Downloading FUEL node ssh master key") |
| 115 | fuel_key = fuel_rpc.get_file_content('/root/.ssh/id_rsa') |
| 116 | network = 'fuelweb_admin' if ctx.fuel_version >= [6, 0] else 'admin' |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 117 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 118 | count = 0 |
| 119 | for count, fuel_node in enumerate(list(cluster.get_nodes())): |
| 120 | ip = str(fuel_node.get_ip(network)) |
koder aka kdanilov | bbbe1dc | 2016-12-20 01:19:56 +0200 | [diff] [blame] | 121 | ctx.merge_node(ConnCreds(to_ip(ip), "root", key=fuel_key), set(fuel_node.get_roles())) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 122 | |
koder aka kdanilov | 962ee5f | 2016-12-19 02:40:08 +0200 | [diff] [blame] | 123 | logger.debug("Found {} FUEL nodes for env {}".format(count, fuel.openstack_env)) |