Ejemplo n.º 1
0
def ansiblelint(rulename, candidate, settings):
    result = Result(candidate.path)
    rules = RulesCollection()
    rules.extend(RulesCollection.create_from_directory(default_rulesdir))
    if settings.lintdir:
        rules.extend(RulesCollection.create_from_directory(settings.lintdir))

    fileinfo = dict(path=candidate.path, type=candidate.filetype)
    matches = rules.run(fileinfo, rulename.split(','))
    result.errors = [Error(match.linenumber, "[%s] %s" % (match.rule.id, match.message))
                     for match in matches]
    return result
Ejemplo n.º 2
0
def ansiblelint(rulename, candidate, settings):
    result = Result(candidate.path)
    rules = RulesCollection()
    rules.extend(RulesCollection.create_from_directory(default_rulesdir))
    if settings.lintdir:
        rules.extend(RulesCollection.create_from_directory(settings.lintdir))

    fileinfo = dict(path=candidate.path, type=candidate.filetype)
    matches = rules.run(fileinfo, rulename.split(','))
    result.errors = [Error(match.linenumber, "[%s] %s" % (match.rule.id, match.message))
                     for match in matches]
    return result
Ejemplo n.º 3
0
def main():

    formatter = formatters.Formatter()

    parser = optparse.OptionParser(
        "%prog [options] playbook.yml [playbook2 ...]",
        version="%prog " + __version__)

    parser.add_option('-L',
                      dest='listrules',
                      default=False,
                      action='store_true',
                      help="list all the rules")
    parser.add_option('-q',
                      dest='quiet',
                      default=False,
                      action='store_true',
                      help="quieter, although not silent output")
    parser.add_option('-p',
                      dest='parseable',
                      default=False,
                      action='store_true',
                      help="parseable output in the format of pep8")
    parser.add_option('-r',
                      action='append',
                      dest='rulesdir',
                      default=[],
                      type='str',
                      help="specify one or more rules directories using "
                      "one or more -r arguments. Any -r flags override "
                      "the default rules in %s, unless -R is also used." %
                      ansiblelint.default_rulesdir)
    parser.add_option('-R',
                      action='store_true',
                      default=False,
                      dest='use_default_rules',
                      help="Use default rules in %s in addition to any extra "
                      "rules directories specified with -r. There is "
                      "no need to specify this if no -r flags are used" %
                      ansiblelint.default_rulesdir)
    parser.add_option('-t',
                      dest='tags',
                      action='append',
                      default=[],
                      help="only check rules whose id/tags match these values")
    parser.add_option('-T',
                      dest='listtags',
                      action='store_true',
                      help="list all the tags")
    parser.add_option('-v',
                      dest='verbosity',
                      action='count',
                      help="Increase verbosity level",
                      default=0)
    parser.add_option('-x',
                      dest='skip_list',
                      default=[],
                      action='append',
                      help="only check rules whose id/tags do not " +
                      "match these values")
    parser.add_option('--nocolor',
                      dest='colored',
                      default=hasattr(sys.stdout, 'isatty')
                      and sys.stdout.isatty(),
                      action='store_false',
                      help="disable colored output")
    parser.add_option(
        '--force-color',
        dest='colored',
        action='store_true',
        help="Try force colored output (relying on ansible's code)")
    parser.add_option('--exclude',
                      dest='exclude_paths',
                      action='append',
                      help='path to directories or files to skip. This option'
                      ' is repeatable.',
                      default=[])
    parser.add_option(
        '-c',
        help='Specify configuration file to use.  Defaults to ".ansible-lint"')
    options, args = parser.parse_args(sys.argv[1:])

    config = load_config(options.c)

    if config:
        if 'quiet' in config:
            options.quiet = options.quiet or config['quiet']

        if 'parseable' in config:
            options.parseable = options.parseable or config['parseable']

        if 'use_default_rules' in config:
            options.use_default_rules = options.use_default_rules or config[
                'use_default_rules']

        if 'verbosity' in config:
            options.verbosity = options.verbosity + config['verbosity']

        if 'exclude_paths' in config:
            options.exclude_paths = options.exclude_paths + config[
                'exclude_paths']

        if 'rulesdir' in config:
            options.rulesdir = options.rulesdir + config['rulesdir']

        if 'skip_list' in config:
            options.skip_list = options.skip_list + config['skip_list']

        if 'tags' in config:
            options.tags = options.tags + config['tags']

    if options.quiet:
        formatter = formatters.QuietFormatter()

    if options.parseable:
        formatter = formatters.ParseableFormatter()

    if len(args) == 0 and not (options.listrules or options.listtags):
        parser.print_help(file=sys.stderr)
        return 1

    if options.use_default_rules:
        rulesdirs = options.rulesdir + [ansiblelint.default_rulesdir]
    else:
        rulesdirs = options.rulesdir or [ansiblelint.default_rulesdir]

    rules = RulesCollection()
    for rulesdir in rulesdirs:
        rules.extend(RulesCollection.create_from_directory(rulesdir))

    if options.listrules:
        print(rules)
        return 0

    if options.listtags:
        print(rules.listtags())
        return 0

    if isinstance(options.tags, six.string_types):
        options.tags = options.tags.split(',')

    skip = set()
    for s in options.skip_list:
        skip.update(s.split(','))
    options.skip_list = frozenset(skip)

    playbooks = set(args)
    matches = list()
    checked_files = set()
    for playbook in playbooks:
        runner = ansiblelint.Runner(rules, playbook, options.tags,
                                    options.skip_list, options.exclude_paths,
                                    options.verbosity, checked_files)
        matches.extend(runner.run())

    matches.sort(key=lambda x: (x.filename, x.linenumber, x.rule.id))

    for match in matches:
        print(formatter.format(match, options.colored))

    if len(matches):
        return 2
    else:
        return 0
