blob: 8d8111cd88bf995233c7bae65ad6a5a9c6550e89 [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
Alex41485522019-04-12 17:26:18 -050034 self.pkg_versions_map = 'versions_map.csv'
35
Alex Savatieiev63576832019-02-27 15:46:26 -060036 self.ssh_uses_sudo = False
37 self.ssh_key = os.environ.get('SSH_KEY', None)
38 self.ssh_user = os.environ.get('SSH_USER', None)
39 self.ssh_host = os.environ.get('SSH_HOST', None)
40
Alex Savatieiev5118de02019-02-20 15:50:42 -060041 self.salt_host = os.environ.get('SALT_URL', None)
42 self.salt_port = os.environ.get('SALT_PORT', '6969')
43 self.salt_user = os.environ.get('SALT_USER', 'salt')
Alex Savatieiev5118de02019-02-20 15:50:42 -060044 self.salt_timeout = os.environ.get('SALT_TIMEOUT', 30)
45 self.salt_file_root = os.environ.get('SALT_FILE_ROOT', None)
46 self.salt_scripts_folder = os.environ.get(
47 'SALT_SCRIPTS_FOLDER',
48 'cfg_checker_scripts'
49 )
Alex Savatieiev9df93a92019-02-27 17:40:16 -060050
Alex Savatieiev5118de02019-02-20 15:50:42 -060051 self.skip_nodes = utils.node_string_to_list(os.environ.get(
52 'CFG_SKIP_NODES',
53 None
54 ))
55
Alex Savatieiev63576832019-02-27 15:46:26 -060056 def _init_env(self, env_name=None):
57 """Inits the environment vars from the env file
58 Uses simple validation for the values and names
Alex Savatieiev5118de02019-02-20 15:50:42 -060059
60 Keyword Arguments:
61 env_name {str} -- environment name to search configuration
62 files in etc/<env_name>.env (default: {None})
63
64 Raises:
65 ConfigException -- on IO error when loading env file
66 ConfigException -- on env file failed validation
67 """
68 # load env file as init os.environment with its values
69 if env_name is None:
Alex Savatieiev63576832019-02-27 15:46:26 -060070 _env_name = 'local'
Alex Savatieiev5118de02019-02-20 15:50:42 -060071 else:
72 _env_name = env_name
73 _config_path = os.path.join(pkg_dir, 'etc', _env_name + '.env')
74 if os.path.isfile(_config_path):
75 with open(_config_path) as _f:
76 _list = _f.read().splitlines()
Alex Savatieiev63576832019-02-27 15:46:26 -060077 logger_cli.info("# Loading env vars from '{}'".format(_config_path))
Alex Savatieiev5118de02019-02-20 15:50:42 -060078 else:
79 raise ConfigException(
Alex Savatieievf808cd22019-03-01 13:17:59 -060080 "# Failed to load enviroment vars from '{}'".format(
Alex Savatieiev5118de02019-02-20 15:50:42 -060081 _config_path
82 )
83 )
84 for index in range(len(_list)):
85 _line = _list[index]
86 # skip comments
87 if _line.strip().startswith('#'):
88 continue
89 # validate
90 _errors = []
91 if _line.find('=') < 0 or _line.count('=') > 1:
92 _errors.append("Line {}: {}".format(index, _line))
93 else:
94 # save values
95 _t = _line.split('=')
96 _key, _value = _t[0], _t[1]
97 os.environ[_key] = _value
98 # if there was errors, report them
99 if _errors:
100 raise ConfigException(
Alex Savatieievf808cd22019-03-01 13:17:59 -0600101 "# Environment file failed validation in lines: {}".format(
Alex Savatieiev5118de02019-02-20 15:50:42 -0600102 "\n".join(_errors)
103 )
104 )
105 else:
Alex Savatieiev63576832019-02-27 15:46:26 -0600106 logger_cli.debug("-> ...loaded total of '{}' vars".format(len(_list)))
107 self.salt_env = _env_name
Alex Savatieiev5118de02019-02-20 15:50:42 -0600108
109 def __init__(self):
110 """Base configuration class. Only values that are common for all scripts
111 """
112 _env = os.getenv('SALT_ENV', None)
113 self._init_env(_env)
114 self._init_values()
115
116
117config = CheckerConfiguration()