savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 1 | import json |
| 2 | import os |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 3 | #import sys |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 4 | |
| 5 | from copy import deepcopy |
| 6 | |
Alex Savatieiev | f526dc0 | 2019-03-06 10:11:32 -0600 | [diff] [blame] | 7 | from cfg_checker.reports import reporter |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 8 | from cfg_checker.common import utils, const |
| 9 | from cfg_checker.common import config, logger, logger_cli, pkg_dir |
| 10 | from cfg_checker.common import salt_utils |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 11 | from cfg_checker.nodes import SaltNodes, node_tmpl |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 12 | |
| 13 | |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 14 | class CloudPackageChecker(SaltNodes): |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 15 | def collect_installed_packages(self): |
| 16 | """ |
| 17 | Collect installed packages on each node |
| 18 | sets 'installed' dict property in the class |
| 19 | |
| 20 | :return: none |
| 21 | """ |
Alex Savatieiev | 42b89fa | 2019-03-07 18:45:26 -0600 | [diff] [blame] | 22 | logger_cli.info("# Collecting installed packages") |
Alex Savatieiev | 01f0d7f | 2019-03-07 17:53:29 -0600 | [diff] [blame] | 23 | _result = self.execute_script_on_active_nodes("pkg_versions.py") |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 24 | |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 25 | for key in self.nodes.keys(): |
| 26 | # due to much data to be passed from salt, it is happening in order |
| 27 | if key in _result: |
| 28 | _text = _result[key] |
Alex Savatieiev | fa5910a | 2019-03-18 18:12:24 -0500 | [diff] [blame] | 29 | try: |
| 30 | _dict = json.loads(_text[_text.find('{'):]) |
| 31 | except ValueError as e: |
| 32 | logger_cli.info("... no JSON for '{}'".format( |
| 33 | key |
| 34 | )) |
Alex Savatieiev | 3db12a7 | 2019-03-22 16:32:31 -0500 | [diff] [blame^] | 35 | logger_cli.debug("ERROR:\n{}\n".format(_text[:_text.find('{')])) |
Alex Savatieiev | fa5910a | 2019-03-18 18:12:24 -0500 | [diff] [blame] | 36 | _dict = {} |
| 37 | |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 38 | self.nodes[key]['packages'] = _dict |
| 39 | else: |
| 40 | self.nodes[key]['packages'] = {} |
Alex Savatieiev | 42b89fa | 2019-03-07 18:45:26 -0600 | [diff] [blame] | 41 | logger_cli.debug("... {} has {} packages installed".format( |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 42 | key, |
| 43 | len(self.nodes[key]['packages'].keys()) |
| 44 | )) |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame] | 45 | logger_cli.info("-> Done") |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 46 | |
| 47 | def collect_packages(self): |
| 48 | """ |
| 49 | Check package versions in repos vs installed |
| 50 | |
| 51 | :return: no return values, all date put to dict in place |
| 52 | """ |
Alex Savatieiev | 3db12a7 | 2019-03-22 16:32:31 -0500 | [diff] [blame^] | 53 | # Collect packages from all of the nodes in flat dict |
| 54 | _diff_packages = {} |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 55 | for node_name, node_value in self.nodes.iteritems(): |
Alex Savatieiev | 3db12a7 | 2019-03-22 16:32:31 -0500 | [diff] [blame^] | 56 | for _name, _value in node_value['packages'].iteritems(): |
| 57 | if _name not in _diff_packages: |
| 58 | _diff_packages[_name] = {} |
| 59 | _diff_packages[_name]['df_nodes'] = {} |
| 60 | _diff_packages[_name]['eq_nodes'] = [] |
| 61 | |
| 62 | # compare packages, mark if equal |
| 63 | if _value['installed'] != _value['candidate']: |
| 64 | # Saving compare value so we not do str compare again |
| 65 | _value['is_equal'] = False |
| 66 | # add node name to list |
| 67 | _diff_packages[_name]['df_nodes'][node_name] = { |
| 68 | 'i': _value['installed'], |
| 69 | 'c': _value['candidate'], |
| 70 | 'raw': _value['raw'] |
| 71 | } |
| 72 | else: |
| 73 | # Saving compare value so we not do str compare again |
| 74 | _value['is_equal'] = True |
| 75 | _diff_packages[_name]['eq_nodes'].append(node_name) |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 76 | |
Alex Savatieiev | 3db12a7 | 2019-03-22 16:32:31 -0500 | [diff] [blame^] | 77 | self.diff_packages = _diff_packages |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 78 | |
| 79 | def create_html_report(self, filename): |
| 80 | """ |
| 81 | Create static html showing packages diff per node |
| 82 | |
| 83 | :return: buff with html |
| 84 | """ |
Alex Savatieiev | 42b89fa | 2019-03-07 18:45:26 -0600 | [diff] [blame] | 85 | logger_cli.info("# Generating report to '{}'".format(filename)) |
Alex Savatieiev | d48994d | 2018-12-13 12:13:00 +0100 | [diff] [blame] | 86 | _report = reporter.ReportToFile( |
| 87 | reporter.HTMLPackageCandidates(), |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 88 | filename |
| 89 | ) |
Alex Savatieiev | 36b938d | 2019-01-21 11:01:18 +0100 | [diff] [blame] | 90 | _report({ |
| 91 | "nodes": self.nodes, |
Alex Savatieiev | 3db12a7 | 2019-03-22 16:32:31 -0500 | [diff] [blame^] | 92 | "rc_diffs": {}, |
| 93 | "pkg_diffs": self.diff_packages |
Alex Savatieiev | 36b938d | 2019-01-21 11:01:18 +0100 | [diff] [blame] | 94 | }) |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame] | 95 | logger_cli.info("-> Done") |