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 | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 5 | from cfg_checker.cli.command import execute_command, helps, parsers |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 6 | from cfg_checker.common import config, logger, logger_cli |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 7 | from cfg_checker.helpers.args_utils import MyParser |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 8 | |
| 9 | pkg_dir = os.path.dirname(__file__) |
| 10 | pkg_dir = os.path.normpath(pkg_dir) |
| 11 | |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 12 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 13 | def config_check_entrypoint(): |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 14 | """ |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 15 | Main entry point. Uses nested parsers structure |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 16 | with a default function to execute the comand |
| 17 | |
| 18 | :return: - no return value |
| 19 | """ |
Alex Savatieiev | 4c40632 | 2019-02-28 17:37:09 -0600 | [diff] [blame] | 20 | # Main entrypoint |
| 21 | parser = MyParser(prog="# Mirantis Cloud configuration checker") |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 22 | |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame] | 23 | parser.add_argument( |
| 24 | "-d", |
| 25 | "--debug", |
| 26 | action="store_true", default=False, |
| 27 | help="Set CLI logging level to DEBUG" |
| 28 | ) |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 29 | parser.add_argument( |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 30 | '-s', |
| 31 | '--sudo', |
| 32 | action='store_true', default=True, |
| 33 | help="Use sudo for getting salt creds" |
| 34 | ) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 35 | subparsers = parser.add_subparsers(dest='command') |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 36 | |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 37 | # create parsers |
| 38 | for _command in helps.keys(): |
| 39 | _parser = subparsers.add_parser( |
| 40 | _command, |
| 41 | help=helps[_command] |
| 42 | ) |
| 43 | parsers[_command](_parser) |
Alex | 4148552 | 2019-04-12 17:26:18 -0500 | [diff] [blame] | 44 | |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 45 | # parse arguments |
Alex Savatieiev | 9c64211 | 2019-02-26 13:55:43 -0600 | [diff] [blame] | 46 | try: |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 47 | args, unknown = parser.parse_known_args() |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 48 | except TypeError: |
Alex Savatieiev | c905571 | 2019-03-01 14:43:56 -0600 | [diff] [blame] | 49 | logger_cli.info("\n# Please, check arguments") |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 50 | sys.exit(0) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 51 | |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 52 | if unknown: |
| 53 | logger_cli.error( |
| 54 | "# Unexpected arguments: {}".format( |
| 55 | ", ".join(["'{}'".format(a) for a in unknown]) |
| 56 | ) |
| 57 | ) |
| 58 | sys.exit(1) |
| 59 | |
Alex Savatieiev | 6357683 | 2019-02-27 15:46:26 -0600 | [diff] [blame] | 60 | # Pass externally configured values |
| 61 | config.ssh_uses_sudo = args.sudo |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 62 | |
Alex Savatieiev | 799bee3 | 2019-02-20 17:19:26 -0600 | [diff] [blame] | 63 | # Handle options |
| 64 | if args.debug: |
| 65 | logger_cli.setLevel(DEBUG) |
| 66 | else: |
| 67 | logger_cli.setLevel(INFO) |
| 68 | |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 69 | # Execute the command |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 70 | result = execute_command(args, args.command) |
Alex Savatieiev | 5118de0 | 2019-02-20 15:50:42 -0600 | [diff] [blame] | 71 | logger.debug(result) |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 72 | sys.exit(result) |
Alex Savatieiev | 4c40632 | 2019-02-28 17:37:09 -0600 | [diff] [blame] | 73 | |
Alex | 3ebc563 | 2019-04-18 16:47:18 -0500 | [diff] [blame] | 74 | |
Alex Savatieiev | 4c40632 | 2019-02-28 17:37:09 -0600 | [diff] [blame] | 75 | if __name__ == '__main__': |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 76 | config_check_entrypoint() |