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