blob: e58ed40e2cc220f9162f7a528ce6b581de8eb2ef [file] [log] [blame]
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00001import os
2import yaml
3import requests
4import re
5
6
7class salt_remote:
Oleksii Zhurba28dea432017-10-05 16:48:16 -05008 def cmd(self, tgt, fun, param=None,expr_form=None,tgt_type=None):
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00009 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 Zhurba28dea432017-10-05 16:48:16 -050015 accept_key_payload = {'fun': fun,'tgt':tgt,'client':'local','expr_form':expr_form,'tgt_type':tgt_type}
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000016 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
24def init_salt_client():
25 local = salt_remote()
26 return local
27
28
29def 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
42def 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 Zhurbaa10927b2017-09-27 22:09:23 +000077 return groups
78
79
80def 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