Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 1 | import os |
| 2 | |
| 3 | from exception import ConfigException |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 4 | from log import logger, logger_cli |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 5 | from other import utils |
| 6 | |
| 7 | pkg_dir = os.path.dirname(__file__) |
| 8 | pkg_dir = os.path.join(pkg_dir, os.pardir, os.pardir) |
| 9 | pkg_dir = os.path.normpath(pkg_dir) |
| 10 | pkg_dir = os.path.abspath(pkg_dir) |
| 11 | |
| 12 | _default_work_folder = os.path.normpath(pkg_dir) |
| 13 | |
| 14 | |
| 15 | class CheckerConfiguration(object): |
Alex Savatieiev | 9df93a9 | 2019-02-27 17:40:16 -0600 | [diff] [blame] | 16 | 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 Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 22 | 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 Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 34 | 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 Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 39 | 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 Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 42 | 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 Savatieiev | 9df93a9 | 2019-02-27 17:40:16 -0600 | [diff] [blame] | 48 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 49 | self.skip_nodes = utils.node_string_to_list(os.environ.get( |
| 50 | 'CFG_SKIP_NODES', |
| 51 | None |
| 52 | )) |
| 53 | |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 54 | 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 Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 57 | |
| 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 Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 68 | _env_name = 'local' |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 69 | 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 Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 75 | logger_cli.info("# Loading env vars from '{}'".format(_config_path)) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 76 | else: |
| 77 | raise ConfigException( |
Alex Savatieiev | f808cd2 | 2019-03-01 13:17:59 -0600 | [diff] [blame^] | 78 | "# Failed to load enviroment vars from '{}'".format( |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 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( |
Alex Savatieiev | f808cd2 | 2019-03-01 13:17:59 -0600 | [diff] [blame^] | 99 | "# Environment file failed validation in lines: {}".format( |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 100 | "\n".join(_errors) |
| 101 | ) |
| 102 | ) |
| 103 | else: |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 104 | logger_cli.debug("-> ...loaded total of '{}' vars".format(len(_list))) |
| 105 | self.salt_env = _env_name |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 106 | |
| 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 | |
| 115 | config = CheckerConfiguration() |