Commit 6ac9d76c by Bálint Máhonfai

Add export operation to storage driver

parent e3730928
...@@ -19,7 +19,7 @@ re_qemu_img = re.compile(r'(file format: (?P<format>(qcow2|raw))|' ...@@ -19,7 +19,7 @@ re_qemu_img = re.compile(r'(file format: (?P<format>(qcow2|raw))|'
r'virtual size: \w+ \((?P<size>[0-9]+) bytes\)|' r'virtual size: \w+ \((?P<size>[0-9]+) bytes\)|'
r'backing file: \S+ \(actual path: (?P<base>\S+)\))$') r'backing file: \S+ \(actual path: (?P<base>\S+)\))$')
maximum_size = float(os.getenv("DOWNLOAD_MAX_SIZE", 1024*1024*1024*10)) maximum_size = float(os.getenv("DOWNLOAD_MAX_SIZE", 1024 * 1024 * 1024 * 10))
class AbortException(Exception): class AbortException(Exception):
...@@ -31,14 +31,14 @@ class FileTooBig(Exception): ...@@ -31,14 +31,14 @@ class FileTooBig(Exception):
class Disk(object): class Disk(object):
""" Storage driver DISK object.
''' Storage driver DISK object.
Handle qcow2, raw and iso images. Handle qcow2, raw and iso images.
TYPES, CREATE_TYPES, SNAPSHOT_TYPES are hand managed restrictions. TYPES, CREATE_TYPES, SNAPSHOT_TYPES are hand managed restrictions.
''' """
TYPES = ['snapshot', 'normal'] TYPES = ['snapshot', 'normal']
FORMATS = ['qcow2', 'raw', 'iso'] FORMATS = ['qcow2', 'raw', 'iso']
CREATE_FORMATS = ['qcow2', 'raw'] CREATE_FORMATS = ['qcow2', 'raw']
EXPORT_FORMATS = ['vdi', 'vpc', 'vmdk']
def __init__(self, dir, name, format, type, size, def __init__(self, dir, name, format, type, size,
base_name, actual_size=0): base_name, actual_size=0):
...@@ -100,8 +100,8 @@ class Disk(object): ...@@ -100,8 +100,8 @@ class Disk(object):
@classmethod @classmethod
def get_legacy(cls, dir, name): def get_legacy(cls, dir, name):
''' Create disk from path """ Create disk from path
''' """
path = os.path.realpath(dir + '/' + name) path = os.path.realpath(dir + '/' + name)
output = subprocess.check_output(['qemu-img', 'info', path]) output = subprocess.check_output(['qemu-img', 'info', path])
...@@ -266,6 +266,23 @@ class Disk(object): ...@@ -266,6 +266,23 @@ 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):
if format not in self.EXPORT_FORMATS:
raise Exception('Invalid export format: %s' % format)
format_dict = {
'vdi': 'vdi',
'vpc': 'vhd',
'vmdk': 'vmdk',
}
cmdline = ['qemu-img',
'convert',
'-O', format,
self.get_path(),
self.get_path() + '.' + format_dict[format]]
subprocess.check_output(cmdline)
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:
isos = z.namelist() isos = z.namelist()
...@@ -285,8 +302,8 @@ class Disk(object): ...@@ -285,8 +302,8 @@ class Disk(object):
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
if self.type != 'snapshot': if self.type != 'snapshot':
raise Exception('Invalid type: %s' % self.type) raise Exception('Invalid type: %s' % self.type)
......
...@@ -42,6 +42,12 @@ class download(AbortableTask): ...@@ -42,6 +42,12 @@ class download(AbortableTask):
@celery.task() @celery.task()
def export(disk_desc, format):
disk = Disk.deserialize(disk_desc)
disk.export(format)
@celery.task()
def delete(json_data): def delete(json_data):
disk = Disk.deserialize(json_data) disk = Disk.deserialize(json_data)
disk.delete() disk.delete()
...@@ -79,7 +85,7 @@ def get(json_data): ...@@ -79,7 +85,7 @@ def get(json_data):
@celery.task() @celery.task()
def get_storage_stat(path): def get_storage_stat(path):
''' Return free disk space avaliable at path in bytes and percent.''' """ Return free disk space avaliable at path in bytes and percent."""
s = statvfs(path) s = statvfs(path)
all_space = s.f_bsize * s.f_blocks all_space = s.f_bsize * s.f_blocks
free_space = s.f_bavail * s.f_frsize free_space = s.f_bavail * s.f_frsize
...@@ -110,8 +116,8 @@ def get_file_statistics(datastore): ...@@ -110,8 +116,8 @@ def get_file_statistics(datastore):
@celery.task @celery.task
def move_to_trash(datastore, disk_name): def move_to_trash(datastore, disk_name):
''' Move path to the trash directory. """ Move path to the trash directory.
''' """
trash_path = path.join(datastore, trash_directory) trash_path = path.join(datastore, trash_directory)
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):
...@@ -122,8 +128,8 @@ def move_to_trash(datastore, disk_name): ...@@ -122,8 +128,8 @@ def move_to_trash(datastore, disk_name):
@celery.task @celery.task
def recover_from_trash(datastore, disk_name): def recover_from_trash(datastore, disk_name):
''' Recover named disk from the trash directory. """ Recover named disk from the trash directory.
''' """
if path.exists(path.join(datastore, disk_name)): if path.exists(path.join(datastore, disk_name)):
return False return False
disk_path = path.join(datastore, trash_directory, disk_name) disk_path = path.join(datastore, trash_directory, disk_name)
...@@ -134,10 +140,10 @@ def recover_from_trash(datastore, disk_name): ...@@ -134,10 +140,10 @@ def recover_from_trash(datastore, disk_name):
@celery.task @celery.task
def make_free_space(datastore, percent=10): def make_free_space(datastore, percent=10):
''' Check for free space on datastore. """ Check for free space on datastore.
If free space is less than the given percent If free space is less than the given percent
removes oldest files to satisfy the given requirement. removes oldest files to satisfy the given requirement.
''' """
trash_path = path.join(datastore, trash_directory) trash_path = path.join(datastore, trash_directory)
def comp(filename): def comp(filename):
......
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