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. |
| 14 | |
Dennis Dmitriev | 1110ac5 | 2017-06-22 21:07:37 +0300 | [diff] [blame] | 15 | import reclass |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 16 | # from reclass.adapters import salt as reclass_salt |
Dennis Dmitriev | 1110ac5 | 2017-06-22 21:07:37 +0300 | [diff] [blame] | 17 | from reclass import config as reclass_config |
| 18 | from reclass import core as reclass_core |
Dennis Dmitriev | de847d9 | 2017-06-26 18:58:05 +0300 | [diff] [blame] | 19 | |
| 20 | from reclass_tools import helpers |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 21 | # import salt.cli.call |
| 22 | # import salt.cli.caller |
Dennis Dmitriev | 1110ac5 | 2017-06-22 21:07:37 +0300 | [diff] [blame] | 23 | |
| 24 | |
| 25 | def get_core(): |
| 26 | """Initializes reclass Core() using /etc/reclass settings""" |
| 27 | |
| 28 | defaults = reclass_config.find_and_read_configfile() |
| 29 | inventory_base_uri = defaults['inventory_base_uri'] |
| 30 | storage_type = defaults['storage_type'] |
| 31 | |
| 32 | nodes_uri, classes_uri = reclass_config.path_mangler(inventory_base_uri, |
| 33 | None, None) |
| 34 | storage = reclass.get_storage(storage_type, nodes_uri, classes_uri, |
| 35 | default_environment='base') |
| 36 | |
| 37 | return reclass_core.Core(storage, None, None) |
| 38 | |
| 39 | |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 40 | # def get_minion_domain(): |
| 41 | # """Try to get domain from the local salt minion""" |
| 42 | # client = salt.cli.call.SaltCall() |
| 43 | # client.parse_args(args=['pillar.items']) |
| 44 | # caller = salt.cli.caller.Caller.factory(client.config) |
| 45 | # result = caller.call() |
| 46 | # # Warning! There is a model-related parameter |
| 47 | # # TODO(ddmitriev): move the path to the parameter to a settings/defaults |
| 48 | # domain = result['return']['_param']['cluster_domain'] |
| 49 | # return domain |
Dennis Dmitriev | 1110ac5 | 2017-06-22 21:07:37 +0300 | [diff] [blame] | 50 | |
| 51 | |
Dennis Dmitriev | 94239b1 | 2017-06-23 13:18:38 +0300 | [diff] [blame] | 52 | def inventory_list(domain=None): |
Dennis Dmitriev | 1110ac5 | 2017-06-22 21:07:37 +0300 | [diff] [blame] | 53 | core = get_core() |
Dennis Dmitriev | de847d9 | 2017-06-26 18:58:05 +0300 | [diff] [blame] | 54 | inventory = core.inventory()['nodes'] |
Dennis Dmitriev | 94239b1 | 2017-06-23 13:18:38 +0300 | [diff] [blame] | 55 | if domain is not None: |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 56 | inventory = {key: val for (key, val) in inventory.items() |
| 57 | if key.endswith(domain)} |
Dennis Dmitriev | de847d9 | 2017-06-26 18:58:05 +0300 | [diff] [blame] | 58 | return inventory |
| 59 | |
| 60 | |
Dennis Dmitriev | 30dfb89 | 2017-06-29 20:58:11 +0300 | [diff] [blame] | 61 | def get_nodeinfo(minion_id): |
| 62 | core = get_core() |
| 63 | return core.nodeinfo(minion_id) |
| 64 | |
| 65 | |
| 66 | def vcp_list(domain=None, inventory=None): |
Dennis Dmitriev | de847d9 | 2017-06-26 18:58:05 +0300 | [diff] [blame] | 67 | """List VCP node names |
| 68 | |
| 69 | Scan all nodes for the object salt.control.cluster.internal.node.XXX.name |
Dennis Dmitriev | 30dfb89 | 2017-06-29 20:58:11 +0300 | [diff] [blame] | 70 | Return set of tuples ((nodename1, domain), (nodename2, domain), ...) |
Dennis Dmitriev | de847d9 | 2017-06-26 18:58:05 +0300 | [diff] [blame] | 71 | """ |
| 72 | |
Dennis Dmitriev | 30dfb89 | 2017-06-29 20:58:11 +0300 | [diff] [blame] | 73 | inventory = inventory or inventory_list(domain=domain) |
Dennis Dmitriev | de847d9 | 2017-06-26 18:58:05 +0300 | [diff] [blame] | 74 | vcp_path = 'parameters.salt.control.cluster.internal.node'.split('.') |
Dennis Dmitriev | 30dfb89 | 2017-06-29 20:58:11 +0300 | [diff] [blame] | 75 | domain_path = 'parameters._param.cluster_domain'.split('.') |
Dennis Dmitriev | de847d9 | 2017-06-26 18:58:05 +0300 | [diff] [blame] | 76 | |
| 77 | vcp_node_names = set() |
| 78 | |
| 79 | for node_name, node in inventory.items(): |
| 80 | vcp_nodes = helpers.get_nested_key(node, path=vcp_path) |
| 81 | if vcp_nodes is not None: |
| 82 | for vcp_node_name, vcp_node in vcp_nodes.items(): |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 83 | vcp_node_names.add(( |
| 84 | vcp_node['name'], |
| 85 | helpers.get_nested_key(node, path=domain_path))) |
Dennis Dmitriev | de847d9 | 2017-06-26 18:58:05 +0300 | [diff] [blame] | 86 | return vcp_node_names |
| 87 | |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 88 | |
Dennis Dmitriev | 30dfb89 | 2017-06-29 20:58:11 +0300 | [diff] [blame] | 89 | def reclass_storage(domain=None, inventory=None): |
| 90 | """List VCP node names |
Dennis Dmitriev | de847d9 | 2017-06-26 18:58:05 +0300 | [diff] [blame] | 91 | |
Dennis Dmitriev | 30dfb89 | 2017-06-29 20:58:11 +0300 | [diff] [blame] | 92 | Scan all nodes for the object salt.control.cluster.internal.node.XXX.name |
| 93 | """ |
| 94 | |
| 95 | inventory = inventory or inventory_list(domain=domain) |
| 96 | storage_path = 'parameters.reclass.storage.node'.split('.') |
| 97 | |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 98 | res = dict() |
Dennis Dmitriev | 30dfb89 | 2017-06-29 20:58:11 +0300 | [diff] [blame] | 99 | for node_name, node in inventory.items(): |
| 100 | storage_nodes = helpers.get_nested_key(node, path=storage_path) |
| 101 | if storage_nodes is not None: |
| 102 | for storage_node_name, storage_node in storage_nodes.items(): |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 103 | if storage_node['domain'] not in res: |
| 104 | res[storage_node['domain']] = dict() |
| 105 | res[storage_node['domain']][storage_node_name] = storage_node |
| 106 | return res |