blob: 2ee67041658214dde631cebccde1c76d17fd0649 [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
Hanna Arhipovaacb94532019-07-18 19:00:35 +0300109 :param tgt, string, target when the salt command will be executed
110 :param param, additional parameter for salt command
111 :param expr_form
112 :param fail_if_empty
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300113 """
114 response = self.cmd(tgt=tgt, fun='pillar.get', param=param, expr_form=expr_form)
115 for node in response.keys():
116 if response[node] or response[node] != '':
117 return response[node]
118 else:
119 if fail_if_empty:
120 raise Exception("No pillar found or it is empty.")
121 else:
122 return False
123
124
125def init_salt_client():
126 local = salt_remote()
127 return local
128
129
130def list_to_target_string(node_list, separator, add_spaces=True):
131 if add_spaces:
132 separator = ' ' + separator.strip() + ' '
133 return separator.join(node_list)
134
135
136def calculate_groups():
137 config = get_configuration()
138 local_salt_client = init_salt_client()
139 node_groups = {}
140 nodes_names = set ()
141 expr_form = ''
Hanna Arhipova1eef8312019-05-06 20:14:18 +0300142 all_nodes = set(local_salt_client.test_ping(tgt='*', expr_form=None))
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300143 if 'groups' in config.keys() and 'PB_GROUPS' in os.environ.keys() and \
144 os.environ['PB_GROUPS'].lower() != 'false':
145 nodes_names.update(config['groups'].keys())
146 expr_form = 'compound'
147 else:
148 for node in all_nodes:
149 index = re.search('[0-9]{1,3}$', node.split('.')[0])
150 if index:
151 nodes_names.add(node.split('.')[0][:-len(index.group(0))])
152 else:
153 nodes_names.add(node)
154 expr_form = 'pcre'
155
156 gluster_nodes = local_salt_client.test_ping(tgt='I@salt:control and '
Hanna Arhipova1eef8312019-05-06 20:14:18 +0300157 'I@glusterfs:server',
158 expr_form='compound')
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300159 kvm_nodes = local_salt_client.test_ping(tgt='I@salt:control and not '
Hanna Arhipova1eef8312019-05-06 20:14:18 +0300160 'I@glusterfs:server',
161 expr_form='compound')
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300162
163 for node_name in nodes_names:
164 skipped_groups = config.get('skipped_groups') or []
165 if node_name in skipped_groups:
166 continue
167 if expr_form == 'pcre':
168 nodes = local_salt_client.test_ping(tgt='{}[0-9]{{1,3}}'.format(node_name),
Hanna Arhipova1eef8312019-05-06 20:14:18 +0300169 expr_form=expr_form)
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300170 else:
171 nodes = local_salt_client.test_ping(tgt=config['groups'][node_name],
Hanna Arhipova1eef8312019-05-06 20:14:18 +0300172 expr_form=expr_form)
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300173 if nodes == {}:
174 continue
175
Hanna Arhipova1eef8312019-05-06 20:14:18 +0300176 node_groups[node_name] = [x for x in nodes
177 if x not in config['skipped_nodes']
178 if x not in gluster_nodes.keys()
179 if x not in kvm_nodes.keys()]
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300180 all_nodes = set(all_nodes - set(node_groups[node_name]))
181 if node_groups[node_name] == []:
182 del node_groups[node_name]
183 if kvm_nodes:
184 node_groups['kvm'] = kvm_nodes.keys()
185 node_groups['kvm_gluster'] = gluster_nodes.keys()
186 all_nodes = set(all_nodes - set(kvm_nodes.keys()))
187 all_nodes = set(all_nodes - set(gluster_nodes.keys()))
188 if all_nodes:
Hanna Arhipova1eef8312019-05-06 20:14:18 +0300189 logging.info("These nodes were not collected {0}. Check config (groups section)".format(all_nodes))
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300190 return node_groups
Hanna Arhipova1eef8312019-05-06 20:14:18 +0300191
192
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300193def get_configuration():
194 """function returns configuration for environment
195 and for test if it's specified"""
Hanna Arhipova474ba922019-07-04 12:43:16 +0300196
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300197 global_config_file = os.path.join(
198 os.path.dirname(os.path.abspath(__file__)), "../global_config.yaml")
199 with open(global_config_file, 'r') as file:
Hanna Arhipovac64990a2019-07-16 16:07:11 +0300200 global_config = yaml.load(file, Loader=yaml.SafeLoader)
Hanna Arhipovae6ed8e42019-05-15 16:27:08 +0300201 for param in global_config.keys():
202 if param in os.environ.keys():
203 if ',' in os.environ[param]:
204 global_config[param] = []
205 for item in os.environ[param].split(','):
206 global_config[param].append(item)
207 else:
208 global_config[param] = os.environ[param]
209
210 return global_config