blob: c05a281d49da3416f0aada4dcaecd357027a2832 [file] [log] [blame]
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00001import os
2import yaml
3import requests
4import re
Oleksii Zhurba3dbed242017-10-31 19:58:53 +00005
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00006
7class salt_remote:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +00008 def cmd(self, tgt, fun, param=None, expr_form=None, tgt_type=None):
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +00009 config = get_configuration()
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000010 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 Zhurbaa10927b2017-09-27 22:09:23 +000015 if param:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000016 accept_key_payload['arg'] = param
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000017
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000018 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 Zhurbaa10927b2017-09-27 22:09:23 +000024 return request.json()['return'][0]
25
26
27def init_salt_client():
28 local = salt_remote()
29 return local
30
31
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000032def 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 Zhurba020fab42017-11-01 20:13:28 +000039def 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 Zhurbae0668ae2017-10-27 23:58:18 +000049def get_active_nodes(test=None):
50 config = get_configuration()
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000051 local_salt_client = init_salt_client()
52
53 skipped_nodes = config.get('skipped_nodes') or []
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000054 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 Zhurba3dbed242017-10-31 19:58:53 +000059 if skipped_nodes != ['']:
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000060 print "\nNotice: {0} nodes will be skipped".format(skipped_nodes)
61 nodes = local_salt_client.cmd(
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000062 '* and not ' + list_to_target_string(skipped_nodes, 'and not'),
63 'test.ping',
64 expr_form='compound')
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000065 else:
66 nodes = local_salt_client.cmd('*', 'test.ping')
67 return nodes
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000068
69
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000070def get_groups(test):
71 config = get_configuration()
72 testname = test.split('.')[0]
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000073 # 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 Zhurbae0668ae2017-10-27 23:58:18 +000076 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 Zhurbaa10927b2017-09-27 22:09:23 +000083 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 Zhurbae0668ae2017-10-27 23:58:18 +000091 if group_name not in groups:
92 if group_name not in skipped_groups:
93 groups.append(group_name)
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000094 else:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000095 if group_name + " - skipped" not in groups:
96 groups.append(group_name + " - skipped")
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000097
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000098 return groups
99
100
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000101def get_configuration():
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000102 """function returns configuration for environment
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000103 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 Zhurbae0668ae2017-10-27 23:58:18 +0000108 for param in global_config.keys():
109 if param in os.environ.keys():
110 if ',' in os.environ[param]:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +0000111 global_config[param] = []
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000112 for item in os.environ[param].split(','):
113 global_config[param].append(item)
114 else:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +0000115 global_config[param] = os.environ[param]
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000116
117 return global_config