Add command 'reclass-vcp-list'
Shows all VM names that will be used for VCP
in the parameters.salt.control.cluster object on all nodes
diff --git a/reclass_tools/cli.py b/reclass_tools/cli.py
index 8b4ebe7..48e3d9f 100644
--- a/reclass_tools/cli.py
+++ b/reclass_tools/cli.py
@@ -91,6 +91,7 @@
verbose=params.verbose,
pretend=pretend)
+
def inventory_list(args=None):
try:
from reclass_tools import reclass_models
@@ -106,4 +107,25 @@
params = parser.parse_args(args)
- reclass_models.inventory_list(domain=params.domain)
+ inventory = reclass_models.inventory_list(domain=params.domain)
+
+ print('\n'.join(sorted(inventory.keys())))
+
+def vcp_list(args=None):
+ try:
+ from reclass_tools import reclass_models
+ except ImportError:
+ print("Please run this tool on the salt-master node with installed 'reclass'")
+ return
+
+ parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter,
+ description="")
+ parser.add_argument('--domain', '-d', dest='domain',
+ help=('Show only the nodes which names are ended with the specified domain, for example:'
+ ' reclass-inventory-list -d example.local'))
+
+ params = parser.parse_args(args)
+
+ vcp_node_names = reclass_models.vcp_list(domain=params.domain)
+ print('\n'.join(sorted(vcp_node_names)))
+
diff --git a/reclass_tools/helpers.py b/reclass_tools/helpers.py
new file mode 100644
index 0000000..fcfc564
--- /dev/null
+++ b/reclass_tools/helpers.py
@@ -0,0 +1,32 @@
+
+def get_nested_key(data, path=None):
+ if type(path) is not list:
+ raise("Use 'list' object with key names for 'path'")
+ for key in path:
+ value = data.get(key, None)
+ if value:
+ data = value
+ else:
+ return None
+ return data
+
+
+def remove_nested_key(data, path=None):
+ if type(path) is not list:
+ raise("Use 'list' object with key names for 'path'")
+
+ # Remove the value from the specified key
+ val = get_nested_key(data, path[:-1])
+ val[path[-1]] = None
+
+ # Clear parent keys if empty
+ while path:
+ val = get_nested_key(data, path)
+ if val:
+ # Non-empty value, nothing to do
+ return
+ else:
+ get_nested_key(data, path[:-1]).pop(path[-1])
+ path = path[:-1]
+
+
diff --git a/reclass_tools/reclass_models.py b/reclass_tools/reclass_models.py
index 20874d6..9f90fa7 100644
--- a/reclass_tools/reclass_models.py
+++ b/reclass_tools/reclass_models.py
@@ -2,6 +2,8 @@
from reclass.adapters import salt as reclass_salt
from reclass import config as reclass_config
from reclass import core as reclass_core
+
+from reclass_tools import helpers
#import salt.cli.call
#import salt.cli.caller
@@ -35,9 +37,28 @@
def inventory_list(domain=None):
core = get_core()
- inventory = core.inventory()
- nodes_list = inventory['nodes'].keys()
+ inventory = core.inventory()['nodes']
if domain is not None:
- #domain = get_minion_domain()
- nodes_list = [node for node in nodes_list if node.endswith(domain)]
- print('\n'.join(sorted(nodes_list)))
+ inventory = {key:val for (key, val) in inventory.items() if key.endswith(domain)}
+ return inventory
+
+
+def vcp_list(domain=None):
+ """List VCP node names
+
+ Scan all nodes for the object salt.control.cluster.internal.node.XXX.name
+ """
+
+ inventory = inventory_list(domain=domain)
+ vcp_path = 'parameters.salt.control.cluster.internal.node'.split('.')
+
+ vcp_node_names = set()
+
+ for node_name, node in inventory.items():
+ vcp_nodes = helpers.get_nested_key(node, path=vcp_path)
+ if vcp_nodes is not None:
+ for vcp_node_name, vcp_node in vcp_nodes.items():
+ vcp_node_names.add(vcp_node['name'])
+ return vcp_node_names
+
+
diff --git a/reclass_tools/walk_models.py b/reclass_tools/walk_models.py
index a07e51b..d57aef5 100644
--- a/reclass_tools/walk_models.py
+++ b/reclass_tools/walk_models.py
@@ -8,6 +8,8 @@
import urllib2
import yaml
+from reclass_tools import helpers
+
def walkfiles(topdir, verbose=False):
walker = os.walk(topdir)
@@ -102,37 +104,6 @@
self.fobj.close()
-def get_nested_key(data, path=None):
- if type(path) is not list:
- raise("Use 'list' object with key names for 'path'")
- for key in path:
- value = data.get(key, None)
- if value:
- data = value
- else:
- return None
- return data
-
-
-def remove_nested_key(data, path=None):
- if type(path) is not list:
- raise("Use 'list' object with key names for 'path'")
-
- # Remove the value from the specified key
- val = get_nested_key(data, path[:-1])
- val[path[-1]] = None
-
- # Clear parent keys if empty
- while path:
- val = get_nested_key(data, path)
- if val:
- # Non-empty value, nothing to do
- return
- else:
- get_nested_key(data, path[:-1]).pop(path[-1])
- path = path[:-1]
-
-
def get_all_reclass_params(paths, verbose=False):
"""Return dict with all used values for each param"""
_params = dict()
@@ -142,7 +113,7 @@
model = yaml_read(log.fname)
if model is not None:
# Collect all params from the models
- _param = get_nested_key(model, ['parameters', '_param'])
+ _param = helpers.get_nested_key(model, ['parameters', '_param'])
if _param:
for key, val in _param.items():
if key in _params:
@@ -173,7 +144,7 @@
if model is not None:
# Clear linux.network.interfaces
- nested_key = get_nested_key(model, remove_key)
+ nested_key = helpers.get_nested_key(model, remove_key)
if nested_key:
found_keys[fyml.fname] = copy.deepcopy(nested_key)
if pretend:
@@ -185,7 +156,7 @@
fyml.fname))
print(yaml.dump(nested_key, default_flow_style=False))
- remove_nested_key(model, remove_key)
+ helpers.remove_nested_key(model, remove_key)
with open(fyml.fname, 'w') as f:
f.write(
diff --git a/setup.cfg b/setup.cfg
index 6fad0ba..10aad7b 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -30,3 +30,4 @@
reclass-remove-key = reclass_tools.cli:remove_key
reclass-show-key = reclass_tools.cli:show_key
reclass-inventory-list = reclass_tools.cli:inventory_list
+ reclass-vcp-list = reclass_tools.cli:vcp_list