blob: 02205ea858b73c83927e48cd41d21be06ed22ec0 [file] [log] [blame]
Alex Savatieiev5118de02019-02-20 15:50:42 -06001import argparse
2import os
3import sys
Alex Savatieieve9613992019-02-21 18:20:35 -06004import traceback
Alex Savatieiev799bee32019-02-20 17:19:26 -06005from logging import INFO, DEBUG
Alex Savatieiev5118de02019-02-20 15:50:42 -06006
7import reporter
8from cfg_checker.common import utils, const
9from cfg_checker.common import config, logger, logger_cli, pkg_dir
10from cfg_checker.clients import salt
11
12from cfg_checker.pkg_check import CloudPackageChecker
Alex Savatieiev9b2f6512019-02-20 18:05:00 -060013from cfg_checker.network_checks import NetworkChecker
Alex Savatieiev5118de02019-02-20 15:50:42 -060014
15pkg_dir = os.path.dirname(__file__)
16pkg_dir = os.path.normpath(pkg_dir)
17
18
19class MyParser(argparse.ArgumentParser):
20 def error(self, message):
21 sys.stderr.write('Error: {0}\n\n'.format(message))
22 self.print_help()
23
24
Alex Savatieiev5118de02019-02-20 15:50:42 -060025def help_message():
26 print"""
27 Please, use following examples to generate info reports:\n
28 cfg_checker packages report\n
29 cfg_checker network check\n
30 cfg_checker network report\n
31 """
32 return
33
34def pkg_check(args):
35 # create package versions report
Alex Savatieieve47f7f42019-02-20 16:41:23 -060036 if args.file:
37 _filename = args.file
38 else:
39 logger_cli.error("ERROR: no report filename supplied")
40 return
Alex Savatieiev5118de02019-02-20 15:50:42 -060041 # init connection to salt and collect minion data
42 pChecker = CloudPackageChecker()
43 # collect data on installed packages
44 pChecker.collect_installed_packages()
45 # diff installed and candidates
46 # pChecker.collect_packages()
47 # report it
48 pChecker.create_html_report(_filename)
49
50
51def net_check(args):
Alex Savatieiev9b2f6512019-02-20 18:05:00 -060052 netChecker = NetworkChecker()
53 netChecker.collect_network_info()
54 netChecker.print_network_report()
Alex Savatieiev5118de02019-02-20 15:50:42 -060055
56 return
57
58
59def net_report(args):
Alex Savatieiev9b2f6512019-02-20 18:05:00 -060060 netChecker = NetworkChecker()
61 netChecker.collect_network_info()
62 netChecker.create_html_report()
Alex Savatieiev5118de02019-02-20 15:50:42 -060063
64 return
65
66
67def config_check_entrypoint():
68 # Main entrypointр
69 parser = MyParser(prog="Cloud configuration checker")
Alex Savatieiev799bee32019-02-20 17:19:26 -060070 parser.add_argument(
71 "-d",
72 "--debug",
73 action="store_true", default=False,
74 help="Set CLI logging level to DEBUG"
75 )
Alex Savatieiev5118de02019-02-20 15:50:42 -060076 subparsers = parser.add_subparsers(dest='command')
77 # packages
78 pkg_parser = subparsers.add_parser(
79 'packages',
80 help="Package versions check (Candidate vs Installed)"
81 )
82 pkg_subparsers = pkg_parser.add_subparsers(dest='type')
83
84 pkg_report_parser = pkg_subparsers.add_parser(
85 'report',
86 help="Report package versions to HTML file"
87 )
88 pkg_report_parser.add_argument(
89 '-f',
90 '--file',
91 help="HTML filename to save report"
92 )
Alex Savatieieve47f7f42019-02-20 16:41:23 -060093 pkg_report_parser.set_defaults(func=pkg_check)
Alex Savatieiev5118de02019-02-20 15:50:42 -060094
95 # networking
96 net_parser = subparsers.add_parser(
97 'network',
98 help="Network infrastructure checks"
99 )
100 net_subparsers = net_parser.add_subparsers(dest='type')
101
102 net_check_parser = net_subparsers.add_parser(
103 'check',
104 help="Do network check and print the result"
105 )
106 net_check_parser.set_defaults(func=net_check)
107
108 net_report_parser = net_subparsers.add_parser(
109 'report',
110 help="Generate network check report"
111 )
112 net_report_parser.add_argument(
113 '-f',
114 '--file',
115 help="HTML filename to save report"
116 )
117 net_report_parser.set_defaults(func=net_report)
118
119 #parse arguments
120 args = parser.parse_args()
121
Alex Savatieiev799bee32019-02-20 17:19:26 -0600122 # Handle options
123 if args.debug:
124 logger_cli.setLevel(DEBUG)
125 else:
126 logger_cli.setLevel(INFO)
127
Alex Savatieiev5118de02019-02-20 15:50:42 -0600128 # Execute the command
129 result = args.func(args)
130
131 logger.debug(result)
132 return
133
134if __name__ == '__main__':
Alex Savatieieve47f7f42019-02-20 16:41:23 -0600135 try:
136 config_check_entrypoint()
137 except Exception as e:
Alex Savatieieve9613992019-02-21 18:20:35 -0600138 exc_type, exc_value, exc_traceback = sys.exc_info()
139 logger_cli.error("\nERROR: {}\n\n{}".format(
140 e.message,
141 "".join(traceback.format_exception(
142 exc_type,
143 exc_value,
144 exc_traceback
145 ))
146 ))