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