Commit d0eab3c8 by Guba Sándor

refactoring function names, fixing type and formats, fix snapshot qemu command

parent 1f0f1ade
...@@ -12,18 +12,20 @@ class Disk(object): ...@@ -12,18 +12,20 @@ class 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 = [('qcow2-norm', 'qcow2 normal'), ('qcow2-snap', 'qcow2 snapshot'), TYPES = ['snapshot', 'normal']
('iso', 'iso'), ('raw-ro', 'raw read-only'), ('raw-rw', 'raw')] FORMATS = ['qcow2', 'raw', 'iso']
CREATE_FORMATS = ['qcow2', 'raw']
CREATE_TYPES = ['qcow2', 'raw'] def __init__(self, dir, name, format, type, size, base_name):
def __init__(self, dir, name, format, size, base_name):
# TODO: tests # TODO: tests
self.name = name self.name = name
self.dir = os.path.realpath(dir) self.dir = os.path.realpath(dir)
if format not in [k[0] for k in self.TYPES]: if format not in self.FORMATS:
raise Exception('Invalid format: %s' % format) raise Exception('Invalid format: %s' % format)
self.format = format self.format = format
if type not in self.TYPES:
raise Exception('Invalid type: %s' % format)
self.type = type
self.size = int(size) self.size = int(size)
self.base_name = base_name self.base_name = base_name
...@@ -78,8 +80,10 @@ class Disk(object): ...@@ -78,8 +80,10 @@ class Disk(object):
self.format van be "qcow2-normal" self.format van be "qcow2-normal"
''' '''
# Check if type is avaliable to create # Check if type is avaliable to create
if self.format not in self.CREATE_TYPES: if self.format not in self.CREATE_FORMATS:
raise Exception('Invalid format: %s' % self.format) raise Exception('Invalid format: %s' % self.format)
if self.type != 'normal':
raise Exception('Invalid type: %s' % self.format)
# Check for file if already exist # Check for file if already exist
if os.path.isfile(self.get_path()): if os.path.isfile(self.get_path()):
raise Exception('File already exists: %s' % self.get_path()) raise Exception('File already exists: %s' % self.get_path())
...@@ -94,9 +98,11 @@ class Disk(object): ...@@ -94,9 +98,11 @@ class Disk(object):
def snapshot(self): def snapshot(self):
''' Creating qcow2 snapshot with base image. ''' Creating qcow2 snapshot with base image.
''' '''
# Check if snapshot type match # Check if snapshot type and qcow2 format matchmatch
if self.format != 'qcow2': if self.format != 'qcow2':
raise Exception('Invalid format: %s' % self.format) raise Exception('Invalid format: %s' % self.format)
if self.type != 'snapshot':
raise Exception('Invalid type: %s' % self.format)
# Check if file already exists # Check if file already exists
if os.path.isfile(self.get_path()): if os.path.isfile(self.get_path()):
raise Exception('File already exists: %s' % self.get_path()) raise Exception('File already exists: %s' % self.get_path())
...@@ -106,9 +112,9 @@ class Disk(object): ...@@ -106,9 +112,9 @@ class Disk(object):
# Build list of Strings as command parameters # Build list of Strings as command parameters
cmdline = ['qemu-img', cmdline = ['qemu-img',
'create', 'create',
'-f', self.format,
'-b', self.get_base(), '-b', self.get_base(),
str(self.size)] '-f', self.format,
self.get_path()]
# Call subprocess # Call subprocess
subprocess.check_output(cmdline) subprocess.check_output(cmdline)
......
...@@ -3,36 +3,36 @@ from storagecelery import celery ...@@ -3,36 +3,36 @@ from storagecelery import celery
@celery.task() @celery.task()
def list_disks(dir): def list(dir):
return [d.get_desc() for d in Disk.list(dir)] return [d.get_desc() for d in Disk.list(dir)]
@celery.task() @celery.task()
def create_disk(disk_desc): def create(disk_desc):
disk = Disk.deserialize(disk_desc) disk = Disk.deserialize(disk_desc)
disk.create() disk.create()
@celery.task() @celery.task()
def delete_disk(json_data): def delete(json_data):
disk = Disk.import_from_json(json_data) disk = Disk.deserialize(json_data)
disk.delete() disk.delete()
@celery.task() @celery.task()
def snapshot(json_data): def snapshot(json_data):
disk = Disk.import_from_json(json_data) disk = Disk.deserialize(json_data)
disk.snapshot() disk.snapshot()
@celery.task() @celery.task()
def merge(old_json, new_json): def merge(old_json, new_json):
disk = Disk.import_from_json(old_json) disk = Disk.deserialize(old_json)
new_disk = Disk.import_from_json(new_json) new_disk = Disk.deserialize(new_json)
disk.merge(new_disk) disk.merge(new_disk)
@celery.task() @celery.task()
def get_disk(json_data): def get(json_data):
disk = Disk.get(dir=json_data['dir'], name=json_data['name']) disk = Disk.get(dir=json_data['dir'], name=json_data['name'])
return disk.get_desc() return disk.get_desc()
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