uncloud-mravi/scripts/uncloud

97 lines
3.2 KiB
Text
Raw Normal View History

#!/usr/bin/env python3
import logging
2020-01-03 18:38:59 +05:00
import sys
2019-12-08 12:28:25 +01:00
import importlib
2020-01-03 18:38:59 +05:00
import argparse
import multiprocessing as mp
2019-12-31 12:15:05 +01:00
from uncloud import UncloudException
2020-01-03 18:38:59 +05:00
from contextlib import suppress
# the components that use etcd
ETCD_COMPONENTS= ['api',
'scheduler',
'host',
'filescanner',
'imagescanner',
'metadata',
'configure' ]
ALL_COMPONENTS = ETCD_COMPONENTS.copy()
ALL_COMPONENTS.append('cli')
2019-12-08 12:28:25 +01:00
2019-12-21 14:36:55 +05:00
def exception_hook(exc_type, exc_value, exc_traceback):
logging.getLogger(__name__).error(
'Uncaught exception',
exc_info=(exc_type, exc_value, exc_traceback)
)
2019-12-22 13:47:16 +05:00
sys.excepthook = exception_hook
2019-12-21 14:36:55 +05:00
if __name__ == '__main__':
# Setting up root logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
2019-12-21 14:36:55 +05:00
arg_parser = argparse.ArgumentParser()
2020-01-03 18:38:59 +05:00
subparsers = arg_parser.add_subparsers(dest='command')
2020-01-03 18:38:59 +05:00
parent_parser = argparse.ArgumentParser(add_help=False)
2020-01-10 11:56:47 +01:00
parent_parser.add_argument('--debug', '-d',
action='store_true',
default=False,
2020-01-03 18:38:59 +05:00
help='More verbose logging')
2020-01-10 11:56:47 +01:00
parent_parser.add_argument('--conf-dir', '-c',
help='Configuration directory')
2019-12-31 15:35:49 +01:00
etcd_parser = argparse.ArgumentParser(add_help=False)
etcd_parser.add_argument('--etcd-host')
etcd_parser.add_argument('--etcd-port')
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")
for component in ALL_COMPONENTS:
2020-01-03 18:38:59 +05:00
mod = importlib.import_module('uncloud.{}.main'.format(component))
parser = getattr(mod, 'arg_parser')
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 14:36:55 +05:00
args = arg_parser.parse_args()
if not args.command:
arg_parser.print_help()
else:
# if we start etcd in seperate process with default settings
# i.e inheriting few things from parent process etcd3 module
# errors out, so the following command configure multiprocessing
# module to not inherit anything from parent.
# mp.set_start_method('spawn')
2019-12-21 14:36:55 +05:00
arguments = vars(args)
name = arguments.pop('command')
mod = importlib.import_module('uncloud.{}.main'.format(name))
main = getattr(mod, 'main')
2020-01-10 11:43:53 +01:00
# If the component requires etcd3, we import it and catch the
# etcd3.exceptions.ConnectionFailedError
if name in ETCD_COMPONENTS:
import etcd3
2020-01-10 11:43:53 +01:00
2019-12-21 14:36:55 +05:00
try:
main(arguments)
2019-12-31 12:15:05 +01:00
except UncloudException as err:
logger.error(err)
sys.exit(1)
2020-01-10 11:43:53 +01:00
except etcd3.exceptions.ConnectionFailedError as err:
logger.error("Cannot connect to etcd")
except Exception as err:
logger.exception(err)
sys.exit(1)