Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 1 | import os |
| 2 | import yaml |
| 3 | import requests |
| 4 | import re |
| 5 | |
| 6 | |
| 7 | class salt_remote: |
Oleksii Zhurba | 28dea43 | 2017-10-05 16:48:16 -0500 | [diff] [blame] | 8 | def cmd(self, tgt, fun, param=None,expr_form=None,tgt_type=None): |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 9 | config = get_configuration(__file__) |
| 10 | for salt_cred in ['SALT_USERNAME', 'SALT_PASSWORD', 'SALT_URL']: |
| 11 | if os.environ.get(salt_cred): |
| 12 | config[salt_cred] = os.environ[salt_cred] |
| 13 | headers = {'Accept':'application/json'} |
| 14 | login_payload = {'username':config['SALT_USERNAME'],'password':config['SALT_PASSWORD'],'eauth':'pam'} |
Oleksii Zhurba | 28dea43 | 2017-10-05 16:48:16 -0500 | [diff] [blame] | 15 | accept_key_payload = {'fun': fun,'tgt':tgt,'client':'local','expr_form':expr_form,'tgt_type':tgt_type} |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 16 | if param: |
| 17 | accept_key_payload['arg']=param |
| 18 | |
| 19 | login_request = requests.post(os.path.join(config['SALT_URL'],'login'),headers=headers,data=login_payload) |
| 20 | request = requests.post(config['SALT_URL'],headers=headers,data=accept_key_payload,cookies=login_request.cookies) |
| 21 | return request.json()['return'][0] |
| 22 | |
| 23 | |
| 24 | def init_salt_client(): |
| 25 | local = salt_remote() |
| 26 | return local |
| 27 | |
| 28 | |
| 29 | def get_active_nodes(config): |
| 30 | local_salt_client = init_salt_client() |
| 31 | |
| 32 | skipped_nodes = config.get('skipped_nodes') or [] |
| 33 | # TODO add skipped nodes to cmd command instead of filtering |
| 34 | nodes = local_salt_client.cmd('*', 'test.ping') |
| 35 | active_nodes = [ |
| 36 | node_name for node_name in nodes |
| 37 | if nodes[node_name] and node_name not in skipped_nodes |
| 38 | ] |
| 39 | return active_nodes |
| 40 | |
| 41 | |
| 42 | def get_groups(config): |
| 43 | # assume that node name is like <name>.domain |
| 44 | # last 1-3 digits of name are index, e.g. 001 in cpu001 |
| 45 | # name doesn't contain dots |
| 46 | active_nodes = get_active_nodes(config) |
| 47 | skipped_group = config.get('skipped_group') or [] |
| 48 | groups = [] |
| 49 | |
| 50 | for node in active_nodes: |
| 51 | index = re.search('[0-9]{1,3}$', node.split('.')[0]) |
| 52 | if index: |
| 53 | group_name = node.split('.')[0][:-len(index.group(0))] |
| 54 | else: |
| 55 | group_name = node |
| 56 | if group_name not in skipped_group and group_name not in groups: |
| 57 | groups.append(group_name) |
| 58 | test_groups = [] |
| 59 | groups_from_config = config.get('groups') |
| 60 | # check if config.yaml contains `groups` key |
| 61 | if groups_from_config is not None: |
| 62 | invalid_groups = [] |
| 63 | for group in groups_from_config: |
| 64 | # check if group name from config |
| 65 | # is substring of one of the groups |
| 66 | grp = [x for x in groups if group in x] |
| 67 | if grp: |
| 68 | test_groups.append(grp[0]) |
| 69 | else: |
| 70 | invalid_groups.append(group) |
| 71 | if invalid_groups: |
| 72 | raise ValueError('Config file contains' |
| 73 | ' invalid groups name: {}'.format(invalid_groups)) |
| 74 | |
| 75 | groups = test_groups if test_groups else groups |
| 76 | |
Oleksii Zhurba | a10927b | 2017-09-27 22:09:23 +0000 | [diff] [blame] | 77 | return groups |
| 78 | |
| 79 | |
| 80 | def get_configuration(path_to_test): |
| 81 | """function returns configuration for environment |
| 82 | |
| 83 | and for test if it's specified""" |
| 84 | global_config_file = os.path.join( |
| 85 | os.path.dirname(os.path.abspath(__file__)), "../global_config.yaml") |
| 86 | with open(global_config_file, 'r') as file: |
| 87 | global_config = yaml.load(file) |
| 88 | |
| 89 | config_file = os.path.join( |
| 90 | os.path.dirname(os.path.abspath(path_to_test)), "config.yaml") |
| 91 | |
| 92 | if os.path.exists(config_file): |
| 93 | with open(config_file, 'r') as file: |
| 94 | global_config.update(yaml.load(file)) |
| 95 | |
| 96 | return global_config |