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