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