2019-12-07 11:58:51 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import logging
|
2019-12-08 11:28:25 +00:00
|
|
|
import importlib
|
2019-12-14 15:23:31 +00:00
|
|
|
import multiprocessing as mp
|
2019-12-21 09:36:55 +00:00
|
|
|
import sys
|
|
|
|
|
2019-12-22 08:47:16 +00:00
|
|
|
import colorama
|
|
|
|
|
2019-12-22 08:14:42 +00:00
|
|
|
from logging.handlers import SysLogHandler
|
|
|
|
|
2019-12-22 07:26:48 +00:00
|
|
|
from ucloud.configure.main import configure_parser
|
2019-12-14 15:23:31 +00:00
|
|
|
|
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):
|
2019-12-22 07:26:48 +00:00
|
|
|
logger.error(
|
|
|
|
'Uncaught exception',
|
|
|
|
exc_info=(exc_type, exc_value, exc_traceback)
|
|
|
|
)
|
2019-12-22 08:14:42 +00:00
|
|
|
# print('Error: ', end='')
|
|
|
|
# print(exc_type, exc_value, exc_traceback)
|
|
|
|
|
2019-12-07 11:58:51 +00:00
|
|
|
|
2019-12-22 08:14:42 +00:00
|
|
|
class NoTracebackStreamHandler(logging.StreamHandler):
|
|
|
|
def handle(self, record):
|
|
|
|
info, cache = record.exc_info, record.exc_text
|
|
|
|
record.exc_info, record.exc_text = None, None
|
2019-12-22 08:47:16 +00:00
|
|
|
|
|
|
|
if record.levelname == 'WARNING':
|
|
|
|
color = colorama.Fore.YELLOW
|
|
|
|
elif record.levelname == 'ERROR':
|
|
|
|
color = colorama.Fore.LIGHTRED_EX
|
|
|
|
else:
|
|
|
|
color = colorama.Fore.RED
|
2019-12-22 08:14:42 +00:00
|
|
|
try:
|
2019-12-22 08:47:16 +00:00
|
|
|
print(color)
|
2019-12-22 08:14:42 +00:00
|
|
|
super().handle(record)
|
|
|
|
finally:
|
|
|
|
record.exc_info = info
|
|
|
|
record.exc_text = cache
|
2019-12-22 08:47:16 +00:00
|
|
|
print(colorama.Style.RESET_ALL)
|
2019-12-21 09:36:55 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.excepthook = exception_hook
|
2019-12-07 11:58:51 +00:00
|
|
|
|
2019-12-21 09:36:55 +00:00
|
|
|
arg_parser = argparse.ArgumentParser()
|
|
|
|
subparsers = arg_parser.add_subparsers(dest="command")
|
|
|
|
|
|
|
|
api_parser = subparsers.add_parser("api")
|
|
|
|
|
|
|
|
host_parser = subparsers.add_parser("host")
|
|
|
|
host_parser.add_argument("--hostname", required=True)
|
2019-12-08 13:15:36 +00:00
|
|
|
|
2019-12-21 09:36:55 +00:00
|
|
|
scheduler_parser = subparsers.add_parser("scheduler")
|
|
|
|
|
|
|
|
filescanner_parser = subparsers.add_parser("filescanner")
|
|
|
|
|
|
|
|
imagescanner_parser = subparsers.add_parser("imagescanner")
|
|
|
|
|
|
|
|
metadata_parser = subparsers.add_parser("metadata")
|
|
|
|
|
|
|
|
config_parser = subparsers.add_parser("configure")
|
|
|
|
configure_parser(config_parser)
|
|
|
|
args = arg_parser.parse_args()
|
2019-12-07 11:58:51 +00:00
|
|
|
|
2019-12-21 09:36:55 +00:00
|
|
|
if not args.command:
|
|
|
|
arg_parser.print_help()
|
|
|
|
else:
|
2019-12-22 08:14:42 +00:00
|
|
|
# Setting up root logger
|
|
|
|
logger = logging.getLogger('ucloud')
|
|
|
|
|
|
|
|
syslog_handler = SysLogHandler(address='/dev/log')
|
|
|
|
syslog_handler.setLevel(logging.DEBUG)
|
|
|
|
syslog_formatter = logging.Formatter('%(pathname)s:%(lineno)d -- %(levelname)-8s %(message)s')
|
|
|
|
syslog_handler.setFormatter(syslog_formatter)
|
|
|
|
|
|
|
|
stream_handler = NoTracebackStreamHandler()
|
|
|
|
stream_handler.setLevel(logging.WARNING)
|
|
|
|
stream_formatter = logging.Formatter('%(message)s')
|
|
|
|
stream_handler.setFormatter(stream_formatter)
|
|
|
|
|
|
|
|
logger.addHandler(syslog_handler)
|
|
|
|
logger.addHandler(stream_handler)
|
|
|
|
|
|
|
|
# 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.
|
2019-12-22 07:26:48 +00:00
|
|
|
mp.set_start_method('spawn')
|
|
|
|
|
2019-12-21 09:36:55 +00:00
|
|
|
arguments = vars(args)
|
|
|
|
try:
|
|
|
|
name = arguments.pop('command')
|
|
|
|
mod = importlib.import_module("ucloud.{}.main".format(name))
|
|
|
|
main = getattr(mod, "main")
|
|
|
|
main(**arguments)
|
2019-12-22 08:14:42 +00:00
|
|
|
except Exception as err:
|
|
|
|
logger.exception(err)
|