refactoring
This commit is contained in:
parent
dc283251d9
commit
436cb881b2
8 changed files with 39 additions and 93 deletions
|
@ -1,17 +1,12 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import click
|
||||
import sys
|
||||
import logging
|
||||
|
||||
from logging.handlers import SysLogHandler
|
||||
|
||||
from uncloud_cli.commands.vm import vm
|
||||
from uncloud_cli.commands.user import user
|
||||
from uncloud_cli.commands.host import host
|
||||
from uncloud_cli.commands.image import image
|
||||
from uncloud_cli.commands.network import network
|
||||
from uncloud_cli.helper import exception_handler, NoTracebackStreamHandler
|
||||
|
||||
|
||||
@click.group()
|
||||
|
@ -19,26 +14,7 @@ def entry_point():
|
|||
pass
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.excepthook = exception_handler
|
||||
|
||||
# Setting up root logger
|
||||
logger = logging.getLogger()
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
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.INFO)
|
||||
stream_formatter = logging.Formatter('%(message)s')
|
||||
stream_handler.setFormatter(stream_formatter)
|
||||
|
||||
logger.addHandler(syslog_handler)
|
||||
logger.addHandler(stream_handler)
|
||||
|
||||
if __name__ == '__main__':
|
||||
entry_point.add_command(vm)
|
||||
entry_point.add_command(user)
|
||||
entry_point.add_command(image)
|
||||
|
|
2
setup.py
2
setup.py
|
@ -1,6 +1,6 @@
|
|||
from setuptools import setup, find_packages
|
||||
|
||||
with open("README.md", "r") as fh:
|
||||
with open('README.md', 'r') as fh:
|
||||
long_description = fh.read()
|
||||
|
||||
setup(name='uncloud_cli',
|
||||
|
|
|
@ -11,16 +11,19 @@ from uncloud_cli.config import config, config_file
|
|||
|
||||
def load_dump_pretty(content):
|
||||
if isinstance(content, bytes):
|
||||
content = content.decode("utf-8")
|
||||
content = content.decode('utf-8')
|
||||
parsed = json.loads(content)
|
||||
return json.dumps(parsed, indent=4, sort_keys=True)
|
||||
|
||||
|
||||
def make_request(*args, data=None, request_method=requests.post):
|
||||
r = request_method(
|
||||
join_path(config['client']['api_server'], *args), json=data
|
||||
join_path(config.get('client', 'api_server'), *args), json=data
|
||||
)
|
||||
print(load_dump_pretty(r.content))
|
||||
try:
|
||||
print(load_dump_pretty(r.content))
|
||||
except Exception:
|
||||
print('Error occurred while getting output from api server.')
|
||||
|
||||
|
||||
def get_token(ctx, param, value):
|
||||
|
@ -28,7 +31,7 @@ def get_token(ctx, param, value):
|
|||
try:
|
||||
token = TOTP(value).now()
|
||||
except binascii.Error:
|
||||
raise click.BadParameter('Please enter the correct seed in {}'.format(config_file))
|
||||
raise click.BadParameter('')
|
||||
else:
|
||||
param.name = 'token'
|
||||
return token
|
||||
|
@ -37,16 +40,17 @@ def get_token(ctx, param, value):
|
|||
def add_otp_options(f):
|
||||
options = [
|
||||
click.option(
|
||||
"--name", required=True, default=config['client']['name'],
|
||||
show_default='name mentioned in {}'.format(config_file)
|
||||
'--name', required=True, default=config.get('client', 'name', fallback=''),
|
||||
show_default='name mentioned in {}'.format(config_file), prompt=True
|
||||
),
|
||||
click.option(
|
||||
"--realm", required=True, default=config['client']['realm'],
|
||||
show_default='realm mentioned in {}'.format(config_file)
|
||||
'--realm', required=True, default=config.get('client', 'realm', fallback=''),
|
||||
show_default='realm mentioned in {}'.format(config_file), prompt=True
|
||||
),
|
||||
click.option(
|
||||
"--seed", required=True, default=config['client']['seed'],
|
||||
callback=get_token, show_default='seed mentioned in {}'.format(config_file)
|
||||
'--seed', required=True, default=config.get('client', 'seed', fallback=''),
|
||||
callback=get_token, prompt=True,
|
||||
show_default='seed mentioned in {}'.format(config_file)
|
||||
)
|
||||
]
|
||||
|
||||
|
|
|
@ -9,16 +9,16 @@ def image():
|
|||
pass
|
||||
|
||||
|
||||
@image.command("list")
|
||||
@click.option("--public", is_flag=True)
|
||||
@image.command('list')
|
||||
@click.option('--public', is_flag=True)
|
||||
def _list(public):
|
||||
if public:
|
||||
make_request('image', 'list-public', request_method=requests.get)
|
||||
|
||||
|
||||
@image.command("create-from-file")
|
||||
@click.option("--name", required=True)
|
||||
@click.option("--uuid", required=True)
|
||||
@click.option("--image-store-name", 'image_store', required=True)
|
||||
@image.command('create-from-file')
|
||||
@click.option('--name', required=True)
|
||||
@click.option('--uuid', required=True)
|
||||
@click.option('--image-store-name', 'image_store', required=True)
|
||||
def create_from_file(**kwargs):
|
||||
make_request('image', 'create', data=kwargs)
|
||||
|
|
|
@ -9,10 +9,10 @@ def network():
|
|||
pass
|
||||
|
||||
|
||||
@network.command("create")
|
||||
@network.command('create')
|
||||
@add_otp_options
|
||||
@click.option("--network-name", required=True)
|
||||
@click.option("--network-type", 'type', required=True)
|
||||
@click.option("--user", required=True, type=bool, default=False)
|
||||
@click.option('--network-name', required=True)
|
||||
@click.option('--network-type', 'type', required=True)
|
||||
@click.option('--user', required=True, type=bool, default=False)
|
||||
def create(**kwargs):
|
||||
make_request('network', 'create', data=kwargs)
|
||||
|
|
|
@ -7,41 +7,41 @@ def user():
|
|||
pass
|
||||
|
||||
|
||||
@user.command("files")
|
||||
@user.command('files')
|
||||
@add_otp_options
|
||||
def list_files(**kwargs):
|
||||
make_request('user', 'files', data=kwargs)
|
||||
|
||||
|
||||
@user.command("vms")
|
||||
@user.command('vms')
|
||||
@add_otp_options
|
||||
def list_vms(**kwargs):
|
||||
make_request('user', 'vms', data=kwargs)
|
||||
|
||||
|
||||
@user.command("networks")
|
||||
@user.command('networks')
|
||||
@add_otp_options
|
||||
def list_networks(**kwargs):
|
||||
make_request('user', 'network', data=kwargs)
|
||||
|
||||
|
||||
@user.command("add-ssh")
|
||||
@user.command('add-ssh')
|
||||
@add_otp_options
|
||||
@click.option("--key-name", required=True)
|
||||
@click.option("--key", required=True)
|
||||
@click.option('--key-name', required=True)
|
||||
@click.option('--key', required=True)
|
||||
def add_ssh(**kwargs):
|
||||
make_request('user', 'add-ssh', data=kwargs)
|
||||
|
||||
|
||||
@user.command("remove-ssh")
|
||||
@user.command('remove-ssh')
|
||||
@add_otp_options
|
||||
@click.option("--key-name", required=True)
|
||||
@click.option('--key-name', required=True)
|
||||
def remove_ssh(**kwargs):
|
||||
make_request('user', 'remove-ssh', data=kwargs)
|
||||
|
||||
|
||||
@user.command("get-ssh")
|
||||
@user.command('get-ssh')
|
||||
@add_otp_options
|
||||
@click.option("--key-name", default="")
|
||||
@click.option('--key-name', default='')
|
||||
def get_ssh(**kwargs):
|
||||
make_request('user', 'get-ssh', data=kwargs)
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import click
|
||||
import requests
|
||||
|
||||
from uncloud_cli.commands.helper import add_otp_options, make_request, add_vm_options
|
||||
|
||||
|
@ -56,9 +55,9 @@ def status(**kwargs):
|
|||
make_request('vm', 'status', data=kwargs)
|
||||
|
||||
|
||||
@vm.command("migrate")
|
||||
@vm.command('migrate')
|
||||
@add_otp_options
|
||||
@click.option("--vm-name", required=True)
|
||||
@click.option("--destination", required=True)
|
||||
@click.option('--vm-name', required=True)
|
||||
@click.option('--destination', required=True)
|
||||
def vm_migration(**kwargs):
|
||||
make_request('vm', 'migrate', data=kwargs)
|
||||
|
|
|
@ -1,33 +0,0 @@
|
|||
import os
|
||||
import sys
|
||||
import logging
|
||||
import click
|
||||
|
||||
|
||||
def exception_handler(exception_type, exception, traceback):
|
||||
if bool(os.getenv('DEBUG_UCLOUD')) is True:
|
||||
sys.__excepthook__(exception_type, exception, traceback)
|
||||
else:
|
||||
print("%s: %s" % (exception_type.__name__, exception))
|
||||
|
||||
|
||||
class NoTracebackStreamHandler(logging.StreamHandler):
|
||||
def handle(self, record):
|
||||
info, cache = record.exc_info, record.exc_text
|
||||
record.exc_info, record.exc_text = None, None
|
||||
|
||||
if record.levelname in ["WARNING", "WARN"]:
|
||||
click.echo(click.style('', fg='yellow', bold=True, reset=False), nl=False)
|
||||
elif record.levelname == "ERROR":
|
||||
click.echo(click.style('', fg='red', bold=True, reset=False), nl=False)
|
||||
elif record.levelname == "INFO":
|
||||
click.echo(click.style('', fg='green', bold=True, reset=False), nl=False)
|
||||
elif record.levelname == "CRITICAL":
|
||||
click.echo(click.style('', fg='cyan', bold=True, reset=False), nl=False)
|
||||
|
||||
try:
|
||||
super().handle(record)
|
||||
finally:
|
||||
record.exc_info = info
|
||||
record.exc_text = cache
|
||||
click.echo(click.style('', 'reset'), nl=False)
|
Loading…
Reference in a new issue