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