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