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