Commit 48b00b23 by Őry Máté

vm: save resultant_state at activity creation

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