blob: 12b8e721a092beca5c4decc5def041213c731619 [file] [log] [blame]
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +03001from builtins import range
2from builtins import object
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +00003import os
4import yaml
5import requests
6import re
Oleksii Zhurbae592ed12018-06-21 18:01:09 -05007import sys, traceback
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -05008import time
Hanna Arhipova56eab942019-05-06 20:14:18 +03009import json
Hanna Arhipova56eab942019-05-06 20:14:18 +030010import logging
Oleksii Zhurba3dbed242017-10-31 19:58:53 +000011
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000012
Mikhail Chernikc492a682018-08-27 22:41:17 +020013class AuthenticationError(Exception):
14 pass
15
16
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +030017class salt_remote(object):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050018 def __init__(self):
19 self.config = get_configuration()
20 self.skipped_nodes = self.config.get('skipped_nodes') or []
21 self.url = self.config['SALT_URL'].strip()
22 if not re.match("^(http|https)://", self.url):
Valentyn Khalin81dd23d2018-09-20 15:39:07 +030023 raise AuthenticationError("Salt URL should start \
Hanna Arhipova56eab942019-05-06 20:14:18 +030024 with http or https, given - {}".format(self.url))
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050025 self.login_payload = {'username': self.config['SALT_USERNAME'],
26 'password': self.config['SALT_PASSWORD'], 'eauth': 'pam'}
27 # TODO: proxies
28 self.proxies = {"http": None, "https": None}
29 self.expires = ''
30 self.cookies = []
31 self.headers = {'Accept': 'application/json'}
32 self._login()
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +000033
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050034 def _login (self):
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050035 try:
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050036 login_request = requests.post(os.path.join(self.url, 'login'),
37 headers={'Accept': 'application/json'},
38 data=self.login_payload,
39 proxies=self.proxies)
Mikhail Chernikc492a682018-08-27 22:41:17 +020040 if not login_request.ok:
41 raise AuthenticationError("Authentication to SaltMaster failed")
Mikhail Chernikc492a682018-08-27 22:41:17 +020042 except Exception as e:
Hanna Arhipova56eab942019-05-06 20:14:18 +030043 logging.warning("\033[91m\nConnection to SaltMaster "
44 "was not established.\n"
45 "Please make sure that you "
46 "provided correct credentials.\n"
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +030047 "Error message: {}\033[0m\n".format(e))
Oleksii Zhurbae592ed12018-06-21 18:01:09 -050048 traceback.print_exc(file=sys.stdout)
49 sys.exit()
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050050 self.expire = login_request.json()['return'][0]['expire']
51 self.cookies = login_request.cookies
52 self.headers['X-Auth-Token'] = login_request.json()['return'][0]['token']
53
54 def cmd(self, tgt, fun='cmd.run', param=None, expr_form=None, tgt_type=None, check_status=False, retries=3):
55 if self.expire < time.time() + 300:
56 self.headers['X-Auth-Token'] = self._login()
57 accept_key_payload = {'fun': fun, 'tgt': tgt, 'client': 'local',
58 'expr_form': expr_form, 'tgt_type': tgt_type,
59 'timeout': self.config['salt_timeout']}
60 if param:
61 accept_key_payload['arg'] = param
62
63 for i in range(retries):
Hanna Arhipova56eab942019-05-06 20:14:18 +030064 logging.info("="*100)
65 logging.info("Send Request: {}".format(json.dumps(
66 accept_key_payload,
67 indent=4,
68 sort_keys=True)))
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050069 request = requests.post(self.url, headers=self.headers,
70 data=accept_key_payload,
71 cookies=self.cookies,
72 proxies=self.proxies)
Hanna Arhipova56eab942019-05-06 20:14:18 +030073 logging.info("-"*100)
74 logging.info("Response: {}".format(json.dumps(
75 request.json(),
76 indent=4
77 )))
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050078 if not request.ok or not isinstance(request.json()['return'][0], dict):
Hanna Arhipova56eab942019-05-06 20:14:18 +030079 logging.warning("Salt master is not responding or response is incorrect. Output: {}".format(request))
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050080 continue
81 response = request.json()['return'][0]
82 result = {key: response[key] for key in response if key not in self.skipped_nodes}
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +030083 if check_status and (False in list(result.values()) or not result):
Hanna Arhipova56eab942019-05-06 20:14:18 +030084 logging.warning("One or several nodes are not responding. Output {}".format(json.dumps(result, indent=4)))
85 continue
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -050086 break
87 else:
88 raise Exception("Error with Salt Master response")
89 return result
90
91 def test_ping(self, tgt, expr_form='pillar'):
92 return self.cmd(tgt=tgt, fun='test.ping', param=None, expr_form=expr_form)
93
94 def cmd_any(self, tgt, param=None, expr_form='pillar'):
95 """
96 This method returns first non-empty result on node or nodes.
97 If all nodes returns nothing, then exception is thrown.
98 """
99 response = self.cmd(tgt=tgt, param=param, expr_form=expr_form)
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300100 for node in list(response.keys()):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500101 if response[node] or response[node] == '':
102 return response[node]
103 else:
104 raise Exception("All minions are down")
105
106 def pillar_get(self, tgt='salt:master', param=None, expr_form='pillar', fail_if_empty=False):
107 """
108 This method is for fetching pillars only.
109 Returns value for pillar, False (if no such pillar) or if fail_if_empty=True - exception
Hanna Arhipovab9635f92019-07-18 19:00:35 +0300110 :param tgt, string, target when the salt command will be executed
111 :param param, additional parameter for salt command
112 :param expr_form
113 :param fail_if_empty
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500114 """
115 response = self.cmd(tgt=tgt, fun='pillar.get', param=param, expr_form=expr_form)
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300116 for node in list(response.keys()):
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500117 if response[node] or response[node] != '':
118 return response[node]
119 else:
120 if fail_if_empty:
121 raise Exception("No pillar found or it is empty.")
122 else:
Hanna Arhipovafae5b5b2019-12-09 15:57:55 +0200123 logging.error(
124 "suppressed incorrect response from pillar_get: {}".format(
125 response
126 ))
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500127 return False
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000128
129
130def init_salt_client():
131 local = salt_remote()
132 return local
133
134
Mikhail Chernik714596e2018-08-10 21:19:07 +0200135def list_to_target_string(node_list, separator, add_spaces=True):
136 if add_spaces:
137 separator = ' ' + separator.strip() + ' '
138 return separator.join(node_list)
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000139
140
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +0000141def calculate_groups():
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000142 config = get_configuration()
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +0000143 local_salt_client = init_salt_client()
Oleksii Zhurba30122e12018-03-29 14:01:50 -0500144 node_groups = {}
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +0000145 nodes_names = set ()
146 expr_form = ''
Hanna Arhipova56eab942019-05-06 20:14:18 +0300147 all_nodes = set(local_salt_client.test_ping(tgt='*', expr_form=None))
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300148 if 'groups' in list(config.keys()) and 'PB_GROUPS' in list(os.environ.keys()) and \
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500149 os.environ['PB_GROUPS'].lower() != 'false':
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300150 nodes_names.update(list(config['groups'].keys()))
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500151 expr_form = 'compound'
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +0000152 else:
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500153 for node in all_nodes:
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +0000154 index = re.search('[0-9]{1,3}$', node.split('.')[0])
155 if index:
156 nodes_names.add(node.split('.')[0][:-len(index.group(0))])
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000157 else:
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +0000158 nodes_names.add(node)
159 expr_form = 'pcre'
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000160
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500161 gluster_nodes = local_salt_client.test_ping(tgt='I@salt:control and '
Hanna Arhipova56eab942019-05-06 20:14:18 +0300162 'I@glusterfs:server',
163 expr_form='compound')
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500164 kvm_nodes = local_salt_client.test_ping(tgt='I@salt:control and not '
Hanna Arhipova56eab942019-05-06 20:14:18 +0300165 'I@glusterfs:server',
166 expr_form='compound')
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500167
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +0000168 for node_name in nodes_names:
169 skipped_groups = config.get('skipped_groups') or []
170 if node_name in skipped_groups:
171 continue
172 if expr_form == 'pcre':
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500173 nodes = local_salt_client.test_ping(tgt='{}[0-9]{{1,3}}'.format(node_name),
Hanna Arhipova56eab942019-05-06 20:14:18 +0300174 expr_form=expr_form)
Oleksii Zhurbae0dedb52018-01-16 00:55:25 +0000175 else:
Oleksii Zhurba4bfd2ee2019-04-10 21:56:58 -0500176 nodes = local_salt_client.test_ping(tgt=config['groups'][node_name],
Hanna Arhipova56eab942019-05-06 20:14:18 +0300177 expr_form=expr_form)
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500178 if nodes == {}:
179 continue
180
Hanna Arhipova56eab942019-05-06 20:14:18 +0300181 node_groups[node_name] = [x for x in nodes
182 if x not in config['skipped_nodes']
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300183 if x not in list(gluster_nodes.keys())
184 if x not in list(kvm_nodes.keys())]
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500185 all_nodes = set(all_nodes - set(node_groups[node_name]))
186 if node_groups[node_name] == []:
187 del node_groups[node_name]
188 if kvm_nodes:
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300189 node_groups['kvm'] = list(kvm_nodes.keys())
190 node_groups['kvm_gluster'] = list(gluster_nodes.keys())
Oleksii Zhurbae592ed12018-06-21 18:01:09 -0500191 all_nodes = set(all_nodes - set(kvm_nodes.keys()))
192 all_nodes = set(all_nodes - set(gluster_nodes.keys()))
193 if all_nodes:
Hanna Arhipova56eab942019-05-06 20:14:18 +0300194 logging.info("These nodes were not collected {0}. Check config (groups section)".format(all_nodes))
Oleksii Zhurbad0ae87f2018-03-26 13:36:25 -0500195 return node_groups
Hanna Arhipova8d4082d2019-07-16 16:07:11 +0300196
197
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000198def get_configuration():
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000199 """function returns configuration for environment
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000200 and for test if it's specified"""
Hanna Arhipova91dc8152019-07-04 12:43:16 +0300201
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000202 global_config_file = os.path.join(
203 os.path.dirname(os.path.abspath(__file__)), "../global_config.yaml")
204 with open(global_config_file, 'r') as file:
Hanna Arhipova8d4082d2019-07-16 16:07:11 +0300205 global_config = yaml.load(file, Loader=yaml.SafeLoader)
Ekaterina Chernovae32e3f92019-11-12 14:56:03 +0300206 for param in list(global_config.keys()):
207 if param in list(os.environ.keys()):
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000208 if ',' in os.environ[param]:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +0000209 global_config[param] = []
Oleksii Zhurbae0668ae2017-10-27 23:58:18 +0000210 for item in os.environ[param].split(','):
211 global_config[param].append(item)
212 else:
Oleksii Zhurba3dbed242017-10-31 19:58:53 +0000213 global_config[param] = os.environ[param]
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000214
Ievgeniia Zadorozhna0c306fd2019-11-12 16:03:51 +0300215 if 'OVERRIDE_CONFIG' in os.environ.keys():
216 try:
217 override_config = yaml.load(
218 os.environ['OVERRIDE_CONFIG'], Loader=yaml.SafeLoader)\
219 .get('override_config')
220 if override_config:
221 for key in override_config:
222 if isinstance(global_config[key], dict):
223 for k in override_config[key]:
224 global_config[key][k] = override_config[key][k]
225 else:
226 global_config[key] = override_config[key]
227 except Exception:
228 pass
229
Oleksii Zhurbaa10927b2017-09-27 22:09:23 +0000230 return global_config