blob: 5a03ed7237c24a83aff56c75cad5a09b4f11b2a3 [file] [log] [blame]
Dennis Dmitriev566db4b2017-07-18 18:13:07 +03001# Copyright 2013 - 2017 Mirantis, Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
Dennis Dmitriev30dfb892017-06-29 20:58:11 +030014
Dennis Dmitriev566db4b2017-07-18 18:13:07 +030015import sys
Dennis Dmitriev30dfb892017-06-29 20:58:11 +030016
17from reclass_tools import helpers
18from reclass_tools import reclass_models
Dennis Dmitriev30dfb892017-06-29 20:58:11 +030019
20
21def create_inventory_context(domain=None, keys=None):
22 """Dumps the current inventory per domain
23
24 Example of context:
25
26 <global_settings>: # only if required
27 ...
28 current_clusters:
29 <cluster_names>:
30 # here are cluster settings if required
31 nodes:
32 <node_names>:
33 name: ctl01
34 reclass_storage_name: openstack_control_node01
35 roles:
36 - vcp # 'vcp' or None
37 parameters: # specified keys to dump, for example
38 # parameters.linux.network.interface below:
39 linux:
40 network:
41 interfaces:
42 ..
43 """
44 inventory = reclass_models.inventory_list(domain=domain)
45 vcp_list = reclass_models.vcp_list(domain=domain, inventory=inventory)
Dennis Dmitriev566db4b2017-07-18 18:13:07 +030046 reclass_storage = reclass_models.reclass_storage(domain=domain,
47 inventory=inventory)
Dennis Dmitriev30dfb892017-06-29 20:58:11 +030048
Dennis Dmitriev65a80ee2017-06-30 17:30:37 +030049 if domain is None:
Dennis Dmitriev566db4b2017-07-18 18:13:07 +030050 sys.exit("Error: please specify a domain name from: \n{}"
51 .format('\n'.join(reclass_storage.keys())))
Dennis Dmitriev65a80ee2017-06-30 17:30:37 +030052
53 for storage_domain, storage_nodes in reclass_storage.items():
54 if storage_domain != domain:
55 continue
Dennis Dmitriev30dfb892017-06-29 20:58:11 +030056
57 current_cluster_nodes = {}
58 for storage_node_name, storage_node in storage_nodes.items():
Dennis Dmitriev566db4b2017-07-18 18:13:07 +030059 inventory_node_name = "{0}.{1}".format(storage_node['name'],
60 storage_node['domain'])
Dennis Dmitriev30dfb892017-06-29 20:58:11 +030061 current_cluster_nodes[inventory_node_name] = {
62 'name': storage_node['name'],
63 'reclass_storage_name': storage_node_name,
64 'roles': list(),
65 'parameters': dict(),
66 }
67
68 if (storage_node['name'], storage_node['domain']) in vcp_list:
69 # Add role 'vcp' to mark the VM nodes.
Dennis Dmitriev566db4b2017-07-18 18:13:07 +030070 current_cluster_nodes[
71 inventory_node_name]['roles'].append('vcp')
Dennis Dmitriev30dfb892017-06-29 20:58:11 +030072
73 if keys:
74 # Dump specified parameters for the node
75 # Will fail with KeyError if 'inventory_node_name' doesn't
76 # exists in reclass inventory
77 # (wasn't generated with reclass.storage yet, for example)
78 node = inventory[inventory_node_name]
79 for key in keys:
80 key_path = key.split('.')
81 reclass_key = helpers.get_nested_key(node, path=key_path)
82 if reclass_key:
Dennis Dmitriev566db4b2017-07-18 18:13:07 +030083 helpers.create_nested_key(
84 current_cluster_nodes[inventory_node_name],
85 path=key_path,
86 value=reclass_key)
Dennis Dmitriev30dfb892017-06-29 20:58:11 +030087
Dennis Dmitriev65a80ee2017-06-30 17:30:37 +030088 current_underlay_context = {
89 'cookiecutter': {
90 'cluster_name': storage_domain,
91 'nodes': current_cluster_nodes,
92 }
Dennis Dmitriev30dfb892017-06-29 20:58:11 +030093 }
94
95 return current_underlay_context