Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 1 | import argparse |
| 2 | import os |
| 3 | import sys |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame^] | 4 | from logging import INFO, DEBUG |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 5 | |
| 6 | import reporter |
| 7 | from cfg_checker.common import utils, const |
| 8 | from cfg_checker.common import config, logger, logger_cli, pkg_dir |
| 9 | from cfg_checker.clients import salt |
| 10 | |
| 11 | from cfg_checker.pkg_check import CloudPackageChecker |
| 12 | |
| 13 | pkg_dir = os.path.dirname(__file__) |
| 14 | pkg_dir = os.path.normpath(pkg_dir) |
| 15 | |
| 16 | |
| 17 | class 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 Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 23 | def 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 | |
| 32 | def pkg_check(args): |
| 33 | # create package versions report |
Alex Savatieiev | e47f7f4 | 2019-02-20 16:41:23 -0600 | [diff] [blame] | 34 | if args.file: |
| 35 | _filename = args.file |
| 36 | else: |
| 37 | logger_cli.error("ERROR: no report filename supplied") |
| 38 | return |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 39 | # 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 | |
| 49 | def net_check(args): |
| 50 | print("This is net check routine") |
| 51 | |
| 52 | return |
| 53 | |
| 54 | |
| 55 | def net_report(args): |
| 56 | print("This is net check routine") |
| 57 | |
| 58 | return |
| 59 | |
| 60 | |
| 61 | def config_check_entrypoint(): |
| 62 | # Main entrypointр |
| 63 | parser = MyParser(prog="Cloud configuration checker") |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame^] | 64 | parser.add_argument( |
| 65 | "-d", |
| 66 | "--debug", |
| 67 | action="store_true", default=False, |
| 68 | help="Set CLI logging level to DEBUG" |
| 69 | ) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 70 | 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 Savatieiev | e47f7f4 | 2019-02-20 16:41:23 -0600 | [diff] [blame] | 87 | pkg_report_parser.set_defaults(func=pkg_check) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 88 | |
| 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 Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame^] | 116 | # Handle options |
| 117 | if args.debug: |
| 118 | logger_cli.setLevel(DEBUG) |
| 119 | else: |
| 120 | logger_cli.setLevel(INFO) |
| 121 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 122 | # Execute the command |
| 123 | result = args.func(args) |
| 124 | |
| 125 | logger.debug(result) |
| 126 | return |
| 127 | |
| 128 | if __name__ == '__main__': |
Alex Savatieiev | e47f7f4 | 2019-02-20 16:41:23 -0600 | [diff] [blame] | 129 | try: |
| 130 | config_check_entrypoint() |
| 131 | except Exception as e: |
| 132 | logger_cli.error("ERROR: {}".format(e.message)) |