Exemple #1
0
def get_file_parser(hostsfile, groups, loader):
    # check to see if the specified file starts with a
    # shebang (#!/), so if an error is raised by the parser
    # class we can show a more apropos error

    shebang_present = False
    processed = False
    myerr = []
    parser = None

    try:
        inv_file = open(hostsfile)
        first_line = inv_file.readlines()[0]
        inv_file.close()
        if first_line.startswith('#!'):
            shebang_present = True
    except:
        pass

    #FIXME: make this 'plugin loop'
    # script
    if loader.is_executable(hostsfile):
        try:
            parser = InventoryScript(loader=loader,
                                     groups=groups,
                                     filename=hostsfile)
            processed = True
        except Exception as e:
            myerr.append(str(e))
    elif shebang_present:
        myerr.append(
            "The file %s looks like it should be an executable inventory script, but is not marked executable. Perhaps you want to correct this with `chmod +x %s`?"
            % (hostsfile, hostsfile))

    # YAML/JSON
    if not processed and not shebang_present and os.path.splitext(
            hostsfile)[-1] in C.YAML_FILENAME_EXTENSIONS:
        try:
            parser = InventoryYAMLParser(loader=loader,
                                         groups=groups,
                                         filename=hostsfile)
            processed = True
        except Exception as e:
            myerr.append(str(e))

    # ini
    if not processed and not shebang_present:
        try:
            parser = InventoryINIParser(loader=loader,
                                        groups=groups,
                                        filename=hostsfile)
            processed = True
        except Exception as e:
            myerr.append(str(e))

    if not processed and myerr:
        raise AnsibleError('\n'.join(myerr))

    return parser
Exemple #2
0
def read_file(project_id, inv_file):
    file_path = get_file_path(project_id)
    if not file_path:
        return ""
    group = Group(name='all')
    groups = { 'all': group }
    parser = InventoryINIParser([], groups, filename = "%s/%s" %(file_path, inv_file))
    return groups
Exemple #3
0
def get_file_parser(hostsfile, groups, loader):
    # check to see if the specified file starts with a
    # shebang (#!/), so if an error is raised by the parser
    # class we can show a more apropos error

    shebang_present = False
    processed = False
    myerr = []
    parser = None

    try:
        with open(hostsfile, 'rb') as inv_file:
            initial_chars = inv_file.read(2)
            if initial_chars.startswith(b'#!'):
                shebang_present = True
    except:
        pass

    #FIXME: make this 'plugin loop'
    # script
    if loader.is_executable(hostsfile):
        try:
            parser = InventoryScript(loader=loader, groups=groups, filename=hostsfile)
            processed = True
        except Exception as e:
            myerr.append('Attempted to execute "%s" as inventory script: %s' % (hostsfile, to_native(e)))
    elif shebang_present:

        myerr.append("The inventory file \'%s\' looks like it should be an executable inventory script, but is not marked executable. "
                     "Perhaps you want to correct this with `chmod +x %s`?" % (hostsfile, hostsfile))

    # YAML/JSON
    if not processed and not shebang_present and os.path.splitext(hostsfile)[-1] in C.YAML_FILENAME_EXTENSIONS:
        try:
            parser = InventoryYAMLParser(loader=loader, groups=groups, filename=hostsfile)
            processed = True
        except Exception as e:
            myerr.append('Attempted to read "%s" as YAML: %s' % (to_native(hostsfile), to_native(e)))

    # ini
    if not processed and not shebang_present:
        try:
            parser = InventoryINIParser(loader=loader, groups=groups, filename=hostsfile)
            processed = True
        except Exception as e:
            myerr.append('Attempted to read "%s" as ini file: %s ' % (to_native(hostsfile), to_native(e)))

    if not processed and myerr:
        raise AnsibleError('\n'.join(myerr))

    return parser
Exemple #4
0
def get_file_parser(hostsfile, groups, loader):
    # check to see if the specified file starts with a
    # shebang (#!/), so if an error is raised by the parser
    # class we can show a more apropos error

    shebang_present = False
    processed = False
    myerr = []
    parser = None

    try:
        inv_file = open(hostsfile)
        first_line = inv_file.readlines()[0]
        inv_file.close()
        if first_line.startswith('#!'):
            shebang_present = True
    except:
        pass

    if loader.is_executable(hostsfile):
        try:
            parser = InventoryScript(loader=loader,
                                     groups=groups,
                                     filename=hostsfile)
            processed = True
        except Exception as e:
            myerr.append("The file %s is marked as executable, but failed to execute correctly. " % hostsfile + \
                            "If this is not supposed to be an executable script, correct this with `chmod -x %s`." % hostsfile)
            myerr.append(str(e))

    if not processed:
        try:
            parser = InventoryINIParser(loader=loader,
                                        groups=groups,
                                        filename=hostsfile)
            processed = True
        except Exception as e:
            if shebang_present and not loader.is_executable(hostsfile):
                myerr.append("The file %s looks like it should be an executable inventory script, but is not marked executable. " % hostsfile + \
                              "Perhaps you want to correct this with `chmod +x %s`?" % hostsfile)
            else:
                myerr.append(str(e))

    if not processed and myerr:
        raise AnsibleError('\n'.join(myerr))

    return parser