koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 1 | import os.path |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 2 | import logging |
koder aka kdanilov | cee4334 | 2015-04-14 22:52:53 +0300 | [diff] [blame] | 3 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 4 | from . import ceph |
| 5 | from . import fuel |
| 6 | from . import openstack |
| 7 | from wally.utils import parse_creds |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 8 | |
| 9 | |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 10 | logger = logging.getLogger("wally.discover") |
| 11 | |
| 12 | |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 13 | openrc_templ = """#!/bin/sh |
| 14 | export LC_ALL=C |
| 15 | export OS_NO_CACHE='true' |
| 16 | export OS_TENANT_NAME='{tenant}' |
| 17 | export OS_USERNAME='{name}' |
| 18 | export OS_PASSWORD='{passwd}' |
| 19 | export OS_AUTH_URL='{auth_url}' |
| 20 | export OS_AUTH_STRATEGY='keystone' |
| 21 | export OS_REGION_NAME='RegionOne' |
| 22 | export CINDER_ENDPOINT_TYPE='publicURL' |
| 23 | export GLANCE_ENDPOINT_TYPE='publicURL' |
| 24 | export KEYSTONE_ENDPOINT_TYPE='publicURL' |
| 25 | export NOVA_ENDPOINT_TYPE='publicURL' |
| 26 | export NEUTRON_ENDPOINT_TYPE='publicURL' |
| 27 | """ |
| 28 | |
| 29 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 30 | def discover(ctx, discover, clusters_info, var_dir, discover_nodes=True): |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 31 | nodes_to_run = [] |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 32 | clean_data = None |
koder aka kdanilov | 88407ff | 2015-05-26 15:35:57 +0300 | [diff] [blame] | 33 | ctx.fuel_openstack_creds = None |
| 34 | |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 35 | for cluster in discover: |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 36 | if cluster == "openstack" and not discover_nodes: |
| 37 | logger.warning("Skip openstack cluster discovery") |
| 38 | elif cluster == "openstack" and discover_nodes: |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 39 | cluster_info = clusters_info["openstack"] |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 40 | conn = cluster_info['connection'] |
| 41 | user, passwd, tenant = parse_creds(conn['creds']) |
| 42 | |
| 43 | auth_data = dict( |
| 44 | auth_url=conn['auth_url'], |
| 45 | username=user, |
| 46 | api_key=passwd, |
| 47 | project_id=tenant) |
| 48 | |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 49 | if not conn: |
| 50 | logger.error("No connection provided for %s. Skipping" |
| 51 | % cluster) |
| 52 | continue |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 53 | |
Yulia Portnova | 0e64ea2 | 2015-03-20 17:27:22 +0200 | [diff] [blame] | 54 | logger.debug("Discovering openstack nodes " |
| 55 | "with connection details: %r" % |
| 56 | conn) |
| 57 | |
koder aka kdanilov | e06762a | 2015-03-22 23:32:09 +0200 | [diff] [blame] | 58 | os_nodes = openstack.discover_openstack_nodes(auth_data, |
| 59 | cluster_info) |
| 60 | nodes_to_run.extend(os_nodes) |
| 61 | |
koder aka kdanilov | c368eb6 | 2015-04-28 18:22:01 +0300 | [diff] [blame] | 62 | elif cluster == "fuel" or cluster == "fuel_openrc_only": |
| 63 | if cluster == "fuel_openrc_only": |
| 64 | discover_nodes = False |
| 65 | |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 66 | res = fuel.discover_fuel_nodes(clusters_info['fuel'], |
| 67 | var_dir, |
| 68 | discover_nodes) |
koder aka kdanilov | cff7b2e | 2015-04-18 20:48:15 +0300 | [diff] [blame] | 69 | nodes, clean_data, openrc_dict = res |
gstepanov | 59d80f7 | 2015-04-10 17:24:27 +0300 | [diff] [blame] | 70 | |
koder aka kdanilov | 88407ff | 2015-05-26 15:35:57 +0300 | [diff] [blame] | 71 | if openrc_dict is None: |
| 72 | ctx.fuel_openstack_creds = None |
| 73 | else: |
| 74 | ctx.fuel_openstack_creds = { |
| 75 | 'name': openrc_dict['username'], |
| 76 | 'passwd': openrc_dict['password'], |
| 77 | 'tenant': openrc_dict['tenant_name'], |
koder aka kdanilov | b719743 | 2015-07-15 00:40:43 +0300 | [diff] [blame^] | 78 | 'auth_url': openrc_dict['os_auth_url'], |
| 79 | 'insecure': openrc_dict.get('insecure', False)} |
gstepanov | 59d80f7 | 2015-04-10 17:24:27 +0300 | [diff] [blame] | 80 | |
koder aka kdanilov | 168f609 | 2015-04-19 02:33:38 +0300 | [diff] [blame] | 81 | env_name = clusters_info['fuel']['openstack_env'] |
| 82 | env_f_name = env_name |
| 83 | for char in "-+ {}()[]": |
| 84 | env_f_name = env_f_name.replace(char, '_') |
| 85 | |
| 86 | fuel_openrc_fname = os.path.join(var_dir, |
| 87 | env_f_name + "_openrc") |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 88 | |
koder aka kdanilov | 88407ff | 2015-05-26 15:35:57 +0300 | [diff] [blame] | 89 | if ctx.fuel_openstack_creds is not None: |
| 90 | with open(fuel_openrc_fname, "w") as fd: |
| 91 | fd.write(openrc_templ.format(**ctx.fuel_openstack_creds)) |
| 92 | msg = "Openrc for cluster {0} saves into {1}" |
koder aka kdanilov | 170936a | 2015-06-27 22:51:17 +0300 | [diff] [blame] | 93 | logger.info(msg.format(env_name, fuel_openrc_fname)) |
gstepanov | 59d80f7 | 2015-04-10 17:24:27 +0300 | [diff] [blame] | 94 | nodes_to_run.extend(nodes) |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 95 | |
| 96 | elif cluster == "ceph": |
koder aka kdanilov | e87ae65 | 2015-04-20 02:14:35 +0300 | [diff] [blame] | 97 | if discover_nodes: |
| 98 | cluster_info = clusters_info["ceph"] |
| 99 | nodes_to_run.extend(ceph.discover_ceph_nodes(cluster_info)) |
| 100 | else: |
| 101 | logger.warning("Skip ceph cluster discovery") |
koder aka kdanilov | da45e88 | 2015-04-06 02:24:42 +0300 | [diff] [blame] | 102 | else: |
| 103 | msg_templ = "Unknown cluster type in 'discover' parameter: {0!r}" |
| 104 | raise ValueError(msg_templ.format(cluster)) |
koder aka kdanilov | 2c47309 | 2015-03-29 17:12:13 +0300 | [diff] [blame] | 105 | |
koder aka kdanilov | f86d7af | 2015-05-06 04:01:54 +0300 | [diff] [blame] | 106 | return nodes_to_run |