uncloud-mravi/scripts/uncloud

95 lines
3.5 KiB
Text
Raw Normal View History

#!/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
import os
from etcd3.exceptions import ConnectionFailedError
from uncloud.common import settings
from uncloud import UncloudException
from uncloud.common.cli import resolve_otp_credentials
2019-12-08 11:28:25 +00:00
2019-12-21 09:36:55 +00: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 08:47:16 +00:00
sys.excepthook = exception_hook
2019-12-21 09:36:55 +00:00
# the components that use etcd
ETCD_COMPONENTS = ['api', 'scheduler', 'host', 'filescanner',
'imagescanner', 'metadata', 'configure', 'hack']
ALL_COMPONENTS = ETCD_COMPONENTS.copy()
#ALL_COMPONENTS.append('cli')
2019-12-21 09:36:55 +00:00
if __name__ == '__main__':
# Setting up root logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
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')
2020-01-03 13:38:59 +00:00
parent_parser = argparse.ArgumentParser(add_help=False)
parent_parser.add_argument('--debug', '-d', action='store_true', default=False,
2020-01-03 13:38:59 +00:00
help='More verbose logging')
parent_parser.add_argument('--conf-dir', '-c', help='Configuration directory',
default=os.path.expanduser('~/uncloud'))
2019-12-31 14:35:49 +00:00
etcd_parser = argparse.ArgumentParser(add_help=False)
2020-01-14 13:23:26 +00:00
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 13:38:59 +00: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])
arguments = vars(arg_parser.parse_args())
etcd_arguments = [key for key, value in arguments.items() if key.startswith('etcd_') and value]
etcd_arguments = {
'etcd': {
key.replace('etcd_', ''): arguments[key]
for key in etcd_arguments
}
}
if not arguments['command']:
2019-12-21 09:36:55 +00:00
arg_parser.print_help()
else:
# Initializing Settings and resolving otp_credentials
# It is neccessary to resolve_otp_credentials after argument parsing is done because
# previously we were reading config file which was fixed to ~/uncloud/uncloud.conf and
# providing the default values for --name, --realm and --seed arguments from the values
# we read from file. But, now we are asking user about where the config file lives. So,
# to providing default value is not possible before parsing arguments. So, we are doing
# it after..
# settings.settings = settings.Settings(arguments['conf_dir'], seed_value=etcd_arguments)
# resolve_otp_credentials(arguments)
name = arguments.pop('command')
mod = importlib.import_module('uncloud.{}.main'.format(name))
main = getattr(mod, 'main')
2019-12-21 09:36:55 +00:00
try:
main(arguments)
2019-12-31 11:15:05 +00:00
except UncloudException as err:
logger.error(err)
2020-01-14 13:23:26 +00:00
# except ConnectionFailedError as err:
# logger.error('Cannot connect to etcd: {}'.format(err))
except Exception as err:
logger.exception(err)