savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 1 | import json |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 2 | |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 3 | from cfg_checker.common import const, logger_cli |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 4 | from cfg_checker.common.exception import ConfigException |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 5 | from cfg_checker.helpers.console_utils import Progress |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 6 | from cfg_checker.modules.packages.repos import RepoManager |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 7 | from cfg_checker.nodes import salt_master |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 8 | from cfg_checker.reports import reporter |
| 9 | |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 10 | from versions import DebianVersion, PkgVersions, VersionCmpResult |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 11 | |
| 12 | |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 13 | class CloudPackageChecker(object): |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 14 | def __init__(self): |
| 15 | # Init salt master info |
| 16 | if not salt_master.nodes: |
| 17 | salt_master.nodes = salt_master.get_nodes() |
| 18 | |
| 19 | # check that this env tag is present in Manager |
Alex | 571bf15 | 2019-05-31 16:28:30 -0500 | [diff] [blame] | 20 | self.rm = RepoManager() |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 21 | _tags = self.rm.get_available_tags(tag=salt_master.mcp_release) |
| 22 | if not _tags: |
| 23 | logger_cli.warning( |
| 24 | "\n# hWARNING: '{0}' is not listed in repo index. " |
| 25 | "Consider running:\n\t{1}\nto add info on this tag's " |
| 26 | "release package versions".format( |
| 27 | salt_master.mcp_release, |
| 28 | "mcp-checker packages versions --tag {0}" |
| 29 | ) |
| 30 | ) |
| 31 | |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 32 | @staticmethod |
| 33 | def presort_packages(all_packages, full=None): |
| 34 | logger_cli.info("-> Presorting packages") |
| 35 | # labels |
| 36 | _data = {} |
| 37 | _data = { |
| 38 | "cs": { |
| 39 | "ok": const.VERSION_OK, |
| 40 | "up": const.VERSION_UP, |
| 41 | "down": const.VERSION_DOWN, |
| 42 | "err": const.VERSION_ERR |
| 43 | }, |
| 44 | "ca": { |
| 45 | "na": const.ACT_NA, |
| 46 | "up": const.ACT_UPGRADE, |
| 47 | "need_up": const.ACT_NEED_UP, |
| 48 | "need_down": const.ACT_NEED_DOWN, |
| 49 | "repo": const.ACT_REPO |
| 50 | } |
| 51 | } |
| 52 | _data['status_err'] = const.VERSION_ERR |
| 53 | _data['status_down'] = const.VERSION_DOWN |
| 54 | |
| 55 | # Presort packages |
| 56 | _data['critical'] = {} |
| 57 | _data['system'] = {} |
| 58 | _data['other'] = {} |
| 59 | _data['unlisted'] = {} |
| 60 | |
| 61 | _l = len(all_packages) |
| 62 | _progress = Progress(_l) |
| 63 | _progress_index = 0 |
| 64 | # counters |
| 65 | _ec = _es = _eo = _eu = 0 |
| 66 | _dc = _ds = _do = _du = 0 |
| 67 | while _progress_index < _l: |
| 68 | # progress bar |
| 69 | _progress_index += 1 |
| 70 | _progress.write_progress(_progress_index) |
| 71 | # sort packages |
| 72 | _pn, _val = all_packages.popitem() |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 73 | _c = _val['desc']['section'] |
| 74 | |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 75 | if not full: |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 76 | # Check if this packet has errors |
| 77 | # if all is ok -> just skip it |
| 78 | _max_status = max(_val['results'].keys()) |
| 79 | if _max_status <= const.VERSION_OK: |
| 80 | _max_action = max(_val['results'][_max_status].keys()) |
| 81 | if _max_action == const.ACT_NA: |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 82 | # this package does not have any comments |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 83 | # ...just skip it from report |
| 84 | continue |
| 85 | |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 86 | if len(_c) > 0 and _val['is_mirantis'] is None: |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 87 | # not listed package in version lib |
| 88 | _data['unlisted'].update({ |
| 89 | _pn: _val |
| 90 | }) |
| 91 | _eu += _val['results'].keys().count(const.VERSION_ERR) |
| 92 | _du += _val['results'].keys().count(const.VERSION_DOWN) |
| 93 | # mirantis/critical |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 94 | # elif len(_c) > 0 and _c != 'System': |
| 95 | elif _val['is_mirantis']: |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 96 | # not blank and not system |
| 97 | _data['critical'].update({ |
| 98 | _pn: _val |
| 99 | }) |
| 100 | _ec += _val['results'].keys().count(const.VERSION_ERR) |
| 101 | _dc += _val['results'].keys().count(const.VERSION_DOWN) |
| 102 | # system |
| 103 | elif _c == 'System': |
| 104 | _data['system'].update({ |
| 105 | _pn: _val |
| 106 | }) |
| 107 | _es += _val['results'].keys().count(const.VERSION_ERR) |
| 108 | _ds += _val['results'].keys().count(const.VERSION_DOWN) |
| 109 | # rest |
| 110 | else: |
| 111 | _data['other'].update({ |
| 112 | _pn: _val |
| 113 | }) |
| 114 | _eo += _val['results'].keys().count(const.VERSION_ERR) |
| 115 | _do += _val['results'].keys().count(const.VERSION_DOWN) |
| 116 | |
Alex | d9fd85e | 2019-05-16 16:58:24 -0500 | [diff] [blame] | 117 | _progress.end() |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 118 | |
| 119 | _data['errors'] = { |
| 120 | 'mirantis': _ec, |
| 121 | 'system': _es, |
| 122 | 'other': _eo, |
| 123 | 'unlisted': _eu |
| 124 | } |
| 125 | _data['downgrades'] = { |
| 126 | 'mirantis': _dc, |
| 127 | 'system': _ds, |
| 128 | 'other': _do, |
| 129 | 'unlisted': _du |
| 130 | } |
| 131 | |
| 132 | return _data |
| 133 | |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 134 | def collect_installed_packages(self): |
| 135 | """ |
| 136 | Collect installed packages on each node |
| 137 | sets 'installed' dict property in the class |
| 138 | |
| 139 | :return: none |
| 140 | """ |
Alex Savatieiev | 42b89fa | 2019-03-07 18:45:26 -0600 | [diff] [blame] | 141 | logger_cli.info("# Collecting installed packages") |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 142 | salt_master.prepare_script_on_active_nodes("pkg_versions.py") |
| 143 | _result = salt_master.execute_script_on_active_nodes("pkg_versions.py") |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 144 | |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 145 | for key in salt_master.nodes.keys(): |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 146 | # due to much data to be passed from salt, it is happening in order |
| 147 | if key in _result: |
| 148 | _text = _result[key] |
Alex Savatieiev | fa5910a | 2019-03-18 18:12:24 -0500 | [diff] [blame] | 149 | try: |
| 150 | _dict = json.loads(_text[_text.find('{'):]) |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 151 | except ValueError: |
Alex Savatieiev | fa5910a | 2019-03-18 18:12:24 -0500 | [diff] [blame] | 152 | logger_cli.info("... no JSON for '{}'".format( |
| 153 | key |
| 154 | )) |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 155 | logger_cli.debug( |
| 156 | "ERROR:\n{}\n".format(_text[:_text.find('{')]) |
| 157 | ) |
Alex Savatieiev | fa5910a | 2019-03-18 18:12:24 -0500 | [diff] [blame] | 158 | _dict = {} |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 159 | |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 160 | salt_master.nodes[key]['packages'] = _dict |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 161 | else: |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 162 | salt_master.nodes[key]['packages'] = {} |
Alex Savatieiev | 42b89fa | 2019-03-07 18:45:26 -0600 | [diff] [blame] | 163 | logger_cli.debug("... {} has {} packages installed".format( |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 164 | key, |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 165 | len(salt_master.nodes[key]['packages'].keys()) |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 166 | )) |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame] | 167 | logger_cli.info("-> Done") |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 168 | |
| 169 | def collect_packages(self): |
| 170 | """ |
| 171 | Check package versions in repos vs installed |
| 172 | |
| 173 | :return: no return values, all date put to dict in place |
| 174 | """ |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 175 | # Preload OpenStack release versions |
| 176 | _desc = PkgVersions() |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 177 | |
| 178 | logger_cli.info( |
| 179 | "# Cross-comparing: Installed vs Candidates vs Release" |
| 180 | ) |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 181 | # shortcuts for this cloud values |
| 182 | _os = salt_master.openstack_release |
| 183 | _mcp = salt_master.mcp_release |
| 184 | # Progress class |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 185 | _progress = Progress(len(salt_master.nodes.keys())) |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 186 | _progress_index = 0 |
| 187 | _total_processed = 0 |
Alex Savatieiev | 3db12a7 | 2019-03-22 16:32:31 -0500 | [diff] [blame] | 188 | # Collect packages from all of the nodes in flat dict |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 189 | _all_packages = {} |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 190 | for node_name, node_value in salt_master.nodes.iteritems(): |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 191 | _uniq_len = len(_all_packages.keys()) |
| 192 | _progress_index += 1 |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 193 | # progress updates shown before next node only |
| 194 | # it is costly operation to do it for each of the 150k packages |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 195 | _progress.write_progress( |
| 196 | _progress_index, |
| 197 | note="/ {} uniq out of {} packages found".format( |
| 198 | _uniq_len, |
| 199 | _total_processed |
| 200 | ) |
| 201 | ) |
Alex Savatieiev | 3db12a7 | 2019-03-22 16:32:31 -0500 | [diff] [blame] | 202 | for _name, _value in node_value['packages'].iteritems(): |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 203 | _total_processed += 1 |
Alex | cf91b18 | 2019-05-31 11:57:07 -0500 | [diff] [blame] | 204 | # Parse versions from nodes |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 205 | _ver_ins = DebianVersion(_value['installed']) |
| 206 | _ver_can = DebianVersion(_value['candidate']) |
| 207 | |
Alex | cf91b18 | 2019-05-31 11:57:07 -0500 | [diff] [blame] | 208 | # Process package description and release version |
| 209 | # at a first sight |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 210 | if _name not in _all_packages: |
Alex | cf91b18 | 2019-05-31 11:57:07 -0500 | [diff] [blame] | 211 | # get node attributes |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 212 | _linux = salt_master.nodes[node_name]['linux_codename'] |
| 213 | _arch = salt_master.nodes[node_name]['linux_arch'] |
Alex | cf91b18 | 2019-05-31 11:57:07 -0500 | [diff] [blame] | 214 | # get versions for this tag and repo headers |
| 215 | # excluding 'nightly' repos |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 216 | _r = self.rm.get_filtered_versions( |
| 217 | _name, |
| 218 | tag=_mcp, |
| 219 | include=[_os, _linux, _arch], |
| 220 | exclude=["nightly"] |
| 221 | ) |
Alex | cf91b18 | 2019-05-31 11:57:07 -0500 | [diff] [blame] | 222 | # repack versions in flat format |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 223 | _vs = {} |
| 224 | _sections = {} |
| 225 | _apps = {} |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 226 | for s, apps in _r.iteritems(): |
| 227 | for a, versions in apps.iteritems(): |
| 228 | for v, repos in versions.iteritems(): |
| 229 | for repo in repos: |
Alex | cf91b18 | 2019-05-31 11:57:07 -0500 | [diff] [blame] | 230 | if v not in _vs: |
| 231 | _vs[v] = [] |
| 232 | _vs[v].append(repo) |
| 233 | if v not in _sections: |
| 234 | _sections[v] = [] |
| 235 | _sections[v].append(s) |
| 236 | if v not in _apps: |
| 237 | _apps[v] = [] |
| 238 | _apps[v].append(a) |
| 239 | # search for the newest version among filtered |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 240 | _r_desc = [] |
| 241 | _vs_keys = _vs.keys() |
| 242 | if _vs_keys: |
| 243 | _newest = _newest = DebianVersion(_vs_keys.pop()) |
| 244 | else: |
| 245 | _newest = DebianVersion('') |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 246 | for v in _vs_keys: |
| 247 | _this = DebianVersion(v) |
| 248 | if _this > _newest: |
| 249 | _newest = _this |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 250 | _release = _newest |
Alex | cf91b18 | 2019-05-31 11:57:07 -0500 | [diff] [blame] | 251 | # Get best description for the package |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 252 | if _release.version != 'n/a': |
| 253 | _r_desc = _vs[_release.version] |
| 254 | # preload special description |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 255 | if _desc[_name]: |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 256 | _pkg_desc = _desc[_name] |
| 257 | else: |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 258 | _pkg_desc = _desc.dummy_desc |
Alex | cf91b18 | 2019-05-31 11:57:07 -0500 | [diff] [blame] | 259 | # Save repos list and desc for this version |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 260 | # Check if we can provide better from the package |
| 261 | if _release.version != 'n/a': |
| 262 | if not _pkg_desc['section']: |
| 263 | _pkg_desc['section'] = \ |
| 264 | "/".join(_sections[_release.version]) |
| 265 | if not _pkg_desc['app']: |
| 266 | _pkg_desc['app'] = \ |
| 267 | "/".join(_apps[_release.version]) |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 268 | |
Alex | cf91b18 | 2019-05-31 11:57:07 -0500 | [diff] [blame] | 269 | # Populate package info, once for package |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 270 | _m = _r_desc[0]["maintainer"] if _r_desc else 'n/a' |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 271 | _all_packages[_name] = { |
| 272 | "desc": _pkg_desc, |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 273 | "repos": _r_desc, |
| 274 | "maintainer": _m, |
| 275 | "is_mirantis": self.rm.is_mirantis( |
| 276 | _name, |
Alex | cf91b18 | 2019-05-31 11:57:07 -0500 | [diff] [blame] | 277 | tag=_mcp |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 278 | ), |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 279 | "results": {}, |
| 280 | "r": _release, |
Alex Savatieiev | 3db12a7 | 2019-03-22 16:32:31 -0500 | [diff] [blame] | 281 | } |
Alex | cf91b18 | 2019-05-31 11:57:07 -0500 | [diff] [blame] | 282 | # Cross-compare versions |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 283 | _cmp = VersionCmpResult( |
| 284 | _ver_ins, |
| 285 | _ver_can, |
| 286 | _all_packages[_name]['r'] |
| 287 | ) |
Alex | cf91b18 | 2019-05-31 11:57:07 -0500 | [diff] [blame] | 288 | # Update results structure |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 289 | # shortcut to results |
| 290 | _res = _all_packages[_name]['results'] |
| 291 | # update status |
| 292 | if _cmp.status not in _res: |
| 293 | _res[_cmp.status] = {} |
| 294 | # update action |
| 295 | if _cmp.action not in _res[_cmp.status]: |
| 296 | _res[_cmp.status][_cmp.action] = {} |
| 297 | # update node |
| 298 | if node_name not in _res[_cmp.status][_cmp.action]: |
| 299 | _res[_cmp.status][_cmp.action][node_name] = {} |
| 300 | # put result |
| 301 | _res[_cmp.status][_cmp.action][node_name] = { |
| 302 | 'i': _ver_ins, |
| 303 | 'c': _ver_can, |
| 304 | 'res': _cmp, |
| 305 | 'raw': _value['raw'] |
| 306 | } |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 307 | |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 308 | self._packages = _all_packages |
Alex | d9fd85e | 2019-05-16 16:58:24 -0500 | [diff] [blame] | 309 | _progress.end() |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 310 | |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 311 | def create_report(self, filename, rtype, full=None): |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 312 | """ |
| 313 | Create static html showing packages diff per node |
| 314 | |
| 315 | :return: buff with html |
| 316 | """ |
Alex Savatieiev | 42b89fa | 2019-03-07 18:45:26 -0600 | [diff] [blame] | 317 | logger_cli.info("# Generating report to '{}'".format(filename)) |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 318 | if rtype == 'html': |
| 319 | _type = reporter.HTMLPackageCandidates() |
| 320 | elif rtype == 'csv': |
| 321 | _type = reporter.CSVAllPackages() |
| 322 | else: |
| 323 | raise ConfigException("Report type not set") |
Alex Savatieiev | d48994d | 2018-12-13 12:13:00 +0100 | [diff] [blame] | 324 | _report = reporter.ReportToFile( |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 325 | _type, |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 326 | filename |
| 327 | ) |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 328 | payload = { |
Alex | e0c5b9e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 329 | "nodes": salt_master.nodes, |
| 330 | "mcp_release": salt_master.mcp_release, |
| 331 | "openstack_release": salt_master.openstack_release |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 332 | } |
| 333 | payload.update(self.presort_packages(self._packages, full)) |
| 334 | _report(payload) |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame] | 335 | logger_cli.info("-> Done") |