Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 1 | import os |
| 2 | import yaml |
| 3 | import requests |
| 4 | import re |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 5 | import sys, traceback |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 6 | |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 7 | |
Mikhail Chernik | c492a68 | 2018-08-27 22:41:17 +0200 | [diff] [blame] | 8 | class AuthenticationError(Exception): |
| 9 | pass |
| 10 | |
| 11 | |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 12 | class salt_remote: |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 13 | def cmd(self, tgt, fun, param=None, expr_form=None, tgt_type=None): |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 14 | config = get_configuration() |
Valentyn Khalin | 81dd23d | 2018-09-20 15:39:07 +0300 | [diff] [blame] | 15 | url = config['SALT_URL'].strip() |
| 16 | if not re.match("^(http|https)://", url): |
| 17 | raise AuthenticationError("Salt URL should start \ |
| 18 | with http or https, given - {}".format(url)) |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 19 | proxies = {"http": None, "https": None} |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 20 | headers = {'Accept': 'application/json'} |
| 21 | login_payload = {'username': config['SALT_USERNAME'], |
| 22 | 'password': config['SALT_PASSWORD'], 'eauth': 'pam'} |
| 23 | accept_key_payload = {'fun': fun, 'tgt': tgt, 'client': 'local', |
Oleksii Zhurba | 7a67111 | 2018-01-12 11:50:56 -0600 | [diff] [blame] | 24 | 'expr_form': expr_form, 'tgt_type': tgt_type, |
| 25 | 'timeout': config['salt_timeout']} |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 26 | if param: |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 27 | accept_key_payload['arg'] = param |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 28 | |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 29 | try: |
| 30 | login_request = requests.post(os.path.join(url, 'login'), |
| 31 | headers=headers, data=login_payload, |
| 32 | proxies=proxies) |
Mikhail Chernik | c492a68 | 2018-08-27 22:41:17 +0200 | [diff] [blame] | 33 | if not login_request.ok: |
| 34 | raise AuthenticationError("Authentication to SaltMaster failed") |
| 35 | |
| 36 | request = requests.post(url, headers=headers, |
| 37 | data=accept_key_payload, |
| 38 | cookies=login_request.cookies, |
| 39 | proxies=proxies) |
| 40 | |
| 41 | response = request.json()['return'][0] |
| 42 | not_responded = [k for k,v in response.iteritems() if v is False] |
| 43 | if not_responded: |
| 44 | print ("WARNING: Some nodes are unavailable and removed " |
| 45 | "from response: {}".format(', '.join(not_responded)) |
| 46 | ) |
| 47 | for node in not_responded: |
| 48 | del response[node] |
| 49 | return response |
| 50 | |
| 51 | except Exception as e: |
| 52 | print ("\033[91m\nConnection to SaltMaster " |
| 53 | "was not established.\n" |
| 54 | "Please make sure that you " |
| 55 | "provided correct credentials.\n" |
| 56 | "Error message: {}\033[0m\n".format(e.message or e) |
| 57 | ) |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 58 | traceback.print_exc(file=sys.stdout) |
| 59 | sys.exit() |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 60 | |
| 61 | |
| 62 | def init_salt_client(): |
| 63 | local = salt_remote() |
| 64 | return local |
| 65 | |
| 66 | |
Mikhail Chernik | 714596e | 2018-08-10 21:19:07 +0200 | [diff] [blame] | 67 | def list_to_target_string(node_list, separator, add_spaces=True): |
| 68 | if add_spaces: |
| 69 | separator = ' ' + separator.strip() + ' ' |
| 70 | return separator.join(node_list) |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 71 | |
| 72 | |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 73 | def get_monitoring_ip(param_name): |
| 74 | local_salt_client = init_salt_client() |
| 75 | salt_output = local_salt_client.cmd( |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 76 | 'salt:master', |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 77 | 'pillar.get', |
| 78 | ['_param:{}'.format(param_name)], |
| 79 | expr_form='pillar') |
| 80 | return salt_output[salt_output.keys()[0]] |
| 81 | |
| 82 | |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 83 | def get_active_nodes(test=None): |
| 84 | config = get_configuration() |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 85 | local_salt_client = init_salt_client() |
| 86 | |
| 87 | skipped_nodes = config.get('skipped_nodes') or [] |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 88 | if test: |
| 89 | testname = test.split('.')[0] |
| 90 | if 'skipped_nodes' in config.get(testname).keys(): |
| 91 | skipped_nodes += config.get(testname)['skipped_nodes'] or [] |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 92 | if skipped_nodes != ['']: |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 93 | print "\nNotice: {0} nodes will be skipped".format(skipped_nodes) |
| 94 | nodes = local_salt_client.cmd( |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 95 | '* and not ' + list_to_target_string(skipped_nodes, 'and not'), |
| 96 | 'test.ping', |
| 97 | expr_form='compound') |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 98 | else: |
| 99 | nodes = local_salt_client.cmd('*', 'test.ping') |
| 100 | return nodes |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 101 | |
| 102 | |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 103 | def calculate_groups(): |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 104 | config = get_configuration() |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 105 | local_salt_client = init_salt_client() |
Oleksii Zhurba | 30122e1 | 2018-03-29 14:01:50 -0500 | [diff] [blame] | 106 | node_groups = {} |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 107 | nodes_names = set () |
| 108 | expr_form = '' |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 109 | all_nodes = set(local_salt_client.cmd('*', 'test.ping')) |
| 110 | if 'groups' in config.keys() and 'PB_GROUPS' in os.environ.keys() and \ |
| 111 | os.environ['PB_GROUPS'].lower() != 'false': |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 112 | nodes_names.update(config['groups'].keys()) |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 113 | expr_form = 'compound' |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 114 | else: |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 115 | for node in all_nodes: |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 116 | index = re.search('[0-9]{1,3}$', node.split('.')[0]) |
| 117 | if index: |
| 118 | nodes_names.add(node.split('.')[0][:-len(index.group(0))]) |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 119 | else: |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 120 | nodes_names.add(node) |
| 121 | expr_form = 'pcre' |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 122 | |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 123 | gluster_nodes = local_salt_client.cmd('I@salt:control and ' |
| 124 | 'I@glusterfs:server', |
| 125 | 'test.ping', expr_form='compound') |
| 126 | kvm_nodes = local_salt_client.cmd('I@salt:control and not ' |
| 127 | 'I@glusterfs:server', |
| 128 | 'test.ping', expr_form='compound') |
| 129 | |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 130 | for node_name in nodes_names: |
| 131 | skipped_groups = config.get('skipped_groups') or [] |
| 132 | if node_name in skipped_groups: |
| 133 | continue |
| 134 | if expr_form == 'pcre': |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 135 | nodes = local_salt_client.cmd('{}[0-9]{{1,3}}'.format(node_name), |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 136 | 'test.ping', |
| 137 | expr_form=expr_form) |
| 138 | else: |
| 139 | nodes = local_salt_client.cmd(config['groups'][node_name], |
| 140 | 'test.ping', |
| 141 | expr_form=expr_form) |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 142 | if nodes == {}: |
| 143 | continue |
| 144 | |
| 145 | node_groups[node_name]=[x for x in nodes |
| 146 | if x not in config['skipped_nodes'] |
| 147 | if x not in gluster_nodes.keys() |
| 148 | if x not in kvm_nodes.keys()] |
| 149 | all_nodes = set(all_nodes - set(node_groups[node_name])) |
| 150 | if node_groups[node_name] == []: |
| 151 | del node_groups[node_name] |
| 152 | if kvm_nodes: |
| 153 | node_groups['kvm'] = kvm_nodes.keys() |
| 154 | node_groups['kvm_gluster'] = gluster_nodes.keys() |
| 155 | all_nodes = set(all_nodes - set(kvm_nodes.keys())) |
| 156 | all_nodes = set(all_nodes - set(gluster_nodes.keys())) |
| 157 | if all_nodes: |
| 158 | print ("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] | 159 | return node_groups |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 160 | |
| 161 | |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 162 | def get_configuration(): |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 163 | """function returns configuration for environment |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 164 | and for test if it's specified""" |
| 165 | global_config_file = os.path.join( |
| 166 | os.path.dirname(os.path.abspath(__file__)), "../global_config.yaml") |
| 167 | with open(global_config_file, 'r') as file: |
| 168 | global_config = yaml.load(file) |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 169 | for param in global_config.keys(): |
| 170 | if param in os.environ.keys(): |
| 171 | if ',' in os.environ[param]: |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 172 | global_config[param] = [] |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 173 | for item in os.environ[param].split(','): |
| 174 | global_config[param].append(item) |
| 175 | else: |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 176 | global_config[param] = os.environ[param] |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 177 | |
| 178 | return global_config |