Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 1 | from builtins import range |
| 2 | from builtins import object |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 3 | import os |
| 4 | import yaml |
| 5 | import requests |
| 6 | import re |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 7 | import sys, traceback |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 8 | import time |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 9 | import json |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 10 | import logging |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 11 | |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 12 | |
Mikhail Chernik | c492a68 | 2018-08-27 22:41:17 +0200 | [diff] [blame] | 13 | class AuthenticationError(Exception): |
| 14 | pass |
| 15 | |
| 16 | |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 17 | class salt_remote(object): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 18 | def __init__(self): |
| 19 | self.config = get_configuration() |
| 20 | self.skipped_nodes = self.config.get('skipped_nodes') or [] |
| 21 | self.url = self.config['SALT_URL'].strip() |
| 22 | if not re.match("^(http|https)://", self.url): |
Valentyn Khalin | 81dd23d | 2018-09-20 15:39:07 +0300 | [diff] [blame] | 23 | raise AuthenticationError("Salt URL should start \ |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 24 | with http or https, given - {}".format(self.url)) |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 25 | self.login_payload = {'username': self.config['SALT_USERNAME'], |
| 26 | 'password': self.config['SALT_PASSWORD'], 'eauth': 'pam'} |
| 27 | # TODO: proxies |
| 28 | self.proxies = {"http": None, "https": None} |
| 29 | self.expires = '' |
| 30 | self.cookies = [] |
| 31 | self.headers = {'Accept': 'application/json'} |
| 32 | self._login() |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 33 | |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 34 | def _login (self): |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 35 | try: |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 36 | login_request = requests.post(os.path.join(self.url, 'login'), |
| 37 | headers={'Accept': 'application/json'}, |
| 38 | data=self.login_payload, |
| 39 | proxies=self.proxies) |
Mikhail Chernik | c492a68 | 2018-08-27 22:41:17 +0200 | [diff] [blame] | 40 | if not login_request.ok: |
| 41 | raise AuthenticationError("Authentication to SaltMaster failed") |
Mikhail Chernik | c492a68 | 2018-08-27 22:41:17 +0200 | [diff] [blame] | 42 | except Exception as e: |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 43 | logging.warning("\033[91m\nConnection to SaltMaster " |
| 44 | "was not established.\n" |
| 45 | "Please make sure that you " |
| 46 | "provided correct credentials.\n" |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 47 | "Error message: {}\033[0m\n".format(e)) |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 48 | traceback.print_exc(file=sys.stdout) |
| 49 | sys.exit() |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 50 | self.expire = login_request.json()['return'][0]['expire'] |
| 51 | self.cookies = login_request.cookies |
| 52 | self.headers['X-Auth-Token'] = login_request.json()['return'][0]['token'] |
| 53 | |
| 54 | def cmd(self, tgt, fun='cmd.run', param=None, expr_form=None, tgt_type=None, check_status=False, retries=3): |
| 55 | if self.expire < time.time() + 300: |
| 56 | self.headers['X-Auth-Token'] = self._login() |
| 57 | accept_key_payload = {'fun': fun, 'tgt': tgt, 'client': 'local', |
| 58 | 'expr_form': expr_form, 'tgt_type': tgt_type, |
| 59 | 'timeout': self.config['salt_timeout']} |
| 60 | if param: |
| 61 | accept_key_payload['arg'] = param |
| 62 | |
| 63 | for i in range(retries): |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 64 | logging.info("="*100) |
| 65 | logging.info("Send Request: {}".format(json.dumps( |
| 66 | accept_key_payload, |
| 67 | indent=4, |
| 68 | sort_keys=True))) |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 69 | request = requests.post(self.url, headers=self.headers, |
| 70 | data=accept_key_payload, |
| 71 | cookies=self.cookies, |
| 72 | proxies=self.proxies) |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 73 | logging.info("-"*100) |
| 74 | logging.info("Response: {}".format(json.dumps( |
| 75 | request.json(), |
| 76 | indent=4 |
| 77 | ))) |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 78 | if not request.ok or not isinstance(request.json()['return'][0], dict): |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 79 | logging.warning("Salt master is not responding or response is incorrect. Output: {}".format(request)) |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 80 | continue |
| 81 | response = request.json()['return'][0] |
| 82 | result = {key: response[key] for key in response if key not in self.skipped_nodes} |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 83 | if check_status and (False in list(result.values()) or not result): |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 84 | logging.warning("One or several nodes are not responding. Output {}".format(json.dumps(result, indent=4))) |
| 85 | continue |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 86 | break |
| 87 | else: |
| 88 | raise Exception("Error with Salt Master response") |
| 89 | return result |
| 90 | |
| 91 | def test_ping(self, tgt, expr_form='pillar'): |
| 92 | return self.cmd(tgt=tgt, fun='test.ping', param=None, expr_form=expr_form) |
| 93 | |
| 94 | def cmd_any(self, tgt, param=None, expr_form='pillar'): |
| 95 | """ |
| 96 | This method returns first non-empty result on node or nodes. |
| 97 | If all nodes returns nothing, then exception is thrown. |
| 98 | """ |
| 99 | response = self.cmd(tgt=tgt, param=param, expr_form=expr_form) |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 100 | for node in list(response.keys()): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 101 | if response[node] or response[node] == '': |
| 102 | return response[node] |
| 103 | else: |
| 104 | raise Exception("All minions are down") |
| 105 | |
| 106 | def pillar_get(self, tgt='salt:master', param=None, expr_form='pillar', fail_if_empty=False): |
| 107 | """ |
| 108 | This method is for fetching pillars only. |
| 109 | Returns value for pillar, False (if no such pillar) or if fail_if_empty=True - exception |
Hanna Arhipova | b9635f9 | 2019-07-18 19:00:35 +0300 | [diff] [blame] | 110 | :param tgt, string, target when the salt command will be executed |
| 111 | :param param, additional parameter for salt command |
| 112 | :param expr_form |
| 113 | :param fail_if_empty |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 114 | """ |
| 115 | response = self.cmd(tgt=tgt, fun='pillar.get', param=param, expr_form=expr_form) |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 116 | for node in list(response.keys()): |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 117 | if response[node] or response[node] != '': |
| 118 | return response[node] |
| 119 | else: |
| 120 | if fail_if_empty: |
| 121 | raise Exception("No pillar found or it is empty.") |
| 122 | else: |
Hanna Arhipova | fae5b5b | 2019-12-09 15:57:55 +0200 | [diff] [blame] | 123 | logging.error( |
| 124 | "suppressed incorrect response from pillar_get: {}".format( |
| 125 | response |
| 126 | )) |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 127 | return False |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 128 | |
| 129 | |
| 130 | def init_salt_client(): |
| 131 | local = salt_remote() |
| 132 | return local |
| 133 | |
| 134 | |
Mikhail Chernik | 714596e | 2018-08-10 21:19:07 +0200 | [diff] [blame] | 135 | def list_to_target_string(node_list, separator, add_spaces=True): |
| 136 | if add_spaces: |
| 137 | separator = ' ' + separator.strip() + ' ' |
| 138 | return separator.join(node_list) |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 139 | |
| 140 | |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 141 | def calculate_groups(): |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 142 | config = get_configuration() |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 143 | local_salt_client = init_salt_client() |
Oleksii Zhurba | 30122e1 | 2018-03-29 14:01:50 -0500 | [diff] [blame] | 144 | node_groups = {} |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 145 | nodes_names = set () |
| 146 | expr_form = '' |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 147 | all_nodes = set(local_salt_client.test_ping(tgt='*', expr_form=None)) |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 148 | if 'groups' in list(config.keys()) and 'PB_GROUPS' in list(os.environ.keys()) and \ |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 149 | os.environ['PB_GROUPS'].lower() != 'false': |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 150 | nodes_names.update(list(config['groups'].keys())) |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 151 | expr_form = 'compound' |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 152 | else: |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 153 | for node in all_nodes: |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 154 | index = re.search('[0-9]{1,3}$', node.split('.')[0]) |
| 155 | if index: |
| 156 | nodes_names.add(node.split('.')[0][:-len(index.group(0))]) |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 157 | else: |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 158 | nodes_names.add(node) |
| 159 | expr_form = 'pcre' |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 160 | |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 161 | gluster_nodes = local_salt_client.test_ping(tgt='I@salt:control and ' |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 162 | 'I@glusterfs:server', |
| 163 | expr_form='compound') |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 164 | kvm_nodes = local_salt_client.test_ping(tgt='I@salt:control and not ' |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 165 | 'I@glusterfs:server', |
| 166 | expr_form='compound') |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 167 | |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 168 | for node_name in nodes_names: |
| 169 | skipped_groups = config.get('skipped_groups') or [] |
| 170 | if node_name in skipped_groups: |
| 171 | continue |
| 172 | if expr_form == 'pcre': |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 173 | nodes = local_salt_client.test_ping(tgt='{}[0-9]{{1,3}}'.format(node_name), |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 174 | expr_form=expr_form) |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 175 | else: |
Oleksii Zhurba | 4bfd2ee | 2019-04-10 21:56:58 -0500 | [diff] [blame] | 176 | nodes = local_salt_client.test_ping(tgt=config['groups'][node_name], |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 177 | expr_form=expr_form) |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 178 | if nodes == {}: |
| 179 | continue |
| 180 | |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 181 | node_groups[node_name] = [x for x in nodes |
| 182 | if x not in config['skipped_nodes'] |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 183 | if x not in list(gluster_nodes.keys()) |
| 184 | if x not in list(kvm_nodes.keys())] |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 185 | all_nodes = set(all_nodes - set(node_groups[node_name])) |
| 186 | if node_groups[node_name] == []: |
| 187 | del node_groups[node_name] |
| 188 | if kvm_nodes: |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 189 | node_groups['kvm'] = list(kvm_nodes.keys()) |
| 190 | node_groups['kvm_gluster'] = list(gluster_nodes.keys()) |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 191 | all_nodes = set(all_nodes - set(kvm_nodes.keys())) |
| 192 | all_nodes = set(all_nodes - set(gluster_nodes.keys())) |
| 193 | if all_nodes: |
Hanna Arhipova | 56eab94 | 2019-05-06 20:14:18 +0300 | [diff] [blame] | 194 | logging.info("These nodes were not collected {0}. Check config (groups section)".format(all_nodes)) |
Oleksii Zhurba | d0ae87f | 2018-03-26 13:36:25 -0500 | [diff] [blame] | 195 | return node_groups |
Hanna Arhipova | 8d4082d | 2019-07-16 16:07:11 +0300 | [diff] [blame] | 196 | |
| 197 | |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 198 | def get_configuration(): |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 199 | """function returns configuration for environment |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 200 | and for test if it's specified""" |
Hanna Arhipova | 91dc815 | 2019-07-04 12:43:16 +0300 | [diff] [blame] | 201 | |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 202 | global_config_file = os.path.join( |
| 203 | os.path.dirname(os.path.abspath(__file__)), "../global_config.yaml") |
| 204 | with open(global_config_file, 'r') as file: |
Hanna Arhipova | 8d4082d | 2019-07-16 16:07:11 +0300 | [diff] [blame] | 205 | global_config = yaml.load(file, Loader=yaml.SafeLoader) |
Ekaterina Chernova | e32e3f9 | 2019-11-12 14:56:03 +0300 | [diff] [blame] | 206 | for param in list(global_config.keys()): |
| 207 | if param in list(os.environ.keys()): |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 208 | if ',' in os.environ[param]: |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 209 | global_config[param] = [] |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 210 | for item in os.environ[param].split(','): |
| 211 | global_config[param].append(item) |
| 212 | else: |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 213 | global_config[param] = os.environ[param] |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 214 | |
Ievgeniia Zadorozhna | 0c306fd | 2019-11-12 16:03:51 +0300 | [diff] [blame] | 215 | if 'OVERRIDE_CONFIG' in os.environ.keys(): |
| 216 | try: |
| 217 | override_config = yaml.load( |
| 218 | os.environ['OVERRIDE_CONFIG'], Loader=yaml.SafeLoader)\ |
| 219 | .get('override_config') |
| 220 | if override_config: |
| 221 | for key in override_config: |
| 222 | if isinstance(global_config[key], dict): |
| 223 | for k in override_config[key]: |
| 224 | global_config[key][k] = override_config[key][k] |
| 225 | else: |
| 226 | global_config[key] = override_config[key] |
| 227 | except Exception: |
| 228 | pass |
| 229 | |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 230 | return global_config |