savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 1 | import json |
| 2 | import os |
| 3 | import sys |
| 4 | |
| 5 | from copy import deepcopy |
| 6 | |
Alex Savatieiev | d48994d | 2018-12-13 12:13:00 +0100 | [diff] [blame] | 7 | import reporter |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 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 |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 12 | |
| 13 | node_tmpl = { |
| 14 | 'role': '', |
| 15 | 'node_group': '', |
| 16 | 'status': const.NODE_DOWN, |
| 17 | 'pillars': {}, |
| 18 | 'grains': {} |
| 19 | } |
| 20 | |
| 21 | |
| 22 | class CloudPackageChecker(object): |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 23 | def __init__(self): |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame^] | 24 | logger_cli.info("### Collecting nodes for package check") |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 25 | # simple salt rest client |
| 26 | self.salt = salt_utils.SaltRemote() |
| 27 | |
| 28 | # Keys for all nodes |
| 29 | # this is not working in scope of 2016.8.3, will overide with list |
| 30 | # cls.node_keys = cls.salt.list_keys() |
| 31 | |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame^] | 32 | logger_cli.info("### Collecting node names existing in the cloud") |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 33 | self.node_keys = { |
Alex Savatieiev | e47f7f4 | 2019-02-20 16:41:23 -0600 | [diff] [blame] | 34 | 'minions': config.all_nodes |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | # all that answer ping |
| 38 | _active = self.salt.get_active_nodes() |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame^] | 39 | logger_cli.debug("-> Nodes responded: {}".format(_active)) |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 40 | # just inventory for faster interaction |
| 41 | # iterate through all accepted nodes and create a dict for it |
| 42 | self.nodes = {} |
| 43 | for _name in self.node_keys['minions']: |
| 44 | _nc = utils.get_node_code(_name) |
| 45 | _rmap = const.all_roles_map |
| 46 | _role = _rmap[_nc] if _nc in _rmap else 'unknown' |
| 47 | _status = const.NODE_UP if _name in _active else const.NODE_DOWN |
| 48 | |
| 49 | self.nodes[_name] = deepcopy(node_tmpl) |
| 50 | self.nodes[_name]['node_group'] = _nc |
| 51 | self.nodes[_name]['role'] = _role |
| 52 | self.nodes[_name]['status'] = _status |
| 53 | |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame^] | 54 | logger_cli.info("-> {} nodes collected".format(len(self.nodes))) |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 55 | |
| 56 | def collect_installed_packages(self): |
| 57 | """ |
| 58 | Collect installed packages on each node |
| 59 | sets 'installed' dict property in the class |
| 60 | |
| 61 | :return: none |
| 62 | """ |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame^] | 63 | logger_cli.info("### Collecting installed packages") |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 64 | # form an all nodes compound string to use in salt |
| 65 | _active_nodes_string = self.salt.compound_string_from_list( |
| 66 | filter( |
| 67 | lambda nd: self.nodes[nd]['status'] == const.NODE_UP, |
| 68 | self.nodes |
| 69 | ) |
| 70 | ) |
| 71 | # Prepare script |
| 72 | _script_filename = "pkg_versions.py" |
Alex Savatieiev | e47f7f4 | 2019-02-20 16:41:23 -0600 | [diff] [blame] | 73 | _p = os.path.join(pkg_dir, 'scripts', _script_filename) |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 74 | with open(_p, 'rt') as fd: |
| 75 | _script = fd.read().splitlines() |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 76 | _storage_path = os.path.join( |
Alex Savatieiev | e47f7f4 | 2019-02-20 16:41:23 -0600 | [diff] [blame] | 77 | config.salt_file_root, config.salt_scripts_folder |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 78 | ) |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame^] | 79 | logger_cli.debug( |
| 80 | "# Uploading script {} to master's file cache folder: '{}'".format( |
savex | ce010ba | 2018-04-27 09:49:23 +0200 | [diff] [blame] | 81 | _script_filename, |
| 82 | _storage_path |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 83 | ) |
| 84 | ) |
savex | ce010ba | 2018-04-27 09:49:23 +0200 | [diff] [blame] | 85 | _result = self.salt.mkdir("cfg01*", _storage_path) |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 86 | # Form cache, source and target path |
| 87 | _cache_path = os.path.join(_storage_path, _script_filename) |
| 88 | _source_path = os.path.join( |
| 89 | 'salt://', |
Alex Savatieiev | e47f7f4 | 2019-02-20 16:41:23 -0600 | [diff] [blame] | 90 | config.salt_scripts_folder, |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 91 | _script_filename |
| 92 | ) |
| 93 | _target_path = os.path.join( |
| 94 | '/root', |
Alex Savatieiev | e47f7f4 | 2019-02-20 16:41:23 -0600 | [diff] [blame] | 95 | config.salt_scripts_folder, |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 96 | _script_filename |
| 97 | ) |
| 98 | |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame^] | 99 | logger_cli.debug("# Creating file in cache '{}'".format(_cache_path)) |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 100 | _result = self.salt.f_touch_master(_cache_path) |
| 101 | _result = self.salt.f_append_master(_cache_path, _script) |
| 102 | # command salt to copy file to minions |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame^] | 103 | logger_cli.debug("# Creating script target folder '{}'".format(_cache_path)) |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 104 | _result = self.salt.mkdir( |
| 105 | _active_nodes_string, |
| 106 | os.path.join( |
| 107 | '/root', |
Alex Savatieiev | e47f7f4 | 2019-02-20 16:41:23 -0600 | [diff] [blame] | 108 | config.salt_scripts_folder |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 109 | ), |
| 110 | tgt_type="compound" |
| 111 | ) |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame^] | 112 | logger_cli.info("-> Running script to all active nodes") |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 113 | _result = self.salt.get_file( |
| 114 | _active_nodes_string, |
| 115 | _source_path, |
| 116 | _target_path, |
| 117 | tgt_type="compound" |
| 118 | ) |
| 119 | # execute pkg collecting script |
| 120 | logger.debug("Running script to all nodes") |
| 121 | # handle results for each node |
| 122 | _result = self.salt.cmd( |
| 123 | _active_nodes_string, |
| 124 | 'cmd.run', |
| 125 | param='python {}'.format(_target_path), |
| 126 | expr_form="compound" |
| 127 | ) |
| 128 | for key in self.nodes.keys(): |
| 129 | # due to much data to be passed from salt, it is happening in order |
| 130 | if key in _result: |
| 131 | _text = _result[key] |
| 132 | _dict = json.loads(_text[_text.find('{'):]) |
| 133 | self.nodes[key]['packages'] = _dict |
| 134 | else: |
| 135 | self.nodes[key]['packages'] = {} |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame^] | 136 | logger_cli.debug("# {} has {} packages installed".format( |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 137 | key, |
| 138 | len(self.nodes[key]['packages'].keys()) |
| 139 | )) |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame^] | 140 | logger_cli.info("-> Done") |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 141 | |
| 142 | def collect_packages(self): |
| 143 | """ |
| 144 | Check package versions in repos vs installed |
| 145 | |
| 146 | :return: no return values, all date put to dict in place |
| 147 | """ |
| 148 | _all_packages = {} |
| 149 | for node_name, node_value in self.nodes.iteritems(): |
| 150 | for package_name in node_value['packages']: |
| 151 | if package_name not in _all_packages: |
| 152 | _all_packages[package_name] = {} |
| 153 | _all_packages[package_name][node_name] = node_value |
| 154 | |
| 155 | # TODO: process data for per-package basis |
| 156 | |
| 157 | self.all_packages = _all_packages |
| 158 | |
| 159 | def create_html_report(self, filename): |
| 160 | """ |
| 161 | Create static html showing packages diff per node |
| 162 | |
| 163 | :return: buff with html |
| 164 | """ |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame^] | 165 | logger_cli.info("### Generating report to '{}'".format(filename)) |
Alex Savatieiev | d48994d | 2018-12-13 12:13:00 +0100 | [diff] [blame] | 166 | _report = reporter.ReportToFile( |
| 167 | reporter.HTMLPackageCandidates(), |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 168 | filename |
| 169 | ) |
Alex Savatieiev | 36b938d | 2019-01-21 11:01:18 +0100 | [diff] [blame] | 170 | _report({ |
| 171 | "nodes": self.nodes, |
| 172 | "diffs": {} |
| 173 | }) |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame^] | 174 | logger_cli.info("-> Done") |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 175 | |
| 176 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 177 | if __name__ == '__main__': |
| 178 | # init connection to salt and collect minion data |
| 179 | cl = CloudPackageChecker() |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 180 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 181 | # collect data on installed packages |
| 182 | cl.collect_installed_packages() |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 183 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 184 | # diff installed and candidates |
| 185 | # cl.collect_packages() |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 186 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 187 | # report it |
| 188 | cl.create_html_report("./pkg_versions.html") |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 189 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 190 | sys.exit(0) |