blob: 44b163f435a9748f2cde0b1f2b9f5dcc708534d0 [file] [log] [blame]
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +03001import os
2import yaml
3import requests
4import re
5import sys, traceback
6import time
Hanna Arhipova1eef8312019-05-06 20:14:18 +03007import json
8import pytest
9import logging
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030010
11
12class AuthenticationError(Exception):
13 pass
14
15
16class salt_remote:
17 def __init__(self):
18 self.config = get_configuration()
19 self.skipped_nodes = self.config.get('skipped_nodes') or []
20 self.url = self.config['SALT_URL'].strip()
21 if not re.match("^(http|https)://", self.url):
22 raise AuthenticationError("Salt URL should start \
Hanna Arhipova1eef8312019-05-06 20:14:18 +030023 with http or https, given - {}".format(self.url))
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030024 self.login_payload = {'username': self.config['SALT_USERNAME'],
25 'password': self.config['SALT_PASSWORD'], 'eauth': 'pam'}
26 # TODO: proxies
27 self.proxies = {"http": None, "https": None}
28 self.expires = ''
29 self.cookies = []
30 self.headers = {'Accept': 'application/json'}
31 self._login()
32
33 def _login (self):
34 try:
35 login_request = requests.post(os.path.join(self.url, 'login'),
36 headers={'Accept': 'application/json'},
37 data=self.login_payload,
38 proxies=self.proxies)
39 if not login_request.ok:
40 raise AuthenticationError("Authentication to SaltMaster failed")
41 except Exception as e:
Hanna Arhipova1eef8312019-05-06 20:14:18 +030042 logging.warning("\033[91m\nConnection to SaltMaster "
43 "was not established.\n"
44 "Please make sure that you "
45 "provided correct credentials.\n"
46 "Error message: {}\033[0m\n".format(e.message or e))
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030047 traceback.print_exc(file=sys.stdout)
48 sys.exit()
49 self.expire = login_request.json()['return'][0]['expire']
50 self.cookies = login_request.cookies
51 self.headers['X-Auth-Token'] = login_request.json()['return'][0]['token']
52
53 def cmd(self, tgt, fun='cmd.run', param=None, expr_form=None, tgt_type=None, check_status=False, retries=3):
54 if self.expire < time.time() + 300:
55 self.headers['X-Auth-Token'] = self._login()
56 accept_key_payload = {'fun': fun, 'tgt': tgt, 'client': 'local',
57 'expr_form': expr_form, 'tgt_type': tgt_type,
58 'timeout': self.config['salt_timeout']}
59 if param:
60 accept_key_payload['arg'] = param
61
62 for i in range(retries):
Hanna Arhipova1eef8312019-05-06 20:14:18 +030063 logging.info("="*100)
64 logging.info("Send Request: {}".format(json.dumps(
65 accept_key_payload,
66 indent=4,
67 sort_keys=True)))
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030068 request = requests.post(self.url, headers=self.headers,
69 data=accept_key_payload,
70 cookies=self.cookies,
71 proxies=self.proxies)
Hanna Arhipova1eef8312019-05-06 20:14:18 +030072 logging.info("-"*100)
73 logging.info("Response: {}".format(json.dumps(
74 request.json(),
75 indent=4
76 )))
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030077 if not request.ok or not isinstance(request.json()['return'][0], dict):
Hanna Arhipova1eef8312019-05-06 20:14:18 +030078 logging.warning("Salt master is not responding or response is incorrect. Output: {}".format(request))
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030079 continue
80 response = request.json()['return'][0]
81 result = {key: response[key] for key in response if key not in self.skipped_nodes}
Oleksii Zhurbab4d88022019-05-28 21:41:46 -050082 if check_status and (False in result.values() or not result):
Hanna Arhipova1eef8312019-05-06 20:14:18 +030083 logging.warning("One or several nodes are not responding. Output {}".format(json.dumps(result, indent=4)))
84 continue
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +030085 break
86 else:
87 raise Exception("Error with Salt Master response")
88 return result
89
90 def test_ping(self, tgt, expr_form='pillar'):
91 return self.cmd(tgt=tgt, fun='test.ping', param=None, expr_form=expr_form)
92
93 def cmd_any(self, tgt, param=None, expr_form='pillar'):
94 """
95 This method returns first non-empty result on node or nodes.
96 If all nodes returns nothing, then exception is thrown.
97 """
98 response = self.cmd(tgt=tgt, param=param, expr_form=expr_form)
99 for node in response.keys():
100 if response[node] or response[node] == '':
101 return response[node]
102 else:
103 raise Exception("All minions are down")
104
105 def pillar_get(self, tgt='salt:master', param=None, expr_form='pillar', fail_if_empty=False):
106 """
107 This method is for fetching pillars only.
108 Returns value for pillar, False (if no such pillar) or if fail_if_empty=True - exception
109 """
110 response = self.cmd(tgt=tgt, fun='pillar.get', param=param, expr_form=expr_form)
111 for node in response.keys():
112 if response[node] or response[node] != '':
113 return response[node]
114 else:
115 if fail_if_empty:
116 raise Exception("No pillar found or it is empty.")
117 else:
118 return False
119
120
121def init_salt_client():
122 local = salt_remote()
123 return local
124
125
126def list_to_target_string(node_list, separator, add_spaces=True):
127 if add_spaces:
128 separator = ' ' + separator.strip() + ' '
129 return separator.join(node_list)
130
131
132def calculate_groups():
133 config = get_configuration()
134 local_salt_client = init_salt_client()
135 node_groups = {}
136 nodes_names = set ()
137 expr_form = ''
Hanna Arhipova1eef8312019-05-06 20:14:18 +0300138 all_nodes = set(local_salt_client.test_ping(tgt='*', expr_form=None))
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300139 if 'groups' in config.keys() and 'PB_GROUPS' in os.environ.keys() and \
140 os.environ['PB_GROUPS'].lower() != 'false':
141 nodes_names.update(config['groups'].keys())
142 expr_form = 'compound'
143 else:
144 for node in all_nodes:
145 index = re.search('[0-9]{1,3}$', node.split('.')[0])
146 if index:
147 nodes_names.add(node.split('.')[0][:-len(index.group(0))])
148 else:
149 nodes_names.add(node)
150 expr_form = 'pcre'
151
152 gluster_nodes = local_salt_client.test_ping(tgt='I@salt:control and '
Hanna Arhipova1eef8312019-05-06 20:14:18 +0300153 'I@glusterfs:server',
154 expr_form='compound')
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300155 kvm_nodes = local_salt_client.test_ping(tgt='I@salt:control and not '
Hanna Arhipova1eef8312019-05-06 20:14:18 +0300156 'I@glusterfs:server',
157 expr_form='compound')
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300158
159 for node_name in nodes_names:
160 skipped_groups = config.get('skipped_groups') or []
161 if node_name in skipped_groups:
162 continue
163 if expr_form == 'pcre':
164 nodes = local_salt_client.test_ping(tgt='{}[0-9]{{1,3}}'.format(node_name),
Hanna Arhipova1eef8312019-05-06 20:14:18 +0300165 expr_form=expr_form)
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300166 else:
167 nodes = local_salt_client.test_ping(tgt=config['groups'][node_name],
Hanna Arhipova1eef8312019-05-06 20:14:18 +0300168 expr_form=expr_form)
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300169 if nodes == {}:
170 continue
171
Hanna Arhipova1eef8312019-05-06 20:14:18 +0300172 node_groups[node_name] = [x for x in nodes
173 if x not in config['skipped_nodes']
174 if x not in gluster_nodes.keys()
175 if x not in kvm_nodes.keys()]
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300176 all_nodes = set(all_nodes - set(node_groups[node_name]))
177 if node_groups[node_name] == []:
178 del node_groups[node_name]
179 if kvm_nodes:
180 node_groups['kvm'] = kvm_nodes.keys()
181 node_groups['kvm_gluster'] = gluster_nodes.keys()
182 all_nodes = set(all_nodes - set(kvm_nodes.keys()))
183 all_nodes = set(all_nodes - set(gluster_nodes.keys()))
184 if all_nodes:
Hanna Arhipova1eef8312019-05-06 20:14:18 +0300185 logging.info("These nodes were not collected {0}. Check config (groups section)".format(all_nodes))
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300186 return node_groups
Hanna Arhipova1eef8312019-05-06 20:14:18 +0300187
188
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300189def get_configuration():
190 """function returns configuration for environment
191 and for test if it's specified"""
Hanna Arhipova474ba922019-07-04 12:43:16 +0300192
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300193 global_config_file = os.path.join(
194 os.path.dirname(os.path.abspath(__file__)), "../global_config.yaml")
195 with open(global_config_file, 'r') as file:
Hanna Arhipovac64990a2019-07-16 16:07:11 +0300196 global_config = yaml.load(file, Loader=yaml.SafeLoader)
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300197 for param in global_config.keys():
198 if param in os.environ.keys():
199 if ',' in os.environ[param]:
200 global_config[param] = []
201 for item in os.environ[param].split(','):
202 global_config[param].append(item)
203 else:
204 global_config[param] = os.environ[param]
205
206 return global_config