Commit 0385e921 by Őry Máté

vm: add InstanceOperation.accept_states/deny_states

remove most check_precond() methods
parent 162c7f55
...@@ -45,6 +45,8 @@ class InstanceOperation(Operation): ...@@ -45,6 +45,8 @@ class InstanceOperation(Operation):
async_operation = abortable_async_instance_operation async_operation = abortable_async_instance_operation
host_cls = Instance host_cls = Instance
concurrency_check = True concurrency_check = True
accept_states = None
deny_states = None
def __init__(self, instance): def __init__(self, instance):
super(InstanceOperation, self).__init__(subject=instance) super(InstanceOperation, self).__init__(subject=instance)
...@@ -53,6 +55,20 @@ class InstanceOperation(Operation): ...@@ -53,6 +55,20 @@ class InstanceOperation(Operation):
def check_precond(self): def check_precond(self):
if self.instance.destroyed_at: if self.instance.destroyed_at:
raise self.instance.InstanceDestroyedError(self.instance) raise self.instance.InstanceDestroyedError(self.instance)
if self.accept_states:
if self.instance.status not in self.accept_states:
logger.debug("precond failed for %s: %s not in %s",
unicode(self.__class__),
unicode(self.instance.status),
unicode(self.accept_states))
raise self.instance.WrongStateError(self.instance)
if self.deny_states:
if self.instance.status in self.deny_states:
logger.debug("precond failed for %s: %s in %s",
unicode(self.__class__),
unicode(self.instance.status),
unicode(self.accept_states))
raise self.instance.WrongStateError(self.instance)
def check_auth(self, user): def check_auth(self, user):
if not self.instance.has_level(user, self.acl_level): if not self.instance.has_level(user, self.acl_level):
...@@ -95,6 +111,7 @@ class AddInterfaceOperation(InstanceOperation): ...@@ -95,6 +111,7 @@ class AddInterfaceOperation(InstanceOperation):
description = _("Add a new network interface for the specified VLAN to " description = _("Add a new network interface for the specified VLAN to "
"the VM.") "the VM.")
required_perms = () required_perms = ()
accept_states = ('STOPPED', 'PENDING', 'RUNNING')
def rollback(self, net, activity): def rollback(self, net, activity):
with activity.sub_activity( with activity.sub_activity(
...@@ -103,11 +120,6 @@ class AddInterfaceOperation(InstanceOperation): ...@@ -103,11 +120,6 @@ class AddInterfaceOperation(InstanceOperation):
net.destroy() net.destroy()
net.delete() net.delete()
def check_precond(self):
super(AddInterfaceOperation, self).check_precond()
if self.instance.status not in ['STOPPED', 'PENDING', 'RUNNING']:
raise self.instance.WrongStateError(self.instance)
def _operation(self, activity, user, system, vlan, managed=None): def _operation(self, activity, user, system, vlan, managed=None):
if not vlan.has_level(user, 'user'): if not vlan.has_level(user, 'user'):
raise humanize_exception(ugettext_noop( raise humanize_exception(ugettext_noop(
...@@ -144,11 +156,7 @@ class CreateDiskOperation(InstanceOperation): ...@@ -144,11 +156,7 @@ class CreateDiskOperation(InstanceOperation):
name = _("create disk") name = _("create disk")
description = _("Create empty disk for the VM.") description = _("Create empty disk for the VM.")
required_perms = ('storage.create_empty_disk', ) required_perms = ('storage.create_empty_disk', )
accept_states = ('STOPPED', 'PENDING', 'RUNNING')
def check_precond(self):
super(CreateDiskOperation, self).check_precond()
if self.instance.status not in ['STOPPED', 'PENDING', 'RUNNING']:
raise self.instance.WrongStateError(self.instance)
def _operation(self, user, size, activity, name=None): def _operation(self, user, size, activity, name=None):
from storage.models import Disk from storage.models import Disk
...@@ -186,11 +194,7 @@ class DownloadDiskOperation(InstanceOperation): ...@@ -186,11 +194,7 @@ class DownloadDiskOperation(InstanceOperation):
abortable = True abortable = True
has_percentage = True has_percentage = True
required_perms = ('storage.download_disk', ) required_perms = ('storage.download_disk', )
accept_states = ('STOPPED', 'PENDING', 'RUNNING')
def check_precond(self):
super(DownloadDiskOperation, self).check_precond()
if self.instance.status not in ['STOPPED', 'PENDING', 'RUNNING']:
raise self.instance.WrongStateError(self.instance)
def _operation(self, user, url, task, activity, name=None): def _operation(self, user, url, task, activity, name=None):
activity.result = url activity.result = url
...@@ -221,11 +225,7 @@ class DeployOperation(InstanceOperation): ...@@ -221,11 +225,7 @@ class DeployOperation(InstanceOperation):
name = _("deploy") name = _("deploy")
description = _("Deploy new virtual machine with network.") description = _("Deploy new virtual machine with network.")
required_perms = () required_perms = ()
deny_states = ('SUSPENDED', 'RUNNING')
def check_precond(self):
super(DeployOperation, self).check_precond()
if self.instance.status in ['RUNNING', 'SUSPENDED']:
raise self.instance.WrongStateError(self.instance)
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,
...@@ -326,6 +326,7 @@ class MigrateOperation(InstanceOperation): ...@@ -326,6 +326,7 @@ class MigrateOperation(InstanceOperation):
name = _("migrate") name = _("migrate")
description = _("Live migrate running VM to another node.") description = _("Live migrate running VM to another node.")
required_perms = () required_perms = ()
accept_states = ('RUNNING', )
def rollback(self, activity): def rollback(self, activity):
with activity.sub_activity( with activity.sub_activity(
...@@ -333,11 +334,6 @@ class MigrateOperation(InstanceOperation): ...@@ -333,11 +334,6 @@ class MigrateOperation(InstanceOperation):
"redeploy network (rollback)")): "redeploy network (rollback)")):
self.instance.deploy_net() self.instance.deploy_net()
def check_precond(self):
super(MigrateOperation, self).check_precond()
if self.instance.status not in ['RUNNING']:
raise self.instance.WrongStateError(self.instance)
def check_auth(self, user): def check_auth(self, user):
if not user.is_superuser: if not user.is_superuser:
raise PermissionDenied() raise PermissionDenied()
...@@ -387,11 +383,7 @@ class RebootOperation(InstanceOperation): ...@@ -387,11 +383,7 @@ class RebootOperation(InstanceOperation):
name = _("reboot") name = _("reboot")
description = _("Reboot virtual machine with Ctrl+Alt+Del signal.") description = _("Reboot virtual machine with Ctrl+Alt+Del signal.")
required_perms = () required_perms = ()
accept_states = ('RUNNING', )
def check_precond(self):
super(RebootOperation, self).check_precond()
if self.instance.status not in ['RUNNING']:
raise self.instance.WrongStateError(self.instance)
def _operation(self, timeout=5): def _operation(self, timeout=5):
self.instance.reboot_vm(timeout=timeout) self.instance.reboot_vm(timeout=timeout)
...@@ -406,11 +398,7 @@ class RemoveInterfaceOperation(InstanceOperation): ...@@ -406,11 +398,7 @@ class RemoveInterfaceOperation(InstanceOperation):
name = _("remove interface") name = _("remove interface")
description = _("Remove the specified network interface from the VM.") description = _("Remove the specified network interface from the VM.")
required_perms = () required_perms = ()
accept_states = ('STOPPED', 'PENDING', 'RUNNING')
def check_precond(self):
super(RemoveInterfaceOperation, self).check_precond()
if self.instance.status not in ['STOPPED', 'PENDING', 'RUNNING']:
raise self.instance.WrongStateError(self.instance)
def _operation(self, activity, user, system, interface): def _operation(self, activity, user, system, interface):
if self.instance.is_running: if self.instance.is_running:
...@@ -431,11 +419,7 @@ class RemoveDiskOperation(InstanceOperation): ...@@ -431,11 +419,7 @@ class RemoveDiskOperation(InstanceOperation):
name = _("remove disk") name = _("remove disk")
description = _("Remove the specified disk from the VM.") description = _("Remove the specified disk from the VM.")
required_perms = () required_perms = ()
accept_states = ('STOPPED', 'PENDING', 'RUNNING')
def check_precond(self):
super(RemoveDiskOperation, self).check_precond()
if self.instance.status not in ['STOPPED', 'PENDING', 'RUNNING']:
raise self.instance.WrongStateError(self.instance)
def _operation(self, activity, user, system, disk): def _operation(self, activity, user, system, disk):
if self.instance.is_running and disk.type not in ["iso"]: if self.instance.is_running and disk.type not in ["iso"]:
...@@ -453,11 +437,7 @@ class ResetOperation(InstanceOperation): ...@@ -453,11 +437,7 @@ class ResetOperation(InstanceOperation):
name = _("reset") name = _("reset")
description = _("Reset virtual machine (reset button).") description = _("Reset virtual machine (reset button).")
required_perms = () required_perms = ()
accept_states = ('RUNNING', )
def check_precond(self):
super(ResetOperation, self).check_precond()
if self.instance.status not in ['RUNNING']:
raise self.instance.WrongStateError(self.instance)
def _operation(self, timeout=5): def _operation(self, timeout=5):
self.instance.reset_vm(timeout=timeout) self.instance.reset_vm(timeout=timeout)
...@@ -476,6 +456,7 @@ class SaveAsTemplateOperation(InstanceOperation): ...@@ -476,6 +456,7 @@ class SaveAsTemplateOperation(InstanceOperation):
""") """)
abortable = True abortable = True
required_perms = ('vm.create_template', ) required_perms = ('vm.create_template', )
accept_states = ('RUNNING', 'PENDING', 'STOPPED')
def is_preferred(self): def is_preferred(self):
return (self.instance.is_base and return (self.instance.is_base and
...@@ -496,11 +477,6 @@ class SaveAsTemplateOperation(InstanceOperation): ...@@ -496,11 +477,6 @@ class SaveAsTemplateOperation(InstanceOperation):
for disk in self.disks: for disk in self.disks:
disk.destroy() disk.destroy()
def check_precond(self):
super(SaveAsTemplateOperation, self).check_precond()
if self.instance.status not in ['RUNNING', 'PENDING', 'STOPPED']:
raise self.instance.WrongStateError(self.instance)
def _operation(self, activity, user, system, timeout=300, name=None, def _operation(self, activity, user, system, timeout=300, name=None,
with_shutdown=True, task=None, **kwargs): with_shutdown=True, task=None, **kwargs):
if with_shutdown: if with_shutdown:
...@@ -570,11 +546,7 @@ class ShutdownOperation(InstanceOperation): ...@@ -570,11 +546,7 @@ class ShutdownOperation(InstanceOperation):
description = _("Shutdown virtual machine with ACPI signal.") description = _("Shutdown virtual machine with ACPI signal.")
abortable = True abortable = True
required_perms = () required_perms = ()
accept_states = ('RUNNING', )
def check_precond(self):
super(ShutdownOperation, self).check_precond()
if self.instance.status not in ['RUNNING']:
raise self.instance.WrongStateError(self.instance)
def on_commit(self, activity): def on_commit(self, activity):
activity.resultant_state = 'STOPPED' activity.resultant_state = 'STOPPED'
...@@ -594,11 +566,7 @@ class ShutOffOperation(InstanceOperation): ...@@ -594,11 +566,7 @@ class ShutOffOperation(InstanceOperation):
name = _("shut off") name = _("shut off")
description = _("Shut off VM (plug-out).") description = _("Shut off VM (plug-out).")
required_perms = () required_perms = ()
accept_states = ('RUNNING', )
def check_precond(self):
super(ShutOffOperation, self).check_precond()
if self.instance.status not in ['RUNNING']:
raise self.instance.WrongStateError(self.instance)
def on_commit(self, activity): def on_commit(self, activity):
activity.resultant_state = 'STOPPED' activity.resultant_state = 'STOPPED'
...@@ -626,16 +594,12 @@ class SleepOperation(InstanceOperation): ...@@ -626,16 +594,12 @@ class SleepOperation(InstanceOperation):
name = _("sleep") name = _("sleep")
description = _("Suspend virtual machine with memory dump.") description = _("Suspend virtual machine with memory dump.")
required_perms = () required_perms = ()
accept_states = ('RUNNING', )
def is_preferred(self): def is_preferred(self):
return (not self.instance.is_base and return (not self.instance.is_base and
self.instance.status == self.instance.STATUS.RUNNING) self.instance.status == self.instance.STATUS.RUNNING)
def check_precond(self):
super(SleepOperation, self).check_precond()
if self.instance.status not in ['RUNNING']:
raise self.instance.WrongStateError(self.instance)
def on_abort(self, activity, error): def on_abort(self, activity, error):
if isinstance(error, TimeLimitExceeded): if isinstance(error, TimeLimitExceeded):
activity.resultant_state = None activity.resultant_state = None
...@@ -673,15 +637,11 @@ class WakeUpOperation(InstanceOperation): ...@@ -673,15 +637,11 @@ class WakeUpOperation(InstanceOperation):
Power on Virtual Machine and load its memory from dump. Power on Virtual Machine and load its memory from dump.
""") """)
required_perms = () required_perms = ()
accept_states = ('SUSPENDED', )
def is_preferred(self): def is_preferred(self):
return self.instance.status == self.instance.STATUS.SUSPENDED return self.instance.status == self.instance.STATUS.SUSPENDED
def check_precond(self):
super(WakeUpOperation, self).check_precond()
if self.instance.status not in ['SUSPENDED']:
raise self.instance.WrongStateError(self.instance)
def on_abort(self, activity, error): def on_abort(self, activity, error):
activity.resultant_state = 'ERROR' activity.resultant_state = 'ERROR'
...@@ -721,11 +681,6 @@ class RenewOperation(InstanceOperation): ...@@ -721,11 +681,6 @@ class RenewOperation(InstanceOperation):
required_perms = () required_perms = ()
concurrency_check = False concurrency_check = False
def check_precond(self):
super(RenewOperation, self).check_precond()
if self.instance.status == 'DESTROYED':
raise self.instance.WrongStateError(self.instance)
def _operation(self, lease=None): def _operation(self, lease=None):
(self.instance.time_of_suspend, (self.instance.time_of_suspend,
self.instance.time_of_delete) = self.instance.get_renew_times(lease) self.instance.time_of_delete) = self.instance.get_renew_times(lease)
...@@ -819,11 +774,7 @@ class ScreenshotOperation(InstanceOperation): ...@@ -819,11 +774,7 @@ class ScreenshotOperation(InstanceOperation):
description = _("Get screenshot") description = _("Get screenshot")
acl_level = "owner" acl_level = "owner"
required_perms = () required_perms = ()
accept_states = ('RUNNING', )
def check_precond(self):
super(ScreenshotOperation, self).check_precond()
if self.instance.status not in ['RUNNING']:
raise self.instance.WrongStateError(self.instance)
def _operation(self): def _operation(self):
return self.instance.get_screenshot(timeout=20) return self.instance.get_screenshot(timeout=20)
...@@ -839,10 +790,13 @@ class RecoverOperation(InstanceOperation): ...@@ -839,10 +790,13 @@ class RecoverOperation(InstanceOperation):
description = _("Recover virtual machine from destroyed state.") description = _("Recover virtual machine from destroyed state.")
acl_level = "owner" acl_level = "owner"
required_perms = ('vm.recover', ) required_perms = ('vm.recover', )
accept_states = ('DESTROYED', )
def check_precond(self): def check_precond(self):
if not self.instance.destroyed_at: try:
raise self.instance.WrongStateError(self.instance) super(RecoverOperation, self).check_precond()
except Instance.InstanceDestroyedError:
pass
def on_commit(self, activity): def on_commit(self, activity):
activity.resultant_state = 'PENDING' activity.resultant_state = 'PENDING'
...@@ -866,11 +820,7 @@ class ResourcesOperation(InstanceOperation): ...@@ -866,11 +820,7 @@ class ResourcesOperation(InstanceOperation):
description = _("Change resources") description = _("Change resources")
acl_level = "owner" acl_level = "owner"
required_perms = ('vm.change_resources', ) required_perms = ('vm.change_resources', )
accept_states = ('STOPPED', 'PENDING', )
def check_precond(self):
super(ResourcesOperation, self).check_precond()
if self.instance.status not in ["STOPPED", "PENDING"]:
raise self.instance.WrongStateError(self.instance)
def _operation(self, user, num_cores, ram_size, max_ram_size, priority): def _operation(self, user, num_cores, ram_size, max_ram_size, priority):
...@@ -893,11 +843,7 @@ class PasswordResetOperation(InstanceOperation): ...@@ -893,11 +843,7 @@ class PasswordResetOperation(InstanceOperation):
description = _("Password reset") description = _("Password reset")
acl_level = "owner" acl_level = "owner"
required_perms = () required_perms = ()
accept_states = ('RUNNING', )
def check_precond(self):
super(PasswordResetOperation, self).check_precond()
if self.instance.status not in ["RUNNING"]:
raise self.instance.WrongStateError(self.instance)
def _operation(self): def _operation(self):
self.instance.pw = pwgen() self.instance.pw = pwgen()
......
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