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