blob: cea1f0c0f253d0a50821a9eb9d4eb19e6f7712e6 [file] [log] [blame]
Alex Savatieiev5118de02019-02-20 15:50:42 -06001import os
2
3from exception import ConfigException
4from log import logger
5from other import utils
6
7pkg_dir = os.path.dirname(__file__)
8pkg_dir = os.path.join(pkg_dir, os.pardir, os.pardir)
9pkg_dir = os.path.normpath(pkg_dir)
10pkg_dir = os.path.abspath(pkg_dir)
11
12_default_work_folder = os.path.normpath(pkg_dir)
13
14
15class CheckerConfiguration(object):
16 def _init_values(self):
17 """Load values from environment variables or put default ones
18 """
19
20 self.name = "CheckerConfig"
21 self.working_folder = os.environ.get(
22 'CFG_TESTS_WORK_DIR',
23 _default_work_folder
24 )
25 self.date_format = "%Y-%m-%d %H:%M:%S.%f%z"
26 self.default_tz = "UTC"
27
28 self.salt_host = os.environ.get('SALT_URL', None)
29 self.salt_port = os.environ.get('SALT_PORT', '6969')
30 self.salt_user = os.environ.get('SALT_USER', 'salt')
31 self.salt_pass = os.environ.get('SALT_PASSWORD', None)
32 self.salt_timeout = os.environ.get('SALT_TIMEOUT', 30)
33 self.salt_file_root = os.environ.get('SALT_FILE_ROOT', None)
34 self.salt_scripts_folder = os.environ.get(
35 'SALT_SCRIPTS_FOLDER',
36 'cfg_checker_scripts'
37 )
38 self.all_nodes = utils.get_nodes_list(
39 os.environ.get('CFG_ALL_NODES', None),
40 os.environ.get('SALT_NODE_LIST_FILE', None)
41 )
42 self.skip_nodes = utils.node_string_to_list(os.environ.get(
43 'CFG_SKIP_NODES',
44 None
45 ))
46
47 @staticmethod
48 def _init_env(env_name=None):
49 """[summary]
50
51 Keyword Arguments:
52 env_name {str} -- environment name to search configuration
53 files in etc/<env_name>.env (default: {None})
54
55 Raises:
56 ConfigException -- on IO error when loading env file
57 ConfigException -- on env file failed validation
58 """
59 # load env file as init os.environment with its values
60 if env_name is None:
61 _env_name = 'default'
62 else:
63 _env_name = env_name
64 _config_path = os.path.join(pkg_dir, 'etc', _env_name + '.env')
65 if os.path.isfile(_config_path):
66 with open(_config_path) as _f:
67 _list = _f.read().splitlines()
68 logger.debug("Loading env vars from '{}'".format(_config_path))
69 else:
70 raise ConfigException(
71 "Failed to load enviroment vars from '{}'".format(
72 _config_path
73 )
74 )
75 for index in range(len(_list)):
76 _line = _list[index]
77 # skip comments
78 if _line.strip().startswith('#'):
79 continue
80 # validate
81 _errors = []
82 if _line.find('=') < 0 or _line.count('=') > 1:
83 _errors.append("Line {}: {}".format(index, _line))
84 else:
85 # save values
86 _t = _line.split('=')
87 _key, _value = _t[0], _t[1]
88 os.environ[_key] = _value
89 # if there was errors, report them
90 if _errors:
91 raise ConfigException(
92 "Environment file failed validation in lines: {}".format(
93 "\n".join(_errors)
94 )
95 )
96 else:
97 logger.debug("Loaded total of '{}' vars".format(len(_list)))
98
99 def __init__(self):
100 """Base configuration class. Only values that are common for all scripts
101 """
102 _env = os.getenv('SALT_ENV', None)
103 self._init_env(_env)
104 self._init_values()
105
106
107config = CheckerConfiguration()