Beispiel #1
0
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)

        storage = get_storage(options.storage_type, options.nodes_uri,
                              options.classes_uri, default_environment='base')
        class_mappings = defaults.get('class_mappings')
        reclass = Core(storage, class_mappings)

        sys.path.append(options.inventory_base_uri)

        if options.mode == MODE_NODEINFO:
            data = reclass.nodeinfo(options.nodename)

        else:
            data = reclass.inventory()

        print output(data, options.output, options.pretty_print)

    except ReclassException, e:
        e.exit_with_message(sys.stderr)
Beispiel #2
0
def cli():
    try:
        inventory_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
        defaults = {'pretty_print' : True,
                    'output' : 'yaml',
                    'inventory_base_uri': inventory_dir
                   }
        defaults.update(find_and_read_configfile())
        options = get_options(RECLASS_NAME, VERSION, DESCRIPTION,
                              inventory_shortopt='-t',
                              inventory_longopt='--top',
                              inventory_help='output the state tops (inventory)',
                              nodeinfo_shortopt='-p',
                              nodeinfo_longopt='--pillar',
                              nodeinfo_dest='nodename',
                              nodeinfo_help='output pillar data for a specific node',
                              defaults=defaults)

        if options.mode == MODE_NODEINFO:
            data = ext_pillar(options.nodename, {},
                              storage_type=options.storage_type,
                              inventory_base_uri=options.inventory_base_uri,
                              nodes_uri=options.nodes_uri,
                              classes_uri=options.classes_uri)
        else:
            data = top(minion_id=None,
                       storage_type=options.storage_type,
                       inventory_base_uri=options.inventory_base_uri,
                       nodes_uri=options.nodes_uri,
                       classes_uri=options.classes_uri)

        print output(data, options.output, options.pretty_print)

    except ReclassException, e:
        e.exit_with_message(sys.stderr)
Beispiel #3
0
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)

        storage = get_storage(options.storage_type,
                              options.nodes_uri,
                              options.classes_uri,
                              default_environment='base')
        class_mappings = defaults.get('class_mappings')
        reclass = Core(storage, class_mappings)

        sys.path.append(options.inventory_base_uri)

        if options.mode == MODE_NODEINFO:
            data = reclass.nodeinfo(options.nodename)

        else:
            data = reclass.inventory()

        print output(data, options.output, options.pretty_print)

    except ReclassException, e:
        e.exit_with_message(sys.stderr)
Beispiel #4
0
def cli():
    try:
        inventory_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
        defaults = {
            'pretty_print': True,
            'no_refs': False,
            'output': 'yaml',
            'inventory_base_uri': inventory_dir
        }
        defaults.update(find_and_read_configfile())
        options = get_options(
            RECLASS_NAME,
            VERSION,
            DESCRIPTION,
            inventory_shortopt='-t',
            inventory_longopt='--top',
            inventory_help='output the state tops (inventory)',
            nodeinfo_shortopt='-p',
            nodeinfo_longopt='--pillar',
            nodeinfo_dest='nodename',
            nodeinfo_help='output pillar data for a specific node',
            defaults=defaults)
        class_mappings = defaults.get('class_mappings')
        defaults.update(vars(options))
        defaults.pop("storage_type", None)
        defaults.pop("inventory_base_uri", None)
        defaults.pop("nodes_uri", None)
        defaults.pop("classes_uri", None)
        defaults.pop("class_mappings", None)

        if options.mode == MODE_NODEINFO:
            data = ext_pillar(options.nodename, {},
                              storage_type=options.storage_type,
                              inventory_base_uri=options.inventory_base_uri,
                              nodes_uri=options.nodes_uri,
                              classes_uri=options.classes_uri,
                              class_mappings=class_mappings,
                              **defaults)
        else:
            data = top(minion_id=None,
                       storage_type=options.storage_type,
                       inventory_base_uri=options.inventory_base_uri,
                       nodes_uri=options.nodes_uri,
                       classes_uri=options.classes_uri,
                       class_mappings=class_mappings,
                       **defaults)

        print output(data, options.output, options.pretty_print,
                     options.no_refs)

    except ReclassException, e:
        e.exit_with_message(sys.stderr)
Beispiel #5
0
def cli():
    try:
        inventory_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
        defaults = {
            "pretty_print": True,
            "output": "yaml",
            "inventory_base_uri": inventory_dir,
            "file_extension": OPT_SALT_FILE_EXTENSION,
        }
        defaults.update(find_and_read_configfile())
        options = get_options(
            RECLASS_NAME,
            VERSION,
            DESCRIPTION,
            inventory_shortopt="-t",
            inventory_longopt="--top",
            inventory_help="output the state tops (inventory)",
            nodeinfo_shortopt="-p",
            nodeinfo_longopt="--pillar",
            nodeinfo_dest="nodename",
            nodeinfo_help="output pillar data for a specific node",
            defaults=defaults,
        )
        class_mappings = defaults.get("class_mappings")

        if options.mode == MODE_NODEINFO:
            data = ext_pillar(
                options.nodename,
                {},
                storage_type=options.storage_type,
                inventory_base_uri=options.inventory_base_uri,
                nodes_uri=options.nodes_uri,
                classes_uri=options.classes_uri,
                file_extension=options.file_extension,
                class_mappings=class_mappings,
            )
        else:
            data = top(
                minion_id=None,
                storage_type=options.storage_type,
                inventory_base_uri=options.inventory_base_uri,
                nodes_uri=options.nodes_uri,
                classes_uri=options.classes_uri,
                file_extension=options.file_extension,
                class_mappings=class_mappings,
            )

        print output(data, options.output, options.pretty_print)

    except ReclassException, e:
        e.exit_with_message(sys.stderr)
