blob: 3c485c5f81799c539ea7fda5b87e832777af9deb [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)
vkhalin8b402672017-12-05 17:47:57 +020021 if login_request.ok:
22 request = requests.post(config['SALT_URL'], headers=headers,
23 data=accept_key_payload,
24 cookies=login_request.cookies)
25 return request.json()['return'][0]
26 else:
27 raise EnvironmentError("401 Not authorized.")
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000028
29
30def init_salt_client():
31 local = salt_remote()
32 return local
33
34
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000035def list_to_target_string(node_list, separator):
36 result = ''
37 for node in node_list:
38 result += node + ' ' + separator + ' '
39 return result.strip(' ' + separator + ' ')
40
41
Oleksii Zhurba020fab42017-11-01 20:13:28 +000042def get_monitoring_ip(param_name):
43 local_salt_client = init_salt_client()
44 salt_output = local_salt_client.cmd(
45 'docker:client:stack:monitoring',
46 'pillar.get',
47 ['_param:{}'.format(param_name)],
48 expr_form='pillar')
49 return salt_output[salt_output.keys()[0]]
50
51
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000052def get_active_nodes(test=None):
53 config = get_configuration()
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000054 local_salt_client = init_salt_client()
55
56 skipped_nodes = config.get('skipped_nodes') or []
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000057 if test:
58 testname = test.split('.')[0]
59 if 'skipped_nodes' in config.get(testname).keys():
60 skipped_nodes += config.get(testname)['skipped_nodes'] or []
61
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000062 if skipped_nodes != ['']:
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000063 print "\nNotice: {0} nodes will be skipped".format(skipped_nodes)
64 nodes = local_salt_client.cmd(
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000065 '* and not ' + list_to_target_string(skipped_nodes, 'and not'),
66 'test.ping',
67 expr_form='compound')
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000068 else:
69 nodes = local_salt_client.cmd('*', 'test.ping')
70 return nodes
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000071
72
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000073def get_groups(test):
74 config = get_configuration()
75 testname = test.split('.')[0]
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000076 # assume that node name is like <name>.domain
77 # last 1-3 digits of name are index, e.g. 001 in cpu001
78 # name doesn't contain dots
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000079 active_nodes = get_active_nodes()
80
81 skipped_groups = config.get('skipped_groups') or []
82 if config.get(testname):
83 if 'skipped_groups' in config.get(testname).keys():
84 skipped_groups += config.get(testname)['skipped_groups'] or []
85
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000086 groups = []
87
88 for node in active_nodes:
89 index = re.search('[0-9]{1,3}$', node.split('.')[0])
90 if index:
91 group_name = node.split('.')[0][:-len(index.group(0))]
92 else:
93 group_name = node
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000094 if group_name not in groups:
95 if group_name not in skipped_groups:
96 groups.append(group_name)
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000097 else:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000098 if group_name + " - skipped" not in groups:
99 groups.append(group_name + " - skipped")
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000100
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000101 return groups
102
103
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000104def get_configuration():
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000105 """function returns configuration for environment
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000106 and for test if it's specified"""
107 global_config_file = os.path.join(
108 os.path.dirname(os.path.abspath(__file__)), "../global_config.yaml")
109 with open(global_config_file, 'r') as file:
110 global_config = yaml.load(file)
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000111 for param in global_config.keys():
112 if param in os.environ.keys():
113 if ',' in os.environ[param]:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +0000114 global_config[param] = []
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000115 for item in os.environ[param].split(','):
116 global_config[param].append(item)
117 else:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +0000118 global_config[param] = os.environ[param]
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000119
120 return global_config