Alex | 0989ecf | 2022-03-29 13:43:21 -0500 | [diff] [blame] | 1 | # Author: Alex Savatieiev (osavatieiev@mirantis.com; a.savex@gmail.com) |
| 2 | # Copyright 2019-2022 Mirantis, Inc. |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 3 | import os |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 4 | import json |
| 5 | import pwd |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 6 | import sys |
Alex | c4f5962 | 2021-08-27 13:42:00 -0500 | [diff] [blame] | 7 | import yaml |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 8 | |
Alex | 359e575 | 2021-08-16 17:28:30 -0500 | [diff] [blame] | 9 | from cfg_checker.common.const import ENV_TYPE_GLOB, ENV_TYPE_SALT |
| 10 | from cfg_checker.common.const import ENV_TYPE_KUBE, ENV_TYPE_LINUX, ENV_LOCAL |
| 11 | from cfg_checker.common.const import supported_envs |
| 12 | |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 13 | from cfg_checker.common.exception import ConfigException |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 14 | from cfg_checker.common.log import logger_cli |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 15 | |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 16 | from cfg_checker.common.other import utils, shell |
| 17 | from cfg_checker.common.ssh_utils import ssh_shell_p |
| 18 | |
| 19 | from cfg_checker.clients import get_kube_remote |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 20 | |
| 21 | pkg_dir = os.path.dirname(__file__) |
| 22 | pkg_dir = os.path.join(pkg_dir, os.pardir, os.pardir) |
| 23 | pkg_dir = os.path.normpath(pkg_dir) |
| 24 | pkg_dir = os.path.abspath(pkg_dir) |
| 25 | |
| 26 | _default_work_folder = os.path.normpath(pkg_dir) |
| 27 | |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 28 | |
| 29 | def _extract_salt_return(_raw): |
| 30 | if not isinstance(_raw, str): |
| 31 | _json = _raw |
Alex | c4f5962 | 2021-08-27 13:42:00 -0500 | [diff] [blame] | 32 | logger_cli.debug("... ambigious return detected") |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 33 | else: |
| 34 | try: |
| 35 | _json = json.loads(_raw) |
| 36 | except ValueError: |
| 37 | _json = _raw |
| 38 | logger_cli.debug( |
Alex | c4f5962 | 2021-08-27 13:42:00 -0500 | [diff] [blame] | 39 | "... return value is not a json: '{}'".format(_raw) |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 40 | ) |
| 41 | |
| 42 | return _json |
| 43 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 44 | |
Alex | f6ec91b | 2021-09-10 10:11:17 -0500 | [diff] [blame] | 45 | def _get_env_value(_key, _default): |
| 46 | _value = os.environ.get(_key, _default) |
| 47 | logger_cli.debug("... shell env: {}={}".format(_key, _value)) |
| 48 | return _value |
| 49 | |
| 50 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 51 | class CheckerConfiguration(object): |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 52 | @staticmethod |
Alex Savatieiev | 9df93a9 | 2019-02-27 17:40:16 -0600 | [diff] [blame] | 53 | def load_nodes_list(): |
Alex | f6ec91b | 2021-09-10 10:11:17 -0500 | [diff] [blame] | 54 | _file = _get_env_value('SALT_NODE_LIST_FILE', None) |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 55 | if _file: |
| 56 | _v, _ = utils.get_nodes_list( |
| 57 | os.path.join(pkg_dir, _file), |
Alex | f6ec91b | 2021-09-10 10:11:17 -0500 | [diff] [blame] | 58 | env_sting=_get_env_value('CFG_ALL_NODES', None) |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 59 | ) |
| 60 | return _v |
| 61 | else: |
| 62 | return None |
Alex Savatieiev | 9df93a9 | 2019-02-27 17:40:16 -0600 | [diff] [blame] | 63 | |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 64 | def _detect(self, _type): |
Alex | c4f5962 | 2021-08-27 13:42:00 -0500 | [diff] [blame] | 65 | logger_cli.debug("... detecting '{}'".format(_type)) |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 66 | if _type is None: |
| 67 | raise ConfigException("# Unexpected supported env type") |
| 68 | elif _type == ENV_TYPE_SALT: |
| 69 | # Detect salt env |
| 70 | _detect_cmd = ["curl", "-s"] |
| 71 | _detect_cmd.append( |
| 72 | "http://" + self.mcp_host + ':' + self.salt_port |
| 73 | ) |
| 74 | # Try to call salt API on target host |
| 75 | _r = None |
Alex | c4f5962 | 2021-08-27 13:42:00 -0500 | [diff] [blame] | 76 | logger_cli.debug("... detecting env type '{}'".format(_type)) |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 77 | if self.env_name == ENV_LOCAL: |
| 78 | _r = shell(" ".join(_detect_cmd)) |
| 79 | else: |
| 80 | _r = ssh_shell_p( |
| 81 | " ".join(_detect_cmd), |
| 82 | self.ssh_host, |
| 83 | username=self.ssh_user, |
| 84 | keypath=self.ssh_key, |
| 85 | piped=False, |
| 86 | use_sudo=self.ssh_uses_sudo, |
| 87 | silent=True |
| 88 | ) |
| 89 | # Parse return |
| 90 | _r = _extract_salt_return(_r) |
| 91 | |
| 92 | if len(_r) < 1: |
| 93 | return False |
| 94 | elif _r["return"] == "Welcome": |
| 95 | return True |
| 96 | else: |
| 97 | return False |
| 98 | elif _type == ENV_TYPE_KUBE: |
| 99 | _kube = get_kube_remote(self) |
Alex | 359e575 | 2021-08-16 17:28:30 -0500 | [diff] [blame] | 100 | if not _kube.initialized: |
| 101 | logger_cli.debug( |
Alex | c4f5962 | 2021-08-27 13:42:00 -0500 | [diff] [blame] | 102 | "... failed to init kube using '{}'".format( |
Alex | 359e575 | 2021-08-16 17:28:30 -0500 | [diff] [blame] | 103 | _kube.kConfigPath |
| 104 | ) |
| 105 | ) |
| 106 | return False |
| 107 | else: |
| 108 | logger_cli.debug( |
| 109 | "... config loaded from '{}'".format( |
| 110 | _kube.kConfigPath |
| 111 | ) |
| 112 | ) |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 113 | try: |
| 114 | _vApi = _kube.get_versions_api() |
| 115 | _v = _vApi.get_code() |
| 116 | if hasattr(_v, "platform") and \ |
| 117 | hasattr(_v, "major") and \ |
| 118 | hasattr(_v, "minor"): |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 119 | logger_cli.info( |
| 120 | "# Kube server found: {}:{} on '{}'".format( |
| 121 | _v.major, |
| 122 | _v.minor, |
Alex | c4f5962 | 2021-08-27 13:42:00 -0500 | [diff] [blame] | 123 | _kube.kConfigPath |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 124 | ) |
| 125 | ) |
| 126 | return True |
| 127 | else: |
| 128 | return False |
| 129 | except Exception as e: |
Alex | 359e575 | 2021-08-16 17:28:30 -0500 | [diff] [blame] | 130 | logger_cli.debug( |
| 131 | "... kube env error: '{}' ".format( |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 132 | str(e) |
| 133 | ) |
| 134 | ) |
| 135 | return False |
| 136 | elif _type == ENV_TYPE_LINUX: |
| 137 | # Detect Linux env |
| 138 | from platform import system, release |
| 139 | _s = system() |
| 140 | _r = release() |
Alex | c4f5962 | 2021-08-27 13:42:00 -0500 | [diff] [blame] | 141 | logger_cli.debug("... running on {} {}".format(_s, _r)) |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 142 | if _s in ['Linux', 'Darwin']: |
| 143 | return True |
| 144 | else: |
| 145 | return False |
| 146 | else: |
| 147 | raise ConfigException( |
| 148 | "# Env type of '{}' is not supported".format( |
| 149 | _type |
| 150 | ) |
| 151 | ) |
| 152 | |
| 153 | def _detect_types(self): |
| 154 | """Try to detect env type based on the name |
| 155 | """ |
| 156 | self.detected_envs = [] |
| 157 | logger_cli.info('# Detecting env types') |
| 158 | for _env in supported_envs: |
| 159 | if self._detect(_env): |
| 160 | logger_cli.info("# '{}' found".format(_env)) |
| 161 | self.detected_envs.append(_env) |
| 162 | else: |
| 163 | logger_cli.info("# '{}' not found".format(_env)) |
| 164 | |
| 165 | return |
| 166 | |
| 167 | def _init_mcp_values(self): |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 168 | """Load values from environment variables or put default ones |
| 169 | """ |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 170 | # filter vars and preload if needed |
| 171 | self.salt_vars = [] |
| 172 | self.kube_vars = [] |
| 173 | for _key, _value in self.vars: |
Alex | c4f5962 | 2021-08-27 13:42:00 -0500 | [diff] [blame] | 174 | if _key in os.environ: |
| 175 | logger_cli.info( |
| 176 | "-> Using env var '{}={}'".format(_key, os.environ[_key]) |
| 177 | ) |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 178 | if _key.startswith(ENV_TYPE_GLOB): |
| 179 | os.environ[_key] = _value |
| 180 | elif _key.startswith(ENV_TYPE_SALT): |
| 181 | self.salt_vars.append([_key, _value]) |
| 182 | elif _key.startswith(ENV_TYPE_KUBE): |
| 183 | self.kube_vars.append([_key, _value]) |
| 184 | else: |
| 185 | logger_cli.warn( |
| 186 | "Unsupported config variable: '{}={}'".format( |
| 187 | _key, |
| 188 | _value |
| 189 | ) |
| 190 | ) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 191 | self.name = "CheckerConfig" |
Alex | f6ec91b | 2021-09-10 10:11:17 -0500 | [diff] [blame] | 192 | self.working_folder = _get_env_value( |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 193 | 'CFG_TESTS_WORK_DIR', |
| 194 | _default_work_folder |
| 195 | ) |
| 196 | self.date_format = "%Y-%m-%d %H:%M:%S.%f%z" |
| 197 | self.default_tz = "UTC" |
| 198 | |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 199 | self.pkg_versions_map = 'versions_map.csv' |
| 200 | |
Alex | 359e575 | 2021-08-16 17:28:30 -0500 | [diff] [blame] | 201 | # self.ssh_uses_sudo = False |
Alex | f6ec91b | 2021-09-10 10:11:17 -0500 | [diff] [blame] | 202 | self.ssh_key = _get_env_value('MCP_SSH_KEY', None) |
| 203 | self.ssh_user = _get_env_value('MCP_SSH_USER', None) |
| 204 | self.ssh_host = _get_env_value('MCP_SSH_HOST', None) |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 205 | self.ssh_connect_timeout = int( |
Alex | f6ec91b | 2021-09-10 10:11:17 -0500 | [diff] [blame] | 206 | _get_env_value('MCP_SSH_TIMEOUT', "15") |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 207 | ) |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 208 | |
Alex | f6ec91b | 2021-09-10 10:11:17 -0500 | [diff] [blame] | 209 | self.mcp_host = _get_env_value('MCP_ENV_HOST', None) |
| 210 | self.salt_port = _get_env_value('MCP_SALT_PORT', '6969') |
| 211 | self.threads = int(_get_env_value('MCP_THREADS', "5")) |
Alex | 0bcf31b | 2022-03-29 17:38:58 -0500 | [diff] [blame] | 212 | self.sage_threads = int(_get_env_value('LOG_COLLECT_THREADS', "15")) |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 213 | self.script_execution_timeout = int( |
Alex | f6ec91b | 2021-09-10 10:11:17 -0500 | [diff] [blame] | 214 | _get_env_value('MCP_SCRIPT_RUN_TIMEOUT', "300") |
Alex | 1f90e7b | 2021-09-03 15:31:28 -0500 | [diff] [blame] | 215 | ) |
Alex Savatieiev | 9df93a9 | 2019-02-27 17:40:16 -0600 | [diff] [blame] | 216 | |
Alex | f6ec91b | 2021-09-10 10:11:17 -0500 | [diff] [blame] | 217 | self.skip_nodes = utils.node_string_to_list(_get_env_value( |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 218 | 'CFG_SKIP_NODES', |
| 219 | None |
| 220 | )) |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 221 | # prebuild user data and folder path |
| 222 | self.pw_user = pwd.getpwuid(os.getuid()) |
| 223 | if self.env_name == "local": |
| 224 | pass |
| 225 | else: |
| 226 | if not self.ssh_key and not self.force_no_key: |
| 227 | raise ConfigException( |
| 228 | "Please, supply a key for the cluster's master node. " |
| 229 | "Use MCP_SSH_KEY, see 'etc/example.env'" |
| 230 | ) |
| 231 | |
| 232 | def _init_env_values(self): |
| 233 | if ENV_TYPE_SALT in self.detected_envs: |
| 234 | for _key, _value in self.salt_vars: |
Alex | c4f5962 | 2021-08-27 13:42:00 -0500 | [diff] [blame] | 235 | if _key not in os.environ: |
| 236 | os.environ[_key] = _value |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 237 | |
Alex | f6ec91b | 2021-09-10 10:11:17 -0500 | [diff] [blame] | 238 | self.salt_user = _get_env_value('SALT_USER', 'salt') |
| 239 | self.salt_timeout = _get_env_value('SALT_TIMEOUT', 30) |
| 240 | self.salt_file_root = _get_env_value( |
Alex | c4f5962 | 2021-08-27 13:42:00 -0500 | [diff] [blame] | 241 | 'SALT_FILE_ROOT', |
| 242 | "/usr/share/salt-formulas/env/" |
| 243 | ) |
Alex | f6ec91b | 2021-09-10 10:11:17 -0500 | [diff] [blame] | 244 | self.salt_scripts_folder = _get_env_value( |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 245 | 'SALT_SCRIPTS_FOLDER', |
| 246 | 'cfg_checker_scripts' |
| 247 | ) |
| 248 | elif ENV_TYPE_KUBE in self.detected_envs: |
| 249 | for _key, _value in self.kube_vars: |
Alex | c4f5962 | 2021-08-27 13:42:00 -0500 | [diff] [blame] | 250 | if _key not in os.environ: |
| 251 | os.environ[_key] = _value |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 252 | |
Alex | f6ec91b | 2021-09-10 10:11:17 -0500 | [diff] [blame] | 253 | self.kube_config_root = _get_env_value('KUBE_CONFIG_ROOT', "/root") |
| 254 | self.kube_scripts_folder = _get_env_value( |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 255 | 'KUBE_SCRIPTS_FOLDER', |
Alex | c4f5962 | 2021-08-27 13:42:00 -0500 | [diff] [blame] | 256 | "cfg-checker-scripts" |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 257 | ) |
Alex | f6ec91b | 2021-09-10 10:11:17 -0500 | [diff] [blame] | 258 | self.kube_node_user = _get_env_value( |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 259 | 'KUBE_NODE_USER', |
| 260 | 'ubuntu' |
| 261 | ) |
Alex | f6ec91b | 2021-09-10 10:11:17 -0500 | [diff] [blame] | 262 | self.kube_node_keypath = _get_env_value( |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 263 | 'KUBE_NODE_KEYPATH', |
| 264 | None |
| 265 | ) |
| 266 | # Warn user only if Kube env is detected locally |
Alex | ccb72e0 | 2021-01-20 16:38:03 -0600 | [diff] [blame] | 267 | if self.env_name == ENV_LOCAL: |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 268 | if not os.path.exists(self.kube_config_path): |
| 269 | logger_cli.warn( |
| 270 | "Kube config path not found on local env: '{}'".format( |
| 271 | self.kube_config_path |
| 272 | ) |
| 273 | ) |
| 274 | # On local envs, KUBE_NODE_KEYPATH is mandatory and is |
| 275 | # provided to give cfg-checker access to kube nodes |
| 276 | if not self.kube_node_keypath and not self.force_no_key: |
| 277 | raise ConfigException( |
| 278 | "Please, supply a key for the cluster nodes. " |
| 279 | "Use KUBE_NODE_KEYPATH, see 'etc/example.env'. " |
| 280 | "Consider checking KUBE_NODE_USER as well" |
| 281 | ) |
Alex | 3630894 | 2020-11-09 17:13:38 -0600 | [diff] [blame] | 282 | self.kube_node_homepath = os.path.join( |
| 283 | '/home', |
| 284 | self.kube_node_user |
| 285 | ) |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 286 | else: |
Alex | 205546c | 2020-12-30 19:22:30 -0600 | [diff] [blame] | 287 | # Init key for nodes |
| 288 | # KUBE_NODE_KEYPATH is provided in case of node keys would be |
| 289 | # different to master node key, which is supplied |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 290 | # using MCP_SSH_KEY (mandatory) and, for the most cases, |
| 291 | # should be the same for remote envs |
| 292 | if not self.kube_node_keypath and not self.force_no_key: |
| 293 | logger_cli.debug( |
| 294 | "... using MCP_SSH_KEY as node keys. " |
| 295 | "Supply KUBE_NODE_KEYPATH to update." |
| 296 | ) |
| 297 | self.kube_node_keypath = self.ssh_key |
Alex | 3630894 | 2020-11-09 17:13:38 -0600 | [diff] [blame] | 298 | self.kube_node_homepath = self.homepath |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 299 | |
Alex | ccb72e0 | 2021-01-20 16:38:03 -0600 | [diff] [blame] | 300 | def _init_env(self, config_path, env_name=None): |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 301 | """Inits the environment vars from the env file |
| 302 | Uses simple validation for the values and names |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 303 | |
| 304 | Keyword Arguments: |
| 305 | env_name {str} -- environment name to search configuration |
| 306 | files in etc/<env_name>.env (default: {None}) |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 307 | env_type {str} -- environment type to use: salt/kube |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 308 | |
| 309 | Raises: |
| 310 | ConfigException -- on IO error when loading env file |
| 311 | ConfigException -- on env file failed validation |
| 312 | """ |
Alex | c4f5962 | 2021-08-27 13:42:00 -0500 | [diff] [blame] | 313 | # detect kubeconfig placement |
Alex | f6ec91b | 2021-09-10 10:11:17 -0500 | [diff] [blame] | 314 | _env_kubeconf_path = _get_env_value('KUBECONFIG', None) |
Alex | c4f5962 | 2021-08-27 13:42:00 -0500 | [diff] [blame] | 315 | if not os.path.exists(self.kube_config_path): |
| 316 | logger_cli.debug( |
| 317 | "... kubeconfig not detected at '{}'".format( |
| 318 | self.kube_config_path |
| 319 | ) |
| 320 | ) |
| 321 | # not exists, get KUBECONFIG var |
| 322 | if _env_kubeconf_path: |
| 323 | # get the env var path |
| 324 | self.kube_config_path = _env_kubeconf_path |
| 325 | logger_cli.debug( |
| 326 | "... KUBECONFIG var points to '{}'".format( |
| 327 | self.kube_config_path |
| 328 | ) |
| 329 | ) |
| 330 | self.kube_config_detected = True |
| 331 | else: |
| 332 | logger_cli.debug("... KUBECONFIG env var not found") |
Alex | 7b0ee9a | 2021-09-21 17:16:17 -0500 | [diff] [blame] | 333 | # do not change it from default |
| 334 | # self.kube_config_path = None |
Alex | c4f5962 | 2021-08-27 13:42:00 -0500 | [diff] [blame] | 335 | self.kube_config_detected = False |
| 336 | else: |
| 337 | logger_cli.debug( |
| 338 | "... kubeconfig detected at '{}'".format( |
| 339 | self.kube_config_path |
| 340 | ) |
| 341 | ) |
| 342 | self.kube_config_detected = True |
| 343 | |
| 344 | # try to load values from KUBECONF |
| 345 | _kube_conf = None |
Alex | 7b0ee9a | 2021-09-21 17:16:17 -0500 | [diff] [blame] | 346 | if self.kube_config_path and self.kube_config_detected: |
Alex | c4f5962 | 2021-08-27 13:42:00 -0500 | [diff] [blame] | 347 | with open(self.kube_config_path) as kF: |
| 348 | _kube_conf = yaml.load(kF, Loader=yaml.SafeLoader) |
| 349 | # extract host ip |
| 350 | try: |
| 351 | _server = _kube_conf["clusters"][0]["cluster"]["server"] |
| 352 | except KeyError as e: |
| 353 | logger_cli.debug( |
| 354 | "... failed to extract server ip: " |
| 355 | "no '{}' key in 'clusters/[0]/cluster/server".format(e) |
| 356 | ) |
| 357 | except IndexError: |
| 358 | logger_cli.debug( |
| 359 | "... failed to extract server ip: empty cluster list" |
| 360 | ) |
| 361 | _ip = _server.split(':') |
| 362 | self.remote_ip = _ip[1].replace('/', '') |
| 363 | logger_cli.debug("... detected ip: '{}'".format(self.remote_ip)) |
| 364 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 365 | # load env file as init os.environment with its values |
Alex | ccb72e0 | 2021-01-20 16:38:03 -0600 | [diff] [blame] | 366 | if os.path.isfile(config_path): |
| 367 | with open(config_path) as _f: |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 368 | _list = _f.read().splitlines() |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 369 | logger_cli.info( |
| 370 | "# Loading env vars from '{}'".format( |
Alex | ccb72e0 | 2021-01-20 16:38:03 -0600 | [diff] [blame] | 371 | config_path |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 372 | ) |
| 373 | ) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 374 | else: |
| 375 | raise ConfigException( |
Alex Savatieiev | f808cd2 | 2019-03-01 13:17:59 -0600 | [diff] [blame] | 376 | "# Failed to load enviroment vars from '{}'".format( |
Alex | ccb72e0 | 2021-01-20 16:38:03 -0600 | [diff] [blame] | 377 | config_path |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 378 | ) |
| 379 | ) |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 380 | self.vars = [] |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 381 | for index in range(len(_list)): |
| 382 | _line = _list[index] |
| 383 | # skip comments |
| 384 | if _line.strip().startswith('#'): |
| 385 | continue |
| 386 | # validate |
| 387 | _errors = [] |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 388 | if len(_line) < 1: |
| 389 | _errors.append("Line {}: empty".format(index)) |
| 390 | elif _line.find('=') < 0 or _line.count('=') > 1: |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 391 | _errors.append("Line {}: {}".format(index, _line)) |
| 392 | else: |
| 393 | # save values |
| 394 | _t = _line.split('=') |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 395 | self.vars.append([_t[0], _t[1]]) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 396 | # if there was errors, report them |
| 397 | if _errors: |
| 398 | raise ConfigException( |
Alex Savatieiev | f808cd2 | 2019-03-01 13:17:59 -0600 | [diff] [blame] | 399 | "# Environment file failed validation in lines: {}".format( |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 400 | "\n".join(_errors) |
| 401 | ) |
| 402 | ) |
| 403 | else: |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 404 | logger_cli.debug( |
Alex | c4f5962 | 2021-08-27 13:42:00 -0500 | [diff] [blame] | 405 | "... loaded total of '{}' vars".format( |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 406 | len(_list) |
| 407 | ) |
| 408 | ) |
Alex | ccb72e0 | 2021-01-20 16:38:03 -0600 | [diff] [blame] | 409 | self.env_name = env_name |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 410 | |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 411 | def __init__(self, args): |
Alex | 336697e | 2023-05-22 16:49:32 -0500 | [diff] [blame] | 412 | """Base configuration class. |
| 413 | Only values that are common for all scripts |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 414 | """ |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 415 | self.ssh_uses_sudo = args.sudo |
Alex | effa068 | 2021-06-04 12:18:33 -0500 | [diff] [blame] | 416 | self.ssh_direct = args.ssh_direct |
Alex | ccb72e0 | 2021-01-20 16:38:03 -0600 | [diff] [blame] | 417 | self.kube_config_path = args.kube_config |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 418 | self.debug = args.debug |
Alex | 3374781 | 2021-04-07 10:11:39 -0500 | [diff] [blame] | 419 | self.insecure = args.insecure |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 420 | self.force_no_key = args.force_no_key |
Alex | 6b4e8fe | 2022-11-10 11:48:37 -0600 | [diff] [blame] | 421 | self.force_node_network = args.force_node_network |
Alex | e9908f7 | 2020-05-19 16:04:53 -0500 | [diff] [blame] | 422 | # Make sure we running on Python 3 |
| 423 | if sys.version_info[0] < 3 and sys.version_info[1] < 5: |
| 424 | logger_cli.error("# ERROR: Python 3.5+ is required") |
| 425 | sys.exit(1) |
| 426 | else: |
| 427 | logger_cli.debug("### Python version is {}.{}".format( |
| 428 | sys.version_info[0], |
| 429 | sys.version_info[1] |
| 430 | )) |
| 431 | |
Alex | ccb72e0 | 2021-01-20 16:38:03 -0600 | [diff] [blame] | 432 | # if env name is default, check var too |
| 433 | if args.env_name == ENV_LOCAL: |
Alex | f6ec91b | 2021-09-10 10:11:17 -0500 | [diff] [blame] | 434 | _env = _get_env_value('MCP_ENV', None) |
Alex | ccb72e0 | 2021-01-20 16:38:03 -0600 | [diff] [blame] | 435 | _env = _env if _env else args.env_name |
Alex | 359e575 | 2021-08-16 17:28:30 -0500 | [diff] [blame] | 436 | _env_config_path = os.path.join(pkg_dir, 'etc', _env + '.env') |
Alex | ccb72e0 | 2021-01-20 16:38:03 -0600 | [diff] [blame] | 437 | else: |
| 438 | _env = args.env_name |
Alex | 359e575 | 2021-08-16 17:28:30 -0500 | [diff] [blame] | 439 | _env_config_path = args.env_config |
Alex | f6ec91b | 2021-09-10 10:11:17 -0500 | [diff] [blame] | 440 | logger_cli.debug( |
| 441 | "... env is '{}', config path is '{}'".format( |
| 442 | _env, |
| 443 | _env_config_path |
| 444 | ) |
| 445 | ) |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 446 | |
| 447 | # Init environment variables from file, validate |
Alex | f6ec91b | 2021-09-10 10:11:17 -0500 | [diff] [blame] | 448 | logger_cli.debug( |
| 449 | "... init environment values from '{}'".format(_env_config_path) |
| 450 | ) |
Alex | 359e575 | 2021-08-16 17:28:30 -0500 | [diff] [blame] | 451 | self._init_env(_env_config_path, env_name=_env) |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 452 | # Load Common vars for any type of the env |
Alex | f6ec91b | 2021-09-10 10:11:17 -0500 | [diff] [blame] | 453 | logger_cli.debug("... loading common variables") |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 454 | self._init_mcp_values() |
| 455 | # Detect env types present |
| 456 | self._detect_types() |
| 457 | # handle forced env type var |
Alex | f6ec91b | 2021-09-10 10:11:17 -0500 | [diff] [blame] | 458 | logger_cli.debug("... handling forced env types") |
| 459 | _forced_type = _get_env_value('MCP_TYPE_FORCE', None) |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 460 | if _forced_type in supported_envs: |
| 461 | self.detected_envs.append(_forced_type) |
| 462 | elif _forced_type is not None: |
| 463 | logger_cli.warn( |
| 464 | "Unsupported forced type of '{}'".format( |
| 465 | _forced_type |
| 466 | ) |
| 467 | ) |
| 468 | # Check if any of the envs detected |
| 469 | if len(self.detected_envs) < 1: |
| 470 | if _env is None: |
| 471 | raise ConfigException("No environment types detected locally") |
| 472 | else: |
| 473 | raise ConfigException( |
| 474 | "No environment types detected at '{}'".format( |
| 475 | self.mcp_host |
| 476 | ) |
| 477 | ) |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 478 | # initialize path to folders |
Alex | ccb72e0 | 2021-01-20 16:38:03 -0600 | [diff] [blame] | 479 | if self.env_name == ENV_LOCAL: |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 480 | # names and folders |
| 481 | self.user = self.pw_user.pw_name |
| 482 | self.homepath = self.pw_user.pw_dir |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 483 | else: |
| 484 | # names and folders in case of remote env |
| 485 | self.user = self.ssh_user |
| 486 | self.homepath = os.path.join('/home', self.ssh_user) |
Alex | f6ec91b | 2021-09-10 10:11:17 -0500 | [diff] [blame] | 487 | logger_cli.debug("... system user for ssh is '{}'".format(self.user)) |
| 488 | logger_cli.debug("... system home path is '{}'".format(self.homepath)) |
Alex | ccb72e0 | 2021-01-20 16:38:03 -0600 | [diff] [blame] | 489 | |
| 490 | # Init vars that is specific to detected envs only |
Alex | f6ec91b | 2021-09-10 10:11:17 -0500 | [diff] [blame] | 491 | logger_cli.debug("... loading detected environment type vars") |
Alex | ccb72e0 | 2021-01-20 16:38:03 -0600 | [diff] [blame] | 492 | self._init_env_values() |
Alex | 0bcf31b | 2022-03-29 17:38:58 -0500 | [diff] [blame] | 493 | # Set internal resource preparation flag default |
| 494 | self.prepare_qa_resources = True |