Beispiel #6
0
def main():
    try:
        defaults = {
            'no_refs': OPT_NO_REFS,
            'pretty_print': OPT_PRETTY_PRINT,
            'output': OPT_OUTPUT
        }
        defaults.update(find_and_read_configfile())

        options = get_options(RECLASS_NAME,
                              VERSION,
                              DESCRIPTION,
                              defaults=defaults)
        storage = get_storage(options.storage_type, options.nodes_uri,
                              options.classes_uri)
        class_mappings = defaults.get('class_mappings')
        defaults.update(vars(options))
        settings = Settings(defaults)
        reclass = Core(storage, class_mappings, settings)

        if options.mode == MODE_NODEINFO:
            data = reclass.nodeinfo(options.nodename)
        else:
            data = reclass.inventory()

        print(
            output(data, options.output, options.pretty_print,
                   options.no_refs))

    except ReclassException as e:
        e.exit_with_message(sys.stderr)

    sys.exit(posix.EX_OK)
Beispiel #7
0
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)
Beispiel #8
0
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)
Beispiel #9
0
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,
            'no_refs': False,
            '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)

        storage = get_storage(options.storage_type, options.nodes_uri,
                              options.classes_uri)
        class_mappings = defaults.get('class_mappings')
        defaults.update(vars(options))
        settings = Settings(defaults)
        reclass = Core(storage, class_mappings, settings)

        if options.mode == MODE_NODEINFO:
            data = reclass.nodeinfo(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 = reclass.inventory()
            # 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,
                     options.no_refs)

    except ReclassException, e:
        e.exit_with_message(sys.stderr)
Beispiel #10
0
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',
                    'no_meta': True
                   }
        defaults.update(find_and_read_configfile())
        sys.path.append(ansible_dir)

        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)

        storage = get_storage(options.storage_type, options.nodes_uri,
                              options.classes_uri)
        class_mappings = defaults.get('class_mappings')
        no_meta = defaults.get('no_meta')
        reclass = Core(storage, class_mappings)

        if options.mode == MODE_NODEINFO:
            data = node_to_node(reclass.nodeinfo(options.hostname))
        else:
            data = reclass.inventory()
            # 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)

            if no_meta:
                data = groups
            else:
                hostvars = dict()
                for node, nodeinfo in data['nodes'].items():
                    hostvars[node] = node_to_node(nodeinfo)
                data = groups
                data['_meta'] = {'hostvars': hostvars}

        print output(data, options.output, options.pretty_print)

    except ReclassException, e:
        e.exit_with_message(sys.stderr)
Beispiel #11
0
def ansible_adapter(ansible_dir, exc_handler):
    try:
        if len(sys.argv) == 1:
            raise InvocationError('Need to specify --list or --host.',
                                  posix.EX_USAGE)

        # The adapter resides in the Ansible directory, so let's look there
        # for an optional configuration file called reclass-config.yml.
        options = {'output':'json', 'pretty_print':True}
        config_path = os.path.join(ansible_dir, 'reclass-config.yml')
        if os.path.exists(config_path) and os.access(config_path, os.R_OK):
            options.update(reclass.config.read_config_file(config_path))

        # Massage options into shape
        if 'storage_type' not in options:
            options['storage_type'] = 'yaml_fs'

        if 'nodes_uri' not in options:
            nodes_uri = os.path.join(ansible_dir, 'nodes')
            if stat.S_ISDIR(os.stat(nodes_uri).st_mode):
                options['nodes_uri'] = nodes_uri
            else:
                raise InvocationError('nodes_uri not specified',
                                      posix.EX_USAGE)

        if 'classes_uri' not in options:
            classes_uri = os.path.join(ansible_dir, 'classes')
            if not stat.S_ISDIR(os.stat(classes_uri).st_mode):
                classes_uri = options['nodes_uri']
            options['classes_uri'] = classes_uri

        if 'applications_postfix' not in options:
            options['applications_postfix'] = '_hosts'

        # Invoke reclass according to what Ansible wants.
        # If the 'node' option is set, we want node information. If the option
        # is False instead, we print the inventory. Yeah for option abuse!
        if sys.argv[1] == '--list':
            if len(sys.argv) > 2:
                raise InvocationError('Unknown arguments: ' + \
                                    ' '.join(sys.argv[2:]), posix.EX_USAGE)
            options['node'] = False

        elif sys.argv[1] == '--host':
            if len(sys.argv) < 3:
                raise InvocationError('Missing hostname.', posix.EX_USAGE)
            elif len(sys.argv) > 3:
                raise InvocationError('Unknown arguments: ' + \
                                    ' '.join(sys.argv[3:]), posix.EX_USAGE)
            options['node'] = sys.argv[2]

        else:
            raise InvocationError('Unknown mode (--list or --host required).',
                                  posix.EX_USAGE)

        data = get_data(options['storage_type'], options['nodes_uri'],
                        options['classes_uri'],
                        options['applications_postfix'], options['node'])

        if options['node']:
            # 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:
            # Ansible inventory is only the list of groups
            data = data['groups']

        print output(data, options['output'], options['pretty_print'])

    except ReclassException, e:
        exc_handler(e.message, e.rc)
Beispiel #12
0
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)