blob: 4956a52ae2a1131f7c421c1d1540939026ba73a6 [file] [log] [blame]
savex4448e132018-04-25 15:51:14 +02001import json
2import os
Alex Savatieievc9055712019-03-01 14:43:56 -06003#import sys
savex4448e132018-04-25 15:51:14 +02004
5from copy import deepcopy
6
Alex Savatieievf526dc02019-03-06 10:11:32 -06007from cfg_checker.reports import reporter
Alex Savatieiev5118de02019-02-20 15:50:42 -06008from cfg_checker.common import utils, const
9from cfg_checker.common import config, logger, logger_cli, pkg_dir
10from cfg_checker.common import salt_utils
Alex Savatieiev9b2f6512019-02-20 18:05:00 -060011from cfg_checker.nodes import SaltNodes, node_tmpl
savex4448e132018-04-25 15:51:14 +020012
13
Alex Savatieiev9b2f6512019-02-20 18:05:00 -060014class CloudPackageChecker(SaltNodes):
savex4448e132018-04-25 15:51:14 +020015 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 Savatieiev42b89fa2019-03-07 18:45:26 -060022 logger_cli.info("# Collecting installed packages")
Alex Savatieiev01f0d7f2019-03-07 17:53:29 -060023 _result = self.execute_script_on_active_nodes("pkg_versions.py")
savex4448e132018-04-25 15:51:14 +020024
savex4448e132018-04-25 15:51:14 +020025 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 Savatieievfa5910a2019-03-18 18:12:24 -050029 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 Savatieiev3db12a72019-03-22 16:32:31 -050035 logger_cli.debug("ERROR:\n{}\n".format(_text[:_text.find('{')]))
Alex Savatieievfa5910a2019-03-18 18:12:24 -050036 _dict = {}
37
savex4448e132018-04-25 15:51:14 +020038 self.nodes[key]['packages'] = _dict
39 else:
40 self.nodes[key]['packages'] = {}
Alex Savatieiev42b89fa2019-03-07 18:45:26 -060041 logger_cli.debug("... {} has {} packages installed".format(
savex4448e132018-04-25 15:51:14 +020042 key,
43 len(self.nodes[key]['packages'].keys())
44 ))
Alex Savatieiev799bee32019-02-20 17:19:26 -060045 logger_cli.info("-> Done")
savex4448e132018-04-25 15:51:14 +020046
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 Savatieiev3db12a72019-03-22 16:32:31 -050053 # Collect packages from all of the nodes in flat dict
54 _diff_packages = {}
savex4448e132018-04-25 15:51:14 +020055 for node_name, node_value in self.nodes.iteritems():
Alex Savatieiev3db12a72019-03-22 16:32:31 -050056 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)
savex4448e132018-04-25 15:51:14 +020076
Alex Savatieiev3db12a72019-03-22 16:32:31 -050077 self.diff_packages = _diff_packages
savex4448e132018-04-25 15:51:14 +020078
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 Savatieiev42b89fa2019-03-07 18:45:26 -060085 logger_cli.info("# Generating report to '{}'".format(filename))
Alex Savatieievd48994d2018-12-13 12:13:00 +010086 _report = reporter.ReportToFile(
87 reporter.HTMLPackageCandidates(),
savex4448e132018-04-25 15:51:14 +020088 filename
89 )
Alex Savatieiev36b938d2019-01-21 11:01:18 +010090 _report({
91 "nodes": self.nodes,
Alex Savatieiev3db12a72019-03-22 16:32:31 -050092 "rc_diffs": {},
93 "pkg_diffs": self.diff_packages
Alex Savatieiev36b938d2019-01-21 11:01:18 +010094 })
Alex Savatieiev799bee32019-02-20 17:19:26 -060095 logger_cli.info("-> Done")