Ejemplo n.º 4
0
def main():

    formatter = formatters.Formatter()

    parser = optparse.OptionParser(
        "%prog [options] playbook.yml [playbook2 ...]",
        version="%prog " + __version__)

    parser.add_option('-L',
                      dest='listrules',
                      default=False,
                      action='store_true',
                      help="list all the rules")
    parser.add_option('-q',
                      dest='quiet',
                      default=False,
                      action='store_true',
                      help="quieter, although not silent output")
    parser.add_option('-p',
                      dest='parseable',
                      default=False,
                      action='store_true',
                      help="parseable output in the format of pep8")
    parser.add_option('--parseable-severity',
                      dest='parseable_severity',
                      default=False,
                      action='store_true',
                      help="parseable output including severity of rule")
    parser.add_option('-r',
                      action='append',
                      dest='rulesdir',
                      default=[],
                      type='str',
                      help="specify one or more rules directories using "
                      "one or more -r arguments. Any -r flags override "
                      "the default rules in %s, unless -R is also used." %
                      ansiblelint.default_rulesdir)
    parser.add_option('-R',
                      action='store_true',
                      default=False,
                      dest='use_default_rules',
                      help="Use default rules in %s in addition to any extra "
                      "rules directories specified with -r. There is "
                      "no need to specify this if no -r flags are used" %
                      ansiblelint.default_rulesdir)
    parser.add_option('-t',
                      dest='tags',
                      action='append',
                      default=[],
                      help="only check rules whose id/tags match these values")
    parser.add_option('-T',
                      dest='listtags',
                      action='store_true',
                      help="list all the tags")
    parser.add_option('-v',
                      dest='verbosity',
                      action='count',
                      help="Increase verbosity level",
                      default=0)
    parser.add_option('-x',
                      dest='skip_list',
                      default=[],
                      action='append',
                      help="only check rules whose id/tags do not " +
                      "match these values")
    parser.add_option('--nocolor',
                      dest='colored',
                      default=hasattr(sys.stdout, 'isatty')
                      and sys.stdout.isatty(),
                      action='store_false',
                      help="disable colored output")
    parser.add_option(
        '--force-color',
        dest='colored',
        action='store_true',
        help="Try force colored output (relying on ansible's code)")
    parser.add_option('--exclude',
                      dest='exclude_paths',
                      action='append',
                      help='path to directories or files to skip. This option'
                      ' is repeatable.',
                      default=[])
    parser.add_option('--token',
                      dest='token',
                      action='store',
                      type='string',
                      help='github token',
                      default="")
    parser.add_option('--roles_dir',
                      dest='roles_dir',
                      action='store',
                      type='string',
                      help='directory where roles are located',
                      default="")
    parser.add_option('--repo_name',
                      dest='repo_name',
                      action='store',
                      type='string',
                      help='name of github repo in format <owner>/<name>',
                      default="")
    parser.add_option('--pr_id',
                      dest='pr_id',
                      action='store',
                      type='int',
                      help='number of pull request',
                      default=0)
    parser.add_option('--publish',
                      dest='publish',
                      action='store_true',
                      help='publish analysis to github',
                      default=False)
    parser.add_option(
        '-c',
        help='Specify configuration file to use.  Defaults to ".ansible-lint"')
    options, args = parser.parse_args(sys.argv[1:])

    config = load_config(options.c)

    if config:
        if 'quiet' in config:
            options.quiet = options.quiet or config['quiet']

        if 'parseable' in config:
            options.parseable = options.parseable or config['parseable']

        if 'parseable_severity' in config:
            options.parseable_severity = options.parseable_severity or \
                                         config['parseable_severity']

        if 'use_default_rules' in config:
            options.use_default_rules = options.use_default_rules or config[
                'use_default_rules']

        if 'verbosity' in config:
            options.verbosity = options.verbosity + config['verbosity']

        if 'exclude_paths' in config:
            options.exclude_paths = options.exclude_paths + config[
                'exclude_paths']

        if 'rulesdir' in config:
            options.rulesdir = options.rulesdir + config['rulesdir']

        if 'skip_list' in config:
            options.skip_list = options.skip_list + config['skip_list']

        if 'tags' in config:
            options.tags = options.tags + config['tags']

    if options.quiet:
        formatter = formatters.QuietFormatter()

    if options.parseable:
        formatter = formatters.ParseableFormatter()

    if options.parseable_severity:
        formatter = formatters.ParseableSeverityFormatter()

    if len(args) == 0 and not (options.listrules or options.listtags):
        parser.print_help(file=sys.stderr)
        return 1

    if options.use_default_rules:
        rulesdirs = options.rulesdir + [ansiblelint.default_rulesdir]
    else:
        rulesdirs = options.rulesdir or [ansiblelint.default_rulesdir]

    rules = RulesCollection()
    for rulesdir in rulesdirs:
        rules.extend(RulesCollection.create_from_directory(rulesdir))

    if options.listrules:
        print(rules)
        return 0

    if options.listtags:
        print(rules.listtags())
        return 0

    if isinstance(options.tags, six.string_types):
        options.tags = options.tags.split(',')

    skip = set()
    for s in options.skip_list:
        skip.update(s.split(','))
    options.skip_list = frozenset(skip)

    playbooks = set(args)
    matches = list()
    checked_files = set()
    for playbook in playbooks:
        runner = ansiblelint.Runner(rules, playbook, options.tags,
                                    options.skip_list, options.exclude_paths,
                                    options.verbosity, checked_files)
        matches.extend(runner.run())

    matches.sort(key=lambda x: (x.filename, x.linenumber, x.rule.id))

    if "ROLES_DIR" in os.environ:
        options.roles_dir = options.roles_dir or os.environ["ROLES_DIR"]
    if "GITHUB_TOKEN" in os.environ:
        options.token = options.token or os.environ["GITHUB_TOKEN"]
    if "REPO_NAME" in os.environ:
        options.repo_name = options.repo_name or os.environ["REPO_NAME"]
    if "PR" in os.environ:
        try:
            options.pr_id = options.pr_id or int(os.environ["PR"])
        except:
            options.pr_id = options.pr_id
    if "PUBLISH" in os.environ:
        options.publish = options.publish or bool(os.environ["PUBLISH"])

    if config:
        if 'roles_dir' in config:
            options.roles_dir = options.roles_dir or config['roles_dir']
        if 'token' in config:
            options.token = options.token or config['token']
        if 'repo_name' in config:
            options.repo_name = options.repo_name or config['repo_name']
        if 'pr_id' in config:
            options.pr_id = options.pr_id or config['pr_id']

    commit = ""
    if options.publish:
        g = Github(options.token)
        repo = g.get_repo(options.repo_name)
        pr = repo.get_pull(options.pr_id)
        commit = pr.get_commits().reversed[0]

    buff_line = 0
    buff = []
    for match in matches:
        if options.roles_dir in match.filename:
            match.filename = match.filename[len(options.roles_dir.rstrip("/")
                                                ) + 1:]

        if match.linenumber < buff_line:
            if buff:
                rs = post_comment(buff, options.token, options.repo_name,
                                  options.pr_id, commit, options.publish)
                print("{}\n".format(rs))
                buff = []
            buff_line = 0
        if match.linenumber > buff_line and buff:
            rs = post_comment(buff, options.token, options.repo_name,
                              options.pr_id, commit, options.publish)
            print("{}\n".format(rs))
            buff = []
        if match.linenumber > buff_line:
            buff_line = match.linenumber
        if match.linenumber == buff_line:
            buff.append(match)


