- 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

@ -1,6 +1,6 @@
import json
import os
import subprocess
import subprocess as sp
import sys
from os.path import isdir
@ -13,7 +13,7 @@ from ucloud.imagescanner import logger
def qemu_img_type(path):
qemu_img_info_command = ["qemu-img", "info", "--output", "json", path]
try:
qemu_img_info = subprocess.check_output(qemu_img_info_command)
qemu_img_info = sp.check_output(qemu_img_info_command)
except Exception as e:
logger.exception(e)
return None
@ -29,7 +29,7 @@ def check():
)
try:
subprocess.check_output(['which', 'qemu-img'])
sp.check_output(['which', 'qemu-img'])
except Exception:
print("qemu-img missing")
sys.exit(1)
@ -67,29 +67,30 @@ def main():
if qemu_img_type(image_full_path) == "qcow2":
try:
# Convert .qcow2 to .raw
subprocess.check_output(qemu_img_convert_command)
except Exception as e:
logger.exception(e)
sp.check_output(qemu_img_convert_command,)
except sp.CalledProcessError:
logger.exception('Image convertion from .qcow2 to .raw failed.')
else:
# Import and Protect
r_status = shared.storage_handler.import_image(src="image.raw",
dest=image_uuid,
protect=True)
dest=image_uuid,
protect=True)
if r_status:
# Everything is successfully done
image.value["status"] = "CREATED"
shared.etcd_client.put(image.key, json.dumps(image.value))
finally:
try:
os.remove("image.raw")
except Exception:
pass
else:
# The user provided image is either not found or of invalid format
image.value["status"] = "INVALID_IMAGE"
shared.etcd_client.put(image.key, json.dumps(image.value))
try:
os.remove("image.raw")
except Exception:
pass
if __name__ == "__main__":
main()