savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 1 | import abc |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame^] | 2 | import jinja2 |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 3 | import os |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame^] | 4 | import six |
| 5 | import time |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 6 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 7 | from cfg_checker.common import const |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame^] | 8 | from cfg_checker.common import logger, logger_cli |
| 9 | from cfg_checker.helpers.console_utils import Progress |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 10 | |
| 11 | pkg_dir = os.path.dirname(__file__) |
Alex Savatieiev | 6d010be | 2019-03-11 10:36:59 -0500 | [diff] [blame] | 12 | pkg_dir = os.path.join(pkg_dir, os.pardir, os.pardir) |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 13 | pkg_dir = os.path.normpath(pkg_dir) |
| 14 | |
| 15 | |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 16 | def line_breaks(text): |
| 17 | # replace python linebreaks with html breaks |
| 18 | return text.replace("\n", "<br />") |
| 19 | |
| 20 | |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame^] | 21 | def get_sorted_keys(td): |
| 22 | # detect if we can sort by desc |
| 23 | # Yes, this is slow, but bullet-proof from empty desc |
| 24 | _desc = all([bool(td[k]['desc']) for k in td.keys()]) |
| 25 | # Get sorted list |
| 26 | if not _desc: |
| 27 | return sorted(td.keys()) |
| 28 | else: |
| 29 | return sorted( |
| 30 | td.keys(), |
| 31 | key=lambda k: ( |
| 32 | td[k]['desc']['component'], |
| 33 | td[k]['desc']['app'], |
| 34 | k |
| 35 | ) |
| 36 | ) |
| 37 | |
| 38 | |
| 39 | def get_max(_list): |
| 40 | return sorted(_list)[-1] |
| 41 | |
| 42 | |
| 43 | def make_action_label(act): |
| 44 | _act_labels = { |
| 45 | const.ACT_UPGRADE: "Upgrade possible", |
| 46 | const.ACT_NEED_UP: "Needs upgrade", |
| 47 | const.ACT_NEED_DOWN: "Needs downgrade", |
| 48 | const.ACT_REPO: "Needs repo update", |
| 49 | const.ACT_NA: "" |
| 50 | } |
| 51 | return _act_labels[act] |
| 52 | |
| 53 | |
| 54 | def make_action_class(act): |
| 55 | _act_classes = { |
| 56 | const.ACT_UPGRADE: "possible", |
| 57 | const.ACT_NEED_UP: "needs_up", |
| 58 | const.ACT_NEED_DOWN: "needs_down", |
| 59 | const.ACT_REPO: "needs_repo", |
| 60 | const.ACT_NA: "" |
| 61 | } |
| 62 | return _act_classes[act] |
| 63 | |
| 64 | |
| 65 | def make_status_label(sts): |
| 66 | _status_labels = { |
| 67 | const.VERSION_OK: "OK", |
| 68 | const.VERSION_UP: "Upgraded", |
| 69 | const.VERSION_DOWN: "Downgraded", |
| 70 | const.VERSION_ERR: "ERROR", |
| 71 | const.VERSION_NA: "N/A" |
| 72 | } |
| 73 | return _status_labels[sts] |
| 74 | |
| 75 | |
| 76 | def make_status_class(sts): |
| 77 | return const.all_statuses[sts] |
| 78 | |
| 79 | |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 80 | @six.add_metaclass(abc.ABCMeta) |
| 81 | class _Base(object): |
| 82 | def __init__(self): |
| 83 | self.jinja2_env = self.init_jinja2_env() |
| 84 | |
| 85 | @abc.abstractmethod |
| 86 | def __call__(self, payload): |
| 87 | pass |
| 88 | |
| 89 | @staticmethod |
| 90 | def init_jinja2_env(): |
| 91 | return jinja2.Environment( |
| 92 | loader=jinja2.FileSystemLoader(os.path.join(pkg_dir, 'templates')), |
| 93 | trim_blocks=True, |
| 94 | lstrip_blocks=True) |
| 95 | |
| 96 | |
| 97 | class _TMPLBase(_Base): |
| 98 | @abc.abstractproperty |
| 99 | def tmpl(self): |
| 100 | pass |
| 101 | |
| 102 | @staticmethod |
| 103 | def _count_totals(data): |
| 104 | data['counters']['total_nodes'] = len(data['nodes']) |
| 105 | |
Alex Savatieiev | 36b938d | 2019-01-21 11:01:18 +0100 | [diff] [blame] | 106 | def __call__(self, payload): |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 107 | # init data structures |
| 108 | data = self.common_data() |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame^] | 109 | # payload should have pre-sorted structure according to report called |
| 110 | # nodes, openstack_release, mcp_release, etc... |
| 111 | data.update(payload) |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 112 | |
| 113 | # add template specific data |
| 114 | self._extend_data(data) |
| 115 | |
| 116 | # do counts global |
| 117 | self._count_totals(data) |
| 118 | |
| 119 | # specific filters |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 120 | self.jinja2_env.filters['linebreaks'] = line_breaks |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame^] | 121 | self.jinja2_env.filters['get_max'] = get_max |
| 122 | |
| 123 | self.jinja2_env.filters['get_sorted_keys'] = get_sorted_keys |
| 124 | self.jinja2_env.filters['make_status_label'] = make_status_label |
| 125 | self.jinja2_env.filters['make_status_class'] = make_status_class |
| 126 | self.jinja2_env.filters['make_action_label'] = make_action_label |
| 127 | self.jinja2_env.filters['make_action_class'] = make_action_class |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 128 | |
| 129 | # render! |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame^] | 130 | logger_cli.info("-> Using template: {}".format(self.tmpl)) |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 131 | tmpl = self.jinja2_env.get_template(self.tmpl) |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame^] | 132 | logger_cli.info("-> Rendering") |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 133 | return tmpl.render(data) |
| 134 | |
| 135 | def common_data(self): |
| 136 | return { |
| 137 | 'counters': {}, |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame^] | 138 | 'salt_info': {}, |
| 139 | 'gen_date': time.strftime("%m/%d/%Y %H:%M:%S") |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | def _extend_data(self, data): |
| 143 | pass |
| 144 | |
| 145 | |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame^] | 146 | # HTML Package versions report |
| 147 | class CSVAllPackages(_TMPLBase): |
| 148 | tmpl = "pkg_versions_csv.j2" |
| 149 | |
| 150 | |
| 151 | # HTML Package versions report |
savex | ce010ba | 2018-04-27 09:49:23 +0200 | [diff] [blame] | 152 | class HTMLPackageCandidates(_TMPLBase): |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame^] | 153 | tmpl = "pkg_versions_html.j2" |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 154 | |
| 155 | |
Alex Savatieiev | d48994d | 2018-12-13 12:13:00 +0100 | [diff] [blame] | 156 | # Package versions report |
| 157 | class HTMLModelCompare(_TMPLBase): |
| 158 | tmpl = "model_tree_cmp_tmpl.j2" |
| 159 | |
| 160 | def _extend_data(self, data): |
Alex Savatieiev | 36b938d | 2019-01-21 11:01:18 +0100 | [diff] [blame] | 161 | # move names into separate place |
Alex Savatieiev | 3db12a7 | 2019-03-22 16:32:31 -0500 | [diff] [blame] | 162 | data["names"] = data["rc_diffs"].pop("diff_names") |
| 163 | data["tabs"] = data.pop("rc_diffs") |
Alex Savatieiev | 4f149d0 | 2019-02-28 17:15:29 -0600 | [diff] [blame] | 164 | |
Alex Savatieiev | d48994d | 2018-12-13 12:13:00 +0100 | [diff] [blame] | 165 | # counters - mdl_diff |
Alex Savatieiev | 4f149d0 | 2019-02-28 17:15:29 -0600 | [diff] [blame] | 166 | for _tab in data["tabs"].keys(): |
| 167 | data['counters'][_tab] = len(data["tabs"][_tab]["diffs"].keys()) |
Alex Savatieiev | d48994d | 2018-12-13 12:13:00 +0100 | [diff] [blame] | 168 | |
| 169 | |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 170 | class HTMLNetworkReport(_TMPLBase): |
| 171 | tmpl = "network_check_tmpl.j2" |
| 172 | |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 173 | |
savex | 4448e13 | 2018-04-25 15:51:14 +0200 | [diff] [blame] | 174 | class ReportToFile(object): |
| 175 | def __init__(self, report, target): |
| 176 | self.report = report |
| 177 | self.target = target |
| 178 | |
| 179 | def __call__(self, payload): |
| 180 | payload = self.report(payload) |
| 181 | |
| 182 | if isinstance(self.target, six.string_types): |
| 183 | self._wrapped_dump(payload) |
| 184 | else: |
| 185 | self._dump(payload, self.target) |
| 186 | |
| 187 | def _wrapped_dump(self, payload): |
| 188 | with open(self.target, 'wt') as target: |
| 189 | self._dump(payload, target) |
| 190 | |
| 191 | @staticmethod |
| 192 | def _dump(payload, target): |
| 193 | target.write(payload) |