blob: 4b8b667b987001ad3cc5d259a3df211bd8abc7f2 [file] [log] [blame]
Alex Savatieievc9055712019-03-01 14:43:56 -06001import os
2
Alex Savatieievc9055712019-03-01 14:43:56 -06003from cfg_checker.common import logger_cli
4from cfg_checker.helpers import args_utils
5from cfg_checker.reports import reporter
6
Alex3ebc5632019-04-18 16:47:18 -05007import comparer
8
9import validator
10
Alexbab1efe2019-04-23 18:51:23 -050011command_help = "Reclass related checks and reports"
12
13
14def 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 Savatieievc9055712019-03-01 14:43:56 -060050
51def do_list(args):
52 logger_cli.info("# Reclass list")
Alex41485522019-04-12 17:26:18 -050053 _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)
Alex3ebc5632019-04-18 16:47:18 -050056
57 logger_cli.info("# ...models path is '{}'".format(_path))
58
Alex Savatieievc9055712019-03-01 14:43:56 -060059 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)
Alex3ebc5632019-04-18 16:47:18 -050067
Alex Savatieievc9055712019-03-01 14:43:56 -060068 if not _validated:
69 logger_cli.info("-> '{}' not a valid model".format(_folder))
70 continue
71 else:
72 models[_folder] = _model_path
Alex3ebc5632019-04-18 16:47:18 -050073
Alex Savatieievc9055712019-03-01 14:43:56 -060074 logger_cli.info("-> '{}' at '{}'".format(_folder, _model_path))
Alex3ebc5632019-04-18 16:47:18 -050075
Alex Savatieievc9055712019-03-01 14:43:56 -060076 # TODO: collect info about the model
77
78 return
79
80
81def do_diff(args):
Alexb8af13a2019-04-16 18:38:12 -050082 logger_cli.info("# Reclass comparer (HTML report)")
Alex41485522019-04-12 17:26:18 -050083 _filename = args_utils.get_arg(args, 'html')
Alex Savatieievc9055712019-03-01 14:43:56 -060084 # checking folder params
85 _model1 = args_utils.get_path_arg(args.model1)
86 _model2 = args_utils.get_path_arg(args.model2)
Alex3ebc5632019-04-18 16:47:18 -050087
Alex Savatieievc9055712019-03-01 14:43:56 -060088 # 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
Alex3ebc5632019-04-18 16:47:18 -050095
Alex Savatieievc9055712019-03-01 14:43:56 -060096 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 })