Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 1 | import os |
| 2 | import sys |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 3 | from logging import DEBUG, INFO |
Alex Savatieiev | 06ab17d | 2019-02-26 18:40:48 -0600 | [diff] [blame] | 4 | |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 5 | from cfg_checker.cli.arguments import add_global_arguments |
Alex | ac2a273 | 2020-09-11 11:00:26 -0500 | [diff] [blame] | 6 | from cfg_checker.cli.command import execute_command, helps, parsers, \ |
| 7 | parsers_inits |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 8 | from cfg_checker.common.settings import CheckerConfiguration |
| 9 | from cfg_checker.common import logger, logger_cli |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 10 | from cfg_checker.helpers.args_utils import MyParser |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 11 | |
| 12 | pkg_dir = os.path.dirname(__file__) |
| 13 | pkg_dir = os.path.normpath(pkg_dir) |
| 14 | |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 15 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 16 | def config_check_entrypoint(): |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 17 | """ |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 18 | Main entry point. Uses nested parsers structure |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 19 | with a default function to execute the comand |
| 20 | |
| 21 | :return: - no return value |
| 22 | """ |
Alex Savatieiev | 4c40632 | 2019-02-28 17:37:09 -0600 | [diff] [blame] | 23 | # Main entrypoint |
| 24 | parser = MyParser(prog="# Mirantis Cloud configuration checker") |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 25 | add_global_arguments(parser) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 26 | subparsers = parser.add_subparsers(dest='command') |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 27 | |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 28 | # create parsers |
| 29 | for _command in helps.keys(): |
| 30 | _parser = subparsers.add_parser( |
| 31 | _command, |
| 32 | help=helps[_command] |
| 33 | ) |
Alex | ac2a273 | 2020-09-11 11:00:26 -0500 | [diff] [blame] | 34 | parsers[_command] = parsers_inits[_command](_parser) |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 35 | |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 36 | # parse arguments |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 37 | try: |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 38 | args, unknown = parser.parse_known_args() |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 39 | except TypeError: |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 40 | logger_cli.info("\n# Please, check arguments") |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 41 | sys.exit(1) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 42 | |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 43 | if unknown: |
| 44 | logger_cli.error( |
| 45 | "# Unexpected arguments: {}".format( |
| 46 | ", ".join(["'{}'".format(a) for a in unknown]) |
| 47 | ) |
| 48 | ) |
| 49 | sys.exit(1) |
| 50 | |
Alex | ac2a273 | 2020-09-11 11:00:26 -0500 | [diff] [blame] | 51 | if not args.command: |
| 52 | parser.print_help() |
| 53 | logger_cli.info("\n# Please, type a command listed above") |
| 54 | sys.exit(1) |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 55 | |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 56 | # Handle debug option before config init |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame] | 57 | if args.debug: |
| 58 | logger_cli.setLevel(DEBUG) |
| 59 | else: |
| 60 | logger_cli.setLevel(INFO) |
| 61 | |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 62 | # Init the config and pass externally configured values |
| 63 | config = CheckerConfiguration(args) |
| 64 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 65 | # Execute the command |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 66 | result = execute_command(args, args.command, config) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 67 | logger.debug(result) |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 68 | sys.exit(result) |
Alex Savatieiev | 4c40632 | 2019-02-28 17:37:09 -0600 | [diff] [blame] | 69 | |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 70 | |
Alex Savatieiev | 4c40632 | 2019-02-28 17:37:09 -0600 | [diff] [blame] | 71 | if __name__ == '__main__': |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 72 | config_check_entrypoint() |