blob: d1eb9acf820dc1434a364dd5f5431afb8ddd9261 [file] [log] [blame]
koder aka kdanilov168f6092015-04-19 02:33:38 +03001import os.path
Yulia Portnova0e64ea22015-03-20 17:27:22 +02002import logging
koder aka kdanilovcee43342015-04-14 22:52:53 +03003
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +03004from paramiko import AuthenticationException
5
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +03006from . import ceph
7from . import fuel
8from . import openstack
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +03009from ..utils import parse_creds, StopTestError
10from ..test_run_class import TestRun
koder aka kdanilov3d2bc4f2016-11-12 18:31:18 +020011from ..node import Node
Yulia Portnova0e64ea22015-03-20 17:27:22 +020012
13
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030014logger = logging.getLogger("wally.discover")
15
16
koder aka kdanilov168f6092015-04-19 02:33:38 +030017openrc_templ = """#!/bin/sh
18export LC_ALL=C
19export OS_NO_CACHE='true'
20export OS_TENANT_NAME='{tenant}'
21export OS_USERNAME='{name}'
22export OS_PASSWORD='{passwd}'
23export OS_AUTH_URL='{auth_url}'
Michael Semenov8d6c0572015-08-25 12:59:05 +030024export OS_INSECURE={insecure}
koder aka kdanilov168f6092015-04-19 02:33:38 +030025export OS_AUTH_STRATEGY='keystone'
26export OS_REGION_NAME='RegionOne'
27export CINDER_ENDPOINT_TYPE='publicURL'
28export GLANCE_ENDPOINT_TYPE='publicURL'
29export KEYSTONE_ENDPOINT_TYPE='publicURL'
30export NOVA_ENDPOINT_TYPE='publicURL'
31export NEUTRON_ENDPOINT_TYPE='publicURL'
32"""
33
34
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030035def discover(testrun: TestRun, discover_cfg, clusters_info, var_dir, discover_nodes=True):
36 """Discover nodes in clusters"""
Yulia Portnova0e64ea22015-03-20 17:27:22 +020037 nodes_to_run = []
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030038 clean_data = None
koder aka kdanilov88407ff2015-05-26 15:35:57 +030039
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030040 for cluster in discover_cfg:
koder aka kdanilove87ae652015-04-20 02:14:35 +030041 if cluster == "openstack" and not discover_nodes:
42 logger.warning("Skip openstack cluster discovery")
43 elif cluster == "openstack" and discover_nodes:
koder aka kdanilovda45e882015-04-06 02:24:42 +030044 cluster_info = clusters_info["openstack"]
koder aka kdanilove06762a2015-03-22 23:32:09 +020045 conn = cluster_info['connection']
46 user, passwd, tenant = parse_creds(conn['creds'])
47
48 auth_data = dict(
49 auth_url=conn['auth_url'],
50 username=user,
51 api_key=passwd,
52 project_id=tenant)
53
Yulia Portnova0e64ea22015-03-20 17:27:22 +020054 if not conn:
55 logger.error("No connection provided for %s. Skipping"
56 % cluster)
57 continue
koder aka kdanilove06762a2015-03-22 23:32:09 +020058
Yulia Portnova0e64ea22015-03-20 17:27:22 +020059 logger.debug("Discovering openstack nodes "
60 "with connection details: %r" %
61 conn)
62
koder aka kdanilove06762a2015-03-22 23:32:09 +020063 os_nodes = openstack.discover_openstack_nodes(auth_data,
64 cluster_info)
65 nodes_to_run.extend(os_nodes)
66
koder aka kdanilovc368eb62015-04-28 18:22:01 +030067 elif cluster == "fuel" or cluster == "fuel_openrc_only":
68 if cluster == "fuel_openrc_only":
69 discover_nodes = False
70
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030071 ssh_creds = clusters_info['fuel']['ssh_creds']
72 fuel_node = Node(NodeInfo(ssh_creds, {'fuel_master'}))
73
74 try:
75 fuel_node.connect_ssh()
76 except AuthenticationException:
77 raise StopTestError("Wrong fuel credentials")
78 except Exception:
79 logger.exception("While connection to FUEL")
80 raise StopTestError("Failed to connect to FUEL")
81
82 fuel_node.connect_rpc()
83
84 res = fuel.discover_fuel_nodes(fuel_node,
85 clusters_info['fuel'],
koder aka kdanilove87ae652015-04-20 02:14:35 +030086 discover_nodes)
koder aka kdanilov05e15b92016-02-07 19:32:46 +020087 nodes, clean_data, openrc_dict, version = res
gstepanov59d80f72015-04-10 17:24:27 +030088
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030089 if openrc_dict:
koder aka kdanilov05e15b92016-02-07 19:32:46 +020090 if version >= [8, 0] and openrc_dict['os_auth_url'].startswith("https://"):
91 logger.warning("Fixing FUEL 8.0 AUTH url - replace https://->http://")
92 openrc_dict['os_auth_url'] = "http" + openrc_dict['os_auth_url'][5:]
93
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +030094 testrun.fuel_openstack_creds = {
koder aka kdanilov88407ff2015-05-26 15:35:57 +030095 'name': openrc_dict['username'],
96 'passwd': openrc_dict['password'],
97 'tenant': openrc_dict['tenant_name'],
koder aka kdanilovb7197432015-07-15 00:40:43 +030098 'auth_url': openrc_dict['os_auth_url'],
Michael Semenov8d6c0572015-08-25 12:59:05 +030099 'insecure': openrc_dict['insecure']}
gstepanov59d80f72015-04-10 17:24:27 +0300100
koder aka kdanilov168f6092015-04-19 02:33:38 +0300101 env_name = clusters_info['fuel']['openstack_env']
102 env_f_name = env_name
103 for char in "-+ {}()[]":
104 env_f_name = env_f_name.replace(char, '_')
105
106 fuel_openrc_fname = os.path.join(var_dir,
107 env_f_name + "_openrc")
koder aka kdanilove87ae652015-04-20 02:14:35 +0300108
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300109 if testrun.fuel_openstack_creds is not None:
koder aka kdanilov88407ff2015-05-26 15:35:57 +0300110 with open(fuel_openrc_fname, "w") as fd:
koder aka kdanilov3b4da8b2016-10-17 00:17:53 +0300111 fd.write(openrc_templ.format(**testrun.fuel_openstack_creds))
112 msg = "Openrc for cluster {0} saves into {1}"
113 logger.info(msg.format(env_name, fuel_openrc_fname))
gstepanov59d80f72015-04-10 17:24:27 +0300114 nodes_to_run.extend(nodes)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300115
116 elif cluster == "ceph":
koder aka kdanilove87ae652015-04-20 02:14:35 +0300117 if discover_nodes:
118 cluster_info = clusters_info["ceph"]
119 nodes_to_run.extend(ceph.discover_ceph_nodes(cluster_info))
120 else:
121 logger.warning("Skip ceph cluster discovery")
koder aka kdanilovda45e882015-04-06 02:24:42 +0300122 else:
123 msg_templ = "Unknown cluster type in 'discover' parameter: {0!r}"
124 raise ValueError(msg_templ.format(cluster))
koder aka kdanilov2c473092015-03-29 17:12:13 +0300125
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300126 return nodes_to_run