Commit b478ebb0 by Dudás Ádám

vm: set handlers on activities

parent ec23470e
...@@ -15,6 +15,7 @@ from django.dispatch import Signal ...@@ -15,6 +15,7 @@ from django.dispatch import Signal
from django.utils import timezone from django.utils import timezone
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from celery.exceptions import TimeoutError
from model_utils.models import TimeStampedModel from model_utils.models import TimeStampedModel
from taggit.managers import TaggableManager from taggit.managers import TaggableManager
...@@ -326,17 +327,22 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel): ...@@ -326,17 +327,22 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel):
inst.save() inst.save()
inst.set_level(inst.owner, 'owner') inst.set_level(inst.owner, 'owner')
# create related entities def __on_commit(activity):
inst.disks.add(*[disk.get_exclusive() for disk in disks]) activity.resultant_state = 'PENDING'
with instance_activity(code_suffix='create', instance=inst,
on_commit=__on_commit, user=inst.owner):
# create related entities
inst.disks.add(*[disk.get_exclusive() for disk in disks])
for net in networks: for net in networks:
Interface.create(instance=inst, vlan=net.vlan, Interface.create(instance=inst, vlan=net.vlan,
owner=inst.owner, managed=net.managed) owner=inst.owner, managed=net.managed)
inst.req_traits.add(*req_traits) inst.req_traits.add(*req_traits)
inst.tags.add(*tags) inst.tags.add(*tags)
return inst return inst
@permalink @permalink
def get_absolute_url(self): def get_absolute_url(self):
...@@ -582,9 +588,12 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel): ...@@ -582,9 +588,12 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel):
if self.destroyed: if self.destroyed:
raise self.InstanceDestroyedError(self) raise self.InstanceDestroyedError(self)
with instance_activity(code_suffix='deploy', instance=self, def __on_commit(activity):
task_uuid=task_uuid, user=user) as act: activity.resultant_state = 'RUNNING'
with instance_activity(code_suffix='deploy', instance=self,
on_commit=__on_commit, task_uuid=task_uuid,
user=user) as act:
self.__schedule_vm(act) self.__schedule_vm(act)
...@@ -697,8 +706,12 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel): ...@@ -697,8 +706,12 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel):
if self.destroyed: if self.destroyed:
return # already destroyed, nothing to do here return # already destroyed, nothing to do here
def __on_commit(activity):
activity.resultant_state = 'DESTROYED'
with instance_activity(code_suffix='destroy', instance=self, with instance_activity(code_suffix='destroy', instance=self,
task_uuid=task_uuid, user=user) as act: on_commit=__on_commit, task_uuid=task_uuid,
user=user) as act:
if self.node: if self.node:
self.__destroy_vm(act) self.__destroy_vm(act)
...@@ -725,7 +738,17 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel): ...@@ -725,7 +738,17 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel):
if self.state not in ['RUNNING']: if self.state not in ['RUNNING']:
raise self.WrongStateError(self) raise self.WrongStateError(self)
def __on_abort(activity, error):
if isinstance(error, TimeoutError):
activity.resultant_state = None
else:
activity.resultant_state = 'ERROR'
def __on_commit(activity):
activity.resultant_state = 'SUSPENDED'
with instance_activity(code_suffix='sleep', instance=self, with instance_activity(code_suffix='sleep', instance=self,
on_abort=__on_abort, on_commit=__on_commit,
task_uuid=task_uuid, user=user): task_uuid=task_uuid, user=user):
queue_name = self.get_remote_queue_name('vm') queue_name = self.get_remote_queue_name('vm')
...@@ -743,7 +766,14 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel): ...@@ -743,7 +766,14 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel):
if self.state not in ['SUSPENDED']: if self.state not in ['SUSPENDED']:
raise self.WrongStateError(self) raise self.WrongStateError(self)
def __on_abort(activity, error):
activity.resultant_state = 'ERROR'
def __on_commit(activity):
activity.resultant_state = 'RUNNING'
with instance_activity(code_suffix='wake_up', instance=self, with instance_activity(code_suffix='wake_up', instance=self,
on_abort=__on_abort, on_commit=__on_commit,
task_uuid=task_uuid, user=user): task_uuid=task_uuid, user=user):
queue_name = self.get_remote_queue_name('vm') queue_name = self.get_remote_queue_name('vm')
...@@ -759,7 +789,17 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel): ...@@ -759,7 +789,17 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel):
def shutdown(self, user=None, task_uuid=None): def shutdown(self, user=None, task_uuid=None):
"""Shutdown virtual machine with ACPI signal. """Shutdown virtual machine with ACPI signal.
""" """
def __on_abort(activity, error):
if isinstance(error, TimeoutError):
activity.resultant_state = None
else:
activity.resultant_state = 'ERROR'
def __on_commit(activity):
activity.resultant_state = 'STOPPED'
with instance_activity(code_suffix='shutdown', instance=self, with instance_activity(code_suffix='shutdown', instance=self,
on_abort=__on_abort, on_commit=__on_commit,
task_uuid=task_uuid, user=user): task_uuid=task_uuid, user=user):
queue_name = self.get_remote_queue_name('vm') queue_name = self.get_remote_queue_name('vm')
......
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