blob: 5b9d3fe67189e1ed753380991ec45bf7ea8ea2fc [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 kdanilovcff7b2e2015-04-18 20:48:15 +03004from . import ceph
5from . import fuel
6from . import openstack
7from wally.utils import parse_creds
Yulia Portnova0e64ea22015-03-20 17:27:22 +02008
9
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030010logger = logging.getLogger("wally.discover")
11
12
koder aka kdanilov168f6092015-04-19 02:33:38 +030013openrc_templ = """#!/bin/sh
14export LC_ALL=C
15export OS_NO_CACHE='true'
16export OS_TENANT_NAME='{tenant}'
17export OS_USERNAME='{name}'
18export OS_PASSWORD='{passwd}'
19export OS_AUTH_URL='{auth_url}'
Michael Semenov8d6c0572015-08-25 12:59:05 +030020export OS_INSECURE={insecure}
koder aka kdanilov168f6092015-04-19 02:33:38 +030021export OS_AUTH_STRATEGY='keystone'
22export OS_REGION_NAME='RegionOne'
23export CINDER_ENDPOINT_TYPE='publicURL'
24export GLANCE_ENDPOINT_TYPE='publicURL'
25export KEYSTONE_ENDPOINT_TYPE='publicURL'
26export NOVA_ENDPOINT_TYPE='publicURL'
27export NEUTRON_ENDPOINT_TYPE='publicURL'
28"""
29
30
koder aka kdanilove87ae652015-04-20 02:14:35 +030031def discover(ctx, discover, clusters_info, var_dir, discover_nodes=True):
Yulia Portnova0e64ea22015-03-20 17:27:22 +020032 nodes_to_run = []
koder aka kdanilovcff7b2e2015-04-18 20:48:15 +030033 clean_data = None
koder aka kdanilov88407ff2015-05-26 15:35:57 +030034 ctx.fuel_openstack_creds = None
35
koder aka kdanilovda45e882015-04-06 02:24:42 +030036 for cluster in discover:
koder aka kdanilove87ae652015-04-20 02:14:35 +030037 if cluster == "openstack" and not discover_nodes:
38 logger.warning("Skip openstack cluster discovery")
39 elif cluster == "openstack" and discover_nodes:
koder aka kdanilovda45e882015-04-06 02:24:42 +030040 cluster_info = clusters_info["openstack"]
koder aka kdanilove06762a2015-03-22 23:32:09 +020041 conn = cluster_info['connection']
42 user, passwd, tenant = parse_creds(conn['creds'])
43
44 auth_data = dict(
45 auth_url=conn['auth_url'],
46 username=user,
47 api_key=passwd,
48 project_id=tenant)
49
Yulia Portnova0e64ea22015-03-20 17:27:22 +020050 if not conn:
51 logger.error("No connection provided for %s. Skipping"
52 % cluster)
53 continue
koder aka kdanilove06762a2015-03-22 23:32:09 +020054
Yulia Portnova0e64ea22015-03-20 17:27:22 +020055 logger.debug("Discovering openstack nodes "
56 "with connection details: %r" %
57 conn)
58
koder aka kdanilove06762a2015-03-22 23:32:09 +020059 os_nodes = openstack.discover_openstack_nodes(auth_data,
60 cluster_info)
61 nodes_to_run.extend(os_nodes)
62
koder aka kdanilovc368eb62015-04-28 18:22:01 +030063 elif cluster == "fuel" or cluster == "fuel_openrc_only":
64 if cluster == "fuel_openrc_only":
65 discover_nodes = False
66
koder aka kdanilove87ae652015-04-20 02:14:35 +030067 res = fuel.discover_fuel_nodes(clusters_info['fuel'],
68 var_dir,
69 discover_nodes)
koder aka kdanilov05e15b92016-02-07 19:32:46 +020070 nodes, clean_data, openrc_dict, version = res
gstepanov59d80f72015-04-10 17:24:27 +030071
koder aka kdanilov88407ff2015-05-26 15:35:57 +030072 if openrc_dict is None:
73 ctx.fuel_openstack_creds = None
74 else:
koder aka kdanilov05e15b92016-02-07 19:32:46 +020075 if version >= [8, 0] and openrc_dict['os_auth_url'].startswith("https://"):
76 logger.warning("Fixing FUEL 8.0 AUTH url - replace https://->http://")
77 openrc_dict['os_auth_url'] = "http" + openrc_dict['os_auth_url'][5:]
78
koder aka kdanilov88407ff2015-05-26 15:35:57 +030079 ctx.fuel_openstack_creds = {
80 'name': openrc_dict['username'],
81 'passwd': openrc_dict['password'],
82 'tenant': openrc_dict['tenant_name'],
koder aka kdanilovb7197432015-07-15 00:40:43 +030083 'auth_url': openrc_dict['os_auth_url'],
Michael Semenov8d6c0572015-08-25 12:59:05 +030084 'insecure': openrc_dict['insecure']}
gstepanov59d80f72015-04-10 17:24:27 +030085
koder aka kdanilov168f6092015-04-19 02:33:38 +030086 env_name = clusters_info['fuel']['openstack_env']
87 env_f_name = env_name
88 for char in "-+ {}()[]":
89 env_f_name = env_f_name.replace(char, '_')
90
91 fuel_openrc_fname = os.path.join(var_dir,
92 env_f_name + "_openrc")
koder aka kdanilove87ae652015-04-20 02:14:35 +030093
koder aka kdanilov88407ff2015-05-26 15:35:57 +030094 if ctx.fuel_openstack_creds is not None:
95 with open(fuel_openrc_fname, "w") as fd:
96 fd.write(openrc_templ.format(**ctx.fuel_openstack_creds))
97 msg = "Openrc for cluster {0} saves into {1}"
koder aka kdanilov170936a2015-06-27 22:51:17 +030098 logger.info(msg.format(env_name, fuel_openrc_fname))
gstepanov59d80f72015-04-10 17:24:27 +030099 nodes_to_run.extend(nodes)
koder aka kdanilovda45e882015-04-06 02:24:42 +0300100
101 elif cluster == "ceph":
koder aka kdanilove87ae652015-04-20 02:14:35 +0300102 if discover_nodes:
103 cluster_info = clusters_info["ceph"]
104 nodes_to_run.extend(ceph.discover_ceph_nodes(cluster_info))
105 else:
106 logger.warning("Skip ceph cluster discovery")
koder aka kdanilovda45e882015-04-06 02:24:42 +0300107 else:
108 msg_templ = "Unknown cluster type in 'discover' parameter: {0!r}"
109 raise ValueError(msg_templ.format(cluster))
koder aka kdanilov2c473092015-03-29 17:12:13 +0300110
koder aka kdanilovf86d7af2015-05-06 04:01:54 +0300111 return nodes_to_run