blob: b2e388651c1154114cc4f3c2e9258ed43da1a3a2 [file] [log] [blame]
Alex Savatieiev5118de02019-02-20 15:50:42 -06001import argparse
2import os
3import sys
Alex Savatieiev799bee32019-02-20 17:19:26 -06004from logging import INFO, DEBUG
Alex Savatieiev5118de02019-02-20 15:50:42 -06005
6import reporter
7from cfg_checker.common import utils, const
8from cfg_checker.common import config, logger, logger_cli, pkg_dir
9from cfg_checker.clients import salt
10
11from cfg_checker.pkg_check import CloudPackageChecker
12
13pkg_dir = os.path.dirname(__file__)
14pkg_dir = os.path.normpath(pkg_dir)
15
16
17class MyParser(argparse.ArgumentParser):
18 def error(self, message):
19 sys.stderr.write('Error: {0}\n\n'.format(message))
20 self.print_help()
21
22
Alex Savatieiev5118de02019-02-20 15:50:42 -060023def help_message():
24 print"""
25 Please, use following examples to generate info reports:\n
26 cfg_checker packages report\n
27 cfg_checker network check\n
28 cfg_checker network report\n
29 """
30 return
31
32def pkg_check(args):
33 # create package versions report
Alex Savatieieve47f7f42019-02-20 16:41:23 -060034 if args.file:
35 _filename = args.file
36 else:
37 logger_cli.error("ERROR: no report filename supplied")
38 return
Alex Savatieiev5118de02019-02-20 15:50:42 -060039 # init connection to salt and collect minion data
40 pChecker = CloudPackageChecker()
41 # collect data on installed packages
42 pChecker.collect_installed_packages()
43 # diff installed and candidates
44 # pChecker.collect_packages()
45 # report it
46 pChecker.create_html_report(_filename)
47
48
49def net_check(args):
50 print("This is net check routine")
51
52 return
53
54
55def net_report(args):
56 print("This is net check routine")
57
58 return
59
60
61def config_check_entrypoint():
62 # Main entrypointр
63 parser = MyParser(prog="Cloud configuration checker")
Alex Savatieiev799bee32019-02-20 17:19:26 -060064 parser.add_argument(
65 "-d",
66 "--debug",
67 action="store_true", default=False,
68 help="Set CLI logging level to DEBUG"
69 )
Alex Savatieiev5118de02019-02-20 15:50:42 -060070 subparsers = parser.add_subparsers(dest='command')
71 # packages
72 pkg_parser = subparsers.add_parser(
73 'packages',
74 help="Package versions check (Candidate vs Installed)"
75 )
76 pkg_subparsers = pkg_parser.add_subparsers(dest='type')
77
78 pkg_report_parser = pkg_subparsers.add_parser(
79 'report',
80 help="Report package versions to HTML file"
81 )
82 pkg_report_parser.add_argument(
83 '-f',
84 '--file',
85 help="HTML filename to save report"
86 )
Alex Savatieieve47f7f42019-02-20 16:41:23 -060087 pkg_report_parser.set_defaults(func=pkg_check)
Alex Savatieiev5118de02019-02-20 15:50:42 -060088
89 # networking
90 net_parser = subparsers.add_parser(
91 'network',
92 help="Network infrastructure checks"
93 )
94 net_subparsers = net_parser.add_subparsers(dest='type')
95
96 net_check_parser = net_subparsers.add_parser(
97 'check',
98 help="Do network check and print the result"
99 )
100 net_check_parser.set_defaults(func=net_check)
101
102 net_report_parser = net_subparsers.add_parser(
103 'report',
104 help="Generate network check report"
105 )
106 net_report_parser.add_argument(
107 '-f',
108 '--file',
109 help="HTML filename to save report"
110 )
111 net_report_parser.set_defaults(func=net_report)
112
113 #parse arguments
114 args = parser.parse_args()
115
Alex Savatieiev799bee32019-02-20 17:19:26 -0600116 # Handle options
117 if args.debug:
118 logger_cli.setLevel(DEBUG)
119 else:
120 logger_cli.setLevel(INFO)
121
Alex Savatieiev5118de02019-02-20 15:50:42 -0600122 # Execute the command
123 result = args.func(args)
124
125 logger.debug(result)
126 return
127
128if __name__ == '__main__':
Alex Savatieieve47f7f42019-02-20 16:41:23 -0600129 try:
130 config_check_entrypoint()
131 except Exception as e:
132 logger_cli.error("ERROR: {}".format(e.message))