blob: cca514287f32b14d7064103f5b2d50a28f76a51d [file] [log] [blame]
Alex Savatieiev5118de02019-02-20 15:50:42 -06001import os
Alexe9908f72020-05-19 16:04:53 -05002import sys
Alex Savatieiev5118de02019-02-20 15:50:42 -06003
Alex3bc95f62020-03-05 17:00:04 -06004from cfg_checker.common.exception import ConfigException
Alex3ebc5632019-04-18 16:47:18 -05005
Alex3bc95f62020-03-05 17:00:04 -06006from cfg_checker.common.log import logger_cli
Alex3ebc5632019-04-18 16:47:18 -05007
Alex3bc95f62020-03-05 17:00:04 -06008from cfg_checker.common.other import utils
Alex Savatieiev5118de02019-02-20 15:50:42 -06009
10pkg_dir = os.path.dirname(__file__)
11pkg_dir = os.path.join(pkg_dir, os.pardir, os.pardir)
12pkg_dir = os.path.normpath(pkg_dir)
13pkg_dir = os.path.abspath(pkg_dir)
14
15_default_work_folder = os.path.normpath(pkg_dir)
16
17
18class CheckerConfiguration(object):
Alexe9908f72020-05-19 16:04:53 -050019 @staticmethod
Alex Savatieiev9df93a92019-02-27 17:40:16 -060020 def load_nodes_list():
Alexe9908f72020-05-19 16:04:53 -050021 _file = os.environ.get('SALT_NODE_LIST_FILE', None)
22 if _file:
23 _v, _ = utils.get_nodes_list(
24 os.path.join(pkg_dir, _file),
25 env_sting=os.environ.get('CFG_ALL_NODES', None)
26 )
27 return _v
28 else:
29 return None
Alex Savatieiev9df93a92019-02-27 17:40:16 -060030
Alex Savatieiev5118de02019-02-20 15:50:42 -060031 def _init_values(self):
32 """Load values from environment variables or put default ones
33 """
34
35 self.name = "CheckerConfig"
36 self.working_folder = os.environ.get(
37 'CFG_TESTS_WORK_DIR',
38 _default_work_folder
39 )
40 self.date_format = "%Y-%m-%d %H:%M:%S.%f%z"
41 self.default_tz = "UTC"
42
Alex41485522019-04-12 17:26:18 -050043 self.pkg_versions_map = 'versions_map.csv'
44
Alex Savatieiev63576832019-02-27 15:46:26 -060045 self.ssh_uses_sudo = False
46 self.ssh_key = os.environ.get('SSH_KEY', None)
47 self.ssh_user = os.environ.get('SSH_USER', None)
48 self.ssh_host = os.environ.get('SSH_HOST', None)
49
Alex Savatieiev5118de02019-02-20 15:50:42 -060050 self.salt_host = os.environ.get('SALT_URL', None)
51 self.salt_port = os.environ.get('SALT_PORT', '6969')
52 self.salt_user = os.environ.get('SALT_USER', 'salt')
Alex Savatieiev5118de02019-02-20 15:50:42 -060053 self.salt_timeout = os.environ.get('SALT_TIMEOUT', 30)
54 self.salt_file_root = os.environ.get('SALT_FILE_ROOT', None)
55 self.salt_scripts_folder = os.environ.get(
56 'SALT_SCRIPTS_FOLDER',
57 'cfg_checker_scripts'
58 )
Alex Savatieiev9df93a92019-02-27 17:40:16 -060059
Alex Savatieiev5118de02019-02-20 15:50:42 -060060 self.skip_nodes = utils.node_string_to_list(os.environ.get(
61 'CFG_SKIP_NODES',
62 None
63 ))
64
Alex Savatieiev63576832019-02-27 15:46:26 -060065 def _init_env(self, env_name=None):
66 """Inits the environment vars from the env file
67 Uses simple validation for the values and names
Alex Savatieiev5118de02019-02-20 15:50:42 -060068
69 Keyword Arguments:
70 env_name {str} -- environment name to search configuration
71 files in etc/<env_name>.env (default: {None})
72
73 Raises:
74 ConfigException -- on IO error when loading env file
75 ConfigException -- on env file failed validation
76 """
77 # load env file as init os.environment with its values
78 if env_name is None:
Alex Savatieiev63576832019-02-27 15:46:26 -060079 _env_name = 'local'
Alex Savatieiev5118de02019-02-20 15:50:42 -060080 else:
81 _env_name = env_name
82 _config_path = os.path.join(pkg_dir, 'etc', _env_name + '.env')
83 if os.path.isfile(_config_path):
84 with open(_config_path) as _f:
85 _list = _f.read().splitlines()
Alex3ebc5632019-04-18 16:47:18 -050086 logger_cli.info(
87 "# Loading env vars from '{}'".format(
88 _config_path
89 )
90 )
Alex Savatieiev5118de02019-02-20 15:50:42 -060091 else:
92 raise ConfigException(
Alex Savatieievf808cd22019-03-01 13:17:59 -060093 "# Failed to load enviroment vars from '{}'".format(
Alex Savatieiev5118de02019-02-20 15:50:42 -060094 _config_path
95 )
96 )
97 for index in range(len(_list)):
98 _line = _list[index]
99 # skip comments
100 if _line.strip().startswith('#'):
101 continue
102 # validate
103 _errors = []
104 if _line.find('=') < 0 or _line.count('=') > 1:
105 _errors.append("Line {}: {}".format(index, _line))
106 else:
107 # save values
108 _t = _line.split('=')
109 _key, _value = _t[0], _t[1]
110 os.environ[_key] = _value
111 # if there was errors, report them
112 if _errors:
113 raise ConfigException(
Alex Savatieievf808cd22019-03-01 13:17:59 -0600114 "# Environment file failed validation in lines: {}".format(
Alex Savatieiev5118de02019-02-20 15:50:42 -0600115 "\n".join(_errors)
116 )
117 )
118 else:
Alex3ebc5632019-04-18 16:47:18 -0500119 logger_cli.debug(
120 "-> ...loaded total of '{}' vars".format(
121 len(_list)
122 )
123 )
Alex Savatieiev63576832019-02-27 15:46:26 -0600124 self.salt_env = _env_name
Alex Savatieiev5118de02019-02-20 15:50:42 -0600125
126 def __init__(self):
127 """Base configuration class. Only values that are common for all scripts
128 """
Alexe9908f72020-05-19 16:04:53 -0500129 # Make sure we running on Python 3
130 if sys.version_info[0] < 3 and sys.version_info[1] < 5:
131 logger_cli.error("# ERROR: Python 3.5+ is required")
132 sys.exit(1)
133 else:
134 logger_cli.debug("### Python version is {}.{}".format(
135 sys.version_info[0],
136 sys.version_info[1]
137 ))
138
Alex Savatieiev5118de02019-02-20 15:50:42 -0600139 _env = os.getenv('SALT_ENV', None)
140 self._init_env(_env)
141 self._init_values()
142
143
144config = CheckerConfiguration()