blob: 33e7c25ddc4f58063f1965ab96a50e99a05c56ec [file] [log] [blame]
Alex Savatieiev5118de02019-02-20 15:50:42 -06001import os
2
3from exception import ConfigException
Alex3ebc5632019-04-18 16:47:18 -05004
5from log import logger_cli
6
Alex Savatieiev5118de02019-02-20 15:50:42 -06007from other import utils
8
9pkg_dir = os.path.dirname(__file__)
10pkg_dir = os.path.join(pkg_dir, os.pardir, os.pardir)
11pkg_dir = os.path.normpath(pkg_dir)
12pkg_dir = os.path.abspath(pkg_dir)
13
14_default_work_folder = os.path.normpath(pkg_dir)
15
16
17class CheckerConfiguration(object):
Alex Savatieiev9df93a92019-02-27 17:40:16 -060018 def load_nodes_list():
19 return utils.get_nodes_list(
20 os.environ.get('CFG_ALL_NODES', None),
21 os.environ.get('SALT_NODE_LIST_FILE', None)
22 )
23
Alex Savatieiev5118de02019-02-20 15:50:42 -060024 def _init_values(self):
25 """Load values from environment variables or put default ones
26 """
27
28 self.name = "CheckerConfig"
29 self.working_folder = os.environ.get(
30 'CFG_TESTS_WORK_DIR',
31 _default_work_folder
32 )
33 self.date_format = "%Y-%m-%d %H:%M:%S.%f%z"
34 self.default_tz = "UTC"
35
Alex41485522019-04-12 17:26:18 -050036 self.pkg_versions_map = 'versions_map.csv'
37
Alex Savatieiev63576832019-02-27 15:46:26 -060038 self.ssh_uses_sudo = False
39 self.ssh_key = os.environ.get('SSH_KEY', None)
40 self.ssh_user = os.environ.get('SSH_USER', None)
41 self.ssh_host = os.environ.get('SSH_HOST', None)
42
Alex Savatieiev5118de02019-02-20 15:50:42 -060043 self.salt_host = os.environ.get('SALT_URL', None)
44 self.salt_port = os.environ.get('SALT_PORT', '6969')
45 self.salt_user = os.environ.get('SALT_USER', 'salt')
Alex Savatieiev5118de02019-02-20 15:50:42 -060046 self.salt_timeout = os.environ.get('SALT_TIMEOUT', 30)
47 self.salt_file_root = os.environ.get('SALT_FILE_ROOT', None)
48 self.salt_scripts_folder = os.environ.get(
49 'SALT_SCRIPTS_FOLDER',
50 'cfg_checker_scripts'
51 )
Alex Savatieiev9df93a92019-02-27 17:40:16 -060052
Alex Savatieiev5118de02019-02-20 15:50:42 -060053 self.skip_nodes = utils.node_string_to_list(os.environ.get(
54 'CFG_SKIP_NODES',
55 None
56 ))
57
Alex Savatieiev63576832019-02-27 15:46:26 -060058 def _init_env(self, env_name=None):
59 """Inits the environment vars from the env file
60 Uses simple validation for the values and names
Alex Savatieiev5118de02019-02-20 15:50:42 -060061
62 Keyword Arguments:
63 env_name {str} -- environment name to search configuration
64 files in etc/<env_name>.env (default: {None})
65
66 Raises:
67 ConfigException -- on IO error when loading env file
68 ConfigException -- on env file failed validation
69 """
70 # load env file as init os.environment with its values
71 if env_name is None:
Alex Savatieiev63576832019-02-27 15:46:26 -060072 _env_name = 'local'
Alex Savatieiev5118de02019-02-20 15:50:42 -060073 else:
74 _env_name = env_name
75 _config_path = os.path.join(pkg_dir, 'etc', _env_name + '.env')
76 if os.path.isfile(_config_path):
77 with open(_config_path) as _f:
78 _list = _f.read().splitlines()
Alex3ebc5632019-04-18 16:47:18 -050079 logger_cli.info(
80 "# Loading env vars from '{}'".format(
81 _config_path
82 )
83 )
Alex Savatieiev5118de02019-02-20 15:50:42 -060084 else:
85 raise ConfigException(
Alex Savatieievf808cd22019-03-01 13:17:59 -060086 "# Failed to load enviroment vars from '{}'".format(
Alex Savatieiev5118de02019-02-20 15:50:42 -060087 _config_path
88 )
89 )
90 for index in range(len(_list)):
91 _line = _list[index]
92 # skip comments
93 if _line.strip().startswith('#'):
94 continue
95 # validate
96 _errors = []
97 if _line.find('=') < 0 or _line.count('=') > 1:
98 _errors.append("Line {}: {}".format(index, _line))
99 else:
100 # save values
101 _t = _line.split('=')
102 _key, _value = _t[0], _t[1]
103 os.environ[_key] = _value
104 # if there was errors, report them
105 if _errors:
106 raise ConfigException(
Alex Savatieievf808cd22019-03-01 13:17:59 -0600107 "# Environment file failed validation in lines: {}".format(
Alex Savatieiev5118de02019-02-20 15:50:42 -0600108 "\n".join(_errors)
109 )
110 )
111 else:
Alex3ebc5632019-04-18 16:47:18 -0500112 logger_cli.debug(
113 "-> ...loaded total of '{}' vars".format(
114 len(_list)
115 )
116 )
Alex Savatieiev63576832019-02-27 15:46:26 -0600117 self.salt_env = _env_name
Alex Savatieiev5118de02019-02-20 15:50:42 -0600118
119 def __init__(self):
120 """Base configuration class. Only values that are common for all scripts
121 """
122 _env = os.getenv('SALT_ENV', None)
123 self._init_env(_env)
124 self._init_values()
125
126
127config = CheckerConfiguration()