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
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import click
|
import click
|
||||||
import sys
|
|
||||||
import logging
|
|
||||||
|
|
||||||
from logging.handlers import SysLogHandler
|
|
||||||
|
|
||||||
from uncloud_cli.commands.vm import vm
|
from uncloud_cli.commands.vm import vm
|
||||||
from uncloud_cli.commands.user import user
|
from uncloud_cli.commands.user import user
|
||||||
from uncloud_cli.commands.host import host
|
from uncloud_cli.commands.host import host
|
||||||
from uncloud_cli.commands.image import image
|
from uncloud_cli.commands.image import image
|
||||||
from uncloud_cli.commands.network import network
|
from uncloud_cli.commands.network import network
|
||||||
from uncloud_cli.helper import exception_handler, NoTracebackStreamHandler
|
|
||||||
|
|
||||||
|
|
||||||
@click.group()
|
@click.group()
|
||||||
|
@ -19,26 +14,7 @@ def entry_point():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
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)
|
|
||||||
|
|
||||||
entry_point.add_command(vm)
|
entry_point.add_command(vm)
|
||||||
entry_point.add_command(user)
|
entry_point.add_command(user)
|
||||||
entry_point.add_command(image)
|
entry_point.add_command(image)
|
||||||
|
|
2
setup.py
2
setup.py
|
@ -1,6 +1,6 @@
|
||||||
from setuptools import setup, find_packages
|
from setuptools import setup, find_packages
|
||||||
|
|
||||||
with open("README.md", "r") as fh:
|
with open('README.md', 'r') as fh:
|
||||||
long_description = fh.read()
|
long_description = fh.read()
|
||||||
|
|
||||||
setup(name='uncloud_cli',
|
setup(name='uncloud_cli',
|
||||||
|
|
|
@ -11,16 +11,19 @@ from uncloud_cli.config import config, config_file
|
||||||
|
|
||||||
def load_dump_pretty(content):
|
def load_dump_pretty(content):
|
||||||
if isinstance(content, bytes):
|
if isinstance(content, bytes):
|
||||||
content = content.decode("utf-8")
|
content = content.decode('utf-8')
|
||||||
parsed = json.loads(content)
|
parsed = json.loads(content)
|
||||||
return json.dumps(parsed, indent=4, sort_keys=True)
|
return json.dumps(parsed, indent=4, sort_keys=True)
|
||||||
|
|
||||||
|
|
||||||
def make_request(*args, data=None, request_method=requests.post):
|
def make_request(*args, data=None, request_method=requests.post):
|
||||||
r = request_method(
|
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):
|
def get_token(ctx, param, value):
|
||||||
|
@ -28,7 +31,7 @@ def get_token(ctx, param, value):
|
||||||
try:
|
try:
|
||||||
token = TOTP(value).now()
|
token = TOTP(value).now()
|
||||||
except binascii.Error:
|
except binascii.Error:
|
||||||
raise click.BadParameter('Please enter the correct seed in {}'.format(config_file))
|
raise click.BadParameter('')
|
||||||
else:
|
else:
|
||||||
param.name = 'token'
|
param.name = 'token'
|
||||||
return token
|
return token
|
||||||
|
@ -37,16 +40,17 @@ def get_token(ctx, param, value):
|
||||||
def add_otp_options(f):
|
def add_otp_options(f):
|
||||||
options = [
|
options = [
|
||||||
click.option(
|
click.option(
|
||||||
"--name", required=True, default=config['client']['name'],
|
'--name', required=True, default=config.get('client', 'name', fallback=''),
|
||||||
show_default='name mentioned in {}'.format(config_file)
|
show_default='name mentioned in {}'.format(config_file), prompt=True
|
||||||
),
|
),
|
||||||
click.option(
|
click.option(
|
||||||
"--realm", required=True, default=config['client']['realm'],
|
'--realm', required=True, default=config.get('client', 'realm', fallback=''),
|
||||||
show_default='realm mentioned in {}'.format(config_file)
|
show_default='realm mentioned in {}'.format(config_file), prompt=True
|
||||||
),
|
),
|
||||||
click.option(
|
click.option(
|
||||||
"--seed", required=True, default=config['client']['seed'],
|
'--seed', required=True, default=config.get('client', 'seed', fallback=''),
|
||||||
callback=get_token, show_default='seed mentioned in {}'.format(config_file)
|
callback=get_token, prompt=True,
|
||||||
|
show_default='seed mentioned in {}'.format(config_file)
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -9,16 +9,16 @@ def image():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
@image.command("list")
|
@image.command('list')
|
||||||
@click.option("--public", is_flag=True)
|
@click.option('--public', is_flag=True)
|
||||||
def _list(public):
|
def _list(public):
|
||||||
if public:
|
if public:
|
||||||
make_request('image', 'list-public', request_method=requests.get)
|
make_request('image', 'list-public', request_method=requests.get)
|
||||||
|
|
||||||
|
|
||||||
@image.command("create-from-file")
|
@image.command('create-from-file')
|
||||||
@click.option("--name", required=True)
|
@click.option('--name', required=True)
|
||||||
@click.option("--uuid", required=True)
|
@click.option('--uuid', required=True)
|
||||||
@click.option("--image-store-name", 'image_store', required=True)
|
@click.option('--image-store-name', 'image_store', required=True)
|
||||||
def create_from_file(**kwargs):
|
def create_from_file(**kwargs):
|
||||||
make_request('image', 'create', data=kwargs)
|
make_request('image', 'create', data=kwargs)
|
||||||
|
|
|
@ -9,10 +9,10 @@ def network():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
@network.command("create")
|
@network.command('create')
|
||||||
@add_otp_options
|
@add_otp_options
|
||||||
@click.option("--network-name", required=True)
|
@click.option('--network-name', required=True)
|
||||||
@click.option("--network-type", 'type', required=True)
|
@click.option('--network-type', 'type', required=True)
|
||||||
@click.option("--user", required=True, type=bool, default=False)
|
@click.option('--user', required=True, type=bool, default=False)
|
||||||
def create(**kwargs):
|
def create(**kwargs):
|
||||||
make_request('network', 'create', data=kwargs)
|
make_request('network', 'create', data=kwargs)
|
||||||
|
|
|
@ -7,41 +7,41 @@ def user():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
@user.command("files")
|
@user.command('files')
|
||||||
@add_otp_options
|
@add_otp_options
|
||||||
def list_files(**kwargs):
|
def list_files(**kwargs):
|
||||||
make_request('user', 'files', data=kwargs)
|
make_request('user', 'files', data=kwargs)
|
||||||
|
|
||||||
|
|
||||||
@user.command("vms")
|
@user.command('vms')
|
||||||
@add_otp_options
|
@add_otp_options
|
||||||
def list_vms(**kwargs):
|
def list_vms(**kwargs):
|
||||||
make_request('user', 'vms', data=kwargs)
|
make_request('user', 'vms', data=kwargs)
|
||||||
|
|
||||||
|
|
||||||
@user.command("networks")
|
@user.command('networks')
|
||||||
@add_otp_options
|
@add_otp_options
|
||||||
def list_networks(**kwargs):
|
def list_networks(**kwargs):
|
||||||
make_request('user', 'network', data=kwargs)
|
make_request('user', 'network', data=kwargs)
|
||||||
|
|
||||||
|
|
||||||
@user.command("add-ssh")
|
@user.command('add-ssh')
|
||||||
@add_otp_options
|
@add_otp_options
|
||||||
@click.option("--key-name", required=True)
|
@click.option('--key-name', required=True)
|
||||||
@click.option("--key", required=True)
|
@click.option('--key', required=True)
|
||||||
def add_ssh(**kwargs):
|
def add_ssh(**kwargs):
|
||||||
make_request('user', 'add-ssh', data=kwargs)
|
make_request('user', 'add-ssh', data=kwargs)
|
||||||
|
|
||||||
|
|
||||||
@user.command("remove-ssh")
|
@user.command('remove-ssh')
|
||||||
@add_otp_options
|
@add_otp_options
|
||||||
@click.option("--key-name", required=True)
|
@click.option('--key-name', required=True)
|
||||||
def remove_ssh(**kwargs):
|
def remove_ssh(**kwargs):
|
||||||
make_request('user', 'remove-ssh', data=kwargs)
|
make_request('user', 'remove-ssh', data=kwargs)
|
||||||
|
|
||||||
|
|
||||||
@user.command("get-ssh")
|
@user.command('get-ssh')
|
||||||
@add_otp_options
|
@add_otp_options
|
||||||
@click.option("--key-name", default="")
|
@click.option('--key-name', default='')
|
||||||
def get_ssh(**kwargs):
|
def get_ssh(**kwargs):
|
||||||
make_request('user', 'get-ssh', data=kwargs)
|
make_request('user', 'get-ssh', data=kwargs)
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import click
|
import click
|
||||||
import requests
|
|
||||||
|
|
||||||
from uncloud_cli.commands.helper import add_otp_options, make_request, add_vm_options
|
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)
|
make_request('vm', 'status', data=kwargs)
|
||||||
|
|
||||||
|
|
||||||
@vm.command("migrate")
|
@vm.command('migrate')
|
||||||
@add_otp_options
|
@add_otp_options
|
||||||
@click.option("--vm-name", required=True)
|
@click.option('--vm-name', required=True)
|
||||||
@click.option("--destination", required=True)
|
@click.option('--destination', required=True)
|
||||||
def vm_migration(**kwargs):
|
def vm_migration(**kwargs):
|
||||||
make_request('vm', 'migrate', data=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