blob: 7ebb1f3adead5a29a44d3c0c7c43b40b185542f9 [file] [log] [blame]
Dennis Dmitriev1110ac52017-06-22 21:07:37 +03001import reclass
2from reclass.adapters import salt as reclass_salt
3from reclass import config as reclass_config
4from reclass import core as reclass_core
Dennis Dmitrievde847d92017-06-26 18:58:05 +03005
6from reclass_tools import helpers
Dennis Dmitriev94239b12017-06-23 13:18:38 +03007#import salt.cli.call
8#import salt.cli.caller
Dennis Dmitriev1110ac52017-06-22 21:07:37 +03009
10
11def 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 Dmitriev94239b12017-06-23 13:18:38 +030026#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 Dmitriev1110ac52017-06-22 21:07:37 +030036
37
Dennis Dmitriev94239b12017-06-23 13:18:38 +030038def inventory_list(domain=None):
Dennis Dmitriev1110ac52017-06-22 21:07:37 +030039 core = get_core()
Dennis Dmitrievde847d92017-06-26 18:58:05 +030040 inventory = core.inventory()['nodes']
Dennis Dmitriev94239b12017-06-23 13:18:38 +030041 if domain is not None:
Dennis Dmitrievde847d92017-06-26 18:58:05 +030042 inventory = {key:val for (key, val) in inventory.items() if key.endswith(domain)}
43 return inventory
44
45
Dennis Dmitriev30dfb892017-06-29 20:58:11 +030046def get_nodeinfo(minion_id):
47 core = get_core()
48 return core.nodeinfo(minion_id)
49
50
51def vcp_list(domain=None, inventory=None):
Dennis Dmitrievde847d92017-06-26 18:58:05 +030052 """List VCP node names
53
54 Scan all nodes for the object salt.control.cluster.internal.node.XXX.name
Dennis Dmitriev30dfb892017-06-29 20:58:11 +030055 Return set of tuples ((nodename1, domain), (nodename2, domain), ...)
Dennis Dmitrievde847d92017-06-26 18:58:05 +030056 """
57
Dennis Dmitriev30dfb892017-06-29 20:58:11 +030058 inventory = inventory or inventory_list(domain=domain)
Dennis Dmitrievde847d92017-06-26 18:58:05 +030059 vcp_path = 'parameters.salt.control.cluster.internal.node'.split('.')
Dennis Dmitriev30dfb892017-06-29 20:58:11 +030060 domain_path = 'parameters._param.cluster_domain'.split('.')
Dennis Dmitrievde847d92017-06-26 18:58:05 +030061
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 Dmitriev30dfb892017-06-29 20:58:11 +030068 vcp_node_names.add((vcp_node['name'], helpers.get_nested_key(node, path=domain_path)))
Dennis Dmitrievde847d92017-06-26 18:58:05 +030069 return vcp_node_names
70
Dennis Dmitriev30dfb892017-06-29 20:58:11 +030071def reclass_storage(domain=None, inventory=None):
72 """List VCP node names
Dennis Dmitrievde847d92017-06-26 18:58:05 +030073
Dennis Dmitriev30dfb892017-06-29 20:58:11 +030074 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