Commit f6e1dcbe by Guba Sándor

save_as fixes

parent 4db5bd68
...@@ -107,6 +107,18 @@ class Disk(AclBase, TimeStampedModel): ...@@ -107,6 +107,18 @@ class Disk(AclBase, TimeStampedModel):
self.disk = disk self.disk = disk
class DiskIsNotReady(Exception):
def __init__(self, disk, message=None):
if message is None:
message = ("The requested operation can'T be performed on "
"disk '%s (%s)' because it has never been"
"deployed." % (disk.name, disk.filename))
Exception.__init__(self, message)
self.disk = disk
@property @property
def ready(self): def ready(self):
return self.activity_log.filter(activity_code__endswith="deploy", return self.activity_log.filter(activity_code__endswith="deploy",
...@@ -306,6 +318,7 @@ class Disk(AclBase, TimeStampedModel): ...@@ -306,6 +318,7 @@ class Disk(AclBase, TimeStampedModel):
datastore = params.pop('datastore', DataStore.objects.get()) datastore = params.pop('datastore', DataStore.objects.get())
disk = cls(filename=str(uuid.uuid4()), datastore=datastore, **params) disk = cls(filename=str(uuid.uuid4()), datastore=datastore, **params)
disk.save() disk.save()
logger.debug("Disk created: %s", params)
with disk_activity(code_suffix="create", with disk_activity(code_suffix="create",
user=user, user=user,
disk=disk): disk=disk):
...@@ -366,8 +379,6 @@ class Disk(AclBase, TimeStampedModel): ...@@ -366,8 +379,6 @@ class Disk(AclBase, TimeStampedModel):
kwargs.setdefault('name', url.split('/')[-1]) kwargs.setdefault('name', url.split('/')[-1])
disk = Disk.create(type="iso", instance=instance, user=user, disk = Disk.create(type="iso", instance=instance, user=user,
size=None, **kwargs) size=None, **kwargs)
# TODO get proper datastore
disk.datastore = DataStore.objects.get()
queue_name = disk.get_remote_queue_name('storage') queue_name = disk.get_remote_queue_name('storage')
def __on_abort(activity, error): def __on_abort(activity, error):
...@@ -439,6 +450,7 @@ class Disk(AclBase, TimeStampedModel): ...@@ -439,6 +450,7 @@ class Disk(AclBase, TimeStampedModel):
""" """
mapping = { mapping = {
'qcow2-snap': ('qcow2-norm', self.base), 'qcow2-snap': ('qcow2-norm', self.base),
'qcow2-norm': ('qcow2-norm', self),
} }
if self.type not in mapping.keys(): if self.type not in mapping.keys():
raise self.WrongDiskTypeError(self.type) raise self.WrongDiskTypeError(self.type)
...@@ -446,6 +458,9 @@ class Disk(AclBase, TimeStampedModel): ...@@ -446,6 +458,9 @@ class Disk(AclBase, TimeStampedModel):
if self.is_in_use: if self.is_in_use:
raise self.DiskInUseError(self) raise self.DiskInUseError(self)
if not self.ready:
raise self.DiskIsNotReady(self)
# from this point on, the caller has to guarantee that the disk is not # from this point on, the caller has to guarantee that the disk is not
# going to be used until the operation is complete # going to be used until the operation is complete
...@@ -455,7 +470,6 @@ class Disk(AclBase, TimeStampedModel): ...@@ -455,7 +470,6 @@ class Disk(AclBase, TimeStampedModel):
name=self.name, size=self.size, name=self.name, size=self.size,
type=new_type) type=new_type)
disk.save()
with disk_activity(code_suffix="save_as", disk=self, with disk_activity(code_suffix="save_as", disk=self,
user=user, task_uuid=task_uuid): user=user, task_uuid=task_uuid):
with disk_activity(code_suffix="deploy", disk=disk, with disk_activity(code_suffix="deploy", disk=disk,
......
from storage.models import DataStore from storage.models import DataStore
import os
from manager.mancelery import celery from manager.mancelery import celery
import logging import logging
from storage.tasks import remote_tasks from storage.tasks import remote_tasks
...@@ -16,13 +15,15 @@ def garbage_collector(timeout=15): ...@@ -16,13 +15,15 @@ def garbage_collector(timeout=15):
deletes oldest images from trash. deletes oldest images from trash.
:param timeout: Seconds before TimeOut exception :param timeout: Seconds before TimeOut exception
:type timeoit: int :type timeout: int
""" """
for ds in DataStore.objects.all(): for ds in DataStore.objects.all():
file_list = os.listdir(ds.path)
disk_list = ds.get_deletable_disks()
queue_name = ds.get_remote_queue_name('storage') queue_name = ds.get_remote_queue_name('storage')
for i in set(file_list).intersection(disk_list): files = set(remote_tasks.list_files.apply_async(
args=[ds.path], queue=queue_name).get(timeout=timeout))
disks = set(ds.get_deletable_disks())
queue_name = ds.get_remote_queue_name('storage')
for i in disks & files:
logger.info("Image: %s at Datastore: %s moved to trash folder." % logger.info("Image: %s at Datastore: %s moved to trash folder." %
(i, ds.path)) (i, ds.path))
remote_tasks.move_to_trash.apply_async( remote_tasks.move_to_trash.apply_async(
......
...@@ -1128,20 +1128,23 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, ...@@ -1128,20 +1128,23 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel,
with instance_activity(code_suffix="save_as_template", instance=self, with instance_activity(code_suffix="save_as_template", instance=self,
task_uuid=task_uuid, user=user) as act: task_uuid=task_uuid, user=user) as act:
# prepare parameters # prepare parameters
kwargs.setdefault('name', name) params = {
kwargs.setdefault('description', self.description) 'access_method': self.access_method,
kwargs.setdefault('parent', self.template) 'arch': self.arch,
kwargs.setdefault('num_cores', self.num_cores) 'boot_menu': self.boot_menu,
kwargs.setdefault('ram_size', self.ram_size) 'description': self.description,
kwargs.setdefault('max_ram_size', self.max_ram_size) 'lease': self.lease, # Can be problem in new VM
kwargs.setdefault('arch', self.arch) 'max_ram_size': self.max_ram_size,
kwargs.setdefault('priority', self.priority) 'name': name,
kwargs.setdefault('boot_menu', self.boot_menu) 'num_cores': self.num_cores,
kwargs.setdefault('raw_data', self.raw_data) 'owner': user,
kwargs.setdefault('lease', self.lease) 'parent': self.template, # Can be problem
kwargs.setdefault('access_method', self.access_method) 'priority': self.priority,
kwargs.setdefault('system', self.template.system 'ram_size': self.ram_size,
if self.template else None) 'raw_data': self.raw_data,
'system': self.template.system if self.template else "",
}
params.update(kwargs)
def __try_save_disk(disk): def __try_save_disk(disk):
try: try:
...@@ -1150,7 +1153,7 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, ...@@ -1150,7 +1153,7 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel,
return disk return disk
# create template and do additional setup # create template and do additional setup
tmpl = InstanceTemplate(**kwargs) tmpl = InstanceTemplate(**params)
tmpl.full_clean() # Avoiding database errors. tmpl.full_clean() # Avoiding database errors.
tmpl.save() tmpl.save()
with act.sub_activity('saving_disks'): with act.sub_activity('saving_disks'):
......
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