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 | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 5 | |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 6 | |
| 7 | class salt_remote: |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 8 | 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] | 9 | config = get_configuration() |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 10 | headers = {'Accept': 'application/json'} |
| 11 | login_payload = {'username': config['SALT_USERNAME'], |
| 12 | 'password': config['SALT_PASSWORD'], 'eauth': 'pam'} |
| 13 | accept_key_payload = {'fun': fun, 'tgt': tgt, 'client': 'local', |
Oleksii Zhurba | 7a67111 | 2018-01-12 11:50:56 -0600 | [diff] [blame] | 14 | 'expr_form': expr_form, 'tgt_type': tgt_type, |
| 15 | 'timeout': config['salt_timeout']} |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 16 | if param: |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 17 | accept_key_payload['arg'] = param |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 18 | |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 19 | login_request = requests.post(os.path.join(config['SALT_URL'], |
| 20 | 'login'), |
| 21 | headers=headers, data=login_payload) |
vkhalin | 8b40267 | 2017-12-05 17:47:57 +0200 | [diff] [blame] | 22 | if login_request.ok: |
| 23 | request = requests.post(config['SALT_URL'], headers=headers, |
| 24 | data=accept_key_payload, |
| 25 | cookies=login_request.cookies) |
| 26 | return request.json()['return'][0] |
| 27 | else: |
| 28 | raise EnvironmentError("401 Not authorized.") |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 29 | |
| 30 | |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 31 | node_groups = {} |
| 32 | |
| 33 | |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 34 | def init_salt_client(): |
| 35 | local = salt_remote() |
| 36 | return local |
| 37 | |
| 38 | |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 39 | def list_to_target_string(node_list, separator): |
| 40 | result = '' |
| 41 | for node in node_list: |
| 42 | result += node + ' ' + separator + ' ' |
Oleksii Zhurba | 26a3a1c | 2018-01-16 19:10:20 +0000 | [diff] [blame^] | 43 | return result[:-(len(separator)+2)] |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 44 | |
| 45 | |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame] | 46 | def get_monitoring_ip(param_name): |
| 47 | local_salt_client = init_salt_client() |
| 48 | salt_output = local_salt_client.cmd( |
| 49 | 'docker:client:stack:monitoring', |
| 50 | 'pillar.get', |
| 51 | ['_param:{}'.format(param_name)], |
| 52 | expr_form='pillar') |
| 53 | return salt_output[salt_output.keys()[0]] |
| 54 | |
| 55 | |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 56 | def get_active_nodes(test=None): |
| 57 | config = get_configuration() |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 58 | local_salt_client = init_salt_client() |
| 59 | |
| 60 | skipped_nodes = config.get('skipped_nodes') or [] |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 61 | if test: |
| 62 | testname = test.split('.')[0] |
| 63 | if 'skipped_nodes' in config.get(testname).keys(): |
| 64 | skipped_nodes += config.get(testname)['skipped_nodes'] or [] |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 65 | if skipped_nodes != ['']: |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 66 | print "\nNotice: {0} nodes will be skipped".format(skipped_nodes) |
| 67 | nodes = local_salt_client.cmd( |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 68 | '* and not ' + list_to_target_string(skipped_nodes, 'and not'), |
| 69 | 'test.ping', |
| 70 | expr_form='compound') |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 71 | else: |
| 72 | nodes = local_salt_client.cmd('*', 'test.ping') |
| 73 | return nodes |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 74 | |
| 75 | |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 76 | def calculate_groups(): |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 77 | config = get_configuration() |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 78 | local_salt_client = init_salt_client() |
| 79 | nodes_names = set () |
| 80 | expr_form = '' |
| 81 | if 'groups' in config.keys(): |
| 82 | nodes_names.update(config['groups'].keys()) |
| 83 | expr_form = 'pillar' |
| 84 | else: |
| 85 | nodes = local_salt_client.cmd('*', 'test.ping') |
| 86 | for node in nodes: |
| 87 | index = re.search('[0-9]{1,3}$', node.split('.')[0]) |
| 88 | if index: |
| 89 | nodes_names.add(node.split('.')[0][:-len(index.group(0))]) |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 90 | else: |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 91 | nodes_names.add(node) |
| 92 | expr_form = 'pcre' |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 93 | |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 94 | for node_name in nodes_names: |
| 95 | skipped_groups = config.get('skipped_groups') or [] |
| 96 | if node_name in skipped_groups: |
| 97 | continue |
| 98 | if expr_form == 'pcre': |
| 99 | nodes = local_salt_client.cmd(node_name, |
| 100 | 'test.ping', |
| 101 | expr_form=expr_form) |
| 102 | else: |
| 103 | nodes = local_salt_client.cmd(config['groups'][node_name], |
| 104 | 'test.ping', |
| 105 | expr_form=expr_form) |
| 106 | node_groups[node_name]=[x for x in nodes if x not in config['skipped_nodes']] |
| 107 | |
| 108 | |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 109 | def get_configuration(): |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 110 | """function returns configuration for environment |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 111 | and for test if it's specified""" |
| 112 | global_config_file = os.path.join( |
| 113 | os.path.dirname(os.path.abspath(__file__)), "../global_config.yaml") |
| 114 | with open(global_config_file, 'r') as file: |
| 115 | global_config = yaml.load(file) |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 116 | for param in global_config.keys(): |
| 117 | if param in os.environ.keys(): |
| 118 | if ',' in os.environ[param]: |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 119 | global_config[param] = [] |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 120 | for item in os.environ[param].split(','): |
| 121 | global_config[param].append(item) |
| 122 | else: |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 123 | global_config[param] = os.environ[param] |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 124 | |
| 125 | return global_config |
Oleksii Zhurba | e0dedb5 | 2018-01-16 00:55:25 +0000 | [diff] [blame] | 126 | |
| 127 | |
| 128 | calculate_groups() |