blob: 92d9e1c381e1abc35b0ffdea53deaf45ec1a9b64 [file] [log] [blame]
savex4448e132018-04-25 15:51:14 +02001import json
savex4448e132018-04-25 15:51:14 +02002
Alex3ebc5632019-04-18 16:47:18 -05003from cfg_checker.common import const, logger_cli
Alex41485522019-04-12 17:26:18 -05004from cfg_checker.common.exception import ConfigException
Alex26b8a8c2019-10-09 17:09:07 -05005from cfg_checker.common.other import merge_dict
Alex41485522019-04-12 17:26:18 -05006from cfg_checker.helpers.console_utils import Progress
Alexd0391d42019-05-21 18:48:55 -05007from cfg_checker.modules.packages.repos import RepoManager
Alexe0c5b9e2019-04-23 18:51:23 -05008from cfg_checker.nodes import salt_master
Alex41485522019-04-12 17:26:18 -05009from cfg_checker.reports import reporter
10
Alex3ebc5632019-04-18 16:47:18 -050011from versions import DebianVersion, PkgVersions, VersionCmpResult
savex4448e132018-04-25 15:51:14 +020012
13
Alexe0c5b9e2019-04-23 18:51:23 -050014class CloudPackageChecker(object):
Alexe9547d82019-06-03 15:22:50 -050015 def __init__(self, force_tag=None, exclude_keywords=[]):
Alexd0391d42019-05-21 18:48:55 -050016 # Init salt master info
17 if not salt_master.nodes:
18 salt_master.nodes = salt_master.get_nodes()
19
20 # check that this env tag is present in Manager
Alex571bf152019-05-31 16:28:30 -050021 self.rm = RepoManager()
Alexd0391d42019-05-21 18:48:55 -050022 _tags = self.rm.get_available_tags(tag=salt_master.mcp_release)
23 if not _tags:
24 logger_cli.warning(
25 "\n# hWARNING: '{0}' is not listed in repo index. "
26 "Consider running:\n\t{1}\nto add info on this tag's "
27 "release package versions".format(
28 salt_master.mcp_release,
29 "mcp-checker packages versions --tag {0}"
30 )
31 )
32
Alexe9547d82019-06-03 15:22:50 -050033 self.force_tag = force_tag
Alex9e4bfaf2019-06-11 15:21:59 -050034 self.exclude_keywords = exclude_keywords
Alexe9547d82019-06-03 15:22:50 -050035
Alex41485522019-04-12 17:26:18 -050036 @staticmethod
37 def presort_packages(all_packages, full=None):
38 logger_cli.info("-> Presorting packages")
39 # labels
40 _data = {}
41 _data = {
42 "cs": {
43 "ok": const.VERSION_OK,
44 "up": const.VERSION_UP,
45 "down": const.VERSION_DOWN,
Alex26b8a8c2019-10-09 17:09:07 -050046 "warn": const.VERSION_WARN,
Alex41485522019-04-12 17:26:18 -050047 "err": const.VERSION_ERR
48 },
49 "ca": {
50 "na": const.ACT_NA,
51 "up": const.ACT_UPGRADE,
52 "need_up": const.ACT_NEED_UP,
53 "need_down": const.ACT_NEED_DOWN,
54 "repo": const.ACT_REPO
55 }
56 }
57 _data['status_err'] = const.VERSION_ERR
Alex26b8a8c2019-10-09 17:09:07 -050058 _data['status_warn'] = const.VERSION_WARN
Alex41485522019-04-12 17:26:18 -050059 _data['status_down'] = const.VERSION_DOWN
60
61 # Presort packages
62 _data['critical'] = {}
63 _data['system'] = {}
64 _data['other'] = {}
65 _data['unlisted'] = {}
66
67 _l = len(all_packages)
68 _progress = Progress(_l)
69 _progress_index = 0
70 # counters
71 _ec = _es = _eo = _eu = 0
Alex26b8a8c2019-10-09 17:09:07 -050072 _wc = _ws = _wo = _wu = 0
Alex41485522019-04-12 17:26:18 -050073 _dc = _ds = _do = _du = 0
74 while _progress_index < _l:
75 # progress bar
76 _progress_index += 1
77 _progress.write_progress(_progress_index)
78 # sort packages
79 _pn, _val = all_packages.popitem()
Alexd0391d42019-05-21 18:48:55 -050080 _c = _val['desc']['section']
81
Alexe0c5b9e2019-04-23 18:51:23 -050082 if not full:
Alex41485522019-04-12 17:26:18 -050083 # Check if this packet has errors
84 # if all is ok -> just skip it
85 _max_status = max(_val['results'].keys())
86 if _max_status <= const.VERSION_OK:
87 _max_action = max(_val['results'][_max_status].keys())
88 if _max_action == const.ACT_NA:
Alexd0391d42019-05-21 18:48:55 -050089 # this package does not have any comments
Alex41485522019-04-12 17:26:18 -050090 # ...just skip it from report
91 continue
92
Alex26b8a8c2019-10-09 17:09:07 -050093 _differ = len(set(_val['results'].keys())) > 1
94 if _differ:
95 # in case package has different status across nodes
96 # Warning becomes Error.
97 if const.VERSION_WARN in _val['results']:
98 if const.VERSION_ERR in _val['results']:
99 # add warns to err
100 # should never happen, though
101 merge_dict(
102 _val['results'].pop(const.VERSION_WARN),
103 _val['results'][const.VERSION_ERR]
104 )
105 else:
106 _val['results'][const.VERSION_ERR] = \
107 _val['results'].pop(const.VERSION_WARN)
108 else:
109 # in case package has same status on all nodes
110 # Error becomes Warning
111 if const.VERSION_ERR in _val['results']:
112 if const.VERSION_WARN in _val['results']:
113 # add warns to err
114 # should never happen, though
115 merge_dict(
116 _val['results'].pop(const.VERSION_ERR),
117 _val['results'][const.VERSION_WARN]
118 )
119 else:
120 _val['results'][const.VERSION_WARN] = \
121 _val['results'].pop(const.VERSION_ERR)
122
Alexd0391d42019-05-21 18:48:55 -0500123 if len(_c) > 0 and _val['is_mirantis'] is None:
Alex41485522019-04-12 17:26:18 -0500124 # not listed package in version lib
125 _data['unlisted'].update({
126 _pn: _val
127 })
128 _eu += _val['results'].keys().count(const.VERSION_ERR)
Alex26b8a8c2019-10-09 17:09:07 -0500129 _wu += _val['results'].keys().count(const.VERSION_WARN)
Alex41485522019-04-12 17:26:18 -0500130 _du += _val['results'].keys().count(const.VERSION_DOWN)
131 # mirantis/critical
Alexd0391d42019-05-21 18:48:55 -0500132 # elif len(_c) > 0 and _c != 'System':
133 elif _val['is_mirantis']:
Alex41485522019-04-12 17:26:18 -0500134 # not blank and not system
135 _data['critical'].update({
136 _pn: _val
137 })
138 _ec += _val['results'].keys().count(const.VERSION_ERR)
Alex26b8a8c2019-10-09 17:09:07 -0500139 _wc += _val['results'].keys().count(const.VERSION_WARN)
Alex41485522019-04-12 17:26:18 -0500140 _dc += _val['results'].keys().count(const.VERSION_DOWN)
141 # system
142 elif _c == 'System':
143 _data['system'].update({
144 _pn: _val
145 })
146 _es += _val['results'].keys().count(const.VERSION_ERR)
Alex26b8a8c2019-10-09 17:09:07 -0500147 _ws += _val['results'].keys().count(const.VERSION_WARN)
Alex41485522019-04-12 17:26:18 -0500148 _ds += _val['results'].keys().count(const.VERSION_DOWN)
149 # rest
150 else:
151 _data['other'].update({
152 _pn: _val
153 })
154 _eo += _val['results'].keys().count(const.VERSION_ERR)
Alex26b8a8c2019-10-09 17:09:07 -0500155 _wo += _val['results'].keys().count(const.VERSION_WARN)
Alex41485522019-04-12 17:26:18 -0500156 _do += _val['results'].keys().count(const.VERSION_DOWN)
157
Alexd9fd85e2019-05-16 16:58:24 -0500158 _progress.end()
Alex41485522019-04-12 17:26:18 -0500159
160 _data['errors'] = {
161 'mirantis': _ec,
162 'system': _es,
163 'other': _eo,
164 'unlisted': _eu
165 }
Alex26b8a8c2019-10-09 17:09:07 -0500166 _data['warnings'] = {
167 'mirantis': _wc,
168 'system': _ws,
169 'other': _wo,
170 'unlisted': _wu
171 }
Alex41485522019-04-12 17:26:18 -0500172 _data['downgrades'] = {
173 'mirantis': _dc,
174 'system': _ds,
175 'other': _do,
176 'unlisted': _du
177 }
178
179 return _data
180
savex4448e132018-04-25 15:51:14 +0200181 def collect_installed_packages(self):
182 """
183 Collect installed packages on each node
184 sets 'installed' dict property in the class
185
186 :return: none
187 """
Alex Savatieiev42b89fa2019-03-07 18:45:26 -0600188 logger_cli.info("# Collecting installed packages")
Alexe0c5b9e2019-04-23 18:51:23 -0500189 salt_master.prepare_script_on_active_nodes("pkg_versions.py")
190 _result = salt_master.execute_script_on_active_nodes("pkg_versions.py")
savex4448e132018-04-25 15:51:14 +0200191
Alexe0c5b9e2019-04-23 18:51:23 -0500192 for key in salt_master.nodes.keys():
savex4448e132018-04-25 15:51:14 +0200193 # due to much data to be passed from salt, it is happening in order
Alexe9547d82019-06-03 15:22:50 -0500194 if key in _result and _result[key]:
savex4448e132018-04-25 15:51:14 +0200195 _text = _result[key]
Alex Savatieievfa5910a2019-03-18 18:12:24 -0500196 try:
197 _dict = json.loads(_text[_text.find('{'):])
Alex3ebc5632019-04-18 16:47:18 -0500198 except ValueError:
Alex Savatieievfa5910a2019-03-18 18:12:24 -0500199 logger_cli.info("... no JSON for '{}'".format(
200 key
201 ))
Alex3ebc5632019-04-18 16:47:18 -0500202 logger_cli.debug(
203 "ERROR:\n{}\n".format(_text[:_text.find('{')])
204 )
Alex Savatieievfa5910a2019-03-18 18:12:24 -0500205 _dict = {}
Alex3ebc5632019-04-18 16:47:18 -0500206
Alexe0c5b9e2019-04-23 18:51:23 -0500207 salt_master.nodes[key]['packages'] = _dict
savex4448e132018-04-25 15:51:14 +0200208 else:
Alexe0c5b9e2019-04-23 18:51:23 -0500209 salt_master.nodes[key]['packages'] = {}
Alex Savatieiev42b89fa2019-03-07 18:45:26 -0600210 logger_cli.debug("... {} has {} packages installed".format(
savex4448e132018-04-25 15:51:14 +0200211 key,
Alexe0c5b9e2019-04-23 18:51:23 -0500212 len(salt_master.nodes[key]['packages'].keys())
savex4448e132018-04-25 15:51:14 +0200213 ))
Alex Savatieiev799bee32019-02-20 17:19:26 -0600214 logger_cli.info("-> Done")
savex4448e132018-04-25 15:51:14 +0200215
216 def collect_packages(self):
217 """
218 Check package versions in repos vs installed
219
220 :return: no return values, all date put to dict in place
221 """
Alex41485522019-04-12 17:26:18 -0500222 # Preload OpenStack release versions
223 _desc = PkgVersions()
Alex3ebc5632019-04-18 16:47:18 -0500224 logger_cli.info(
225 "# Cross-comparing: Installed vs Candidates vs Release"
226 )
Alexd0391d42019-05-21 18:48:55 -0500227 # shortcuts for this cloud values
228 _os = salt_master.openstack_release
229 _mcp = salt_master.mcp_release
Alexe9547d82019-06-03 15:22:50 -0500230 _t = [self.force_tag] if self.force_tag else []
231 _t.append(_mcp)
232
233 logger_cli.info("# Tag search list: {}".format(", ".join(_t)))
234 logger_cli.info("# Openstack version: {}".format(_os))
235 logger_cli.info(
236 "# Release versions repos keyword exclude list: {}".format(
Alex9e4bfaf2019-06-11 15:21:59 -0500237 ", ".join(self.exclude_keywords)
Alexe9547d82019-06-03 15:22:50 -0500238 )
239 )
240
Alexd0391d42019-05-21 18:48:55 -0500241 # Progress class
Alexe0c5b9e2019-04-23 18:51:23 -0500242 _progress = Progress(len(salt_master.nodes.keys()))
Alex41485522019-04-12 17:26:18 -0500243 _progress_index = 0
244 _total_processed = 0
Alex Savatieiev3db12a72019-03-22 16:32:31 -0500245 # Collect packages from all of the nodes in flat dict
Alex41485522019-04-12 17:26:18 -0500246 _all_packages = {}
Alexe0c5b9e2019-04-23 18:51:23 -0500247 for node_name, node_value in salt_master.nodes.iteritems():
Alex41485522019-04-12 17:26:18 -0500248 _uniq_len = len(_all_packages.keys())
249 _progress_index += 1
Alexd0391d42019-05-21 18:48:55 -0500250 # progress updates shown before next node only
251 # it is costly operation to do it for each of the 150k packages
Alex41485522019-04-12 17:26:18 -0500252 _progress.write_progress(
253 _progress_index,
254 note="/ {} uniq out of {} packages found".format(
255 _uniq_len,
256 _total_processed
257 )
258 )
Alex Savatieiev3db12a72019-03-22 16:32:31 -0500259 for _name, _value in node_value['packages'].iteritems():
Alex41485522019-04-12 17:26:18 -0500260 _total_processed += 1
Alexcf91b182019-05-31 11:57:07 -0500261 # Parse versions from nodes
Alex41485522019-04-12 17:26:18 -0500262 _ver_ins = DebianVersion(_value['installed'])
263 _ver_can = DebianVersion(_value['candidate'])
264
Alexcf91b182019-05-31 11:57:07 -0500265 # Process package description and release version
266 # at a first sight
Alex41485522019-04-12 17:26:18 -0500267 if _name not in _all_packages:
Alexcf91b182019-05-31 11:57:07 -0500268 # get node attributes
Alexd0391d42019-05-21 18:48:55 -0500269 _linux = salt_master.nodes[node_name]['linux_codename']
270 _arch = salt_master.nodes[node_name]['linux_arch']
Alexe9547d82019-06-03 15:22:50 -0500271 # get versions for tag, Openstack release and repo headers
272 # excluding 'nightly' repos by default
273 _r = {}
274 # if there is a forced tag = use it
275 if self.force_tag:
276 _r = self.rm.get_filtered_versions(
277 _name,
278 tag=self.force_tag,
279 include=[_os, _linux, _arch],
Alex9e4bfaf2019-06-11 15:21:59 -0500280 exclude=self.exclude_keywords
Alexe9547d82019-06-03 15:22:50 -0500281 )
282 # if nothing found, look everywhere
Alex9e4bfaf2019-06-11 15:21:59 -0500283 # but with no word 'openstack'
Alexe9547d82019-06-03 15:22:50 -0500284 if not _r:
285 _r = self.rm.get_filtered_versions(
286 _name,
287 tag=self.force_tag,
288 include=[_linux, _arch],
Alex9e4bfaf2019-06-11 15:21:59 -0500289 exclude=self.exclude_keywords + ['openstack']
Alexe9547d82019-06-03 15:22:50 -0500290 )
291 # if nothing is found at this point,
292 # repeat search using normal tags
293 if not _r:
294 _r = self.rm.get_filtered_versions(
295 _name,
296 tag=_mcp,
297 include=[_os, _linux, _arch],
Alex9e4bfaf2019-06-11 15:21:59 -0500298 exclude=self.exclude_keywords
Alexe9547d82019-06-03 15:22:50 -0500299 )
300 # Once again, if nothing found, look everywhere
301 if not _r:
302 _r = self.rm.get_filtered_versions(
303 _name,
304 tag=_mcp,
305 include=[_linux, _arch],
Alex9e4bfaf2019-06-11 15:21:59 -0500306 exclude=self.exclude_keywords + ['openstack']
Alexe9547d82019-06-03 15:22:50 -0500307 )
Alexcf91b182019-05-31 11:57:07 -0500308 # repack versions in flat format
Alexd0391d42019-05-21 18:48:55 -0500309 _vs = {}
310 _sections = {}
311 _apps = {}
Alexd0391d42019-05-21 18:48:55 -0500312 for s, apps in _r.iteritems():
313 for a, versions in apps.iteritems():
314 for v, repos in versions.iteritems():
315 for repo in repos:
Alexcf91b182019-05-31 11:57:07 -0500316 if v not in _vs:
317 _vs[v] = []
318 _vs[v].append(repo)
319 if v not in _sections:
320 _sections[v] = []
321 _sections[v].append(s)
322 if v not in _apps:
323 _apps[v] = []
324 _apps[v].append(a)
325 # search for the newest version among filtered
Alexd0391d42019-05-21 18:48:55 -0500326 _r_desc = []
327 _vs_keys = _vs.keys()
328 if _vs_keys:
329 _newest = _newest = DebianVersion(_vs_keys.pop())
330 else:
331 _newest = DebianVersion('')
Alexd0391d42019-05-21 18:48:55 -0500332 for v in _vs_keys:
333 _this = DebianVersion(v)
334 if _this > _newest:
335 _newest = _this
Alexd0391d42019-05-21 18:48:55 -0500336 _release = _newest
Alexcf91b182019-05-31 11:57:07 -0500337 # Get best description for the package
Alexd0391d42019-05-21 18:48:55 -0500338 if _release.version != 'n/a':
339 _r_desc = _vs[_release.version]
340 # preload special description
Alex41485522019-04-12 17:26:18 -0500341 if _desc[_name]:
Alex41485522019-04-12 17:26:18 -0500342 _pkg_desc = _desc[_name]
343 else:
Alex41485522019-04-12 17:26:18 -0500344 _pkg_desc = _desc.dummy_desc
Alexcf91b182019-05-31 11:57:07 -0500345 # Save repos list and desc for this version
Alexd0391d42019-05-21 18:48:55 -0500346 # Check if we can provide better from the package
347 if _release.version != 'n/a':
348 if not _pkg_desc['section']:
349 _pkg_desc['section'] = \
350 "/".join(_sections[_release.version])
351 if not _pkg_desc['app']:
352 _pkg_desc['app'] = \
353 "/".join(_apps[_release.version])
Alex3ebc5632019-04-18 16:47:18 -0500354
Alexcf91b182019-05-31 11:57:07 -0500355 # Populate package info, once for package
Alexd0391d42019-05-21 18:48:55 -0500356 _m = _r_desc[0]["maintainer"] if _r_desc else 'n/a'
Alex41485522019-04-12 17:26:18 -0500357 _all_packages[_name] = {
358 "desc": _pkg_desc,
Alexd0391d42019-05-21 18:48:55 -0500359 "repos": _r_desc,
360 "maintainer": _m,
361 "is_mirantis": self.rm.is_mirantis(
362 _name,
Alexcf91b182019-05-31 11:57:07 -0500363 tag=_mcp
Alexd0391d42019-05-21 18:48:55 -0500364 ),
Alex41485522019-04-12 17:26:18 -0500365 "results": {},
366 "r": _release,
Alex Savatieiev3db12a72019-03-22 16:32:31 -0500367 }
Alexcf91b182019-05-31 11:57:07 -0500368 # Cross-compare versions
Alex41485522019-04-12 17:26:18 -0500369 _cmp = VersionCmpResult(
370 _ver_ins,
371 _ver_can,
372 _all_packages[_name]['r']
373 )
Alexcf91b182019-05-31 11:57:07 -0500374 # Update results structure
Alex41485522019-04-12 17:26:18 -0500375 # shortcut to results
376 _res = _all_packages[_name]['results']
377 # update status
378 if _cmp.status not in _res:
379 _res[_cmp.status] = {}
380 # update action
381 if _cmp.action not in _res[_cmp.status]:
382 _res[_cmp.status][_cmp.action] = {}
383 # update node
384 if node_name not in _res[_cmp.status][_cmp.action]:
385 _res[_cmp.status][_cmp.action][node_name] = {}
386 # put result
387 _res[_cmp.status][_cmp.action][node_name] = {
388 'i': _ver_ins,
389 'c': _ver_can,
390 'res': _cmp,
391 'raw': _value['raw']
392 }
savex4448e132018-04-25 15:51:14 +0200393
Alex41485522019-04-12 17:26:18 -0500394 self._packages = _all_packages
Alexd9fd85e2019-05-16 16:58:24 -0500395 _progress.end()
savex4448e132018-04-25 15:51:14 +0200396
Alex41485522019-04-12 17:26:18 -0500397 def create_report(self, filename, rtype, full=None):
savex4448e132018-04-25 15:51:14 +0200398 """
399 Create static html showing packages diff per node
400
401 :return: buff with html
402 """
Alex Savatieiev42b89fa2019-03-07 18:45:26 -0600403 logger_cli.info("# Generating report to '{}'".format(filename))
Alex41485522019-04-12 17:26:18 -0500404 if rtype == 'html':
405 _type = reporter.HTMLPackageCandidates()
406 elif rtype == 'csv':
407 _type = reporter.CSVAllPackages()
408 else:
409 raise ConfigException("Report type not set")
Alex Savatieievd48994d2018-12-13 12:13:00 +0100410 _report = reporter.ReportToFile(
Alex41485522019-04-12 17:26:18 -0500411 _type,
savex4448e132018-04-25 15:51:14 +0200412 filename
413 )
Alex41485522019-04-12 17:26:18 -0500414 payload = {
Alexe0c5b9e2019-04-23 18:51:23 -0500415 "nodes": salt_master.nodes,
416 "mcp_release": salt_master.mcp_release,
417 "openstack_release": salt_master.openstack_release
Alex41485522019-04-12 17:26:18 -0500418 }
419 payload.update(self.presort_packages(self._packages, full))
420 _report(payload)
Alex Savatieiev799bee32019-02-20 17:19:26 -0600421 logger_cli.info("-> Done")