Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 1 | import os |
| 2 | |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 3 | from cfg_checker.common import logger_cli |
| 4 | from cfg_checker.helpers import args_utils |
| 5 | from cfg_checker.reports import reporter |
| 6 | |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 7 | import comparer |
| 8 | |
| 9 | import validator |
| 10 | |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame^] | 11 | command_help = "Reclass related checks and reports" |
| 12 | |
| 13 | |
| 14 | def init_parser(_parser): |
| 15 | # reclass subparsers |
| 16 | reclass_subparsers = _parser.add_subparsers(dest='type') |
| 17 | reclass_list_parser = reclass_subparsers.add_parser( |
| 18 | 'list', |
| 19 | help="List models available to compare" |
| 20 | ) |
| 21 | reclass_list_parser.add_argument( |
| 22 | "-p", |
| 23 | "--models-path", |
| 24 | default="/srv/salt/", |
| 25 | help="Global path to search models in" |
| 26 | ) |
| 27 | |
| 28 | reclass_diff_parser = reclass_subparsers.add_parser( |
| 29 | 'diff', |
| 30 | help="List models available to compare" |
| 31 | ) |
| 32 | reclass_diff_parser.add_argument( |
| 33 | "--model1", |
| 34 | required=True, |
| 35 | help="Model A <path>. Model name is the folder name" |
| 36 | ) |
| 37 | reclass_diff_parser.add_argument( |
| 38 | "--model2", |
| 39 | required=True, |
| 40 | help="Model B <path>. Model name is the folder name" |
| 41 | ) |
| 42 | reclass_diff_parser.add_argument( |
| 43 | '--html', |
| 44 | metavar='reclass_html_filename', |
| 45 | help="HTML filename to save report" |
| 46 | ) |
| 47 | |
| 48 | return _parser |
| 49 | |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 50 | |
| 51 | def do_list(args): |
| 52 | logger_cli.info("# Reclass list") |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 53 | _arg_path = args_utils.get_arg(args, 'models_path') |
| 54 | logger_cli.info("-> Current path is: {}".format(_arg_path)) |
| 55 | _path = args_utils.get_path_arg(_arg_path) |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 56 | |
| 57 | logger_cli.info("# ...models path is '{}'".format(_path)) |
| 58 | |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 59 | models = {} |
| 60 | for _folder in os.listdir(args.models_path): |
| 61 | # validate item as a model |
| 62 | _model_path = os.path.join( |
| 63 | args.models_path, |
| 64 | _folder |
| 65 | ) |
| 66 | _validated = validator.basic_model_validation_by_path(_model_path) |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 67 | |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 68 | if not _validated: |
| 69 | logger_cli.info("-> '{}' not a valid model".format(_folder)) |
| 70 | continue |
| 71 | else: |
| 72 | models[_folder] = _model_path |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 73 | |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 74 | logger_cli.info("-> '{}' at '{}'".format(_folder, _model_path)) |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 75 | |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 76 | # TODO: collect info about the model |
| 77 | |
| 78 | return |
| 79 | |
| 80 | |
| 81 | def do_diff(args): |
Alex | b8af13a | 2019-04-16 18:38:12 -0500 | [diff] [blame] | 82 | logger_cli.info("# Reclass comparer (HTML report)") |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 83 | _filename = args_utils.get_arg(args, 'html') |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 84 | # checking folder params |
| 85 | _model1 = args_utils.get_path_arg(args.model1) |
| 86 | _model2 = args_utils.get_path_arg(args.model2) |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 87 | |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 88 | # Do actual compare using hardcoded model names |
| 89 | mComparer = comparer.ModelComparer() |
| 90 | |
| 91 | mComparer.model_name_1 = os.path.split(_model1)[1] |
| 92 | mComparer.model_path_1 = _model1 |
| 93 | mComparer.model_name_2 = os.path.split(_model2)[1] |
| 94 | mComparer.model_path_2 = _model2 |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 95 | |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 96 | mComparer.load_model_tree( |
| 97 | mComparer.model_name_1, |
| 98 | mComparer.model_path_1 |
| 99 | ) |
| 100 | mComparer.load_model_tree( |
| 101 | mComparer.model_name_2, |
| 102 | mComparer.model_path_2 |
| 103 | ) |
| 104 | |
| 105 | diffs = mComparer.generate_model_report_tree() |
| 106 | |
| 107 | report = reporter.ReportToFile( |
| 108 | reporter.HTMLModelCompare(), |
| 109 | _filename |
| 110 | ) |
| 111 | logger_cli.info("# Generating report to {}".format(_filename)) |
| 112 | report({ |
| 113 | "nodes": {}, |
| 114 | "diffs": diffs |
| 115 | }) |