Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame^] | 1 | import json |
| 2 | import os |
| 3 | import sys |
| 4 | |
| 5 | from copy import deepcopy |
| 6 | |
| 7 | import reporter |
| 8 | |
| 9 | from cfg_checker.common import utils, const |
| 10 | from cfg_checker.common import config, logger, logger_cli, pkg_dir |
| 11 | from cfg_checker.common import salt_utils |
| 12 | from cfg_checker.nodes import SaltNodes, node_tmpl |
| 13 | |
| 14 | |
| 15 | class NetworkChecker(SaltNodes): |
| 16 | def collect_network_info(self): |
| 17 | """ |
| 18 | Collects info on the network using ifs_data.py script |
| 19 | |
| 20 | :return: none |
| 21 | """ |
| 22 | logger_cli.info("### Collecting network data") |
| 23 | _result = self.execute_script("ifs_data.py", args=["json"]) |
| 24 | |
| 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]['networks'] = _dict |
| 31 | else: |
| 32 | self.nodes[key]['networks'] = {} |
| 33 | logger_cli.debug("# {} has {} networks".format( |
| 34 | key, |
| 35 | len(self.nodes[key]['networks'].keys()) |
| 36 | )) |
| 37 | logger_cli.info("-> Done collecting networks data") |
| 38 | |
| 39 | return |
| 40 | |
| 41 | def print_network_report(self): |
| 42 | """ |
| 43 | Create text report for CLI |
| 44 | |
| 45 | :return: none |
| 46 | """ |
| 47 | |
| 48 | return |
| 49 | |
| 50 | def create_html_report(self, filename): |
| 51 | """ |
| 52 | Create static html showing network schema-like report |
| 53 | |
| 54 | :return: none |
| 55 | """ |
| 56 | logger_cli.info("### Generating report to '{}'".format(filename)) |
| 57 | _report = reporter.ReportToFile( |
| 58 | reporter.HTMLNetworkReport(), |
| 59 | filename |
| 60 | ) |
| 61 | _report({ |
| 62 | "nodes": self.nodes, |
| 63 | "diffs": {} |
| 64 | }) |
| 65 | logger_cli.info("-> Done") |
| 66 | |
| 67 | |
| 68 | if __name__ == '__main__': |
| 69 | # init connection to salt and collect minion data |
| 70 | cl = NetworkChecker() |
| 71 | |
| 72 | # collect data on installed packages |
| 73 | cl.collect_network_info() |
| 74 | |
| 75 | # diff installed and candidates |
| 76 | # cl.collect_packages() |
| 77 | |
| 78 | # report it |
| 79 | cl.create_html_report("./pkg_versions.html") |
| 80 | |
| 81 | sys.exit(0) |