Commit 5dd8ad59 by Guba Sándor

storage: refactored disk creation

parent a710e628
...@@ -172,12 +172,11 @@ class Disk(AclBase, TimeStampedModel): ...@@ -172,12 +172,11 @@ class Disk(AclBase, TimeStampedModel):
if self.type not in type_mapping.keys(): if self.type not in type_mapping.keys():
raise self.WrongDiskTypeError(self.type) raise self.WrongDiskTypeError(self.type)
filename = self.filename if self.type == 'iso' else None
new_type = type_mapping[self.type] new_type = type_mapping[self.type]
return Disk.objects.create(base=self, datastore=self.datastore, return Disk.create(base=self, datastore=self.datastore,
filename=filename, name=self.name, name=self.name, size=self.size,
size=self.size, type=new_type) type=new_type)
def get_vmdisk_desc(self): def get_vmdisk_desc(self):
"""Serialize disk object to the vmdriver.""" """Serialize disk object to the vmdriver."""
...@@ -215,11 +214,6 @@ class Disk(AclBase, TimeStampedModel): ...@@ -215,11 +214,6 @@ class Disk(AclBase, TimeStampedModel):
self.size = self.base.size self.size = self.base.size
super(Disk, self).clean(*args, **kwargs) super(Disk, self).clean(*args, **kwargs)
def save(self, *args, **kwargs):
if self.filename is None:
self.generate_filename()
return super(Disk, self).save(*args, **kwargs)
def deploy(self, user=None, task_uuid=None, timeout=15): def deploy(self, user=None, task_uuid=None, timeout=15):
"""Reify the disk model on the associated data store. """Reify the disk model on the associated data store.
...@@ -250,7 +244,7 @@ class Disk(AclBase, TimeStampedModel): ...@@ -250,7 +244,7 @@ class Disk(AclBase, TimeStampedModel):
# Delegate create / snapshot jobs # Delegate create / snapshot jobs
queue_name = self.get_remote_queue_name('storage') queue_name = self.get_remote_queue_name('storage')
disk_desc = self.get_disk_desc() disk_desc = self.get_disk_desc()
if self.type == 'qcow2-snap': if self.base is not None:
with act.sub_activity('creating_snapshot'): with act.sub_activity('creating_snapshot'):
remote_tasks.snapshot.apply_async(args=[disk_desc], remote_tasks.snapshot.apply_async(args=[disk_desc],
queue=queue_name queue=queue_name
...@@ -272,10 +266,11 @@ class Disk(AclBase, TimeStampedModel): ...@@ -272,10 +266,11 @@ class Disk(AclBase, TimeStampedModel):
return local_tasks.deploy.apply_async(args=[self, user], return local_tasks.deploy.apply_async(args=[self, user],
queue="localhost.man") queue="localhost.man")
def generate_filename(self): @classmethod
"""Generate a unique filename and set it on the object. def create(cls, **params):
""" disk = cls(filename=str(uuid.uuid4()), **params)
self.filename = str(uuid.uuid4()) disk.save()
return disk
@classmethod @classmethod
def create_empty(cls, instance=None, user=None, **kwargs): def create_empty(cls, instance=None, user=None, **kwargs):
...@@ -288,13 +283,8 @@ class Disk(AclBase, TimeStampedModel): ...@@ -288,13 +283,8 @@ class Disk(AclBase, TimeStampedModel):
:return: Disk object without a real image, to be .deploy()ed later. :return: Disk object without a real image, to be .deploy()ed later.
""" """
with disk_activity(code_suffix="create", user=user) as act: disk = cls.create(**kwargs)
disk = cls(**kwargs) with disk_activity(code_suffix="create", user=user, disk=disk):
if disk.filename is None:
disk.generate_filename()
disk.save()
act.disk = disk
act.save()
if instance: if instance:
instance.disks.add(disk) instance.disks.add(disk)
return disk return disk
...@@ -337,7 +327,6 @@ class Disk(AclBase, TimeStampedModel): ...@@ -337,7 +327,6 @@ class Disk(AclBase, TimeStampedModel):
""" """
kwargs.setdefault('name', url.split('/')[-1]) kwargs.setdefault('name', url.split('/')[-1])
disk = cls(**kwargs) disk = cls(**kwargs)
disk.generate_filename()
disk.type = "iso" disk.type = "iso"
disk.size = 1 disk.size = 1
# TODO get proper datastore # TODO get proper datastore
......
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