#        print(formatter.format(match, options.colored))
    if buff:
        rs = post_comment(buff, options.token, options.repo_name,
                          options.pr_id, commit, options.publish)
        print("{}\n".format(rs))
        buff = []
Ejemplo n.º 5
0
def lint(book_id, options, config=None):
    """
    base on ansiblelint
    refer to ansiblelint.__main__.py
    :param book_id:
    :param options:
    :param config:
    :return: None
    """
    formatter = formatters.Formatter()
    options = get_default_options(options)
    where = {
        'book_id': str(book_id),
        'role': 'entry',
    }
    entries = Playbook.find(where)
    if not entries:
        return False

    book = Book.find_by_id(book_id)
    if not book:
        return False

    if config:
        if 'quiet' in config:
            options.quiet = options.quiet or config['quiet']

        if 'parseable' in config:
            options.parseable = options.parseable or config['parseable']

        if 'parseable_severity' in config:
            options.parseable_severity = options.parseable_severity or \
                                         config['parseable_severity']

        if 'use_default_rules' in config:
            options.use_default_rules = options.use_default_rules or config['use_default_rules']

        if 'verbosity' in config:
            options.verbosity = options.verbosity + config['verbosity']

        options.exclude_paths.extend(
            config.get('exclude_paths', []))

        if 'rulesdir' in config:
            options.rulesdir = options.rulesdir + config['rulesdir']

        if 'skip_list' in config:
            options.skip_list = options.skip_list + config['skip_list']

        if 'tags' in config:
            options.tags = options.tags + config['tags']

    if options.quiet:
        formatter = formatters.QuietFormatter()

    if options.parseable:
        formatter = formatters.ParseableFormatter()

    if options.parseable_severity:
        formatter = formatters.ParseableSeverityFormatter()

    # no args triggers auto-detection mode
    # if len(args) == 0 and not (options.listrules or options.listtags):
    #     args = get_playbooks_and_roles(options=options)

    if options.use_default_rules:
        rulesdirs = options.rulesdir + [default_rulesdir]
    else:
        rulesdirs = options.rulesdir or [default_rulesdir]

    rules = RulesCollection()
    for rulesdir in rulesdirs:
        rules.extend(RulesCollection.create_from_directory(rulesdir))

    if options.listrules:
        return 0

    if options.listtags:
        return 0

    if isinstance(options.tags, six.string_types):
        options.tags = options.tags.split(',')

    skip = set()
    for s in options.skip_list:
        skip.update(str(s).split(','))

    options.skip_list = frozenset(skip)
    with build_book_from_db(book.get('name'), options.get('roles')) as book_path:
        playbooks = []
        for record in entries:
            entry = os.path.join(book_path, record['path'][1:])
            playbooks.append(entry)

        playbooks = sorted(set(playbooks))
        matches = list()
        checked_files = set()
        for playbook in playbooks:
            runner = Runner(rules, playbook, options.tags,
                            options.skip_list, options.exclude_paths,
                            options.verbosity, checked_files)
            matches.extend(runner.run())

        matches.sort(key=lambda x: (normpath(x.filename), x.linenumber, x.rule.id))
        results = []
        for match in matches:
            filename = str(match.filename)
            filename = filename.replace(book_path, '')
            results.append({
                'lineNumber': match.linenumber,
                'line': str(match.line),
                'rule': match.rule.id,
                'filename': filename,
                'message': match.message,
            })

        return results
