blob: 39aeff2128b26f5611f8b4599d3eebd83d7ea0ca [file] [log] [blame]
Alex Savatieiev5118de02019-02-20 15:50:42 -06001import os
2
3from exception import ConfigException
Alex Savatieiev63576832019-02-27 15:46:26 -06004from log import logger, logger_cli
Alex Savatieiev5118de02019-02-20 15:50:42 -06005from 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):
Alex Savatieiev9df93a92019-02-27 17:40:16 -060016 def load_nodes_list():
17 return utils.get_nodes_list(
18 os.environ.get('CFG_ALL_NODES', None),
19 os.environ.get('SALT_NODE_LIST_FILE', None)
20 )
21
Alex Savatieiev5118de02019-02-20 15:50:42 -060022 def _init_values(self):
23 """Load values from environment variables or put default ones
24 """
25
26 self.name = "CheckerConfig"
27 self.working_folder = os.environ.get(
28 'CFG_TESTS_WORK_DIR',
29 _default_work_folder
30 )
31 self.date_format = "%Y-%m-%d %H:%M:%S.%f%z"
32 self.default_tz = "UTC"
33
Alex Savatieiev63576832019-02-27 15:46:26 -060034 self.ssh_uses_sudo = False
35 self.ssh_key = os.environ.get('SSH_KEY', None)
36 self.ssh_user = os.environ.get('SSH_USER', None)
37 self.ssh_host = os.environ.get('SSH_HOST', None)
38
Alex Savatieiev5118de02019-02-20 15:50:42 -060039 self.salt_host = os.environ.get('SALT_URL', None)
40 self.salt_port = os.environ.get('SALT_PORT', '6969')
41 self.salt_user = os.environ.get('SALT_USER', 'salt')
Alex Savatieiev5118de02019-02-20 15:50:42 -060042 self.salt_timeout = os.environ.get('SALT_TIMEOUT', 30)
43 self.salt_file_root = os.environ.get('SALT_FILE_ROOT', None)
44 self.salt_scripts_folder = os.environ.get(
45 'SALT_SCRIPTS_FOLDER',
46 'cfg_checker_scripts'
47 )
Alex Savatieiev9df93a92019-02-27 17:40:16 -060048
Alex Savatieiev5118de02019-02-20 15:50:42 -060049 self.skip_nodes = utils.node_string_to_list(os.environ.get(
50 'CFG_SKIP_NODES',
51 None
52 ))
53
Alex Savatieiev63576832019-02-27 15:46:26 -060054 def _init_env(self, env_name=None):
55 """Inits the environment vars from the env file
56 Uses simple validation for the values and names
Alex Savatieiev5118de02019-02-20 15:50:42 -060057
58 Keyword Arguments:
59 env_name {str} -- environment name to search configuration
60 files in etc/<env_name>.env (default: {None})
61
62 Raises:
63 ConfigException -- on IO error when loading env file
64 ConfigException -- on env file failed validation
65 """
66 # load env file as init os.environment with its values
67 if env_name is None:
Alex Savatieiev63576832019-02-27 15:46:26 -060068 _env_name = 'local'
Alex Savatieiev5118de02019-02-20 15:50:42 -060069 else:
70 _env_name = env_name
71 _config_path = os.path.join(pkg_dir, 'etc', _env_name + '.env')
72 if os.path.isfile(_config_path):
73 with open(_config_path) as _f:
74 _list = _f.read().splitlines()
Alex Savatieiev63576832019-02-27 15:46:26 -060075 logger_cli.info("# Loading env vars from '{}'".format(_config_path))
Alex Savatieiev5118de02019-02-20 15:50:42 -060076 else:
77 raise ConfigException(
78 "Failed to load enviroment vars from '{}'".format(
79 _config_path
80 )
81 )
82 for index in range(len(_list)):
83 _line = _list[index]
84 # skip comments
85 if _line.strip().startswith('#'):
86 continue
87 # validate
88 _errors = []
89 if _line.find('=') < 0 or _line.count('=') > 1:
90 _errors.append("Line {}: {}".format(index, _line))
91 else:
92 # save values
93 _t = _line.split('=')
94 _key, _value = _t[0], _t[1]
95 os.environ[_key] = _value
96 # if there was errors, report them
97 if _errors:
98 raise ConfigException(
99 "Environment file failed validation in lines: {}".format(
100 "\n".join(_errors)
101 )
102 )
103 else:
Alex Savatieiev63576832019-02-27 15:46:26 -0600104 logger_cli.debug("-> ...loaded total of '{}' vars".format(len(_list)))
105 self.salt_env = _env_name
Alex Savatieiev5118de02019-02-20 15:50:42 -0600106
107 def __init__(self):
108 """Base configuration class. Only values that are common for all scripts
109 """
110 _env = os.getenv('SALT_ENV', None)
111 self._init_env(_env)
112 self._init_values()
113
114
115config = CheckerConfiguration()