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] |
Mikhail Chernik | c492a68 | 2018-08-27 22:41:17 +0200 | [diff] [blame] | 42 | return response |
| 43 | |
| 44 | except Exception as e: |
| 45 | print ("\033[91m\nConnection to SaltMaster " |
| 46 | "was not established.\n" |
| 47 | "Please make sure that you " |
| 48 | "provided correct credentials.\n" |
| 49 | "Error message: {}\033[0m\n".format(e.message or e) |
| 50 | ) |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 51 | traceback.print_exc(file=sys.stdout) |
| 52 | sys.exit() |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | def init_salt_client(): |
| 56 | local = salt_remote() |
| 57 | return local |
| 58 | |
| 59 | |
Mikhail Chernik | 714596e | 2018-08-10 21:19:07 +0200 | [diff] [blame] | 60 | def list_to_target_string(node_list, separator, add_spaces=True): |
| 61 | if add_spaces: |
| 62 | separator = ' ' + separator.strip() + ' ' |
| 63 | return separator.join(node_list) |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 64 | |
| 65 | |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 66 | def get_monitoring_ip(param_name): |
| 67 | local_salt_client = init_salt_client() |
| 68 | salt_output = local_salt_client.cmd( |
Oleksii Zhurba | 8ce9fcf | 2018-10-05 18:38:22 +0300 | [diff] [blame] | 69 | 'salt:master', |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 70 | 'pillar.get', |
| 71 | ['_param:{}'.format(param_name)], |
| 72 | expr_form='pillar') |
| 73 | return salt_output[salt_output.keys()[0]] |
| 74 | |
| 75 | |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 76 | def get_active_nodes(test=None): |
| 77 | config = get_configuration() |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 78 | local_salt_client = init_salt_client() |
| 79 | |
| 80 | skipped_nodes = config.get('skipped_nodes') or [] |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 81 | if test: |
| 82 | testname = test.split('.')[0] |
| 83 | if 'skipped_nodes' in config.get(testname).keys(): |
| 84 | skipped_nodes += config.get(testname)['skipped_nodes'] or [] |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 85 | if skipped_nodes != ['']: |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 86 | print "\nNotice: {0} nodes will be skipped".format(skipped_nodes) |
| 87 | nodes = local_salt_client.cmd( |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 88 | '* and not ' + list_to_target_string(skipped_nodes, 'and not'), |
| 89 | 'test.ping', |
| 90 | expr_form='compound') |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 91 | else: |
| 92 | nodes = local_salt_client.cmd('*', 'test.ping') |
| 93 | return nodes |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 94 | |
| 95 | |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 96 | def calculate_groups(): |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 97 | config = get_configuration() |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 98 | local_salt_client = init_salt_client() |
Oleksii Zhurba | 30122e1 | 2018-03-29 14:01:50 -0500 | [diff] [blame] | 99 | node_groups = {} |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 100 | nodes_names = set () |
| 101 | expr_form = '' |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 102 | all_nodes = set(local_salt_client.cmd('*', 'test.ping')) |
| 103 | if 'groups' in config.keys() and 'PB_GROUPS' in os.environ.keys() and \ |
| 104 | os.environ['PB_GROUPS'].lower() != 'false': |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 105 | nodes_names.update(config['groups'].keys()) |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 106 | expr_form = 'compound' |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 107 | else: |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 108 | for node in all_nodes: |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 109 | index = re.search('[0-9]{1,3}$', node.split('.')[0]) |
| 110 | if index: |
| 111 | nodes_names.add(node.split('.')[0][:-len(index.group(0))]) |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 112 | else: |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 113 | nodes_names.add(node) |
| 114 | expr_form = 'pcre' |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 115 | |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 116 | gluster_nodes = local_salt_client.cmd('I@salt:control and ' |
| 117 | 'I@glusterfs:server', |
| 118 | 'test.ping', expr_form='compound') |
| 119 | kvm_nodes = local_salt_client.cmd('I@salt:control and not ' |
| 120 | 'I@glusterfs:server', |
| 121 | 'test.ping', expr_form='compound') |
| 122 | |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 123 | for node_name in nodes_names: |
| 124 | skipped_groups = config.get('skipped_groups') or [] |
| 125 | if node_name in skipped_groups: |
| 126 | continue |
| 127 | if expr_form == 'pcre': |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 128 | nodes = local_salt_client.cmd('{}[0-9]{{1,3}}'.format(node_name), |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 129 | 'test.ping', |
| 130 | expr_form=expr_form) |
| 131 | else: |
| 132 | nodes = local_salt_client.cmd(config['groups'][node_name], |
| 133 | 'test.ping', |
| 134 | expr_form=expr_form) |
Oleksii Zhurba | e592ed1 | 2018-06-21 18:01:09 -0500 | [diff] [blame] | 135 | if nodes == {}: |
| 136 | continue |
| 137 | |
| 138 | node_groups[node_name]=[x for x in nodes |
| 139 | if x not in config['skipped_nodes'] |
| 140 | if x not in gluster_nodes.keys() |
| 141 | if x not in kvm_nodes.keys()] |
| 142 | all_nodes = set(all_nodes - set(node_groups[node_name])) |
| 143 | if node_groups[node_name] == []: |
| 144 | del node_groups[node_name] |
| 145 | if kvm_nodes: |
| 146 | node_groups['kvm'] = kvm_nodes.keys() |
| 147 | node_groups['kvm_gluster'] = gluster_nodes.keys() |
| 148 | all_nodes = set(all_nodes - set(kvm_nodes.keys())) |
| 149 | all_nodes = set(all_nodes - set(gluster_nodes.keys())) |
| 150 | if all_nodes: |
| 151 | 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] | 152 | return node_groups |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 153 | |
| 154 | |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 155 | def get_configuration(): |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 156 | """function returns configuration for environment |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 157 | and for test if it's specified""" |
| 158 | global_config_file = os.path.join( |
| 159 | os.path.dirname(os.path.abspath(__file__)), "../global_config.yaml") |
| 160 | with open(global_config_file, 'r') as file: |
| 161 | global_config = yaml.load(file) |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 162 | for param in global_config.keys(): |
| 163 | if param in os.environ.keys(): |
| 164 | if ',' in os.environ[param]: |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 165 | global_config[param] = [] |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 166 | for item in os.environ[param].split(','): |
| 167 | global_config[param].append(item) |
| 168 | else: |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 169 | global_config[param] = os.environ[param] |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 170 | |
| 171 | return global_config |