Ejemplo n.º 6
0
def main():

    formatter = formatters.Formatter()

    parser = optparse.OptionParser("%prog [options] playbook.yml [playbook2 ...]",
                                   version="%prog " + __version__)

    parser.add_option('-L', dest='listrules', default=False,
                      action='store_true', help="list all the rules")
    parser.add_option('-q', dest='quiet',
                      default=False,
                      action='store_true',
                      help="quieter, although not silent output")
    parser.add_option('-p', dest='parseable',
                      default=False,
                      action='store_true',
                      help="parseable output in the format of pep8")
    parser.add_option('--parseable-severity', dest='parseable_severity',
                      default=False,
                      action='store_true',
                      help="parseable output including severity of rule")
    parser.add_option('-r', action='append', dest='rulesdir',
                      default=[], type='str',
                      help="specify one or more rules directories using "
                           "one or more -r arguments. Any -r flags override "
                           "the default rules in %s, unless -R is also used."
                           % ansiblelint.default_rulesdir)
    parser.add_option('-R', action='store_true',
                      default=False,
                      dest='use_default_rules',
                      help="Use default rules in %s in addition to any extra "
                           "rules directories specified with -r. There is "
                           "no need to specify this if no -r flags are used"
                           % ansiblelint.default_rulesdir)
    parser.add_option('-t', dest='tags',
                      action='append',
                      default=[],
                      help="only check rules whose id/tags match these values")
    parser.add_option('-T', dest='listtags', action='store_true',
                      help="list all the tags")
    parser.add_option('-v', dest='verbosity', action='count',
                      help="Increase verbosity level",
                      default=0)
    parser.add_option('-x', dest='skip_list', default=[], action='append',
                      help="only check rules whose id/tags do not " +
                      "match these values")
    parser.add_option('--nocolor', dest='colored',
                      default=hasattr(sys.stdout, 'isatty') and sys.stdout.isatty(),
                      action='store_false',
                      help="disable colored output")
    parser.add_option('--force-color', dest='colored',
                      action='store_true',
                      help="Try force colored output (relying on ansible's code)")
    parser.add_option('--exclude', dest='exclude_paths', action='append',
                      help='path to directories or files to skip. This option'
                           ' is repeatable.',
                      default=[])
    parser.add_option('-c', help='Specify configuration file to use.  Defaults to ".ansible-lint"')
    options, args = parser.parse_args(sys.argv[1:])

    config = load_config(options.c)

    if config:
        if 'quiet' in config:
            options.quiet = options.quiet or config['quiet']

        if 'parseable' in config:
            options.parseable = options.parseable or config['parseable']

        if 'parseable_severity' in config:
            options.parseable_severity = options.parseable_severity or \
                                         config['parseable_severity']

        if 'use_default_rules' in config:
            options.use_default_rules = options.use_default_rules or config['use_default_rules']

        if 'verbosity' in config:
            options.verbosity = options.verbosity + config['verbosity']

        if 'exclude_paths' in config:
            options.exclude_paths = options.exclude_paths + config['exclude_paths']

        if 'rulesdir' in config:
            options.rulesdir = options.rulesdir + config['rulesdir']

        if 'skip_list' in config:
            options.skip_list = options.skip_list + config['skip_list']

        if 'tags' in config:
            options.tags = options.tags + config['tags']

    if options.quiet:
        formatter = formatters.QuietFormatter()

    if options.parseable:
        formatter = formatters.ParseableFormatter()

    if options.parseable_severity:
        formatter = formatters.ParseableSeverityFormatter()

    if len(args) == 0 and not (options.listrules or options.listtags):
        parser.print_help(file=sys.stderr)
        return 1

    if options.use_default_rules:
        rulesdirs = options.rulesdir + [ansiblelint.default_rulesdir]
    else:
        rulesdirs = options.rulesdir or [ansiblelint.default_rulesdir]

    rules = RulesCollection()
    for rulesdir in rulesdirs:
        rules.extend(RulesCollection.create_from_directory(rulesdir))

    if options.listrules:
        print(rules)
        return 0

    if options.listtags:
        print(rules.listtags())
        return 0

    if isinstance(options.tags, six.string_types):
        options.tags = options.tags.split(',')

    skip = set()
    for s in options.skip_list:
        skip.update(str(s).split(','))
    options.skip_list = frozenset(skip)

    playbooks = set(args)
    matches = list()
    checked_files = set()
    for playbook in playbooks:
        runner = ansiblelint.Runner(rules, playbook, options.tags,
                                    options.skip_list, options.exclude_paths,
                                    options.verbosity, checked_files)
        matches.extend(runner.run())

    matches.sort(key=lambda x: (x.filename, x.linenumber, x.rule.id))

    for match in matches:
        print(formatter.format(match, options.colored))

    if len(matches):
        return 2
    else:
        return 0