blob: c2617528046201d5fc6a11000412ee5b33aa8e3e [file] [log] [blame]
Alexe0c5b9e2019-04-23 18:51:23 -05001import json
Alex Savatieiev9b2f6512019-02-20 18:05:00 -06002import os
Alex3ebc5632019-04-18 16:47:18 -05003from copy import deepcopy
Alex Savatieiev9b2f6512019-02-20 18:05:00 -06004
Alexe0c5b9e2019-04-23 18:51:23 -05005from cfg_checker.clients import get_salt_remote, salt
Alex3ebc5632019-04-18 16:47:18 -05006from cfg_checker.common import config, const
Alex7c9494e2019-04-22 10:40:59 -05007from cfg_checker.common import logger, logger_cli
Alexe0c5b9e2019-04-23 18:51:23 -05008from cfg_checker.common import utils
9from cfg_checker.common.exception import SaltException
Alex7c9494e2019-04-22 10:40:59 -050010from cfg_checker.common.settings import pkg_dir
Alex Savatieiev9b2f6512019-02-20 18:05:00 -060011
12node_tmpl = {
13 'role': '',
14 'node_group': '',
15 'status': const.NODE_DOWN,
16 'pillars': {},
17 'grains': {}
18}
19
20
21class SaltNodes(object):
22 def __init__(self):
Alexe0c5b9e2019-04-23 18:51:23 -050023 logger_cli.info("# Gathering environment information")
Alex Savatieiev9b2f6512019-02-20 18:05:00 -060024 # simple salt rest client
Alexe0c5b9e2019-04-23 18:51:23 -050025 self.salt = salt
26 self.nodes = None
Alex3ebc5632019-04-18 16:47:18 -050027
Alexe0c5b9e2019-04-23 18:51:23 -050028 def gather_node_info(self):
Alex Savatieiev9b2f6512019-02-20 18:05:00 -060029 # Keys for all nodes
30 # this is not working in scope of 2016.8.3, will overide with list
Alexb151fbe2019-04-22 16:53:30 -050031 logger_cli.debug("... collecting node names existing in the cloud")
Alexe0c5b9e2019-04-23 18:51:23 -050032 if not self.salt:
33 self.salt = get_salt_remote(config)
34
Alex Savatieiev9df93a92019-02-27 17:40:16 -060035 try:
36 _keys = self.salt.list_keys()
37 _str = []
Alex3bc95f62020-03-05 17:00:04 -060038 for _k, _v in _keys.items():
Alex Savatieiev9df93a92019-02-27 17:40:16 -060039 _str.append("{}: {}".format(_k, len(_v)))
40 logger_cli.info("-> keys collected: {}".format(", ".join(_str)))
Alex Savatieiev9b2f6512019-02-20 18:05:00 -060041
Alex Savatieiev9df93a92019-02-27 17:40:16 -060042 self.node_keys = {
43 'minions': _keys['minions']
44 }
Alex3ebc5632019-04-18 16:47:18 -050045 except Exception:
Alex Savatieiev9df93a92019-02-27 17:40:16 -060046 _keys = None
47 self.node_keys = None
Alex3ebc5632019-04-18 16:47:18 -050048
Alex Savatieiev9df93a92019-02-27 17:40:16 -060049 # List of minions with grains
50 _minions = self.salt.list_minions()
51 if _minions:
Alex3ebc5632019-04-18 16:47:18 -050052 logger_cli.info(
53 "-> api reported {} active minions".format(len(_minions))
54 )
Alex Savatieiev9df93a92019-02-27 17:40:16 -060055 elif not self.node_keys:
56 # this is the last resort
57 _minions = config.load_nodes_list()
Alex3ebc5632019-04-18 16:47:18 -050058 logger_cli.info(
59 "-> {} nodes loaded from list file".format(len(_minions))
60 )
Alex Savatieiev9df93a92019-02-27 17:40:16 -060061 else:
62 _minions = self.node_keys['minions']
Alex Savatieiev9b2f6512019-02-20 18:05:00 -060063
Alex Savatieiev9df93a92019-02-27 17:40:16 -060064 # in case API not listed minions, we need all that answer ping
Alex Savatieiev9b2f6512019-02-20 18:05:00 -060065 _active = self.salt.get_active_nodes()
Alex Savatieiev9df93a92019-02-27 17:40:16 -060066 logger_cli.info("-> nodes responded: {}".format(len(_active)))
Alex Savatieiev9b2f6512019-02-20 18:05:00 -060067 # iterate through all accepted nodes and create a dict for it
68 self.nodes = {}
Alex Savatieievefa79c42019-03-14 19:14:04 -050069 self.skip_list = []
Alex Savatieiev9df93a92019-02-27 17:40:16 -060070 for _name in _minions:
Alex Savatieiev9b2f6512019-02-20 18:05:00 -060071 _nc = utils.get_node_code(_name)
72 _rmap = const.all_roles_map
73 _role = _rmap[_nc] if _nc in _rmap else 'unknown'
74 _status = const.NODE_UP if _name in _active else const.NODE_DOWN
Alex Savatieievefa79c42019-03-14 19:14:04 -050075 if _status == const.NODE_DOWN:
76 self.skip_list.append(_name)
77 logger_cli.info("-> '{}' is down, marked to skip".format(
78 _name
79 ))
Alex Savatieiev9b2f6512019-02-20 18:05:00 -060080 self.nodes[_name] = deepcopy(node_tmpl)
81 self.nodes[_name]['node_group'] = _nc
82 self.nodes[_name]['role'] = _role
83 self.nodes[_name]['status'] = _status
Alex Savatieievefa79c42019-03-14 19:14:04 -050084 logger_cli.info("-> {} nodes inactive".format(len(self.skip_list)))
Alex Savatieiev9b2f6512019-02-20 18:05:00 -060085 logger_cli.info("-> {} nodes collected".format(len(self.nodes)))
86
Alex Savatieiev9b2f6512019-02-20 18:05:00 -060087 # form an all nodes compound string to use in salt
Alex Savatieiev01f0d7f2019-03-07 17:53:29 -060088 self.active_nodes_compound = self.salt.compound_string_from_list(
Alex Savatieiev9b2f6512019-02-20 18:05:00 -060089 filter(
90 lambda nd: self.nodes[nd]['status'] == const.NODE_UP,
91 self.nodes
92 )
93 )
Alex41485522019-04-12 17:26:18 -050094 # get master node fqdn
Alex3bc95f62020-03-05 17:00:04 -060095 # _filtered = filter(
96 # lambda nd: self.nodes[nd]['role'] == const.all_roles_map['cfg'],
97 # self.nodes
98 # )
99 _role = const.all_roles_map['cfg']
100 _filtered = [n for n, v in self.nodes.items() if v['role'] == _role]
Alexe0c5b9e2019-04-23 18:51:23 -0500101 if len(_filtered) < 1:
102 raise SaltException(
103 "No master node detected! Check/Update node role map."
104 )
105 else:
106 self.salt.master_node = _filtered[0]
Alex3ebc5632019-04-18 16:47:18 -0500107
Alex41485522019-04-12 17:26:18 -0500108 # OpenStack versions
109 self.mcp_release = self.salt.pillar_get(
Alexe0c5b9e2019-04-23 18:51:23 -0500110 self.salt.master_node,
Alex41485522019-04-12 17:26:18 -0500111 "_param:apt_mk_version"
Alexe0c5b9e2019-04-23 18:51:23 -0500112 )[self.salt.master_node]
Alex41485522019-04-12 17:26:18 -0500113 self.openstack_release = self.salt.pillar_get(
Alexe0c5b9e2019-04-23 18:51:23 -0500114 self.salt.master_node,
Alex41485522019-04-12 17:26:18 -0500115 "_param:openstack_version"
Alexe0c5b9e2019-04-23 18:51:23 -0500116 )[self.salt.master_node]
Alexd0391d42019-05-21 18:48:55 -0500117 # Preload codenames
118 # do additional queries to get linux codename and arch for each node
119 self.get_specific_pillar_for_nodes("_param:linux_system_codename")
120 self.get_specific_pillar_for_nodes("_param:linux_system_architecture")
121 for _name in self.nodes.keys():
Alexe9547d82019-06-03 15:22:50 -0500122 _n = self.nodes[_name]
123 if _name not in self.skip_list:
124 _p = _n['pillars']['_param']
125 _n['linux_codename'] = _p['linux_system_codename']
126 _n['linux_arch'] = _p['linux_system_architecture']
Alex41485522019-04-12 17:26:18 -0500127
Alex Savatieieva1f6f8c2019-03-18 17:13:55 -0500128 def skip_node(self, node):
129 # Add node to skip list
130 # Fro example if it is fails to comply with the rules
131
132 # check if we know such node
133 if node in self.nodes.keys() and node not in self.skip_list:
134 # yes, add it
135 self.skip_list.append(node)
136 return True
137 else:
138 return False
139
Alex Savatieiev01f0d7f2019-03-07 17:53:29 -0600140 def get_nodes(self):
Alexe0c5b9e2019-04-23 18:51:23 -0500141 if not self.nodes:
142 self.gather_node_info()
Alex Savatieiev01f0d7f2019-03-07 17:53:29 -0600143 return self.nodes
144
Alex836fac82019-08-22 13:36:16 -0500145 def get_info(self):
146 _info = {
147 'mcp_release': self.mcp_release,
148 'openstack_release': self.openstack_release
149 }
150 return _info
151
Alex1839bbf2019-08-22 17:17:21 -0500152 def get_cmd_for_nodes(self, cmd, target_key, target_dict=None, nodes=None):
Alex836fac82019-08-22 13:36:16 -0500153 """Function runs. cmd.run and parses result into place
154 or into dict structure provided
155
156 :return: no return value, data pulished internally
157 """
158 logger_cli.debug(
159 "... collecting results for '{}'".format(cmd)
160 )
161 if target_dict:
162 _nodes = target_dict
163 else:
164 _nodes = self.nodes
Alex1839bbf2019-08-22 17:17:21 -0500165 _result = self.execute_cmd_on_active_nodes(cmd, nodes=nodes)
Alex3bc95f62020-03-05 17:00:04 -0600166 for node, data in _nodes.items():
Alexf3dbe862019-10-07 15:17:04 -0500167
Alex836fac82019-08-22 13:36:16 -0500168 if node in self.skip_list:
169 logger_cli.debug(
170 "... '{}' skipped while collecting '{}'".format(
171 node,
172 cmd
173 )
174 )
175 continue
176 # Prepare target key
177 if target_key not in data:
178 data[target_key] = None
179 # Save data
180 if data['status'] == const.NODE_DOWN:
181 data[target_key] = None
Alex1839bbf2019-08-22 17:17:21 -0500182 elif node not in _result:
183 continue
Alex836fac82019-08-22 13:36:16 -0500184 elif not _result[node]:
185 logger_cli.debug(
186 "... '{}' not responded after '{}'".format(
187 node,
188 config.salt_timeout
189 )
190 )
191 data[target_key] = None
192 else:
193 data[target_key] = _result[node]
194
Alex Savatieiev01f0d7f2019-03-07 17:53:29 -0600195 def get_specific_pillar_for_nodes(self, pillar_path):
196 """Function gets pillars on given path for all nodes
197
198 :return: no return value, data pulished internally
199 """
Alex3ebc5632019-04-18 16:47:18 -0500200 logger_cli.debug(
Alexb151fbe2019-04-22 16:53:30 -0500201 "... collecting node pillars for '{}'".format(pillar_path)
Alex3ebc5632019-04-18 16:47:18 -0500202 )
Alex Savatieiev01f0d7f2019-03-07 17:53:29 -0600203 _result = self.salt.pillar_get(self.active_nodes_compound, pillar_path)
Alex Savatieievefa79c42019-03-14 19:14:04 -0500204 self.not_responded = []
Alex3bc95f62020-03-05 17:00:04 -0600205 for node, data in self.nodes.items():
Alex Savatieievefa79c42019-03-14 19:14:04 -0500206 if node in self.skip_list:
207 logger_cli.debug(
208 "... '{}' skipped while collecting '{}'".format(
209 node,
210 pillar_path
211 )
212 )
213 continue
Alex Savatieiev01f0d7f2019-03-07 17:53:29 -0600214 _pillar_keys = pillar_path.split(':')
215 _data = data['pillars']
216 # pre-create nested dict
217 for idx in range(0, len(_pillar_keys)-1):
218 _key = _pillar_keys[idx]
219 if _key not in _data:
220 _data[_key] = {}
221 _data = _data[_key]
Alex Savatieievefa79c42019-03-14 19:14:04 -0500222 if data['status'] == const.NODE_DOWN:
223 _data[_pillar_keys[-1]] = None
224 elif not _result[node]:
225 logger_cli.debug(
226 "... '{}' not responded after '{}'".format(
227 node,
228 config.salt_timeout
229 )
230 )
231 _data[_pillar_keys[-1]] = None
232 self.not_responded.append(node)
233 else:
234 _data[_pillar_keys[-1]] = _result[node]
Alex3ebc5632019-04-18 16:47:18 -0500235
Alexe0c5b9e2019-04-23 18:51:23 -0500236 def prepare_json_on_node(self, node, _dict, filename):
237 # this function assumes that all folders are created
238 _dumps = json.dumps(_dict, indent=2).splitlines()
239 _storage_path = os.path.join(
240 config.salt_file_root, config.salt_scripts_folder
241 )
242 logger_cli.debug(
243 "... uploading data as '{}' "
244 "to master's file cache folder: '{}'".format(
245 filename,
246 _storage_path
247 )
248 )
249 _cache_path = os.path.join(_storage_path, filename)
250 _source_path = os.path.join(
251 'salt://',
252 config.salt_scripts_folder,
253 filename
254 )
255 _target_path = os.path.join(
256 '/root',
257 config.salt_scripts_folder,
258 filename
259 )
260
261 logger_cli.debug("... creating file in cache '{}'".format(_cache_path))
262 self.salt.f_touch_master(_cache_path)
263 self.salt.f_append_master(_cache_path, _dumps)
264 logger.debug("... syncing file to '{}'".format(node))
265 self.salt.get_file(
266 node,
267 _source_path,
268 _target_path,
269 tgt_type="compound"
270 )
271 return _target_path
272
273 def prepare_script_on_active_nodes(self, script_filename):
Alex Savatieiev9b2f6512019-02-20 18:05:00 -0600274 # Prepare script
275 _p = os.path.join(pkg_dir, 'scripts', script_filename)
276 with open(_p, 'rt') as fd:
277 _script = fd.read().splitlines()
278 _storage_path = os.path.join(
279 config.salt_file_root, config.salt_scripts_folder
280 )
281 logger_cli.debug(
Alexb151fbe2019-04-22 16:53:30 -0500282 "... uploading script {} "
Alex3ebc5632019-04-18 16:47:18 -0500283 "to master's file cache folder: '{}'".format(
Alex Savatieiev9b2f6512019-02-20 18:05:00 -0600284 script_filename,
285 _storage_path
286 )
287 )
Alexe0c5b9e2019-04-23 18:51:23 -0500288 self.salt.mkdir(self.salt.master_node, _storage_path)
Alex Savatieiev9b2f6512019-02-20 18:05:00 -0600289 # Form cache, source and target path
290 _cache_path = os.path.join(_storage_path, script_filename)
291 _source_path = os.path.join(
292 'salt://',
293 config.salt_scripts_folder,
294 script_filename
295 )
296 _target_path = os.path.join(
297 '/root',
298 config.salt_scripts_folder,
299 script_filename
300 )
301
Alexb151fbe2019-04-22 16:53:30 -0500302 logger_cli.debug("... creating file in cache '{}'".format(_cache_path))
Alex3ebc5632019-04-18 16:47:18 -0500303 self.salt.f_touch_master(_cache_path)
304 self.salt.f_append_master(_cache_path, _script)
Alex Savatieiev9b2f6512019-02-20 18:05:00 -0600305 # command salt to copy file to minions
Alex3ebc5632019-04-18 16:47:18 -0500306 logger_cli.debug(
Alexb151fbe2019-04-22 16:53:30 -0500307 "... creating script target folder '{}'".format(
Alex3ebc5632019-04-18 16:47:18 -0500308 _cache_path
309 )
310 )
311 self.salt.mkdir(
Alex Savatieiev01f0d7f2019-03-07 17:53:29 -0600312 self.active_nodes_compound,
Alex Savatieiev9b2f6512019-02-20 18:05:00 -0600313 os.path.join(
314 '/root',
315 config.salt_scripts_folder
316 ),
317 tgt_type="compound"
318 )
Alex3ebc5632019-04-18 16:47:18 -0500319 logger.debug("... syncing file to nodes")
320 self.salt.get_file(
Alex Savatieiev01f0d7f2019-03-07 17:53:29 -0600321 self.active_nodes_compound,
Alex Savatieiev9b2f6512019-02-20 18:05:00 -0600322 _source_path,
323 _target_path,
324 tgt_type="compound"
325 )
Alexe0c5b9e2019-04-23 18:51:23 -0500326 # return path on nodes, just in case
327 return _target_path
328
329 def execute_script_on_node(self, node, script_filename, args=[]):
330 # Prepare path
331 _target_path = os.path.join(
332 '/root',
333 config.salt_scripts_folder,
334 script_filename
335 )
336
337 # execute script
338 logger.debug("... running script on '{}'".format(node))
339 # handle results for each node
340 _script_arguments = " ".join(args) if args else ""
341 self.not_responded = []
342 _r = self.salt.cmd(
343 node,
344 'cmd.run',
345 param='python {} {}'.format(_target_path, _script_arguments),
346 expr_form="compound"
347 )
348
349 # all false returns means that there is no response
350 self.not_responded = [_n for _n in _r.keys() if not _r[_n]]
351 return _r
352
353 def execute_script_on_active_nodes(self, script_filename, args=[]):
354 # Prepare path
355 _target_path = os.path.join(
356 '/root',
357 config.salt_scripts_folder,
358 script_filename
359 )
360
361 # execute script
Alexd0391d42019-05-21 18:48:55 -0500362 logger_cli.debug("... running script")
Alex Savatieiev9b2f6512019-02-20 18:05:00 -0600363 # handle results for each node
364 _script_arguments = " ".join(args) if args else ""
Alex Savatieievefa79c42019-03-14 19:14:04 -0500365 self.not_responded = []
366 _r = self.salt.cmd(
Alex Savatieiev01f0d7f2019-03-07 17:53:29 -0600367 self.active_nodes_compound,
Alex Savatieiev9b2f6512019-02-20 18:05:00 -0600368 'cmd.run',
369 param='python {} {}'.format(_target_path, _script_arguments),
370 expr_form="compound"
371 )
372
Alex Savatieievefa79c42019-03-14 19:14:04 -0500373 # all false returns means that there is no response
Alex3ebc5632019-04-18 16:47:18 -0500374 self.not_responded = [_n for _n in _r.keys() if not _r[_n]]
Alex Savatieievefa79c42019-03-14 19:14:04 -0500375 return _r
Alex Savatieiev9b2f6512019-02-20 18:05:00 -0600376
Alex1839bbf2019-08-22 17:17:21 -0500377 def execute_cmd_on_active_nodes(self, cmd, nodes=None):
Alex836fac82019-08-22 13:36:16 -0500378 # execute cmd
379 self.not_responded = []
380 _r = self.salt.cmd(
Alex1839bbf2019-08-22 17:17:21 -0500381 nodes if nodes else self.active_nodes_compound,
Alex836fac82019-08-22 13:36:16 -0500382 'cmd.run',
383 param=cmd,
384 expr_form="compound"
385 )
386
387 # all false returns means that there is no response
388 self.not_responded = [_n for _n in _r.keys() if not _r[_n]]
389 return _r
390
Alex Savatieievefa79c42019-03-14 19:14:04 -0500391 def is_node_available(self, node, log=True):
392 if node in self.skip_list:
393 if log:
394 logger_cli.info("-> node '{}' not active".format(node))
395 return False
396 elif node in self.not_responded:
397 if log:
398 logger_cli.info("-> node '{}' not responded".format(node))
399 return False
400 else:
401 return True
Alexe0c5b9e2019-04-23 18:51:23 -0500402
403
404salt_master = SaltNodes()