blob: 1a2d79fcd33d4b6f7c4425294ea9793d3a76de34 [file] [log] [blame]
Alex Savatieiev5118de02019-02-20 15:50:42 -06001import os
2import sys
Alex3ebc5632019-04-18 16:47:18 -05003from logging import DEBUG, INFO
Alex Savatieiev06ab17d2019-02-26 18:40:48 -06004
Alexbab1efe2019-04-23 18:51:23 -05005from cfg_checker.cli.command import execute_command, helps, parsers
Alex Savatieievc9055712019-03-01 14:43:56 -06006from cfg_checker.common import config, logger, logger_cli
Alex265f45e2019-04-23 18:51:23 -05007from cfg_checker.helpers.args_utils import MyParser
Alex Savatieiev5118de02019-02-20 15:50:42 -06008
9pkg_dir = os.path.dirname(__file__)
10pkg_dir = os.path.normpath(pkg_dir)
11
Alex Savatieiev9c642112019-02-26 13:55:43 -060012
Alex Savatieiev5118de02019-02-20 15:50:42 -060013def config_check_entrypoint():
Alex Savatieiev9c642112019-02-26 13:55:43 -060014 """
Alex3ebc5632019-04-18 16:47:18 -050015 Main entry point. Uses nested parsers structure
Alex Savatieiev9c642112019-02-26 13:55:43 -060016 with a default function to execute the comand
17
18 :return: - no return value
19 """
Alex Savatieiev4c406322019-02-28 17:37:09 -060020 # Main entrypoint
21 parser = MyParser(prog="# Mirantis Cloud configuration checker")
Alex3ebc5632019-04-18 16:47:18 -050022
Alex Savatieiev799bee32019-02-20 17:19:26 -060023 parser.add_argument(
24 "-d",
25 "--debug",
26 action="store_true", default=False,
27 help="Set CLI logging level to DEBUG"
28 )
Alex Savatieiev9c642112019-02-26 13:55:43 -060029 parser.add_argument(
Alex Savatieiev63576832019-02-27 15:46:26 -060030 '-s',
31 '--sudo',
32 action='store_true', default=True,
33 help="Use sudo for getting salt creds"
34 )
Alex Savatieiev5118de02019-02-20 15:50:42 -060035 subparsers = parser.add_subparsers(dest='command')
Alex265f45e2019-04-23 18:51:23 -050036
Alexbab1efe2019-04-23 18:51:23 -050037 # create parsers
38 for _command in helps.keys():
39 _parser = subparsers.add_parser(
40 _command,
41 help=helps[_command]
42 )
43 parsers[_command](_parser)
Alex41485522019-04-12 17:26:18 -050044
Alex3ebc5632019-04-18 16:47:18 -050045 # parse arguments
Alex Savatieiev9c642112019-02-26 13:55:43 -060046 try:
Alexd0391d42019-05-21 18:48:55 -050047 args, unknown = parser.parse_known_args()
Alex3ebc5632019-04-18 16:47:18 -050048 except TypeError:
Alex Savatieievc9055712019-03-01 14:43:56 -060049 logger_cli.info("\n# Please, check arguments")
Alex265f45e2019-04-23 18:51:23 -050050 sys.exit(0)
Alex Savatieiev5118de02019-02-20 15:50:42 -060051
Alexd0391d42019-05-21 18:48:55 -050052 if unknown:
53 logger_cli.error(
54 "# Unexpected arguments: {}".format(
55 ", ".join(["'{}'".format(a) for a in unknown])
56 )
57 )
58 sys.exit(1)
59
Alex Savatieiev63576832019-02-27 15:46:26 -060060 # Pass externally configured values
61 config.ssh_uses_sudo = args.sudo
Alex3ebc5632019-04-18 16:47:18 -050062
Alex Savatieiev799bee32019-02-20 17:19:26 -060063 # Handle options
64 if args.debug:
65 logger_cli.setLevel(DEBUG)
66 else:
67 logger_cli.setLevel(INFO)
68
Alex Savatieiev5118de02019-02-20 15:50:42 -060069 # Execute the command
Alex265f45e2019-04-23 18:51:23 -050070 result = execute_command(args, args.command)
Alex Savatieiev5118de02019-02-20 15:50:42 -060071 logger.debug(result)
Alex265f45e2019-04-23 18:51:23 -050072 sys.exit(result)
Alex Savatieiev4c406322019-02-28 17:37:09 -060073
Alex3ebc5632019-04-18 16:47:18 -050074
Alex Savatieiev4c406322019-02-28 17:37:09 -060075if __name__ == '__main__':
Alex265f45e2019-04-23 18:51:23 -050076 config_check_entrypoint()