blob: 840b4ea413cfb1ab5ca97c293ef96790df3b061d [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',
Oleksii Zhurba7a671112018-01-12 11:50:56 -060014 'expr_form': expr_form, 'tgt_type': tgt_type,
15 'timeout': config['salt_timeout']}
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000016 if param:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000017 accept_key_payload['arg'] = param
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000018
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000019 login_request = requests.post(os.path.join(config['SALT_URL'],
20 'login'),
21 headers=headers, data=login_payload)
vkhalin8b402672017-12-05 17:47:57 +020022 if login_request.ok:
23 request = requests.post(config['SALT_URL'], headers=headers,
24 data=accept_key_payload,
25 cookies=login_request.cookies)
26 return request.json()['return'][0]
27 else:
28 raise EnvironmentError("401 Not authorized.")
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000029
30
31def init_salt_client():
32 local = salt_remote()
33 return local
34
35
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000036def list_to_target_string(node_list, separator):
37 result = ''
38 for node in node_list:
39 result += node + ' ' + separator + ' '
Oleksii Zhurba26a3a1c2018-01-16 19:10:20 +000040 return result[:-(len(separator)+2)]
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000041
42
Oleksii Zhurba020fab42017-11-01 20:13:28 +000043def get_monitoring_ip(param_name):
44 local_salt_client = init_salt_client()
45 salt_output = local_salt_client.cmd(
46 'docker:client:stack:monitoring',
47 'pillar.get',
48 ['_param:{}'.format(param_name)],
49 expr_form='pillar')
50 return salt_output[salt_output.keys()[0]]
51
52
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000053def get_active_nodes(test=None):
54 config = get_configuration()
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000055 local_salt_client = init_salt_client()
56
57 skipped_nodes = config.get('skipped_nodes') or []
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000058 if test:
59 testname = test.split('.')[0]
60 if 'skipped_nodes' in config.get(testname).keys():
61 skipped_nodes += config.get(testname)['skipped_nodes'] or []
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 Zhurbae0dedb52018-01-16 00:55:25 +000073def calculate_groups():
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +000074 config = get_configuration()
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +000075 local_salt_client = init_salt_client()
Oleksii Zhurba30122e12018-03-29 14:01:50 -050076 node_groups = {}
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +000077 nodes_names = set ()
78 expr_form = ''
79 if 'groups' in config.keys():
80 nodes_names.update(config['groups'].keys())
81 expr_form = 'pillar'
82 else:
83 nodes = local_salt_client.cmd('*', 'test.ping')
84 for node in nodes:
85 index = re.search('[0-9]{1,3}$', node.split('.')[0])
86 if index:
87 nodes_names.add(node.split('.')[0][:-len(index.group(0))])
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000088 else:
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +000089 nodes_names.add(node)
90 expr_form = 'pcre'
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000091
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +000092 for node_name in nodes_names:
93 skipped_groups = config.get('skipped_groups') or []
94 if node_name in skipped_groups:
95 continue
96 if expr_form == 'pcre':
97 nodes = local_salt_client.cmd(node_name,
98 'test.ping',
99 expr_form=expr_form)
100 else:
101 nodes = local_salt_client.cmd(config['groups'][node_name],
102 'test.ping',
103 expr_form=expr_form)
104 node_groups[node_name]=[x for x in nodes if x not in config['skipped_nodes']]
Oleksii Zhurbad0ae87f2018-03-26 13:36:25 -0500105 return node_groups
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +0000106
107
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000108def get_configuration():
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000109 """function returns configuration for environment
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000110 and for test if it's specified"""
111 global_config_file = os.path.join(
112 os.path.dirname(os.path.abspath(__file__)), "../global_config.yaml")
113 with open(global_config_file, 'r') as file:
114 global_config = yaml.load(file)
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000115 for param in global_config.keys():
116 if param in os.environ.keys():
117 if ',' in os.environ[param]:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +0000118 global_config[param] = []
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000119 for item in os.environ[param].split(','):
120 global_config[param].append(item)
121 else:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +0000122 global_config[param] = os.environ[param]
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000123
124 return global_config