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] |
| 29 | _dict = json.loads(_text[_text.find('{'):]) |
| 30 | self.nodes[key]['packages'] = _dict |
| 31 | else: |
| 32 | self.nodes[key]['packages'] = {} |
Alex Savatieiev | 42b89fa | 2019-03-07 18:45:26 -0600 | [diff] [blame] | 33 | logger_cli.debug("... {} has {} packages installed".format( |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 34 | key, |
| 35 | len(self.nodes[key]['packages'].keys()) |
| 36 | )) |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame] | 37 | logger_cli.info("-> Done") |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 38 | |
| 39 | def collect_packages(self): |
| 40 | """ |
| 41 | Check package versions in repos vs installed |
| 42 | |
| 43 | :return: no return values, all date put to dict in place |
| 44 | """ |
| 45 | _all_packages = {} |
| 46 | for node_name, node_value in self.nodes.iteritems(): |
| 47 | for package_name in node_value['packages']: |
| 48 | if package_name not in _all_packages: |
| 49 | _all_packages[package_name] = {} |
| 50 | _all_packages[package_name][node_name] = node_value |
| 51 | |
| 52 | # TODO: process data for per-package basis |
| 53 | |
| 54 | self.all_packages = _all_packages |
| 55 | |
| 56 | def create_html_report(self, filename): |
| 57 | """ |
| 58 | Create static html showing packages diff per node |
| 59 | |
| 60 | :return: buff with html |
| 61 | """ |
Alex Savatieiev | 42b89fa | 2019-03-07 18:45:26 -0600 | [diff] [blame] | 62 | logger_cli.info("# Generating report to '{}'".format(filename)) |
Alex Savatieiev | d48994d | 2018-12-13 12:13:00 +0100 | [diff] [blame] | 63 | _report = reporter.ReportToFile( |
| 64 | reporter.HTMLPackageCandidates(), |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 65 | filename |
| 66 | ) |
Alex Savatieiev | 36b938d | 2019-01-21 11:01:18 +0100 | [diff] [blame] | 67 | _report({ |
| 68 | "nodes": self.nodes, |
| 69 | "diffs": {} |
| 70 | }) |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame] | 71 | logger_cli.info("-> Done") |