blob: 4f85e5ed8455912e700aa83f527a902596f7ac40 [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.
14
Dennis Dmitriev1110ac52017-06-22 21:07:37 +030015import reclass
Dennis Dmitriev566db4b2017-07-18 18:13:07 +030016# from reclass.adapters import salt as reclass_salt
Dennis Dmitriev1110ac52017-06-22 21:07:37 +030017from reclass import config as reclass_config
18from reclass import core as reclass_core
Dennis Dmitrievde847d92017-06-26 18:58:05 +030019
20from reclass_tools import helpers
Dennis Dmitriev566db4b2017-07-18 18:13:07 +030021# import salt.cli.call
22# import salt.cli.caller
Dennis Dmitriev1110ac52017-06-22 21:07:37 +030023
24
25def 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 Dmitriev566db4b2017-07-18 18:13:07 +030040# 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 Dmitriev1110ac52017-06-22 21:07:37 +030050
51
Dennis Dmitriev94239b12017-06-23 13:18:38 +030052def inventory_list(domain=None):
Dennis Dmitriev1110ac52017-06-22 21:07:37 +030053 core = get_core()
Dennis Dmitrievde847d92017-06-26 18:58:05 +030054 inventory = core.inventory()['nodes']
Dennis Dmitriev94239b12017-06-23 13:18:38 +030055 if domain is not None:
Dennis Dmitriev566db4b2017-07-18 18:13:07 +030056 inventory = {key: val for (key, val) in inventory.items()
57 if key.endswith(domain)}
Dennis Dmitrievde847d92017-06-26 18:58:05 +030058 return inventory
59
60
Dennis Dmitriev30dfb892017-06-29 20:58:11 +030061def get_nodeinfo(minion_id):
62 core = get_core()
63 return core.nodeinfo(minion_id)
64
65
66def vcp_list(domain=None, inventory=None):
Dennis Dmitrievde847d92017-06-26 18:58:05 +030067 """List VCP node names
68
69 Scan all nodes for the object salt.control.cluster.internal.node.XXX.name
Dennis Dmitriev30dfb892017-06-29 20:58:11 +030070 Return set of tuples ((nodename1, domain), (nodename2, domain), ...)
Dennis Dmitrievde847d92017-06-26 18:58:05 +030071 """
72
Dennis Dmitriev30dfb892017-06-29 20:58:11 +030073 inventory = inventory or inventory_list(domain=domain)
Dennis Dmitrievde847d92017-06-26 18:58:05 +030074 vcp_path = 'parameters.salt.control.cluster.internal.node'.split('.')
Dennis Dmitriev30dfb892017-06-29 20:58:11 +030075 domain_path = 'parameters._param.cluster_domain'.split('.')
Dennis Dmitrievde847d92017-06-26 18:58:05 +030076
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 Dmitriev566db4b2017-07-18 18:13:07 +030083 vcp_node_names.add((
84 vcp_node['name'],
85 helpers.get_nested_key(node, path=domain_path)))
Dennis Dmitrievde847d92017-06-26 18:58:05 +030086 return vcp_node_names
87
Dennis Dmitriev566db4b2017-07-18 18:13:07 +030088
Dennis Dmitriev30dfb892017-06-29 20:58:11 +030089def reclass_storage(domain=None, inventory=None):
90 """List VCP node names
Dennis Dmitrievde847d92017-06-26 18:58:05 +030091
Dennis Dmitriev30dfb892017-06-29 20:58:11 +030092 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 Dmitriev566db4b2017-07-18 18:13:07 +030098 res = dict()
Dennis Dmitriev30dfb892017-06-29 20:58:11 +030099 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 Dmitriev566db4b2017-07-18 18:13:07 +0300103 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