forked from uncloud/uncloud
uncloud cli converted to argparse
This commit is contained in:
parent
50fb135726
commit
3296e524cc
13 changed files with 284 additions and 287 deletions
|
|
@ -1,14 +1,13 @@
|
|||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import logging
|
||||
import importlib
|
||||
import multiprocessing as mp
|
||||
import sys
|
||||
|
||||
from logging.handlers import SysLogHandler
|
||||
from uncloud.configure.main import configure_parser
|
||||
import importlib
|
||||
import argparse
|
||||
import multiprocessing as mp
|
||||
|
||||
from uncloud import UncloudException
|
||||
from contextlib import suppress
|
||||
|
||||
|
||||
def exception_hook(exc_type, exc_value, exc_traceback):
|
||||
logging.getLogger(__name__).error(
|
||||
|
|
@ -19,40 +18,25 @@ def exception_hook(exc_type, exc_value, exc_traceback):
|
|||
|
||||
sys.excepthook = exception_hook
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Setting up root logger
|
||||
logger = logging.getLogger()
|
||||
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
parent_parser = argparse.ArgumentParser(add_help=False)
|
||||
parent_parser.add_argument("--debug", "-d", action='store_true')
|
||||
|
||||
arg_parser = argparse.ArgumentParser()
|
||||
subparsers = arg_parser.add_subparsers(dest='command')
|
||||
|
||||
subparsers = arg_parser.add_subparsers(dest="command")
|
||||
parent_parser = argparse.ArgumentParser(add_help=False)
|
||||
parent_parser.add_argument('--debug', '-d', action='store_true', default=False,
|
||||
help='More verbose logging')
|
||||
|
||||
api_parser = subparsers.add_parser("api", parents=[parent_parser])
|
||||
api_parser.add_argument("--port", "-p")
|
||||
for component in ['api', 'scheduler', 'host', 'filescanner', 'imagescanner',
|
||||
'metadata', 'configure', 'cli']:
|
||||
mod = importlib.import_module('uncloud.{}.main'.format(component))
|
||||
parser = getattr(mod, 'arg_parser')
|
||||
subparsers.add_parser(name=parser.prog, parents=[parser, parent_parser])
|
||||
|
||||
host_parser = subparsers.add_parser("host")
|
||||
host_parser.add_argument("--hostname", required=True)
|
||||
|
||||
scheduler_parser = subparsers.add_parser("scheduler", parents=[parent_parser])
|
||||
|
||||
|
||||
filescanner_parser = subparsers.add_parser("filescanner")
|
||||
imagescanner_parser = subparsers.add_parser("imagescanner")
|
||||
|
||||
metadata_parser = subparsers.add_parser("metadata")
|
||||
metadata_parser.add_argument("--port", "-p")
|
||||
|
||||
config_parser = subparsers.add_parser("configure")
|
||||
|
||||
configure_parser(config_parser)
|
||||
args = arg_parser.parse_args()
|
||||
|
||||
if not args.command:
|
||||
arg_parser.print_help()
|
||||
else:
|
||||
|
|
@ -62,12 +46,11 @@ if __name__ == '__main__':
|
|||
# errors out, so the following command configure multiprocessing
|
||||
# module to not inherit anything from parent.
|
||||
mp.set_start_method('spawn')
|
||||
|
||||
arguments = vars(args)
|
||||
try:
|
||||
name = arguments.pop('command')
|
||||
mod = importlib.import_module("uncloud.{}.main".format(name))
|
||||
main = getattr(mod, "main")
|
||||
mod = importlib.import_module('uncloud.{}.main'.format(name))
|
||||
main = getattr(mod, 'main')
|
||||
main(**arguments)
|
||||
except UncloudException as err:
|
||||
logger.error(err)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue