ansible-later/ansiblelater/candidate.py

273 lines
7.4 KiB
Python
Raw Normal View History

2019-04-05 20:05:06 +00:00
"""Review candidates."""
2019-04-02 14:34:03 +00:00
import codecs
2019-04-03 15:42:46 +00:00
import copy
2019-04-02 14:34:03 +00:00
import os
from ansible.plugins.loader import module_loader
from ansiblelater import LOG
from ansiblelater.logger import flag_extra
from ansiblelater.rule import RuleBase, SingleRules
2019-04-02 14:34:03 +00:00
class Candidate:
2019-04-02 14:34:03 +00:00
"""
Meta object for all files which later has to process.
Each file passed to later will be classified by type and
bundled with necessary meta informations for rule processing.
"""
def __init__(self, filename, settings={}, rules=[]): # noqa
2019-04-02 14:34:03 +00:00
self.path = filename
self.binary = False
self.vault = False
self.filetype = type(self).__name__.lower()
self.faulty = False
self.config = settings.config
self.settings = settings
2019-04-02 14:34:03 +00:00
try:
with codecs.open(filename, mode="rb", encoding="utf-8") as f:
if f.readline().startswith("$ANSIBLE_VAULT"):
self.vault = True
except UnicodeDecodeError:
self.binary = True
def _filter_rules(self):
target_rules = []
includes = self.config["rules"]["include_filter"]
excludes = self.config["rules"]["exclude_filter"]
2019-04-03 15:42:46 +00:00
2019-04-17 10:33:23 +00:00
if len(includes) == 0:
includes = [s.rid for s in self.rules]
2019-04-17 10:33:23 +00:00
for rule in self.rules:
if rule.rid in includes and rule.rid not in excludes:
target_rules.append(rule)
2019-04-03 15:42:46 +00:00
return target_rules
2019-04-03 15:42:46 +00:00
def review(self):
2019-04-02 14:34:03 +00:00
errors = 0
self.rules = SingleRules(self.config["rules"]["dir"]).rules
2019-04-02 14:34:03 +00:00
for rule in self._filter_rules():
2024-01-28 20:38:56 +00:00
if type(self).__name__.lower() not in rule.types:
2019-04-02 14:34:03 +00:00
continue
2019-04-15 15:26:02 +00:00
result = rule.check(self, self.config)
2019-04-02 14:34:03 +00:00
if not result:
LOG.error(f"rule '{rule.rid}' returns an empty result object. Check failed!")
continue
2020-04-05 12:33:43 +00:00
labels = {
"tag": "review",
"rule": rule.description,
2020-04-05 12:33:43 +00:00
"file": self.path,
"passed": True,
2020-04-05 12:33:43 +00:00
}
2019-04-03 15:42:46 +00:00
if rule.rid and rule.rid.strip():
labels["rid"] = rule.rid
for err in result.errors:
2019-04-03 15:42:46 +00:00
err_labels = copy.copy(labels)
err_labels["passed"] = False
rid = self._format_id(rule.rid)
path = self.path
description = rule.description
if isinstance(err, RuleBase.Error):
2019-04-03 15:42:46 +00:00
err_labels.update(err.to_dict())
msg = f"{rid}rule '{description}' not met:\n{path}:{err}"
if rule.rid not in self.config["rules"]["warning_filter"]:
LOG.error(msg, extra=flag_extra(err_labels))
errors = errors + 1
else:
LOG.warning(msg, extra=flag_extra(err_labels))
2019-04-02 14:34:03 +00:00
2019-04-15 15:26:02 +00:00
return errors
@staticmethod
def classify(filename, settings={}, rules=[]): # noqa
parentdir = os.path.basename(os.path.dirname(filename))
basename = os.path.basename(filename)
ext = os.path.splitext(filename)[1][1:]
if parentdir in ["tasks"]:
return Task(filename, settings, rules)
if parentdir in ["handlers"]:
return Handler(filename, settings, rules)
if parentdir in ["vars", "defaults"]:
return RoleVars(filename, settings, rules)
if "group_vars" in filename.split(os.sep):
return GroupVars(filename, settings, rules)
if "host_vars" in filename.split(os.sep):
return HostVars(filename, settings, rules)
if parentdir in ["meta"] and "main" in basename:
return Meta(filename, settings, rules)
if parentdir in ["meta"] and "argument_specs" in basename:
return ArgumentSpecs(filename, settings, rules)
if parentdir in [
"library",
"lookup_plugins",
"callback_plugins",
"filter_plugins",
] or filename.endswith(".py"):
return Code(filename, settings, rules)
if basename == "inventory" or basename == "hosts" or parentdir in ["inventories"]:
return Inventory(filename, settings, rules)
if "rolesfile" in basename or ("requirements" in basename and ext in ["yaml", "yml"]):
return Rolesfile(filename, settings, rules)
if "Makefile" in basename:
return Makefile(filename, settings, rules)
if "templates" in filename.split(os.sep) or basename.endswith(".j2"):
return Template(filename, settings, rules)
if "files" in filename.split(os.sep):
return File(filename, settings, rules)
if basename.endswith(".yml") or basename.endswith(".yaml"):
return Playbook(filename, settings, rules)
if "README" in basename:
return Doc(filename, settings, rules)
return None
def _format_id(self, rule_id):
rid = rule_id.strip()
if rid:
rule_id = f"[{rid}] "
return rule_id
def __repr__(self):
2024-01-28 20:38:56 +00:00
return f"{type(self).__name__.lower()} ({self.path})"
2019-04-02 14:34:03 +00:00
def __getitem__(self, item):
2019-04-02 14:34:03 +00:00
return self.__dict__.get(item)
class RoleFile(Candidate):
2020-04-05 12:54:39 +00:00
"""Object classified as Ansible role file."""
2020-04-05 12:33:43 +00:00
def __init__(self, filename, settings={}, rules=[]): # noqa
super().__init__(filename, settings, rules)
2019-04-02 14:34:03 +00:00
2019-04-03 15:42:46 +00:00
parentdir = os.path.dirname(os.path.abspath(filename))
while parentdir != os.path.dirname(parentdir):
role_modules = os.path.join(parentdir, "library")
if os.path.exists(role_modules):
module_loader.add_directory(role_modules)
break
parentdir = os.path.dirname(parentdir)
2019-04-02 14:34:03 +00:00
class Playbook(Candidate):
2020-04-05 12:54:39 +00:00
"""Object classified as Ansible playbook."""
2019-04-02 14:34:03 +00:00
pass
class Task(RoleFile):
2020-04-05 12:54:39 +00:00
"""Object classified as Ansible task file."""
2020-04-05 12:33:43 +00:00
def __init__(self, filename, settings={}, rules=[]): # noqa
super().__init__(filename, settings, rules)
2019-04-02 14:34:03 +00:00
self.filetype = "tasks"
class Handler(RoleFile):
2020-04-05 12:54:39 +00:00
"""Object classified as Ansible handler file."""
2020-04-05 12:33:43 +00:00
def __init__(self, filename, settings={}, rules=[]): # noqa
super().__init__(filename, settings, rules)
2019-04-02 14:34:03 +00:00
self.filetype = "handlers"
class Vars(Candidate):
2020-04-05 12:54:39 +00:00
"""Object classified as Ansible vars file."""
2019-04-02 14:34:03 +00:00
pass
class InventoryVars(Candidate):
2020-04-05 12:54:39 +00:00
"""Object classified as Ansible inventory vars."""
2019-04-02 14:34:03 +00:00
pass
class HostVars(InventoryVars):
2020-04-05 12:54:39 +00:00
"""Object classified as Ansible host vars."""
2019-04-02 14:34:03 +00:00
pass
class GroupVars(InventoryVars):
2020-04-05 12:54:39 +00:00
"""Object classified as Ansible group vars."""
2019-04-02 14:34:03 +00:00
pass
class RoleVars(RoleFile):
2020-04-05 12:54:39 +00:00
"""Object classified as Ansible role vars."""
2019-04-02 14:34:03 +00:00
pass
class Meta(RoleFile):
2020-04-05 12:54:39 +00:00
"""Object classified as Ansible meta file."""
2019-04-02 14:34:03 +00:00
pass
class ArgumentSpecs(RoleFile):
"""Object classified as Ansible roles argument specs file."""
pass
class Inventory(Candidate):
2020-04-05 12:54:39 +00:00
"""Object classified as Ansible inventory file."""
2019-04-02 14:34:03 +00:00
pass
class Code(Candidate):
2020-04-05 12:54:39 +00:00
"""Object classified as code file."""
2019-04-02 14:34:03 +00:00
pass
class Template(RoleFile):
2020-04-05 12:54:39 +00:00
"""Object classified as Ansible template file."""
2019-04-02 14:34:03 +00:00
pass
class Doc(Candidate):
2020-04-05 12:54:39 +00:00
"""Object classified as documentation file."""
2019-04-02 14:34:03 +00:00
pass
class Makefile(Candidate):
2020-04-05 12:54:39 +00:00
"""Object classified as makefile."""
2019-04-02 14:34:03 +00:00
pass
class File(RoleFile):
2020-04-05 12:54:39 +00:00
"""Object classified as generic file."""
2019-04-02 14:34:03 +00:00
pass
class Rolesfile(Candidate):
2020-04-05 12:54:39 +00:00
"""Object classified as Ansible roles file."""
2019-04-02 14:34:03 +00:00
pass