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
|
|
|
|
2019-03-28 15:54:45 +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
|
|
|
|
2023-02-10 07:51:17 +00:00
|
|
|
from ansiblelater import LOG, __version__, logger
|
2021-01-30 15:52:48 +00:00
|
|
|
from ansiblelater.candidate import Candidate
|
2024-01-25 20:40:15 +00:00
|
|
|
from ansiblelater.rule import SingleRules
|
2021-01-30 15:52:48 +00:00
|
|
|
from ansiblelater.settings import Settings
|
2018-12-19 10:19:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2019-04-04 14:06:18 +00:00
|
|
|
"""Run main program."""
|
2019-03-28 15:54:45 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
2020-06-03 14:23:46 +00:00
|
|
|
description="Validate Ansible files against best practice guideline"
|
2020-04-05 12:33:43 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-01-30 15:52:48 +00:00
|
|
|
"-c", "--config", dest="config_file", metavar="CONFIG", help="path to configuration file"
|
2020-04-05 12:33:43 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-01-30 15:52:48 +00:00
|
|
|
"-r",
|
|
|
|
"--rules-dir",
|
2024-01-25 20:40:15 +00:00
|
|
|
dest="rules.dir",
|
|
|
|
metavar="DIR",
|
2021-01-30 15:52:48 +00:00
|
|
|
action="append",
|
2024-01-25 20:40:15 +00:00
|
|
|
help="directory of rules",
|
2021-01-30 15:52:48 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-B",
|
2024-01-25 20:40:15 +00:00
|
|
|
"--no-builtin",
|
|
|
|
dest="rules.builtin",
|
2021-01-30 15:52:48 +00:00
|
|
|
action="store_false",
|
2024-01-25 20:40:15 +00:00
|
|
|
help="disables built-in rules",
|
2020-04-05 12:33:43 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2024-01-25 20:40:15 +00:00
|
|
|
"-i",
|
|
|
|
"--include-rules",
|
|
|
|
dest="rules.include_filter",
|
|
|
|
metavar="TAGS",
|
2020-04-05 12:33:43 +00:00
|
|
|
action="append",
|
2024-01-25 20:40:15 +00:00
|
|
|
help="limit rules to given id/tags",
|
2020-04-05 12:33:43 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-x",
|
2024-01-25 20:40:15 +00:00
|
|
|
"--exclude-rules",
|
2020-04-05 12:33:43 +00:00
|
|
|
dest="rules.exclude_filter",
|
2024-01-25 20:40:15 +00:00
|
|
|
metavar="TAGS",
|
2020-04-05 12:33:43 +00:00
|
|
|
action="append",
|
2024-01-25 20:40:15 +00:00
|
|
|
help="exclude rules by given it/tags",
|
2020-04-05 12:33:43 +00:00
|
|
|
)
|
|
|
|
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="*")
|
2023-01-09 10:59:25 +00:00
|
|
|
parser.add_argument("-V", "--version", action="version", version=f"%(prog)s {__version__}")
|
2019-03-28 15:54:45 +00:00
|
|
|
|
|
|
|
args = parser.parse_args().__dict__
|
2018-12-19 10:19:07 +00:00
|
|
|
|
2021-01-30 15:52:48 +00:00
|
|
|
settings = Settings(args=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"])
|
2024-01-25 20:40:15 +00:00
|
|
|
SingleRules(config["rules"]["dir"])
|
2019-04-03 15:42:46 +00:00
|
|
|
|
2019-04-16 09:33:21 +00:00
|
|
|
workers = max(multiprocessing.cpu_count() - 2, 2)
|
|
|
|
p = multiprocessing.Pool(workers)
|
|
|
|
tasks = []
|
2021-01-30 15:52:48 +00:00
|
|
|
for filename in config["rules"]["files"]:
|
|
|
|
candidate = Candidate.classify(filename, settings)
|
2019-04-02 14:34:03 +00:00
|
|
|
if candidate:
|
|
|
|
if candidate.binary:
|
2023-01-09 10:59:25 +00:00
|
|
|
LOG.info(f"Not reviewing binary file {filename}")
|
2019-04-02 14:34:03 +00:00
|
|
|
continue
|
|
|
|
if candidate.vault:
|
2023-01-09 10:59:25 +00:00
|
|
|
LOG.info(f"Not reviewing vault file {filename}")
|
2019-04-02 14:34:03 +00:00
|
|
|
continue
|
2023-02-10 07:51:17 +00:00
|
|
|
|
|
|
|
LOG.info(f"Reviewing all of {candidate}")
|
|
|
|
tasks.append(candidate)
|
2019-04-02 14:34:03 +00:00
|
|
|
else:
|
2023-01-09 10:59:25 +00:00
|
|
|
LOG.info(f"Couldn't classify file {filename}")
|
2019-04-08 14:55:07 +00:00
|
|
|
|
2023-11-10 13:50:48 +00:00
|
|
|
errors = sum(p.map(_review_wrapper, tasks))
|
2019-04-16 09:33:21 +00:00
|
|
|
p.close()
|
|
|
|
p.join()
|
|
|
|
|
2023-02-10 07:51:17 +00:00
|
|
|
return_code = 1 if errors != 0 else 0
|
2019-04-16 08:09:14 +00:00
|
|
|
|
|
|
|
sys.exit(return_code)
|
2019-04-15 15:26:02 +00:00
|
|
|
|
2018-12-19 10:19:07 +00:00
|
|
|
|
2021-01-30 15:52:48 +00:00
|
|
|
def _review_wrapper(candidate):
|
|
|
|
return candidate.review()
|
2019-04-16 09:33:21 +00:00
|
|
|
|
|
|
|
|
2019-01-08 15:22:19 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|