2019-12-07 11:58:51 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import logging
|
2020-01-03 13:38:59 +00:00
|
|
|
import sys
|
2019-12-08 11:28:25 +00:00
|
|
|
import importlib
|
2020-01-03 13:38:59 +00:00
|
|
|
import argparse
|
2019-12-14 15:23:31 +00:00
|
|
|
|
2019-12-31 11:15:05 +00:00
|
|
|
from uncloud import UncloudException
|
2020-01-03 13:38:59 +00:00
|
|
|
|
2020-01-10 10:30:23 +00:00
|
|
|
# the components that use etcd
|
2020-01-10 10:45:48 +00:00
|
|
|
ETCD_COMPONENTS = ['api', 'scheduler', 'host', 'filescanner', 'imagescanner', 'metadata', 'configure']
|
2020-01-10 10:30:23 +00:00
|
|
|
|
|
|
|
ALL_COMPONENTS = ETCD_COMPONENTS.copy()
|
2020-01-10 11:42:07 +00:00
|
|
|
ALL_COMPONENTS.append('cli')
|
2019-12-08 11:28:25 +00:00
|
|
|
|
2020-01-10 10:45:48 +00:00
|
|
|
|
2019-12-21 09:36:55 +00:00
|
|
|
def exception_hook(exc_type, exc_value, exc_traceback):
|
2019-12-29 18:14:39 +00:00
|
|
|
logging.getLogger(__name__).error(
|
2019-12-22 07:26:48 +00:00
|
|
|
'Uncaught exception',
|
|
|
|
exc_info=(exc_type, exc_value, exc_traceback)
|
|
|
|
)
|
2019-12-07 11:58:51 +00:00
|
|
|
|
2019-12-22 08:47:16 +00:00
|
|
|
|
2019-12-23 07:58:04 +00:00
|
|
|
sys.excepthook = exception_hook
|
2019-12-21 09:36:55 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2019-12-29 18:14:39 +00:00
|
|
|
# Setting up root logger
|
|
|
|
logger = logging.getLogger()
|
2019-12-31 10:56:28 +00:00
|
|
|
logger.setLevel(logging.DEBUG)
|
2019-12-29 18:14:39 +00:00
|
|
|
|
2019-12-21 09:36:55 +00:00
|
|
|
arg_parser = argparse.ArgumentParser()
|
2020-01-03 13:38:59 +00:00
|
|
|
subparsers = arg_parser.add_subparsers(dest='command')
|
2019-12-31 10:35:51 +00:00
|
|
|
|
2020-01-03 13:38:59 +00:00
|
|
|
parent_parser = argparse.ArgumentParser(add_help=False)
|
2020-01-10 10:56:47 +00:00
|
|
|
parent_parser.add_argument('--debug', '-d',
|
|
|
|
action='store_true',
|
|
|
|
default=False,
|
2020-01-03 13:38:59 +00:00
|
|
|
help='More verbose logging')
|
2020-01-10 10:56:47 +00:00
|
|
|
parent_parser.add_argument('--conf-dir', '-c',
|
|
|
|
help='Configuration directory')
|
2019-12-31 14:35:49 +00:00
|
|
|
|
2020-01-10 10:30:23 +00:00
|
|
|
etcd_parser = argparse.ArgumentParser(add_help=False)
|
|
|
|
etcd_parser.add_argument('--etcd-host')
|
|
|
|
etcd_parser.add_argument('--etcd-port')
|
2020-01-10 10:45:48 +00:00
|
|
|
etcd_parser.add_argument('--etcd-ca-cert', help='CA that signed the etcd certificate')
|
|
|
|
etcd_parser.add_argument('--etcd-cert-cert', help='Path to client certificate')
|
|
|
|
etcd_parser.add_argument('--etcd-cert-key', help='Path to client certificate key')
|
2020-01-10 10:30:23 +00:00
|
|
|
|
|
|
|
for component in ALL_COMPONENTS:
|
2020-01-03 13:38:59 +00:00
|
|
|
mod = importlib.import_module('uncloud.{}.main'.format(component))
|
|
|
|
parser = getattr(mod, 'arg_parser')
|
2020-01-10 10:30:23 +00:00
|
|
|
|
|
|
|
if component in ETCD_COMPONENTS:
|
|
|
|
subparsers.add_parser(name=parser.prog, parents=[parser, parent_parser, etcd_parser])
|
|
|
|
else:
|
|
|
|
subparsers.add_parser(name=parser.prog, parents=[parser, parent_parser])
|
|
|
|
|
2019-12-21 09:36:55 +00:00
|
|
|
args = arg_parser.parse_args()
|
|
|
|
if not args.command:
|
|
|
|
arg_parser.print_help()
|
|
|
|
else:
|
|
|
|
arguments = vars(args)
|
2020-01-10 10:35:04 +00:00
|
|
|
name = arguments.pop('command')
|
|
|
|
mod = importlib.import_module('uncloud.{}.main'.format(name))
|
|
|
|
main = getattr(mod, 'main')
|
|
|
|
|
2020-01-10 10:43:53 +00:00
|
|
|
# If the component requires etcd3, we import it and catch the
|
|
|
|
# etcd3.exceptions.ConnectionFailedError
|
|
|
|
if name in ETCD_COMPONENTS:
|
2020-01-10 10:35:04 +00:00
|
|
|
import etcd3
|
2020-01-10 10:43:53 +00:00
|
|
|
|
2019-12-21 09:36:55 +00:00
|
|
|
try:
|
2020-01-10 10:30:23 +00:00
|
|
|
main(arguments)
|
2019-12-31 11:15:05 +00:00
|
|
|
except UncloudException as err:
|
|
|
|
logger.error(err)
|
2020-01-10 09:07:01 +00:00
|
|
|
sys.exit(1)
|
2019-12-22 08:14:42 +00:00
|
|
|
except Exception as err:
|
|
|
|
logger.exception(err)
|
2020-01-10 09:07:01 +00:00
|
|
|
sys.exit(1)
|