Commit 4b88e9f4 by Bálint Máhonfai

Implement uploading exported disk to storeserver, remove disk from the datastore after upload

parent 06326eea
......@@ -11,6 +11,7 @@ from time import sleep
from hashlib import md5
import re
from requests_toolbelt import MultipartEncoder
import requests
logger = logging.getLogger(__name__)
......@@ -265,22 +266,39 @@ class Disk(object):
raise Exception("Invalid file format. Only qcow and "
"iso files are allowed. Image from: %s" % url)
def export(self, format):
def export(self, format, exported_name, upload_link):
format_dict = {
'vmdk': 'vmdk',
'qcow2': 'qcow2',
'vdi': 'vdi',
'vpc': 'vhd',
}
exported_path = self.get_path() + '.' + format_dict[format]
cmdline = ['qemu-img',
'convert',
'-O', format,
self.get_path(),
self.get_path() + '.' + format_dict[format]]
exported_path]
subprocess.check_output(cmdline)
def extract_iso_from_zip(self, disk_path):
with open(exported_path, 'rb') as exported_disk:
try:
m = MultipartEncoder(
{'data': (exported_name + '.' + format_dict[format], exported_disk)}
)
response = requests.post(upload_link,
data=m,
headers={'Content-Type': m.content_type},
params={'no_redirect': ''})
if response.status_code != 200:
raise Exception("Invalid response status code: %s" %
response.status_code)
finally:
os.unlink(exported_path)
def extract_iso_from_zip(self, disk_path):
with ZipFile(disk_path, 'r') as z:
isos = z.namelist()
if len(isos) != 1:
......@@ -298,7 +316,8 @@ class Disk(object):
logger.info("Extracting %s failed, keeping original.",
disk_path)
def snapshot(self):
def snapshot(self):
""" Creating qcow2 snapshot with base image.
"""
# Check if snapshot type and qcow2 format matchmatch
......@@ -324,7 +343,8 @@ class Disk(object):
# Call subprocess
subprocess.check_output(cmdline)
def merge_disk_with_base(self, task, new_disk, parent_id=None):
def merge_disk_with_base(self, task, new_disk, parent_id=None):
proc = None
try:
cmdline = [
......@@ -376,7 +396,8 @@ class Disk(object):
os.unlink(new_disk.get_path())
raise
def merge_disk_without_base(self, task, new_disk, parent_id=None,
def merge_disk_without_base(self, task, new_disk, parent_id=None,
length=1024 * 1024):
try:
fsrc = open(self.get_path(), 'rb')
......@@ -413,7 +434,8 @@ class Disk(object):
os.unlink(new_disk.get_path())
raise
def merge(self, task, new_disk, parent_id=None):
def merge(self, task, new_disk, parent_id=None):
""" Merging a new_disk from the actual disk and its base.
"""
......@@ -431,12 +453,14 @@ class Disk(object):
else:
self.merge_disk_without_base(task, new_disk, parent_id)
def delete(self):
def delete(self):
""" Delete file. """
if os.path.isfile(self.get_path()):
os.unlink(self.get_path())
@classmethod
def list(cls, dir):
@classmethod
def list(cls, dir):
""" List all files in <dir> directory."""
return [cls.get(dir, file) for file in os.listdir(dir)]
celery==3.1.17
requests==2.5.3
requests-toolbelt==0.9.1
filemagic==1.6
......@@ -42,9 +42,9 @@ class download(AbortableTask):
@celery.task()
def export(disk_desc, format):
def export(disk_desc, format, exported_name, upload_link):
disk = Disk.deserialize(disk_desc)
disk.export(format)
disk.export(format, exported_name, upload_link)
@celery.task()
......
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