blob: 99f8dbe0ac26879eeeeaaf5c69633295ecc80906 [file] [log] [blame]
Alex0989ecf2022-03-29 13:43:21 -05001# Author: Alex Savatieiev (osavatieiev@mirantis.com; a.savex@gmail.com)
2# Copyright 2019-2022 Mirantis, Inc.
Alex Savatieiev5118de02019-02-20 15:50:42 -06003import os
4import sys
Alex3ebc5632019-04-18 16:47:18 -05005from logging import DEBUG, INFO
Alex Savatieiev06ab17d2019-02-26 18:40:48 -06006
Alex9a4ad212020-10-01 18:04:25 -05007from cfg_checker.cli.arguments import add_global_arguments
Alexac2a2732020-09-11 11:00:26 -05008from cfg_checker.cli.command import execute_command, helps, parsers, \
9 parsers_inits
Alex9a4ad212020-10-01 18:04:25 -050010from cfg_checker.common.settings import CheckerConfiguration
11from cfg_checker.common import logger, logger_cli
Alex265f45e2019-04-23 18:51:23 -050012from cfg_checker.helpers.args_utils import MyParser
Alex Savatieiev5118de02019-02-20 15:50:42 -060013
14pkg_dir = os.path.dirname(__file__)
15pkg_dir = os.path.normpath(pkg_dir)
16
Alex Savatieiev9c642112019-02-26 13:55:43 -060017
Alex Savatieiev5118de02019-02-20 15:50:42 -060018def config_check_entrypoint():
Alex Savatieiev9c642112019-02-26 13:55:43 -060019 """
Alex3ebc5632019-04-18 16:47:18 -050020 Main entry point. Uses nested parsers structure
Alex Savatieiev9c642112019-02-26 13:55:43 -060021 with a default function to execute the comand
22
23 :return: - no return value
24 """
Alex Savatieiev4c406322019-02-28 17:37:09 -060025 # Main entrypoint
26 parser = MyParser(prog="# Mirantis Cloud configuration checker")
Alex9a4ad212020-10-01 18:04:25 -050027 add_global_arguments(parser)
Alex Savatieiev5118de02019-02-20 15:50:42 -060028 subparsers = parser.add_subparsers(dest='command')
Alex265f45e2019-04-23 18:51:23 -050029
Alexbab1efe2019-04-23 18:51:23 -050030 # create parsers
31 for _command in helps.keys():
32 _parser = subparsers.add_parser(
33 _command,
34 help=helps[_command]
35 )
Alexac2a2732020-09-11 11:00:26 -050036 parsers[_command] = parsers_inits[_command](_parser)
Alex41485522019-04-12 17:26:18 -050037
Alex3ebc5632019-04-18 16:47:18 -050038 # parse arguments
Alex Savatieiev9c642112019-02-26 13:55:43 -060039 try:
Alexd0391d42019-05-21 18:48:55 -050040 args, unknown = parser.parse_known_args()
Alex3ebc5632019-04-18 16:47:18 -050041 except TypeError:
Alex Savatieievc9055712019-03-01 14:43:56 -060042 logger_cli.info("\n# Please, check arguments")
Alex3bc95f62020-03-05 17:00:04 -060043 sys.exit(1)
Alex Savatieiev5118de02019-02-20 15:50:42 -060044
Alexd0391d42019-05-21 18:48:55 -050045 if unknown:
46 logger_cli.error(
47 "# Unexpected arguments: {}".format(
48 ", ".join(["'{}'".format(a) for a in unknown])
49 )
50 )
51 sys.exit(1)
52
Alexac2a2732020-09-11 11:00:26 -050053 if not args.command:
54 parser.print_help()
55 logger_cli.info("\n# Please, type a command listed above")
56 sys.exit(1)
Alex3ebc5632019-04-18 16:47:18 -050057
Alex9a4ad212020-10-01 18:04:25 -050058 # Handle debug option before config init
Alex Savatieiev799bee32019-02-20 17:19:26 -060059 if args.debug:
60 logger_cli.setLevel(DEBUG)
61 else:
62 logger_cli.setLevel(INFO)
63
Alex9a4ad212020-10-01 18:04:25 -050064 # Init the config and pass externally configured values
65 config = CheckerConfiguration(args)
66
Alex Savatieiev5118de02019-02-20 15:50:42 -060067 # Execute the command
Alex9a4ad212020-10-01 18:04:25 -050068 result = execute_command(args, args.command, config)
Alex Savatieiev5118de02019-02-20 15:50:42 -060069 logger.debug(result)
Alex265f45e2019-04-23 18:51:23 -050070 sys.exit(result)
Alex Savatieiev4c406322019-02-28 17:37:09 -060071
Alex3ebc5632019-04-18 16:47:18 -050072
Alex Savatieiev4c406322019-02-28 17:37:09 -060073if __name__ == '__main__':
Alex265f45e2019-04-23 18:51:23 -050074 config_check_entrypoint()