- Better error reporting.

- Flask now uses application's logger instead of its own.
- ucloud file scanner refactored.
This commit is contained in:
ahmadbilalkhalid 2019-12-23 12:58:04 +05:00
commit 972bb5a920
14 changed files with 157 additions and 154 deletions

View file

@ -27,7 +27,7 @@ def readable_errors(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
func(*args, **kwargs)
return func(*args, **kwargs)
except etcd3.exceptions.ConnectionFailedError as err:
raise etcd3.exceptions.ConnectionFailedError('etcd connection failed') from err
except etcd3.exceptions.ConnectionTimeoutError as err:

24
ucloud/common/logging.py Normal file
View file

@ -0,0 +1,24 @@
import logging
import colorama
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 == 'WARNING':
color = colorama.Fore.YELLOW
elif record.levelname in ['ERROR', 'EXCEPTION']:
color = colorama.Fore.LIGHTRED_EX
elif record.levelname == 'INFO':
color = colorama.Fore.LIGHTBLUE_EX
else:
color = colorama.Fore.WHITE
try:
print(color, end='', flush=True)
super().handle(record)
finally:
record.exc_info = info
record.exc_text = cache
print(colorama.Style.RESET_ALL, end='', flush=True)

View file

@ -11,6 +11,8 @@ from ucloud.settings import settings as config
class ImageStorageHandler(ABC):
handler_name = 'base'
def __init__(self, image_base, vm_base):
self.image_base = image_base
self.vm_base = vm_base
@ -45,13 +47,17 @@ class ImageStorageHandler(ABC):
def delete_vm_image(self, path):
raise NotImplementedError()
def execute_command(self, command, report=True):
def execute_command(self, command, report=True, error_origin=None):
if not error_origin:
error_origin = self.handler_name
command = list(map(str, command))
try:
output = sp.check_output(command, stderr=sp.PIPE)
except Exception as e:
sp.check_output(command, stderr=sp.PIPE)
except sp.CalledProcessError as e:
_stderr = e.stderr.decode('utf-8').strip()
if report:
logger.exception(e)
logger.exception('%s:- %s', error_origin, _stderr)
return False
return True
@ -66,6 +72,8 @@ class ImageStorageHandler(ABC):
class FileSystemBasedImageStorageHandler(ImageStorageHandler):
handler_name = 'Filesystem'
def import_image(self, src, dest, protect=False):
dest = join_path(self.image_base, dest)
try:
@ -118,17 +126,23 @@ class FileSystemBasedImageStorageHandler(ImageStorageHandler):
class CEPHBasedImageStorageHandler(ImageStorageHandler):
handler_name = 'Ceph'
def import_image(self, src, dest, protect=False):
dest = join_path(self.image_base, dest)
command = ["rbd", "import", src, dest]
import_command = ["rbd", "import", src, dest]
commands = [import_command]
if protect:
snap_create_command = ["rbd", "snap", "create", "{}@protected".format(dest)]
snap_protect_command = ["rbd", "snap", "protect", "{}@protected".format(dest)]
commands.append(snap_create_command)
commands.append(snap_protect_command)
return self.execute_command(command) and self.execute_command(snap_create_command) and\
self.execute_command(snap_protect_command)
result = True
for command in commands:
result = result and self.execute_command(command)
return self.execute_command(command)
return result
def make_vm_image(self, src, dest):
src = join_path(self.image_base, src)