blob: 8d498c3ff4eed02b3d4596abaff021e6467b7f94 [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
Alex9a4ad212020-10-01 18:04:25 -05004from cfg_checker.common.settings import ENV_TYPE_LINUX
Alex Savatieievc9055712019-03-01 14:43:56 -06005from cfg_checker.helpers import args_utils
6from cfg_checker.reports import reporter
7
Alex3bc95f62020-03-05 17:00:04 -06008from . import comparer
Alex3ebc5632019-04-18 16:47:18 -05009
Alex3bc95f62020-03-05 17:00:04 -060010from . import validator
Alex3ebc5632019-04-18 16:47:18 -050011
Alexbab1efe2019-04-23 18:51:23 -050012command_help = "Reclass related checks and reports"
Alex9a4ad212020-10-01 18:04:25 -050013supported_envs = [ENV_TYPE_LINUX]
Alexbab1efe2019-04-23 18:51:23 -050014
15
16def 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 Savatieievc9055712019-03-01 14:43:56 -060052
Alex9a4ad212020-10-01 18:04:25 -050053def 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 Savatieievc9055712019-03-01 14:43:56 -060057 logger_cli.info("# Reclass list")
Alex41485522019-04-12 17:26:18 -050058 _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)
Alex3ebc5632019-04-18 16:47:18 -050061
62 logger_cli.info("# ...models path is '{}'".format(_path))
63
Alex Savatieievc9055712019-03-01 14:43:56 -060064 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)
Alex3ebc5632019-04-18 16:47:18 -050072
Alex Savatieievc9055712019-03-01 14:43:56 -060073 if not _validated:
74 logger_cli.info("-> '{}' not a valid model".format(_folder))
75 continue
76 else:
77 models[_folder] = _model_path
Alex3ebc5632019-04-18 16:47:18 -050078
Alex Savatieievc9055712019-03-01 14:43:56 -060079 logger_cli.info("-> '{}' at '{}'".format(_folder, _model_path))
Alex3ebc5632019-04-18 16:47:18 -050080
Alex Savatieievc9055712019-03-01 14:43:56 -060081 # TODO: collect info about the model
82
83 return
84
85
Alex9a4ad212020-10-01 18:04:25 -050086def 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
Alexb8af13a2019-04-16 18:38:12 -050090 logger_cli.info("# Reclass comparer (HTML report)")
Alex41485522019-04-12 17:26:18 -050091 _filename = args_utils.get_arg(args, 'html')
Alex Savatieievc9055712019-03-01 14:43:56 -060092 # checking folder params
93 _model1 = args_utils.get_path_arg(args.model1)
94 _model2 = args_utils.get_path_arg(args.model2)
Alex3ebc5632019-04-18 16:47:18 -050095
Alex Savatieievc9055712019-03-01 14:43:56 -060096 # 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
Alex3ebc5632019-04-18 16:47:18 -0500103
Alex Savatieievc9055712019-03-01 14:43:56 -0600104 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 })