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