ansible-later/ansiblelater/__main__.py

99 lines
2.9 KiB
Python
Raw Normal View History

2018-12-19 10:19:07 +00:00
#!/usr/bin/env python
2019-04-04 14:06:18 +00:00
"""Main program."""
2018-12-19 10:19:07 +00:00
import argparse
2019-04-16 09:33:21 +00:00
import multiprocessing
2019-04-16 08:09:14 +00:00
import sys
2019-03-28 00:20:43 +00:00
from ansiblelater import LOG
from ansiblelater import __version__
from ansiblelater import logger
from ansiblelater.command import base
from ansiblelater.command import candidates
2018-12-19 10:19:07 +00:00
def main():
2019-04-04 14:06:18 +00:00
"""Run main program."""
parser = argparse.ArgumentParser(
2020-04-05 12:33:43 +00:00
description="Validate ansible files against best pratice guideline"
)
parser.add_argument(
"-c", "--config", dest="config_file", help="location of configuration file"
)
parser.add_argument(
"-r", "--rules", dest="rules.standards", help="location of standards rules"
)
parser.add_argument(
"-s",
"--standards",
dest="rules.filter",
action="append",
help="limit standards to given ID's"
)
parser.add_argument(
"-x",
"--exclude-standards",
dest="rules.exclude_filter",
action="append",
help="exclude standards by given ID's"
)
parser.add_argument(
"-v", dest="logging.level", action="append_const", const=-1, help="increase log level"
)
parser.add_argument(
"-q", dest="logging.level", action="append_const", const=1, help="decrease log level"
)
2019-04-02 14:34:03 +00:00
parser.add_argument("rules.files", nargs="*")
parser.add_argument("--version", action="version", version="%(prog)s {}".format(__version__))
args = parser.parse_args().__dict__
2018-12-19 10:19:07 +00:00
settings = base.get_settings(args)
2019-04-03 15:42:46 +00:00
config = settings.config
2019-04-02 14:34:03 +00:00
2019-04-03 15:42:46 +00:00
logger.update_logger(LOG, config["logging"]["level"], config["logging"]["json"])
files = config["rules"]["files"]
standards = base.get_standards(config["rules"]["standards"])
2019-04-16 09:33:21 +00:00
workers = max(multiprocessing.cpu_count() - 2, 2)
p = multiprocessing.Pool(workers)
tasks = []
2019-04-02 14:34:03 +00:00
for filename in files:
lines = None
candidate = candidates.classify(filename, settings, standards)
if candidate:
if candidate.binary:
2020-04-11 14:20:41 +00:00
LOG.info("Not reviewing binary file {name}".format(name=filename))
2019-04-02 14:34:03 +00:00
continue
if candidate.vault:
2020-04-11 14:20:41 +00:00
LOG.info("Not reviewing vault file {name}".format(name=filename))
2019-04-02 14:34:03 +00:00
continue
if lines:
2020-04-11 14:20:41 +00:00
LOG.info("Reviewing {candidate} lines {no}".format(candidate=candidate, no=lines))
2019-04-02 14:34:03 +00:00
else:
2020-04-11 14:20:41 +00:00
LOG.info("Reviewing all of {candidate}".format(candidate=candidate))
2019-04-16 09:33:21 +00:00
tasks.append((candidate, settings, lines))
2019-04-02 14:34:03 +00:00
else:
2020-04-11 14:20:41 +00:00
LOG.info("Couldn't classify file {name}".format(name=filename))
2019-04-16 09:33:21 +00:00
errors = (sum(p.map(_review_wrapper, tasks)))
p.close()
p.join()
2019-04-16 08:09:14 +00:00
if not errors == 0:
return_code = 1
else:
return_code = 0
sys.exit(return_code)
2019-04-15 15:26:02 +00:00
2018-12-19 10:19:07 +00:00
2019-04-16 09:33:21 +00:00
def _review_wrapper(args):
(candidate, settings, lines) = args
return candidate.review(settings, lines)
2019-01-08 15:22:19 +00:00
if __name__ == "__main__":
main()