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', |
| 14 | 'expr_form': expr_form, 'tgt_type': tgt_type} |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 15 | if param: |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 16 | accept_key_payload['arg'] = param |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 17 | |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 18 | login_request = requests.post(os.path.join(config['SALT_URL'], |
| 19 | 'login'), |
| 20 | headers=headers, data=login_payload) |
| 21 | request = requests.post(config['SALT_URL'], headers=headers, |
| 22 | data=accept_key_payload, |
| 23 | cookies=login_request.cookies) |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 24 | return request.json()['return'][0] |
| 25 | |
| 26 | |
| 27 | def init_salt_client(): |
| 28 | local = salt_remote() |
| 29 | return local |
| 30 | |
| 31 | |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 32 | def list_to_target_string(node_list, separator): |
| 33 | result = '' |
| 34 | for node in node_list: |
| 35 | result += node + ' ' + separator + ' ' |
| 36 | return result.strip(' ' + separator + ' ') |
| 37 | |
| 38 | |
Oleksii Zhurba | 020fab4 | 2017-11-01 20:13:28 +0000 | [diff] [blame^] | 39 | def get_monitoring_ip(param_name): |
| 40 | local_salt_client = init_salt_client() |
| 41 | salt_output = local_salt_client.cmd( |
| 42 | 'docker:client:stack:monitoring', |
| 43 | 'pillar.get', |
| 44 | ['_param:{}'.format(param_name)], |
| 45 | expr_form='pillar') |
| 46 | return salt_output[salt_output.keys()[0]] |
| 47 | |
| 48 | |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 49 | def get_active_nodes(test=None): |
| 50 | config = get_configuration() |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 51 | local_salt_client = init_salt_client() |
| 52 | |
| 53 | skipped_nodes = config.get('skipped_nodes') or [] |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 54 | if test: |
| 55 | testname = test.split('.')[0] |
| 56 | if 'skipped_nodes' in config.get(testname).keys(): |
| 57 | skipped_nodes += config.get(testname)['skipped_nodes'] or [] |
| 58 | |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 59 | if skipped_nodes != ['']: |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 60 | print "\nNotice: {0} nodes will be skipped".format(skipped_nodes) |
| 61 | nodes = local_salt_client.cmd( |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 62 | '* and not ' + list_to_target_string(skipped_nodes, 'and not'), |
| 63 | 'test.ping', |
| 64 | expr_form='compound') |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 65 | else: |
| 66 | nodes = local_salt_client.cmd('*', 'test.ping') |
| 67 | return nodes |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 68 | |
| 69 | |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 70 | def get_groups(test): |
| 71 | config = get_configuration() |
| 72 | testname = test.split('.')[0] |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 73 | # assume that node name is like <name>.domain |
| 74 | # last 1-3 digits of name are index, e.g. 001 in cpu001 |
| 75 | # name doesn't contain dots |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 76 | active_nodes = get_active_nodes() |
| 77 | |
| 78 | skipped_groups = config.get('skipped_groups') or [] |
| 79 | if config.get(testname): |
| 80 | if 'skipped_groups' in config.get(testname).keys(): |
| 81 | skipped_groups += config.get(testname)['skipped_groups'] or [] |
| 82 | |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 83 | groups = [] |
| 84 | |
| 85 | for node in active_nodes: |
| 86 | index = re.search('[0-9]{1,3}$', node.split('.')[0]) |
| 87 | if index: |
| 88 | group_name = node.split('.')[0][:-len(index.group(0))] |
| 89 | else: |
| 90 | group_name = node |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 91 | if group_name not in groups: |
| 92 | if group_name not in skipped_groups: |
| 93 | groups.append(group_name) |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 94 | else: |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 95 | if group_name + " - skipped" not in groups: |
| 96 | groups.append(group_name + " - skipped") |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 97 | |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 98 | return groups |
| 99 | |
| 100 | |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 101 | def get_configuration(): |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 102 | """function returns configuration for environment |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 103 | and for test if it's specified""" |
| 104 | global_config_file = os.path.join( |
| 105 | os.path.dirname(os.path.abspath(__file__)), "../global_config.yaml") |
| 106 | with open(global_config_file, 'r') as file: |
| 107 | global_config = yaml.load(file) |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 108 | for param in global_config.keys(): |
| 109 | if param in os.environ.keys(): |
| 110 | if ',' in os.environ[param]: |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 111 | global_config[param] = [] |
Oleksii Zhurba | e0668ae | 2017-10-27 23:58:18 +0000 | [diff] [blame] | 112 | for item in os.environ[param].split(','): |
| 113 | global_config[param].append(item) |
| 114 | else: |
Oleksii Zhurba | 3dbed24 | 2017-10-31 19:58:53 +0000 | [diff] [blame] | 115 | global_config[param] = os.environ[param] |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 116 | |
| 117 | return global_config |