2019-04-01 13:07:22 +00:00
|
|
|
"""Global settings object definition."""
|
|
|
|
|
2019-03-28 00:20:43 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
import anyconfig
|
2021-01-09 12:16:08 +00:00
|
|
|
import jsonschema.exceptions
|
2019-04-11 12:17:44 +00:00
|
|
|
import pathspec
|
2019-03-28 15:54:45 +00:00
|
|
|
from appdirs import AppDirs
|
2019-04-01 13:07:22 +00:00
|
|
|
from jsonschema._utils import format_as_index
|
2019-03-28 00:20:43 +00:00
|
|
|
from pkg_resources import resource_filename
|
|
|
|
|
2019-04-04 13:35:47 +00:00
|
|
|
from ansiblelater import utils
|
2019-03-28 00:20:43 +00:00
|
|
|
|
2019-03-28 15:54:45 +00:00
|
|
|
config_dir = AppDirs("ansible-later").user_config_dir
|
|
|
|
default_config_file = os.path.join(config_dir, "config.yml")
|
2019-03-27 16:00:38 +00:00
|
|
|
|
2019-03-28 15:54:45 +00:00
|
|
|
|
2019-04-01 13:07:22 +00:00
|
|
|
class Settings(object):
|
|
|
|
"""
|
|
|
|
Create an object with all necessary settings.
|
2019-03-28 15:54:45 +00:00
|
|
|
|
2019-04-01 13:07:22 +00:00
|
|
|
Settings are loade from multiple locations in defined order (last wins):
|
|
|
|
- default settings defined by `self._get_defaults()`
|
|
|
|
- yaml config file, defaults to OS specific user config dir (https://pypi.org/project/appdirs/)
|
|
|
|
- provides cli parameters
|
|
|
|
"""
|
2019-03-28 15:54:45 +00:00
|
|
|
|
|
|
|
def __init__(self, args={}, config_file=default_config_file):
|
2019-04-01 13:07:22 +00:00
|
|
|
"""
|
|
|
|
Initialize a new settings class.
|
|
|
|
|
|
|
|
:param args: An optional dict of options, arguments and commands from the CLI.
|
|
|
|
:param config_file: An optional path to a yaml config file.
|
|
|
|
:returns: None
|
|
|
|
|
|
|
|
"""
|
2019-03-28 15:54:45 +00:00
|
|
|
self.config_file = config_file
|
2019-04-15 09:20:12 +00:00
|
|
|
self.schema = None
|
2019-04-08 14:55:07 +00:00
|
|
|
self.args_files = False
|
2019-04-01 13:07:22 +00:00
|
|
|
self.args = self._set_args(args)
|
2019-03-28 00:20:43 +00:00
|
|
|
self.config = self._get_config()
|
2019-04-04 13:35:47 +00:00
|
|
|
self._update_filelist()
|
2019-03-28 00:20:43 +00:00
|
|
|
|
2019-04-01 13:07:22 +00:00
|
|
|
def _set_args(self, args):
|
2019-04-05 12:02:14 +00:00
|
|
|
defaults = self._get_defaults()
|
2019-03-28 15:54:45 +00:00
|
|
|
self.config_file = args.get("config_file") or default_config_file
|
2019-03-28 00:20:43 +00:00
|
|
|
|
2019-04-01 13:07:22 +00:00
|
|
|
tmp_args = dict(filter(lambda item: item[1] is not None, args.items()))
|
2021-01-30 15:52:48 +00:00
|
|
|
tmp_args.pop("config_file", None)
|
2019-03-28 15:54:45 +00:00
|
|
|
|
2019-04-01 13:07:22 +00:00
|
|
|
tmp_dict = {}
|
|
|
|
for key, value in tmp_args.items():
|
|
|
|
tmp_dict = utils.add_dict_branch(tmp_dict, key.split("."), value)
|
2019-03-28 15:54:45 +00:00
|
|
|
|
2019-04-01 13:07:22 +00:00
|
|
|
# Override correct log level from argparse
|
2019-04-05 12:02:14 +00:00
|
|
|
levels = ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
|
2019-04-11 08:50:38 +00:00
|
|
|
log_level = levels.index(defaults["logging"]["level"])
|
2019-04-01 13:07:22 +00:00
|
|
|
if tmp_dict.get("logging"):
|
2019-04-05 12:02:14 +00:00
|
|
|
for adjustment in tmp_dict["logging"]["level"]:
|
|
|
|
log_level = min(len(levels) - 1, max(log_level + adjustment, 0))
|
2019-04-11 11:00:36 +00:00
|
|
|
tmp_dict["logging"]["level"] = levels[log_level]
|
2019-04-01 13:07:22 +00:00
|
|
|
|
2019-04-08 14:55:07 +00:00
|
|
|
if len(tmp_dict["rules"]["files"]) == 0:
|
2019-04-11 12:17:44 +00:00
|
|
|
tmp_dict["rules"]["files"] = ["*"]
|
2019-04-08 14:55:07 +00:00
|
|
|
else:
|
|
|
|
tmp_dict["rules"]["files"] = tmp_dict["rules"]["files"]
|
|
|
|
self.args_files = True
|
2019-04-02 14:34:03 +00:00
|
|
|
|
2019-04-01 13:07:22 +00:00
|
|
|
return tmp_dict
|
2019-03-28 00:20:43 +00:00
|
|
|
|
|
|
|
def _get_config(self):
|
|
|
|
defaults = self._get_defaults()
|
2019-04-09 08:36:52 +00:00
|
|
|
source_files = []
|
|
|
|
source_files.append(self.config_file)
|
2020-06-27 20:41:51 +00:00
|
|
|
source_files.append(os.path.join(os.getcwd(), ".later"))
|
|
|
|
source_files.append(os.path.join(os.getcwd(), ".later.yml"))
|
|
|
|
source_files.append(os.path.join(os.getcwd(), ".later.yaml"))
|
2019-03-28 15:54:45 +00:00
|
|
|
cli_options = self.args
|
2019-03-28 00:20:43 +00:00
|
|
|
|
2019-04-09 08:36:52 +00:00
|
|
|
for config in source_files:
|
|
|
|
if config and os.path.exists(config):
|
|
|
|
with utils.open_file(config) as stream:
|
|
|
|
s = stream.read()
|
|
|
|
sdict = utils.safe_load(s)
|
|
|
|
if self._validate(sdict):
|
|
|
|
anyconfig.merge(defaults, sdict, ac_merge=anyconfig.MS_DICTS)
|
2019-04-15 16:55:54 +00:00
|
|
|
defaults["logging"]["level"] = defaults["logging"]["level"].upper()
|
2019-03-28 00:20:43 +00:00
|
|
|
|
2019-04-01 13:07:22 +00:00
|
|
|
if cli_options and self._validate(cli_options):
|
2019-03-28 15:54:45 +00:00
|
|
|
anyconfig.merge(defaults, cli_options, ac_merge=anyconfig.MS_DICTS)
|
|
|
|
|
2020-06-27 20:33:51 +00:00
|
|
|
library = os.path.relpath(os.path.normpath(os.path.join(os.getcwd(), "library")))
|
|
|
|
autodetect = []
|
|
|
|
if os.path.exists(library):
|
|
|
|
autodetect = [
|
|
|
|
os.path.splitext(f)[0]
|
|
|
|
for f in os.listdir(library)
|
|
|
|
if os.path.isfile(os.path.join(library, f)) and not f.startswith(".")
|
|
|
|
]
|
|
|
|
|
|
|
|
for f in autodetect:
|
|
|
|
if f not in defaults["ansible"]["custom_modules"]:
|
|
|
|
defaults["ansible"]["custom_modules"].append(f)
|
|
|
|
|
2021-01-30 15:52:48 +00:00
|
|
|
if defaults["rules"]["buildin"]:
|
|
|
|
defaults["rules"]["standards"].append(
|
|
|
|
os.path.join(resource_filename("ansiblelater", "rules"))
|
|
|
|
)
|
|
|
|
|
|
|
|
defaults["rules"]["standards"] = [
|
|
|
|
os.path.relpath(os.path.normpath(p)) for p in defaults["rules"]["standards"]
|
|
|
|
]
|
|
|
|
|
2019-03-28 00:20:43 +00:00
|
|
|
return defaults
|
|
|
|
|
|
|
|
def _get_defaults(self):
|
2019-04-01 13:07:22 +00:00
|
|
|
defaults = {
|
|
|
|
"rules": {
|
2021-01-30 15:52:48 +00:00
|
|
|
"buildin": True,
|
|
|
|
"standards": [],
|
2019-04-01 13:07:22 +00:00
|
|
|
"filter": [],
|
2019-04-17 10:33:23 +00:00
|
|
|
"exclude_filter": [],
|
2021-10-10 15:00:44 +00:00
|
|
|
"warning_filter": ["ANSIBLE9999"],
|
2019-04-04 13:35:47 +00:00
|
|
|
"ignore_dotfiles": True,
|
2021-02-01 19:27:00 +00:00
|
|
|
"exclude_files": [],
|
|
|
|
"version": ""
|
2019-03-28 00:20:43 +00:00
|
|
|
},
|
2019-04-01 13:07:22 +00:00
|
|
|
"logging": {
|
2019-04-11 09:01:22 +00:00
|
|
|
"level": "WARNING",
|
2019-04-03 15:42:46 +00:00
|
|
|
"json": False
|
2019-03-28 00:20:43 +00:00
|
|
|
},
|
2019-04-01 13:07:22 +00:00
|
|
|
"ansible": {
|
|
|
|
"custom_modules": [],
|
2019-04-10 12:44:08 +00:00
|
|
|
"double-braces": {
|
|
|
|
"min-spaces-inside": 1,
|
|
|
|
"max-spaces-inside": 1,
|
|
|
|
},
|
2021-10-07 20:59:39 +00:00
|
|
|
"literal-bools": ["True", "False", "yes", "no"],
|
2021-10-10 16:30:40 +00:00
|
|
|
"named-task": {
|
|
|
|
"exclude": [
|
|
|
|
"meta",
|
|
|
|
"debug",
|
|
|
|
"block",
|
|
|
|
"include_role",
|
|
|
|
"import_role",
|
|
|
|
"include_tasks",
|
|
|
|
"import_tasks",
|
|
|
|
"include_vars",
|
|
|
|
],
|
|
|
|
},
|
2021-10-10 20:59:52 +00:00
|
|
|
"native-yaml": {
|
|
|
|
"exclude": [],
|
|
|
|
},
|
2019-04-10 12:44:08 +00:00
|
|
|
},
|
|
|
|
"yamllint": {
|
|
|
|
"empty-lines": {
|
|
|
|
"max": 1,
|
|
|
|
"max-start": 0,
|
|
|
|
"max-end": 1,
|
|
|
|
},
|
|
|
|
"indentation": {
|
|
|
|
"spaces": 2,
|
|
|
|
"check-multi-line-strings": False,
|
|
|
|
"indent-sequences": True,
|
|
|
|
},
|
|
|
|
"hyphens": {
|
|
|
|
"max-spaces-after": 1
|
|
|
|
},
|
|
|
|
"document-start": {
|
|
|
|
"present": True
|
|
|
|
},
|
2019-10-15 22:31:25 +00:00
|
|
|
"document-end": {
|
|
|
|
"present": True
|
|
|
|
},
|
2019-04-10 12:44:08 +00:00
|
|
|
"colons": {
|
|
|
|
"max-spaces-before": 0,
|
|
|
|
"max-spaces-after": 1
|
|
|
|
},
|
|
|
|
},
|
2019-03-28 00:20:43 +00:00
|
|
|
}
|
2019-04-15 09:20:12 +00:00
|
|
|
|
2019-04-01 13:07:22 +00:00
|
|
|
self.schema = anyconfig.gen_schema(defaults)
|
2019-03-28 15:54:45 +00:00
|
|
|
|
2019-04-01 13:07:22 +00:00
|
|
|
return defaults
|
2019-03-28 15:54:45 +00:00
|
|
|
|
2019-04-01 13:07:22 +00:00
|
|
|
def _validate(self, config):
|
|
|
|
try:
|
|
|
|
anyconfig.validate(config, self.schema, ac_schema_safe=False)
|
|
|
|
return True
|
2021-01-09 12:16:08 +00:00
|
|
|
except jsonschema.exceptions.ValidationError as e:
|
|
|
|
schema_error = (
|
|
|
|
"Error while loading configuration:\n"
|
|
|
|
"Failed validating '{validator}' in schema{schema}"
|
|
|
|
).format(
|
2020-04-05 12:33:43 +00:00
|
|
|
validator=e.validator, schema=format_as_index(list(e.relative_schema_path)[:-1])
|
|
|
|
)
|
|
|
|
utils.sysexit_with_message(
|
|
|
|
"{schema}: {msg}".format(schema=schema_error, msg=e.message)
|
2019-04-01 13:07:22 +00:00
|
|
|
)
|
2019-04-04 13:35:47 +00:00
|
|
|
|
|
|
|
def _update_filelist(self):
|
2019-04-11 12:17:44 +00:00
|
|
|
includes = self.config["rules"]["files"]
|
2019-04-04 13:35:47 +00:00
|
|
|
excludes = self.config["rules"]["exclude_files"]
|
|
|
|
ignore_dotfiles = self.config["rules"]["ignore_dotfiles"]
|
|
|
|
|
2019-04-08 14:55:07 +00:00
|
|
|
if ignore_dotfiles and not self.args_files:
|
2019-04-11 12:17:44 +00:00
|
|
|
excludes.append(".*")
|
2019-04-08 14:55:07 +00:00
|
|
|
else:
|
|
|
|
del excludes[:]
|
|
|
|
|
|
|
|
filelist = []
|
|
|
|
for root, dirs, files in os.walk("."):
|
|
|
|
for filename in files:
|
2020-04-05 12:33:43 +00:00
|
|
|
filelist.append(os.path.relpath(os.path.normpath(os.path.join(root, filename))))
|
2019-04-08 14:55:07 +00:00
|
|
|
|
|
|
|
valid = []
|
2019-04-11 12:24:50 +00:00
|
|
|
includespec = pathspec.PathSpec.from_lines("gitwildmatch", includes)
|
|
|
|
excludespec = pathspec.PathSpec.from_lines("gitwildmatch", excludes)
|
2019-04-08 14:55:07 +00:00
|
|
|
for item in filelist:
|
2019-04-11 12:17:44 +00:00
|
|
|
if includespec.match_file(item) and not excludespec.match_file(item):
|
2019-04-08 14:55:07 +00:00
|
|
|
valid.append(item)
|
2019-04-04 13:35:47 +00:00
|
|
|
|
2019-04-08 14:55:07 +00:00
|
|
|
self.config["rules"]["files"] = valid
|