Add reclass-inventory-list command
Example:
$ reclass-inventory-list
$ reclass-inventory-list --all
diff --git a/reclass_tools/cli.py b/reclass_tools/cli.py
index 418d5dc..d6aa10c 100644
--- a/reclass_tools/cli.py
+++ b/reclass_tools/cli.py
@@ -90,3 +90,22 @@
params.key_name,
verbose=params.verbose,
pretend=pretend)
+
+def inventory_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('--all', dest='all_nodes', action='store_const', const=True,
+ help=('Show all the nodes available for reclass. '
+ 'By default, show nodes only for the same domain '
+ 'as used for the current minion'),
+ default=False)
+
+ params = parser.parse_args(args)
+
+ reclass_models.inventory_list(all_nodes=params.all_nodes)
diff --git a/reclass_tools/reclass_models.py b/reclass_tools/reclass_models.py
new file mode 100644
index 0000000..1bc83de
--- /dev/null
+++ b/reclass_tools/reclass_models.py
@@ -0,0 +1,43 @@
+import reclass
+from reclass.adapters import salt as reclass_salt
+from reclass import config as reclass_config
+from reclass import core as reclass_core
+import salt.cli.call
+import salt.cli.caller
+
+
+def get_core():
+ """Initializes reclass Core() using /etc/reclass settings"""
+
+ defaults = reclass_config.find_and_read_configfile()
+ inventory_base_uri = defaults['inventory_base_uri']
+ storage_type = defaults['storage_type']
+
+ nodes_uri, classes_uri = reclass_config.path_mangler(inventory_base_uri,
+ None, None)
+ storage = reclass.get_storage(storage_type, nodes_uri, classes_uri,
+ default_environment='base')
+
+ return reclass_core.Core(storage, None, None)
+
+
+def get_minion_domain():
+ """Try to get domain from the local salt minion"""
+ client = salt.cli.call.SaltCall()
+ client.parse_args(args=['pillar.items'])
+ caller = salt.cli.caller.Caller.factory(client.config)
+ result = caller.call()
+ # Warning! There is a model-related parameter
+ # TODO: move the path to the parameter to a settings/defaults
+ domain = result['return']['_param']['cluster_domain']
+ return domain
+
+
+def inventory_list(all_nodes=False):
+ core = get_core()
+ inventory = core.inventory()
+ nodes_list = inventory['nodes'].keys()
+ if not all_nodes:
+ domain = get_minion_domain()
+ nodes_list = [node for node in nodes_list if domain in node]
+ print('\n'.join(sorted(nodes_list)))
diff --git a/reclass_tools/walk_models.py b/reclass_tools/walk_models.py
index dbfd211..a07e51b 100644
--- a/reclass_tools/walk_models.py
+++ b/reclass_tools/walk_models.py
@@ -1,5 +1,3 @@
-#!/usr/bin/python
-# -*- coding: utf-8 -*-
import copy
import hashlib
diff --git a/setup.cfg b/setup.cfg
index 6bafdf7..6fad0ba 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -29,3 +29,4 @@
reclass-dump-params = reclass_tools.cli:dump_params
reclass-remove-key = reclass_tools.cli:remove_key
reclass-show-key = reclass_tools.cli:show_key
+ reclass-inventory-list = reclass_tools.cli:inventory_list