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