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 | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 3 | import pkgutil |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 4 | import sys |
| 5 | import traceback |
| 6 | |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 7 | from cfg_checker.cli.arguments import add_global_arguments |
| 8 | from cfg_checker.common import logger, logger_cli |
| 9 | from cfg_checker.common.exception import CheckerException, \ |
| 10 | CommandNotSupportedException |
| 11 | from cfg_checker.common.settings import CheckerConfiguration |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 12 | from cfg_checker.helpers.args_utils import MyParser |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 13 | |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 14 | main_pkg_name = __name__.split('.')[0] |
| 15 | mods_package_name = "modules" |
| 16 | mods_import_path = main_pkg_name + '.' + mods_package_name |
| 17 | mods_prefix = mods_import_path + '.' |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 18 | |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 19 | commands = {} |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 20 | supports = {} |
Alex | ac2a273 | 2020-09-11 11:00:26 -0500 | [diff] [blame] | 21 | parsers_inits = {} |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 22 | parsers = {} |
| 23 | helps = {} |
| 24 | # Pure dynamic magic, loading all 'do_*' methods from available modules |
| 25 | _m = __import__(mods_import_path, fromlist=[main_pkg_name]) |
| 26 | for _imp, modName, isMod in pkgutil.iter_modules(_m.__path__, mods_prefix): |
| 27 | # iterate all packages, add to dict |
| 28 | if isMod: |
| 29 | # load module |
| 30 | _p = _imp.find_module(modName).load_module(modName) |
| 31 | # create a shortname |
| 32 | mod_name = modName.split('.')[-1] |
| 33 | # A package! Create it and add commands |
| 34 | commands[mod_name] = \ |
| 35 | [_n[3:] for _n in dir(_p) if _n.startswith("do_")] |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 36 | supports[mod_name] = _p.supported_envs |
Alex | ac2a273 | 2020-09-11 11:00:26 -0500 | [diff] [blame] | 37 | parsers_inits[mod_name] = getattr(_p, 'init_parser') |
| 38 | parsers[mod_name] = {} |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 39 | helps[mod_name] = getattr(_p, 'command_help') |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 40 | |
| 41 | |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 42 | def execute_command(args, command, config): |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 43 | # Validate the commands |
Alex | cf91b18 | 2019-05-31 11:57:07 -0500 | [diff] [blame] | 44 | # check commands |
Alex | ac2a273 | 2020-09-11 11:00:26 -0500 | [diff] [blame] | 45 | if command not in commands: |
Alex | ac2a273 | 2020-09-11 11:00:26 -0500 | [diff] [blame] | 46 | logger_cli.info("\n# Please, type a command listed above") |
| 47 | return 1 |
Alex | cf91b18 | 2019-05-31 11:57:07 -0500 | [diff] [blame] | 48 | if not hasattr(args, 'type') or not args.type: |
Alex | ac2a273 | 2020-09-11 11:00:26 -0500 | [diff] [blame] | 49 | parsers[command].print_help() |
Alex | cf91b18 | 2019-05-31 11:57:07 -0500 | [diff] [blame] | 50 | logger_cli.info("\n# Please, type a command listed above") |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 51 | return 1 |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 52 | _type = args.type.replace("-", "_") if "-" in args.type else args.type |
Alex | ac2a273 | 2020-09-11 11:00:26 -0500 | [diff] [blame] | 53 | if _type not in commands[command]: |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 54 | # check type |
| 55 | logger_cli.info( |
| 56 | "\n# Please, select '{}' command type listed above".format( |
| 57 | command |
| 58 | ) |
| 59 | ) |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 60 | return 1 |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 61 | else: |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 62 | # check if command is supported |
| 63 | if len( |
| 64 | set(supports[command]).intersection(set(config.detected_envs)) |
| 65 | ) < 1: |
| 66 | raise CommandNotSupportedException( |
| 67 | "'{}' is not supported on any of the currently " |
| 68 | "detected environments: {}".format( |
| 69 | command, |
| 70 | ",".join(config.detected_envs) |
| 71 | ) |
| 72 | ) |
| 73 | return 1 |
| 74 | |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 75 | # form function name to call |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 76 | _method_name = "do_" + _type |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 77 | _target_module = __import__( |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 78 | mods_prefix + command, |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 79 | fromlist=[""] |
| 80 | ) |
| 81 | _method = getattr(_target_module, _method_name) |
| 82 | |
| 83 | # Execute the command |
| 84 | try: |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 85 | # Compatibility between entrypoints |
| 86 | if not hasattr(args, 'command'): |
| 87 | args.command = command |
| 88 | _method(args, config) |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 89 | return 0 |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 90 | except CommandNotSupportedException as e: |
| 91 | logger_cli.error("\n{}".format(e.message)) |
| 92 | return 1 |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 93 | except CheckerException as e: |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 94 | logger_cli.error("\n{}".format(e.message)) |
Alex | 265f45e | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 95 | |
| 96 | exc_type, exc_value, exc_traceback = sys.exc_info() |
| 97 | logger_cli.debug("\n{}".format( |
| 98 | "".join(traceback.format_exception( |
| 99 | exc_type, |
| 100 | exc_value, |
| 101 | exc_traceback |
| 102 | )) |
| 103 | )) |
| 104 | return 1 |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 105 | |
| 106 | |
| 107 | def cli_command(_title, _name): |
| 108 | my_parser = MyParser(_title) |
Alex | ac2a273 | 2020-09-11 11:00:26 -0500 | [diff] [blame] | 109 | parsers[_name] = parsers_inits[_name](my_parser) |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 110 | |
| 111 | # parse arguments |
| 112 | try: |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 113 | add_global_arguments(my_parser) |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 114 | args, unknown = my_parser.parse_known_args() |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 115 | except TypeError: |
| 116 | logger_cli.info("\n# Please, check arguments") |
Alex | 3bc95f6 | 2020-03-05 17:00:04 -0600 | [diff] [blame] | 117 | sys.exit(1) |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 118 | |
Alex | d0391d4 | 2019-05-21 18:48:55 -0500 | [diff] [blame] | 119 | if unknown: |
| 120 | logger_cli.error( |
| 121 | "# Unexpected arguments: {}".format( |
| 122 | ", ".join(["'{}'".format(a) for a in unknown]) |
| 123 | ) |
| 124 | ) |
| 125 | sys.exit(1) |
| 126 | |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 127 | # Init the config |
| 128 | config = CheckerConfiguration(args) |
| 129 | |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 130 | # force use of sudo |
| 131 | config.ssh_uses_sudo = True |
| 132 | |
| 133 | # Execute the command |
Alex | 9a4ad21 | 2020-10-01 18:04:25 -0500 | [diff] [blame] | 134 | try: |
| 135 | result = execute_command(args, _name, config) |
| 136 | logger.debug(result) |
| 137 | except CommandNotSupportedException as e: |
| 138 | logger_cli.error("\nERROR: {}".format( |
| 139 | e.message |
| 140 | )) |
Alex | bab1efe | 2019-04-23 18:51:23 -0500 | [diff] [blame] | 141 | sys.exit(result) |