Commit 48b00b23 by Őry Máté

vm: save resultant_state at activity creation

parent 5d59c0a9
......@@ -90,7 +90,8 @@ class InstanceActivity(ActivityModel):
@classmethod
def create(cls, code_suffix, instance, task_uuid=None, user=None,
concurrency_check=True, readable_name=None):
concurrency_check=True, readable_name=None,
resultant_state=None):
readable_name = _normalize_readable_name(readable_name, code_suffix)
# Check for concurrent activities
......@@ -100,14 +101,14 @@ class InstanceActivity(ActivityModel):
activity_code = join_activity_code(cls.ACTIVITY_CODE_BASE, code_suffix)
act = cls(activity_code=activity_code, instance=instance, parent=None,
resultant_state=None, started=timezone.now(),
resultant_state=resultant_state, started=timezone.now(),
readable_name_data=readable_name.to_dict(),
task_uuid=task_uuid, user=user)
act.save()
return act
def create_sub(self, code_suffix, task_uuid=None, concurrency_check=True,
readable_name=None):
readable_name=None, resultant_state=None):
readable_name = _normalize_readable_name(readable_name, code_suffix)
# Check for concurrent activities
......@@ -117,7 +118,8 @@ class InstanceActivity(ActivityModel):
act = InstanceActivity(
activity_code=join_activity_code(self.activity_code, code_suffix),
instance=self.instance, parent=self, resultant_state=None,
instance=self.instance, parent=self,
resultant_state=resultant_state,
readable_name_data=readable_name.to_dict(), started=timezone.now(),
task_uuid=task_uuid, user=self.user)
act.save()
......@@ -194,14 +196,15 @@ class InstanceActivity(ActivityModel):
@contextmanager
def instance_activity(code_suffix, instance, on_abort=None, on_commit=None,
task_uuid=None, user=None, concurrency_check=True,
readable_name=None):
readable_name=None, resultant_state=None):
"""Create a transactional context for an instance activity.
"""
if not readable_name:
warn("Set readable_name", stacklevel=3)
act = InstanceActivity.create(code_suffix, instance, task_uuid, user,
concurrency_check,
readable_name=readable_name)
readable_name=readable_name,
resultant_state=resultant_state)
return activitycontextimpl(act, on_abort=on_abort, on_commit=on_commit)
......
......@@ -51,6 +51,7 @@ class InstanceOperation(Operation):
concurrency_check = True
accept_states = None
deny_states = None
resultant_state = None
def __init__(self, instance):
super(InstanceOperation, self).__init__(subject=instance)
......@@ -95,12 +96,14 @@ class InstanceOperation(Operation):
"provided as parameter.")
return parent.create_sub(code_suffix=self.activity_code_suffix,
readable_name=name)
readable_name=name,
resultant_state=self.resultant_state)
else:
return InstanceActivity.create(
code_suffix=self.activity_code_suffix, instance=self.instance,
readable_name=name, user=user,
concurrency_check=self.concurrency_check)
concurrency_check=self.concurrency_check,
resultant_state=self.resultant_state)
def is_preferred(self):
"""If this is the recommended op in the current state of the instance.
......@@ -246,15 +249,13 @@ class DeployOperation(InstanceOperation):
"and network configuration).")
required_perms = ()
deny_states = ('SUSPENDED', 'RUNNING')
resultant_state = 'RUNNING'
def is_preferred(self):
return self.instance.status in (self.instance.STATUS.STOPPED,
self.instance.STATUS.PENDING,
self.instance.STATUS.ERROR)
def on_commit(self, activity):
activity.resultant_state = 'RUNNING'
def _operation(self, activity, timeout=15):
# Allocate VNC port and host node
self.instance.allocate_vnc_port()
......@@ -301,9 +302,7 @@ class DestroyOperation(InstanceOperation):
description = _("Permanently destroy virtual machine, its network "
"settings and disks.")
required_perms = ()
def on_commit(self, activity):
activity.resultant_state = 'DESTROYED'
resultant_state = 'DESTROYED'
def _operation(self, activity):
# Destroy networks
......@@ -599,9 +598,7 @@ class ShutdownOperation(InstanceOperation):
abortable = True
required_perms = ()
accept_states = ('RUNNING', )
def on_commit(self, activity):
activity.resultant_state = 'STOPPED'
resultant_state = 'STOPPED'
def _operation(self, task=None):
self.instance.shutdown_vm(task=task)
......@@ -625,9 +622,7 @@ class ShutOffOperation(InstanceOperation):
"of a physical machine.")
required_perms = ()
accept_states = ('RUNNING', )
def on_commit(self, activity):
activity.resultant_state = 'STOPPED'
resultant_state = 'STOPPED'
def _operation(self, activity):
# Shutdown networks
......@@ -660,6 +655,7 @@ class SleepOperation(InstanceOperation):
"storage resources, and keep network resources allocated.")
required_perms = ()
accept_states = ('RUNNING', )
resultant_state = 'SUSPENDED'
def is_preferred(self):
return (not self.instance.is_base and
......@@ -671,9 +667,6 @@ class SleepOperation(InstanceOperation):
else:
activity.resultant_state = 'ERROR'
def on_commit(self, activity):
activity.resultant_state = 'SUSPENDED'
def _operation(self, activity, timeout=240):
# Destroy networks
with activity.sub_activity('shutdown_net', readable_name=ugettext_noop(
......@@ -702,6 +695,7 @@ class WakeUpOperation(InstanceOperation):
"virtual machine from this state.")
required_perms = ()
accept_states = ('SUSPENDED', )
resultant_state = 'RUNNING'
def is_preferred(self):
return self.instance.status == self.instance.STATUS.SUSPENDED
......@@ -709,9 +703,6 @@ class WakeUpOperation(InstanceOperation):
def on_abort(self, activity, error):
activity.resultant_state = 'ERROR'
def on_commit(self, activity):
activity.resultant_state = 'RUNNING'
def _operation(self, activity, timeout=60):
# Schedule vm
self.instance.allocate_vnc_port()
......@@ -882,6 +873,7 @@ class RecoverOperation(InstanceOperation):
acl_level = "owner"
required_perms = ('vm.recover', )
accept_states = ('DESTROYED', )
resultant_state = 'PENDING'
def check_precond(self):
try:
......@@ -889,9 +881,6 @@ class RecoverOperation(InstanceOperation):
except Instance.InstanceDestroyedError:
pass
def on_commit(self, activity):
activity.resultant_state = 'PENDING'
def _operation(self):
for disk in self.instance.disks.all():
disk.destroyed = None
......
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