Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 1 | # 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 Dmitriev | 30dfb89 | 2017-06-29 20:58:11 +0300 | [diff] [blame] | 14 | |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 15 | import sys |
Dennis Dmitriev | 30dfb89 | 2017-06-29 20:58:11 +0300 | [diff] [blame] | 16 | |
| 17 | from reclass_tools import helpers |
| 18 | from reclass_tools import reclass_models |
Dennis Dmitriev | 30dfb89 | 2017-06-29 20:58:11 +0300 | [diff] [blame] | 19 | |
| 20 | |
| 21 | def 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 Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 46 | reclass_storage = reclass_models.reclass_storage(domain=domain, |
| 47 | inventory=inventory) |
Dennis Dmitriev | 30dfb89 | 2017-06-29 20:58:11 +0300 | [diff] [blame] | 48 | |
Dennis Dmitriev | 65a80ee | 2017-06-30 17:30:37 +0300 | [diff] [blame] | 49 | if domain is None: |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 50 | sys.exit("Error: please specify a domain name from: \n{}" |
| 51 | .format('\n'.join(reclass_storage.keys()))) |
Dennis Dmitriev | 65a80ee | 2017-06-30 17:30:37 +0300 | [diff] [blame] | 52 | |
| 53 | for storage_domain, storage_nodes in reclass_storage.items(): |
| 54 | if storage_domain != domain: |
| 55 | continue |
Dennis Dmitriev | 30dfb89 | 2017-06-29 20:58:11 +0300 | [diff] [blame] | 56 | |
| 57 | current_cluster_nodes = {} |
| 58 | for storage_node_name, storage_node in storage_nodes.items(): |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 59 | inventory_node_name = "{0}.{1}".format(storage_node['name'], |
| 60 | storage_node['domain']) |
Dennis Dmitriev | 30dfb89 | 2017-06-29 20:58:11 +0300 | [diff] [blame] | 61 | 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 Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 70 | current_cluster_nodes[ |
| 71 | inventory_node_name]['roles'].append('vcp') |
Dennis Dmitriev | 30dfb89 | 2017-06-29 20:58:11 +0300 | [diff] [blame] | 72 | |
| 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 Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 83 | helpers.create_nested_key( |
| 84 | current_cluster_nodes[inventory_node_name], |
| 85 | path=key_path, |
| 86 | value=reclass_key) |
Dennis Dmitriev | 30dfb89 | 2017-06-29 20:58:11 +0300 | [diff] [blame] | 87 | |
Dennis Dmitriev | 65a80ee | 2017-06-30 17:30:37 +0300 | [diff] [blame] | 88 | current_underlay_context = { |
| 89 | 'cookiecutter': { |
| 90 | 'cluster_name': storage_domain, |
| 91 | 'nodes': current_cluster_nodes, |
| 92 | } |
Dennis Dmitriev | 30dfb89 | 2017-06-29 20:58:11 +0300 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | return current_underlay_context |