Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 1 | import argparse |
| 2 | import os |
Alex Savatieiev | 06ab17d | 2019-02-26 18:40:48 -0600 | [diff] [blame] | 3 | import reclass |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 4 | import sys |
Alex Savatieiev | e961399 | 2019-02-21 18:20:35 -0600 | [diff] [blame] | 5 | import traceback |
Alex Savatieiev | 06ab17d | 2019-02-26 18:40:48 -0600 | [diff] [blame] | 6 | |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame] | 7 | from logging import INFO, DEBUG |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 8 | |
| 9 | import reporter |
Alex Savatieiev | 06ab17d | 2019-02-26 18:40:48 -0600 | [diff] [blame] | 10 | |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 11 | from cfg_checker.common.exception import CheckerException, ConfigException |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 12 | from cfg_checker.common import utils, const |
| 13 | from cfg_checker.common import config, logger, logger_cli, pkg_dir |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 14 | |
Alex Savatieiev | 06ab17d | 2019-02-26 18:40:48 -0600 | [diff] [blame] | 15 | import cfg_checker.reclass_cmp as reclass_cmp |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 16 | from cfg_checker.pkg_check import CloudPackageChecker |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 17 | from cfg_checker.network_checks import NetworkChecker |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 18 | |
| 19 | pkg_dir = os.path.dirname(__file__) |
| 20 | pkg_dir = os.path.normpath(pkg_dir) |
| 21 | |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 22 | commands = { |
| 23 | 'packages': ['report'], |
| 24 | 'network': ['check', 'report'], |
| 25 | 'reclass': ['list', 'diff'] |
| 26 | } |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 27 | |
| 28 | class MyParser(argparse.ArgumentParser): |
| 29 | def error(self, message): |
| 30 | sys.stderr.write('Error: {0}\n\n'.format(message)) |
| 31 | self.print_help() |
| 32 | |
| 33 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 34 | def help_message(): |
| 35 | print""" |
| 36 | Please, use following examples to generate info reports:\n |
| 37 | cfg_checker packages report\n |
| 38 | cfg_checker network check\n |
| 39 | cfg_checker network report\n |
| 40 | """ |
| 41 | return |
| 42 | |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 43 | |
| 44 | def get_file_arg(args): |
Alex Savatieiev | e47f7f4 | 2019-02-20 16:41:23 -0600 | [diff] [blame] | 45 | if args.file: |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 46 | return args.file |
Alex Savatieiev | e47f7f4 | 2019-02-20 16:41:23 -0600 | [diff] [blame] | 47 | else: |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 48 | raise ConfigException("No report filename supplied") |
| 49 | |
| 50 | |
Alex Savatieiev | 06ab17d | 2019-02-26 18:40:48 -0600 | [diff] [blame] | 51 | def get_path_arg(path): |
| 52 | if os.path.exists(path): |
| 53 | return path |
| 54 | else: |
| 55 | raise ConfigException("'{}' not exists".format(path)) |
| 56 | |
| 57 | |
| 58 | def validate_model(path): |
| 59 | logger_cli.debug("\t...validating '{}' as a model".format(path)) |
| 60 | _checks = [] |
| 61 | _is_folder = os.path.isdir(path) |
| 62 | logger_cli.debug("\t- folder? -> {}".format(_is_folder)) |
| 63 | _checks.append(_is_folder) |
| 64 | _has_classes = os.path.isdir(os.path.join(path, "classes")) |
| 65 | logger_cli.debug("\t- has classes? -> {}".format(_has_classes)) |
| 66 | _checks.append(_has_classes) |
| 67 | _has_cluster = os.path.isdir(os.path.join(path, "classes", "cluster")) |
| 68 | logger_cli.debug("\t- has classes/cluster? -> {}".format(_has_cluster)) |
| 69 | _checks.append(_has_cluster) |
| 70 | _has_system = os.path.isdir(os.path.join(path, "classes", "system")) |
| 71 | logger_cli.debug("\t- has classes/system? -> {}".format(_has_system)) |
| 72 | _checks.append(_has_system) |
| 73 | _has_nodes = os.path.isdir(os.path.join(path, "nodes")) |
| 74 | logger_cli.debug("\t- has nodes? -> {}".format(_has_nodes)) |
| 75 | _checks.append(_has_nodes) |
| 76 | |
| 77 | logger_cli.debug("\t-> {}".format( |
| 78 | all(_checks) |
| 79 | )) |
| 80 | |
| 81 | return all(_checks) |
| 82 | |
| 83 | |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 84 | def packages_report(args): |
| 85 | """Create package versions report |
| 86 | |
| 87 | :args: - parser arguments |
| 88 | :return: - no return value |
| 89 | """ |
| 90 | _filename = get_file_arg(args) |
| 91 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 92 | # init connection to salt and collect minion data |
| 93 | pChecker = CloudPackageChecker() |
| 94 | # collect data on installed packages |
| 95 | pChecker.collect_installed_packages() |
| 96 | # diff installed and candidates |
| 97 | # pChecker.collect_packages() |
| 98 | # report it |
| 99 | pChecker.create_html_report(_filename) |
| 100 | |
| 101 | |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 102 | def network_check(args): |
| 103 | logger_cli.info("# Network check (CLI output)") |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 104 | netChecker = NetworkChecker() |
| 105 | netChecker.collect_network_info() |
| 106 | netChecker.print_network_report() |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 107 | |
| 108 | return |
| 109 | |
| 110 | |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 111 | def network_report(args): |
| 112 | logger_cli.info("# Network check (HTML report: '{}')".format(args.file)) |
| 113 | _filename = get_file_arg(args) |
| 114 | |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 115 | netChecker = NetworkChecker() |
| 116 | netChecker.collect_network_info() |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 117 | netChecker.create_html_report(_filename) |
| 118 | |
| 119 | return |
| 120 | |
| 121 | |
| 122 | def reclass_list(args): |
Alex Savatieiev | 06ab17d | 2019-02-26 18:40:48 -0600 | [diff] [blame] | 123 | logger_cli.info("# Reclass list") |
| 124 | _path = get_path_arg(args.models_path) |
| 125 | |
| 126 | logger_cli.info("# ...models path is '{}'".format(args.models_path)) |
| 127 | |
| 128 | models = {} |
| 129 | for _folder in os.listdir(args.models_path): |
| 130 | # validate item as a model |
| 131 | _model_path = os.path.join( |
| 132 | args.models_path, |
| 133 | _folder |
| 134 | ) |
| 135 | _validated = validate_model(_model_path) |
| 136 | |
| 137 | if not _validated: |
| 138 | logger_cli.info("-> '{}' not a valid model".format(_folder)) |
| 139 | continue |
| 140 | else: |
| 141 | models[_folder] = _model_path |
| 142 | |
| 143 | logger_cli.info("-> '{}' at '{}'".format(_folder, _model_path)) |
| 144 | |
| 145 | # TODO: collect info about the model |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 146 | |
| 147 | return |
| 148 | |
| 149 | |
| 150 | def reclass_diff(args): |
| 151 | logger_cli.info("Reclass comparer (HTML report: '{}'".format(args.file)) |
| 152 | _filename = get_file_arg(args) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 153 | |
Alex Savatieiev | 06ab17d | 2019-02-26 18:40:48 -0600 | [diff] [blame] | 154 | # checking folder params |
| 155 | _model1 = get_path_arg(args.model1) |
| 156 | _model2 = get_path_arg(args.model2) |
| 157 | |
| 158 | # Do actual compare using hardcoded model names |
| 159 | mComparer = reclass_cmp.ModelComparer() |
| 160 | |
| 161 | mComparer.model_name_1 = os.path.split(_model1)[1] |
| 162 | mComparer.model_path_1 = _model1 |
| 163 | mComparer.model_name_2 = os.path.split(_model2)[1] |
| 164 | mComparer.model_path_2 = _model2 |
| 165 | |
| 166 | mComparer.load_model_tree( |
| 167 | mComparer.model_name_1, |
| 168 | mComparer.model_path_1 |
| 169 | ) |
| 170 | mComparer.load_model_tree( |
| 171 | mComparer.model_name_2, |
| 172 | mComparer.model_path_2 |
| 173 | ) |
| 174 | |
| 175 | diffs = mComparer.generate_model_report_tree() |
| 176 | |
| 177 | report = reporter.ReportToFile( |
| 178 | reporter.HTMLModelCompare(), |
| 179 | _filename |
| 180 | ) |
| 181 | logger_cli.info("# Generating report to {}".format(_filename)) |
| 182 | report({ |
| 183 | "nodes": {}, |
| 184 | "diffs": diffs |
| 185 | }) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 186 | |
| 187 | |
| 188 | def config_check_entrypoint(): |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 189 | """ |
| 190 | Main entry point. Uses nested parsers structure |
| 191 | with a default function to execute the comand |
| 192 | |
| 193 | :return: - no return value |
| 194 | """ |
Alex Savatieiev | 4c40632 | 2019-02-28 17:37:09 -0600 | [diff] [blame^] | 195 | # Main entrypoint |
| 196 | parser = MyParser(prog="# Mirantis Cloud configuration checker") |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 197 | |
| 198 | # Parsers (each parser can have own arguments) |
| 199 | # - subparsers (command) |
| 200 | # |- pkg_parser |
| 201 | # | - pkg_subparsers (type) |
| 202 | # | - pkg_report_parser (default func - pkg_check) |
| 203 | # |- net_parser |
| 204 | # | - net_subparsers (type) |
| 205 | # | - net_check_parser (default func - net_check) |
| 206 | # | - net_report_parser (default func - net_report) |
| 207 | # - reclass_parser |
| 208 | # - reclass_list (default func - reclass_list) |
| 209 | # - reclass_compare (default func - reclass_diff) |
| 210 | |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame] | 211 | parser.add_argument( |
| 212 | "-d", |
| 213 | "--debug", |
| 214 | action="store_true", default=False, |
| 215 | help="Set CLI logging level to DEBUG" |
| 216 | ) |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 217 | parser.add_argument( |
| 218 | '-f', |
| 219 | '--file', |
| 220 | help="HTML filename to save report" |
| 221 | ) |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 222 | parser.add_argument( |
| 223 | '-s', |
| 224 | '--sudo', |
| 225 | action='store_true', default=True, |
| 226 | help="Use sudo for getting salt creds" |
| 227 | ) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 228 | subparsers = parser.add_subparsers(dest='command') |
| 229 | # packages |
| 230 | pkg_parser = subparsers.add_parser( |
| 231 | 'packages', |
| 232 | help="Package versions check (Candidate vs Installed)" |
| 233 | ) |
| 234 | pkg_subparsers = pkg_parser.add_subparsers(dest='type') |
| 235 | |
| 236 | pkg_report_parser = pkg_subparsers.add_parser( |
| 237 | 'report', |
| 238 | help="Report package versions to HTML file" |
| 239 | ) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 240 | |
| 241 | # networking |
| 242 | net_parser = subparsers.add_parser( |
| 243 | 'network', |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 244 | help="Network infrastructure checks and reports" |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 245 | ) |
| 246 | net_subparsers = net_parser.add_subparsers(dest='type') |
| 247 | |
| 248 | net_check_parser = net_subparsers.add_parser( |
| 249 | 'check', |
| 250 | help="Do network check and print the result" |
| 251 | ) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 252 | |
| 253 | net_report_parser = net_subparsers.add_parser( |
| 254 | 'report', |
| 255 | help="Generate network check report" |
| 256 | ) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 257 | |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 258 | # reclass |
| 259 | reclass_parser = subparsers.add_parser( |
| 260 | 'reclass', |
| 261 | help="Reclass related checks and reports" |
| 262 | ) |
| 263 | reclass_subparsers = reclass_parser.add_subparsers(dest='type') |
| 264 | reclass_list_parser = reclass_subparsers.add_parser( |
| 265 | 'list', |
| 266 | help="List models available to compare" |
| 267 | ) |
Alex Savatieiev | 06ab17d | 2019-02-26 18:40:48 -0600 | [diff] [blame] | 268 | reclass_list_parser.add_argument( |
| 269 | "-p", |
| 270 | "--models-path", |
| 271 | default="/srv/salt/", |
| 272 | help="Global path to search models in" |
| 273 | ) |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 274 | |
| 275 | reclass_diff_parser = reclass_subparsers.add_parser( |
| 276 | 'diff', |
| 277 | help="List models available to compare" |
| 278 | ) |
Alex Savatieiev | 06ab17d | 2019-02-26 18:40:48 -0600 | [diff] [blame] | 279 | reclass_diff_parser.add_argument( |
| 280 | "--model1", |
| 281 | required=True, |
| 282 | help="Model A <path>. Model name is the folder name" |
| 283 | ) |
| 284 | reclass_diff_parser.add_argument( |
| 285 | "--model2", |
| 286 | required=True, |
| 287 | help="Model B <path>. Model name is the folder name" |
| 288 | ) |
| 289 | |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 290 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 291 | #parse arguments |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 292 | try: |
| 293 | args = parser.parse_args() |
| 294 | except TypeError as e: |
| 295 | logger_cli.info("\nPlease, check arguments") |
| 296 | return |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 297 | |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 298 | # Pass externally configured values |
| 299 | config.ssh_uses_sudo = args.sudo |
| 300 | |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame] | 301 | # Handle options |
| 302 | if args.debug: |
| 303 | logger_cli.setLevel(DEBUG) |
| 304 | else: |
| 305 | logger_cli.setLevel(INFO) |
| 306 | |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 307 | # Validate the commands |
| 308 | # check command |
| 309 | if args.command not in commands: |
| 310 | logger_cli.info("\nPlease, type a command listed above") |
| 311 | return |
| 312 | elif args.type not in commands[args.command]: |
| 313 | # check type |
| 314 | logger_cli.info( |
| 315 | "\nPlease, select '{}' command type listed above".format( |
| 316 | args.command |
| 317 | ) |
| 318 | ) |
| 319 | return |
| 320 | else: |
| 321 | # form function name to call |
| 322 | _method_name = args.command + "_" + args.type |
| 323 | _this_module = sys.modules[__name__] |
| 324 | _method = getattr(_this_module, _method_name) |
| 325 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 326 | # Execute the command |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 327 | result = _method(args) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 328 | |
| 329 | logger.debug(result) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 330 | |
Alex Savatieiev | 4c40632 | 2019-02-28 17:37:09 -0600 | [diff] [blame^] | 331 | def cli_main(): |
Alex Savatieiev | e47f7f4 | 2019-02-20 16:41:23 -0600 | [diff] [blame] | 332 | try: |
| 333 | config_check_entrypoint() |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 334 | except CheckerException as e: |
Alex Savatieiev | 06ab17d | 2019-02-26 18:40:48 -0600 | [diff] [blame] | 335 | logger_cli.error("\nERROR: {}".format( |
| 336 | e.message |
| 337 | )) |
| 338 | |
Alex Savatieiev | e961399 | 2019-02-21 18:20:35 -0600 | [diff] [blame] | 339 | exc_type, exc_value, exc_traceback = sys.exc_info() |
Alex Savatieiev | 06ab17d | 2019-02-26 18:40:48 -0600 | [diff] [blame] | 340 | logger_cli.debug("\n{}".format( |
Alex Savatieiev | e961399 | 2019-02-21 18:20:35 -0600 | [diff] [blame] | 341 | "".join(traceback.format_exception( |
| 342 | exc_type, |
| 343 | exc_value, |
| 344 | exc_traceback |
| 345 | )) |
| 346 | )) |
Alex Savatieiev | 4c40632 | 2019-02-28 17:37:09 -0600 | [diff] [blame^] | 347 | |
| 348 | if __name__ == '__main__': |
| 349 | cli_main() |