blob: 902752aa4d72b0853e7d58c60a4def126a29b65e [file] [log] [blame]
koder aka kdanilov39e449e2016-12-17 15:15:26 +02001import logging
2from typing import Dict, List, NamedTuple, Union, cast
3
4from paramiko.ssh_exception import AuthenticationException
5
6from .fuel_rest_api import get_cluster_id, reflect_cluster, FuelInfo, KeystoneAuth
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +02007from .ssh_utils import ConnCreds
8from .utils import StopTestError, parse_creds, to_ip
koder aka kdanilov39e449e2016-12-17 15:15:26 +02009from .stage import Stage, StepOrder
10from .test_run_class import TestRun
11from .node import connect, setup_rpc
12from .config import ConfigBlock
13from .openstack_api import OSCreds
14
15
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020016logger = logging.getLogger("wally")
koder aka kdanilov39e449e2016-12-17 15:15:26 +020017
18
19FuelNodeInfo = NamedTuple("FuelNodeInfo",
20 [("version", List[int]),
21 ("fuel_ext_iface", str),
22 ("openrc", Dict[str, Union[str, bool]])])
23
24
25
26class 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:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020040 discovery = ctx.config.get("discovery")
41 if discovery == 'disable':
42 logger.info("Skip FUEL discovery due to config setting")
43 return
44
45 if 'all_nodes' in ctx.storage:
46 logger.debug("Skip FUEL discovery, use previously discovered nodes")
47 ctx.fuel_openstack_creds = ctx.storage['fuel_os_creds'] # type: ignore
48 ctx.fuel_version = ctx.storage['fuel_version'] # type: ignore
49 return
50
51 fuel = ctx.config.fuel
52 fuel_node_info = ctx.merge_node(fuel.ssh_creds, {'fuel_master'})
53 creds = dict(zip(("user", "passwd", "tenant"), parse_creds(fuel.creds)))
54 fuel_conn = KeystoneAuth(fuel.url, creds)
55
56 # get cluster information from REST API
57 if "fuel_os_creds" in ctx.storage and 'fuel_version' in ctx.storage:
58 ctx.fuel_openstack_creds = ctx.storage['fuel_os_creds'] # type: ignore
59 ctx.fuel_version = ctx.storage['fuel_version'] # type: ignore
60 return
61
62 cluster_id = get_cluster_id(fuel_conn, fuel.openstack_env)
63 cluster = reflect_cluster(fuel_conn, cluster_id)
64 ctx.fuel_version = FuelInfo(fuel_conn).get_version()
65 ctx.storage["fuel_version"] = ctx.fuel_version
66
67 logger.info("Found FUEL {0}".format(".".join(map(str, ctx.fuel_version))))
68 openrc = cluster.get_openrc()
69
70 if openrc:
71 auth_url = cast(str, openrc['os_auth_url'])
72 if ctx.fuel_version >= [8, 0] and auth_url.startswith("https://"):
73 logger.warning("Fixing FUEL 8.0 AUTH url - replace https://->http://")
74 auth_url = auth_url.replace("https", "http", 1)
75
76 os_creds = OSCreds(name=cast(str, openrc['username']),
77 passwd=cast(str, openrc['password']),
78 tenant=cast(str, openrc['tenant_name']),
79 auth_url=cast(str, auth_url),
80 insecure=cast(bool, openrc['insecure']))
81
82 ctx.fuel_openstack_creds = os_creds
koder aka kdanilov39e449e2016-12-17 15:15:26 +020083 else:
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020084 ctx.fuel_openstack_creds = None
85 ctx.storage["fuel_os_creds"] = ctx.fuel_openstack_creds
koder aka kdanilov39e449e2016-12-17 15:15:26 +020086
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020087 if discovery == 'metadata':
88 logger.debug("Skip FUEL nodes discovery due to discovery settings")
89 return
koder aka kdanilov39e449e2016-12-17 15:15:26 +020090
koder aka kdanilov962ee5f2016-12-19 02:40:08 +020091 try:
92 fuel_rpc = setup_rpc(connect(fuel_node_info),
93 ctx.rpc_code,
94 ctx.default_rpc_plugins,
95 log_level=ctx.config.rpc_log_level)
96 except AuthenticationException:
97 msg = "FUEL nodes discovery failed - wrong FUEL master SSH credentials"
98 if discovery != 'ignore_errors':
99 raise StopTestError(msg)
100 logger.warning(msg)
101 return
102 except Exception as exc:
103 if discovery != 'ignore_errors':
104 logger.exception("While connection to FUEL")
105 raise StopTestError("Failed to connect to FUEL")
106 logger.warning("Failed to connect to FUEL - %s", exc)
107 return
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200108
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200109 logger.debug("Downloading FUEL node ssh master key")
110 fuel_key = fuel_rpc.get_file_content('/root/.ssh/id_rsa')
111 network = 'fuelweb_admin' if ctx.fuel_version >= [6, 0] else 'admin'
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200112
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200113 count = 0
114 for count, fuel_node in enumerate(list(cluster.get_nodes())):
115 ip = str(fuel_node.get_ip(network))
koder aka kdanilovbbbe1dc2016-12-20 01:19:56 +0200116 ctx.merge_node(ConnCreds(to_ip(ip), "root", key=fuel_key), set(fuel_node.get_roles()))
koder aka kdanilov39e449e2016-12-17 15:15:26 +0200117
koder aka kdanilov962ee5f2016-12-19 02:40:08 +0200118 logger.debug("Found {} FUEL nodes for env {}".format(count, fuel.openstack_env))