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 | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame^] | 5 | from logging import DEBUG, INFO |
Alex Savatieiev | 06ab17d | 2019-02-26 18:40:48 -0600 | [diff] [blame] | 6 | |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 7 | from cfg_checker.common import config, logger, logger_cli |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame^] | 8 | from cfg_checker.common.exception import CheckerException |
Alex Savatieiev | 06ab17d | 2019-02-26 18:40:48 -0600 | [diff] [blame] | 9 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 10 | |
| 11 | pkg_dir = os.path.dirname(__file__) |
| 12 | pkg_dir = os.path.normpath(pkg_dir) |
| 13 | |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 14 | commands = { |
| 15 | 'packages': ['report'], |
| 16 | 'network': ['check', 'report'], |
| 17 | 'reclass': ['list', 'diff'] |
| 18 | } |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 19 | |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame^] | 20 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 21 | class MyParser(argparse.ArgumentParser): |
| 22 | def error(self, message): |
| 23 | sys.stderr.write('Error: {0}\n\n'.format(message)) |
| 24 | self.print_help() |
| 25 | |
| 26 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 27 | def help_message(): |
| 28 | print""" |
| 29 | Please, use following examples to generate info reports:\n |
| 30 | cfg_checker packages report\n |
| 31 | cfg_checker network check\n |
| 32 | cfg_checker network report\n |
| 33 | """ |
| 34 | return |
| 35 | |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 36 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 37 | def config_check_entrypoint(): |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 38 | """ |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame^] | 39 | Main entry point. Uses nested parsers structure |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 40 | with a default function to execute the comand |
| 41 | |
| 42 | :return: - no return value |
| 43 | """ |
Alex Savatieiev | 4c40632 | 2019-02-28 17:37:09 -0600 | [diff] [blame] | 44 | # Main entrypoint |
| 45 | parser = MyParser(prog="# Mirantis Cloud configuration checker") |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame^] | 46 | |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 47 | # Parsers (each parser can have own arguments) |
| 48 | # - subparsers (command) |
| 49 | # |- pkg_parser |
| 50 | # | - pkg_subparsers (type) |
| 51 | # | - pkg_report_parser (default func - pkg_check) |
| 52 | # |- net_parser |
| 53 | # | - net_subparsers (type) |
| 54 | # | - net_check_parser (default func - net_check) |
| 55 | # | - net_report_parser (default func - net_report) |
| 56 | # - reclass_parser |
| 57 | # - reclass_list (default func - reclass_list) |
| 58 | # - reclass_compare (default func - reclass_diff) |
| 59 | |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame] | 60 | parser.add_argument( |
| 61 | "-d", |
| 62 | "--debug", |
| 63 | action="store_true", default=False, |
| 64 | help="Set CLI logging level to DEBUG" |
| 65 | ) |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 66 | parser.add_argument( |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 67 | '-s', |
| 68 | '--sudo', |
| 69 | action='store_true', default=True, |
| 70 | help="Use sudo for getting salt creds" |
| 71 | ) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 72 | subparsers = parser.add_subparsers(dest='command') |
| 73 | # packages |
| 74 | pkg_parser = subparsers.add_parser( |
| 75 | 'packages', |
| 76 | help="Package versions check (Candidate vs Installed)" |
| 77 | ) |
| 78 | pkg_subparsers = pkg_parser.add_subparsers(dest='type') |
| 79 | |
| 80 | pkg_report_parser = pkg_subparsers.add_parser( |
| 81 | 'report', |
| 82 | help="Report package versions to HTML file" |
| 83 | ) |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 84 | pkg_report_parser.add_argument( |
| 85 | '--full', |
Alex | 682e7df | 2019-04-12 17:51:55 -0500 | [diff] [blame] | 86 | action="store_true", default=False, |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 87 | help="HTML report will have all of the packages, not just errors" |
| 88 | ) |
| 89 | pkg_report_parser.add_argument( |
| 90 | '--html', |
| 91 | metavar='packages_html_filename', |
| 92 | help="HTML filename to save report" |
| 93 | ) |
| 94 | pkg_report_parser.add_argument( |
| 95 | '--csv', |
| 96 | metavar='packages_csv_filename', |
| 97 | help="CSV filename to save report" |
| 98 | ) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 99 | |
| 100 | # networking |
| 101 | net_parser = subparsers.add_parser( |
| 102 | 'network', |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 103 | help="Network infrastructure checks and reports" |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 104 | ) |
| 105 | net_subparsers = net_parser.add_subparsers(dest='type') |
| 106 | |
| 107 | net_check_parser = net_subparsers.add_parser( |
| 108 | 'check', |
| 109 | help="Do network check and print the result" |
| 110 | ) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 111 | |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame^] | 112 | net_check_parser.add_argument( |
| 113 | '--detailed', |
| 114 | action="store_true", default=False, |
| 115 | help="Print error details after summary" |
| 116 | ) |
| 117 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 118 | net_report_parser = net_subparsers.add_parser( |
| 119 | 'report', |
| 120 | help="Generate network check report" |
| 121 | ) |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 122 | |
| 123 | net_report_parser.add_argument( |
| 124 | '--html', |
| 125 | metavar='network_html_filename', |
| 126 | help="HTML filename to save report" |
| 127 | ) |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame^] | 128 | |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 129 | # reclass |
| 130 | reclass_parser = subparsers.add_parser( |
| 131 | 'reclass', |
| 132 | help="Reclass related checks and reports" |
| 133 | ) |
| 134 | reclass_subparsers = reclass_parser.add_subparsers(dest='type') |
| 135 | reclass_list_parser = reclass_subparsers.add_parser( |
| 136 | 'list', |
| 137 | help="List models available to compare" |
| 138 | ) |
Alex Savatieiev | 06ab17d | 2019-02-26 18:40:48 -0600 | [diff] [blame] | 139 | reclass_list_parser.add_argument( |
| 140 | "-p", |
| 141 | "--models-path", |
| 142 | default="/srv/salt/", |
| 143 | help="Global path to search models in" |
| 144 | ) |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 145 | |
| 146 | reclass_diff_parser = reclass_subparsers.add_parser( |
| 147 | 'diff', |
| 148 | help="List models available to compare" |
| 149 | ) |
Alex Savatieiev | 06ab17d | 2019-02-26 18:40:48 -0600 | [diff] [blame] | 150 | reclass_diff_parser.add_argument( |
| 151 | "--model1", |
| 152 | required=True, |
| 153 | help="Model A <path>. Model name is the folder name" |
| 154 | ) |
| 155 | reclass_diff_parser.add_argument( |
| 156 | "--model2", |
| 157 | required=True, |
| 158 | help="Model B <path>. Model name is the folder name" |
| 159 | ) |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 160 | reclass_diff_parser.add_argument( |
| 161 | '--html', |
| 162 | metavar='reclass_html_filename', |
| 163 | help="HTML filename to save report" |
| 164 | ) |
| 165 | |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame^] | 166 | # parse arguments |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 167 | try: |
| 168 | args = parser.parse_args() |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame^] | 169 | except TypeError: |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 170 | logger_cli.info("\n# Please, check arguments") |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 171 | return |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 172 | |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 173 | # Pass externally configured values |
| 174 | config.ssh_uses_sudo = args.sudo |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame^] | 175 | |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame] | 176 | # Handle options |
| 177 | if args.debug: |
| 178 | logger_cli.setLevel(DEBUG) |
| 179 | else: |
| 180 | logger_cli.setLevel(INFO) |
| 181 | |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 182 | # Validate the commands |
| 183 | # check command |
| 184 | if args.command not in commands: |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 185 | logger_cli.info("\n# Please, type a command listed above") |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 186 | return |
| 187 | elif args.type not in commands[args.command]: |
| 188 | # check type |
| 189 | logger_cli.info( |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 190 | "\n# Please, select '{}' command type listed above".format( |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 191 | args.command |
| 192 | ) |
| 193 | ) |
| 194 | return |
| 195 | else: |
| 196 | # form function name to call |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 197 | _method_name = "do_" + args.type |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame^] | 198 | _target_module = __import__( |
| 199 | "cfg_checker.modules."+args.command, |
| 200 | fromlist=[""] |
| 201 | ) |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 202 | _method = getattr(_target_module, _method_name) |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame^] | 203 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 204 | # Execute the command |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 205 | result = _method(args) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 206 | |
| 207 | logger.debug(result) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 208 | |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame^] | 209 | |
Alex Savatieiev | 4c40632 | 2019-02-28 17:37:09 -0600 | [diff] [blame] | 210 | def cli_main(): |
Alex Savatieiev | e47f7f4 | 2019-02-20 16:41:23 -0600 | [diff] [blame] | 211 | try: |
| 212 | config_check_entrypoint() |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 213 | except CheckerException as e: |
Alex Savatieiev | 06ab17d | 2019-02-26 18:40:48 -0600 | [diff] [blame] | 214 | logger_cli.error("\nERROR: {}".format( |
| 215 | e.message |
| 216 | )) |
| 217 | |
Alex Savatieiev | e961399 | 2019-02-21 18:20:35 -0600 | [diff] [blame] | 218 | exc_type, exc_value, exc_traceback = sys.exc_info() |
Alex Savatieiev | 06ab17d | 2019-02-26 18:40:48 -0600 | [diff] [blame] | 219 | logger_cli.debug("\n{}".format( |
Alex Savatieiev | e961399 | 2019-02-21 18:20:35 -0600 | [diff] [blame] | 220 | "".join(traceback.format_exception( |
| 221 | exc_type, |
| 222 | exc_value, |
| 223 | exc_traceback |
| 224 | )) |
| 225 | )) |
Alex Savatieiev | 4c40632 | 2019-02-28 17:37:09 -0600 | [diff] [blame] | 226 | |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame^] | 227 | |
Alex Savatieiev | 4c40632 | 2019-02-28 17:37:09 -0600 | [diff] [blame] | 228 | if __name__ == '__main__': |
| 229 | cli_main() |