Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 1 | import argparse |
| 2 | import os |
| 3 | import sys |
Alex Savatieiev | e961399 | 2019-02-21 18:20:35 -0600 | [diff] [blame^] | 4 | import traceback |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame] | 5 | from logging import INFO, DEBUG |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 6 | |
| 7 | import reporter |
| 8 | from cfg_checker.common import utils, const |
| 9 | from cfg_checker.common import config, logger, logger_cli, pkg_dir |
| 10 | from cfg_checker.clients import salt |
| 11 | |
| 12 | from cfg_checker.pkg_check import CloudPackageChecker |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 13 | from cfg_checker.network_checks import NetworkChecker |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 14 | |
| 15 | pkg_dir = os.path.dirname(__file__) |
| 16 | pkg_dir = os.path.normpath(pkg_dir) |
| 17 | |
| 18 | |
| 19 | class 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 Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 25 | def 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 | |
| 34 | def pkg_check(args): |
| 35 | # create package versions report |
Alex Savatieiev | e47f7f4 | 2019-02-20 16:41:23 -0600 | [diff] [blame] | 36 | if args.file: |
| 37 | _filename = args.file |
| 38 | else: |
| 39 | logger_cli.error("ERROR: no report filename supplied") |
| 40 | return |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 41 | # 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 | |
| 51 | def net_check(args): |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 52 | netChecker = NetworkChecker() |
| 53 | netChecker.collect_network_info() |
| 54 | netChecker.print_network_report() |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 55 | |
| 56 | return |
| 57 | |
| 58 | |
| 59 | def net_report(args): |
Alex Savatieiev | 9b2f651 | 2019-02-20 18:05:00 -0600 | [diff] [blame] | 60 | netChecker = NetworkChecker() |
| 61 | netChecker.collect_network_info() |
| 62 | netChecker.create_html_report() |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 63 | |
| 64 | return |
| 65 | |
| 66 | |
| 67 | def config_check_entrypoint(): |
| 68 | # Main entrypointр |
| 69 | parser = MyParser(prog="Cloud configuration checker") |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame] | 70 | parser.add_argument( |
| 71 | "-d", |
| 72 | "--debug", |
| 73 | action="store_true", default=False, |
| 74 | help="Set CLI logging level to DEBUG" |
| 75 | ) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 76 | 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 Savatieiev | e47f7f4 | 2019-02-20 16:41:23 -0600 | [diff] [blame] | 93 | pkg_report_parser.set_defaults(func=pkg_check) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 94 | |
| 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 Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame] | 122 | # Handle options |
| 123 | if args.debug: |
| 124 | logger_cli.setLevel(DEBUG) |
| 125 | else: |
| 126 | logger_cli.setLevel(INFO) |
| 127 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 128 | # Execute the command |
| 129 | result = args.func(args) |
| 130 | |
| 131 | logger.debug(result) |
| 132 | return |
| 133 | |
| 134 | if __name__ == '__main__': |
Alex Savatieiev | e47f7f4 | 2019-02-20 16:41:23 -0600 | [diff] [blame] | 135 | try: |
| 136 | config_check_entrypoint() |
| 137 | except Exception as e: |
Alex Savatieiev | e961399 | 2019-02-21 18:20:35 -0600 | [diff] [blame^] | 138 | 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 | )) |