blob: 1ee36b48a33119fd5b819201d088d4cd9bb91827 [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):
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
Alex Savatieiev63576832019-02-27 15:46:26 -060028 self.ssh_uses_sudo = False
29 self.ssh_key = os.environ.get('SSH_KEY', None)
30 self.ssh_user = os.environ.get('SSH_USER', None)
31 self.ssh_host = os.environ.get('SSH_HOST', None)
32
Alex Savatieiev5118de02019-02-20 15:50:42 -060033 self.salt_host = os.environ.get('SALT_URL', None)
34 self.salt_port = os.environ.get('SALT_PORT', '6969')
35 self.salt_user = os.environ.get('SALT_USER', 'salt')
Alex Savatieiev5118de02019-02-20 15:50:42 -060036 self.salt_timeout = os.environ.get('SALT_TIMEOUT', 30)
37 self.salt_file_root = os.environ.get('SALT_FILE_ROOT', None)
38 self.salt_scripts_folder = os.environ.get(
39 'SALT_SCRIPTS_FOLDER',
40 'cfg_checker_scripts'
41 )
42 self.all_nodes = utils.get_nodes_list(
43 os.environ.get('CFG_ALL_NODES', None),
44 os.environ.get('SALT_NODE_LIST_FILE', None)
45 )
46 self.skip_nodes = utils.node_string_to_list(os.environ.get(
47 'CFG_SKIP_NODES',
48 None
49 ))
50
Alex Savatieiev63576832019-02-27 15:46:26 -060051 def _init_env(self, env_name=None):
52 """Inits the environment vars from the env file
53 Uses simple validation for the values and names
Alex Savatieiev5118de02019-02-20 15:50:42 -060054
55 Keyword Arguments:
56 env_name {str} -- environment name to search configuration
57 files in etc/<env_name>.env (default: {None})
58
59 Raises:
60 ConfigException -- on IO error when loading env file
61 ConfigException -- on env file failed validation
62 """
63 # load env file as init os.environment with its values
64 if env_name is None:
Alex Savatieiev63576832019-02-27 15:46:26 -060065 _env_name = 'local'
Alex Savatieiev5118de02019-02-20 15:50:42 -060066 else:
67 _env_name = env_name
68 _config_path = os.path.join(pkg_dir, 'etc', _env_name + '.env')
69 if os.path.isfile(_config_path):
70 with open(_config_path) as _f:
71 _list = _f.read().splitlines()
Alex Savatieiev63576832019-02-27 15:46:26 -060072 logger_cli.info("# Loading env vars from '{}'".format(_config_path))
Alex Savatieiev5118de02019-02-20 15:50:42 -060073 else:
74 raise ConfigException(
75 "Failed to load enviroment vars from '{}'".format(
76 _config_path
77 )
78 )
79 for index in range(len(_list)):
80 _line = _list[index]
81 # skip comments
82 if _line.strip().startswith('#'):
83 continue
84 # validate
85 _errors = []
86 if _line.find('=') < 0 or _line.count('=') > 1:
87 _errors.append("Line {}: {}".format(index, _line))
88 else:
89 # save values
90 _t = _line.split('=')
91 _key, _value = _t[0], _t[1]
92 os.environ[_key] = _value
93 # if there was errors, report them
94 if _errors:
95 raise ConfigException(
96 "Environment file failed validation in lines: {}".format(
97 "\n".join(_errors)
98 )
99 )
100 else:
Alex Savatieiev63576832019-02-27 15:46:26 -0600101 logger_cli.debug("-> ...loaded total of '{}' vars".format(len(_list)))
102 self.salt_env = _env_name
Alex Savatieiev5118de02019-02-20 15:50:42 -0600103
104 def __init__(self):
105 """Base configuration class. Only values that are common for all scripts
106 """
107 _env = os.getenv('SALT_ENV', None)
108 self._init_env(_env)
109 self._init_values()
110
111
112config = CheckerConfiguration()