def top(minion_id, storage_type=OPT_STORAGE_TYPE, inventory_base_uri=OPT_INVENTORY_BASE_URI, nodes_uri=OPT_NODES_URI, classes_uri=OPT_CLASSES_URI): env = 'base' # TODO: node environments # if the minion_id is not None, then return just the applications for the # specific minion, otherwise return the entire top data (which we need for # CLI invocations of the adapter): if minion_id is not None: data = get_nodeinfo(storage_type, inventory_base_uri, nodes_uri, classes_uri, minion_id) applications = data.get('applications', []) return {env: applications} else: data = get_inventory(storage_type, inventory_base_uri, nodes_uri, classes_uri) nodes = {} for node_id, node_data in data['nodes'].iteritems(): nodes[node_id] = node_data['applications'] return {env: nodes}
def top( minion_id, storage_type=OPT_STORAGE_TYPE, inventory_base_uri=OPT_INVENTORY_BASE_URI, nodes_uri=OPT_NODES_URI, classes_uri=OPT_CLASSES_URI, class_mappings=None, ): env = "base" # TODO: node environments # if the minion_id is not None, then return just the applications for the # specific minion, otherwise return the entire top data (which we need for # CLI invocations of the adapter): if minion_id is not None: data = get_nodeinfo(storage_type, inventory_base_uri, nodes_uri, classes_uri, minion_id, class_mappings) applications = data.get("applications", []) return {env: applications} else: data = get_inventory(storage_type, inventory_base_uri, nodes_uri, classes_uri, class_mappings) nodes = {} for node_id, node_data in data["nodes"].iteritems(): nodes[node_id] = node_data["applications"] return {env: nodes}
def ext_pillar(minion_id, pillar, storage_type=OPT_STORAGE_TYPE, inventory_base_uri=OPT_INVENTORY_BASE_URI, nodes_uri=OPT_NODES_URI, classes_uri=OPT_CLASSES_URI): data = get_nodeinfo(storage_type, inventory_base_uri, nodes_uri, classes_uri, minion_id) params = data.get('parameters', {}) params['__reclass__'] = {} params['__reclass__']['applications'] = data['applications'] params['__reclass__']['classes'] = data['classes'] return params
def ext_pillar( minion_id, pillar, storage_type=OPT_STORAGE_TYPE, inventory_base_uri=OPT_INVENTORY_BASE_URI, nodes_uri=OPT_NODES_URI, classes_uri=OPT_CLASSES_URI, class_mappings=None, ): data = get_nodeinfo(storage_type, inventory_base_uri, nodes_uri, classes_uri, minion_id, class_mappings) params = data.get("parameters", {}) params["__reclass__"] = {} params["__reclass__"]["applications"] = data["applications"] params["__reclass__"]["classes"] = data["classes"] return params
def main(): try: defaults = {'pretty_print' : OPT_PRETTY_PRINT, 'output' : OPT_OUTPUT } defaults.update(find_and_read_configfile()) options = get_options(RECLASS_NAME, VERSION, DESCRIPTION, defaults=defaults) if options.mode == MODE_NODEINFO: data = get_nodeinfo(options.storage_type, options.inventory_base_uri, options.nodes_uri, options.classes_uri, options.nodename) else: data = get_inventory(options.storage_type, options.inventory_base_uri, options.nodes_uri, options.classes_uri) print output(data, options.output, options.pretty_print) except ReclassException, e: e.exit_with_message(sys.stderr)
def main(): try: defaults = {'pretty_print': OPT_PRETTY_PRINT, 'output': OPT_OUTPUT} defaults.update(find_and_read_configfile()) options = get_options(RECLASS_NAME, VERSION, DESCRIPTION, defaults=defaults) if options.mode == MODE_NODEINFO: data = get_nodeinfo(options.storage_type, options.inventory_base_uri, options.nodes_uri, options.classes_uri, options.nodename) else: data = get_inventory(options.storage_type, options.inventory_base_uri, options.nodes_uri, options.classes_uri) print output(data, options.output, options.pretty_print) except ReclassException, e: e.exit_with_message(sys.stderr)
def cli(): try: # this adapter has to be symlinked to ansible_dir, so we can use this # information to initialise the inventory_base_uri to ansible_dir: ansible_dir = os.path.abspath(os.path.dirname(sys.argv[0])) defaults = {'inventory_base_uri': ansible_dir, 'pretty_print' : True, 'output' : 'json', 'applications_postfix': '_hosts', 'file_extension': OPT_ANSIBLE_FILE_EXTENSION } defaults.update(find_and_read_configfile()) def add_ansible_options_group(parser, defaults): group = optparse.OptionGroup(parser, 'Ansible options', 'Ansible-specific options') group.add_option('--applications-postfix', dest='applications_postfix', default=defaults.get('applications_postfix'), help='postfix to append to applications to '\ 'turn them into groups') parser.add_option_group(group) options = get_options(RECLASS_NAME, VERSION, DESCRIPTION, inventory_shortopt='-l', inventory_longopt='--list', inventory_help='output the inventory', nodeinfo_shortopt='-t', nodeinfo_longopt='--host', nodeinfo_dest='hostname', nodeinfo_help='output host_vars for the given host', add_options_cb=add_ansible_options_group, defaults=defaults) class_mappings = defaults.get('class_mappings') if options.mode == MODE_NODEINFO: data = get_nodeinfo(options.storage_type, options.inventory_base_uri, options.nodes_uri, options.classes_uri, options.file_extension, options.hostname, class_mappings) # Massage and shift the data like Ansible wants it data['parameters']['__reclass__'] = data['__reclass__'] for i in ('classes', 'applications'): data['parameters']['__reclass__'][i] = data[i] data = data['parameters'] else: data = get_inventory(options.storage_type, options.inventory_base_uri, options.nodes_uri, options.classes_uri, options.file_extension, class_mappings) # Ansible inventory is only the list of groups. Groups are the set # of classes plus the set of applications with the postfix added: groups = data['classes'] apps = data['applications'] if options.applications_postfix: postfix = options.applications_postfix groups.update([(k + postfix, v) for k,v in apps.iteritems()]) else: groups.update(apps) data = groups print output(data, options.output, options.pretty_print) except ReclassException, e: e.exit_with_message(sys.stderr)
def cli(): try: # this adapter has to be symlinked to ansible_dir, so we can use this # information to initialise the inventory_base_uri to ansible_dir: ansible_dir = os.path.abspath(os.path.dirname(sys.argv[0])) defaults = {'inventory_base_uri': ansible_dir, 'pretty_print' : True, 'output' : 'json', 'applications_postfix': '_hosts' } defaults.update(find_and_read_configfile()) def add_ansible_options_group(parser, defaults): group = optparse.OptionGroup(parser, 'Ansible options', 'Ansible-specific options') group.add_option('--applications-postfix', dest='applications_postfix', default=defaults.get('applications_postfix'), help='postfix to append to applications to '\ 'turn them into groups') parser.add_option_group(group) options = get_options(RECLASS_NAME, VERSION, DESCRIPTION, inventory_shortopt='-l', inventory_longopt='--list', inventory_help='output the inventory', nodeinfo_shortopt='-t', nodeinfo_longopt='--host', nodeinfo_dest='hostname', nodeinfo_help='output host_vars for the given host', add_options_cb=add_ansible_options_group, defaults=defaults) if options.mode == MODE_NODEINFO: data = get_nodeinfo(options.storage_type, options.inventory_base_uri, options.nodes_uri, options.classes_uri, options.hostname) # Massage and shift the data like Ansible wants it data['parameters']['__reclass__'] = data['__reclass__'] for i in ('classes', 'applications'): data['parameters']['__reclass__'][i] = data[i] data = data['parameters'] else: data = get_inventory(options.storage_type, options.inventory_base_uri, options.nodes_uri, options.classes_uri) # Ansible inventory is only the list of groups. Groups are the set # of classes plus the set of applications with the postfix added: groups = data['classes'] apps = data['applications'] if options.applications_postfix: postfix = options.applications_postfix groups.update([(k + postfix, v) for k,v in apps.iteritems()]) else: groups.update(apps) data = groups print output(data, options.output, options.pretty_print) except ReclassException, e: e.exit_with_message(sys.stderr)