Commit 7d71f8e0 by Bach Dániel

Merge branch 'disk-download-enhancement' into 'master'

Disk Download Enhancement
parents 0282f543 f3a27e66
...@@ -3,6 +3,7 @@ import os ...@@ -3,6 +3,7 @@ import os
import subprocess import subprocess
import re import re
import logging import logging
import magic
from shutil import move, copy, copyfileobj from shutil import move, copy, copyfileobj
from zipfile import ZipFile, is_zipfile from zipfile import ZipFile, is_zipfile
from zlib import decompressobj, MAX_WBITS from zlib import decompressobj, MAX_WBITS
...@@ -116,12 +117,30 @@ class Disk(object): ...@@ -116,12 +117,30 @@ class Disk(object):
# Call subprocess # Call subprocess
subprocess.check_output(cmdline) subprocess.check_output(cmdline)
def check_valid_image(self):
"""Check wether the downloaded image is valid.
Set the proper type for valid images."""
format_map = [
("qcow", "qcow2-norm"),
("iso", "iso")
]
with magic.Magic() as m:
ftype = m.id_filename(self.get_path())
logger.debug("Downloaded file type is: %s", ftype)
for file_type, disk_format in format_map:
if file_type in ftype.lower():
self.format = disk_format
return True
return False
def download(self, task, url, parent_id=None): # noqa def download(self, task, url, parent_id=None): # noqa
"""Download image from url.""" """Download image from url."""
disk_path = self.get_path() disk_path = self.get_path()
logger.info("Downloading image from %s to %s", url, disk_path) logger.info("Downloading image from %s to %s", url, disk_path)
r = requests.get(url, stream=True) r = requests.get(url, stream=True)
if r.status_code == 200: if r.status_code != 200:
raise Exception("Invalid response status code: %s" % r.status_code)
class AbortException(Exception): class AbortException(Exception):
pass pass
if task.is_aborted(): if task.is_aborted():
...@@ -178,6 +197,10 @@ class Disk(object): ...@@ -178,6 +197,10 @@ class Disk(object):
meta={'size': actsize, 'extracting': 'zip', meta={'size': actsize, 'extracting': 'zip',
'percent': 99}) 'percent': 99})
self.extract_iso_from_zip(disk_path) self.extract_iso_from_zip(disk_path)
if not self.check_valid_image():
os.unlink(disk_path)
raise Exception("Invalid file format. Only qcow and "
"iso files are allowed.")
def extract_iso_from_zip(self, disk_path): def extract_iso_from_zip(self, disk_path):
with ZipFile(disk_path, 'r') as z: with ZipFile(disk_path, 'r') as z:
......
...@@ -36,7 +36,8 @@ class download(AbortableTask): ...@@ -36,7 +36,8 @@ class download(AbortableTask):
parent_id = kwargs.get("parent_id", None) parent_id = kwargs.get("parent_id", None)
disk = Disk.deserialize(disk_desc) disk = Disk.deserialize(disk_desc)
disk.download(self, url, parent_id) disk.download(self, url, parent_id)
return disk.size return {'size': disk.size,
'type': disk.format}
@celery.task() @celery.task()
...@@ -89,7 +90,7 @@ def move_to_trash(datastore, disk_name): ...@@ -89,7 +90,7 @@ def move_to_trash(datastore, disk_name):
disk_path = path.join(datastore, disk_name) disk_path = path.join(datastore, disk_name)
if not path.isdir(trash_path): if not path.isdir(trash_path):
mkdir(trash_path) mkdir(trash_path)
#TODO: trash dir configurable? # TODO: trash dir configurable?
move(disk_path, trash_path) move(disk_path, trash_path)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment