Commit 31624382 authored by Iustin Pop's avatar Iustin Pop
Browse files

Abstract check that an instance is down



Multiple LUs require that an instance is not running while they operate
on the instance (reinstall, rename, modify, recreate disks, deactivate
disks). The code to do this check is duplicate many times, and not very
consistent (some use call_instance_list, some call_instance_info).

The patch moves this check into a separate function that is then reused.
The only drawback is that _SafeShutdowInstanceDisks now raises an
OpPrereqError (even though it is run during Exec()), but this use case
is fine (there are no other modifications in that Exec).
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent 701384a9
......@@ -552,6 +552,22 @@ def _CheckDiskTemplate(template):
raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
def _CheckInstanceDown(lu, instance, reason):
"""Ensure that an instance is not running."""
if instance.admin_up:
raise errors.OpPrereqError("Instance %s is marked to be up, %s" %
(instance.name, reason), errors.ECODE_STATE)
pnode = instance.primary_node
ins_l = lu.rpc.call_instance_list([pnode], [instance.hypervisor])[pnode]
ins_l.Raise("Can't contact node %s for instance information" % pnode,
prereq=True, ecode=errors.ECODE_ENVIRON)
if instance.name in ins_l.payload:
raise errors.OpPrereqError("Instance %s is running, %s" %
(instance.name, reason), errors.ECODE_STATE)
def _ExpandItemName(fn, name, kind):
"""Expand an item name.
......@@ -3730,14 +3746,7 @@ def _SafeShutdownInstanceDisks(lu, instance):
_ShutdownInstanceDisks.
"""
pnode = instance.primary_node
ins_l = lu.rpc.call_instance_list([pnode], [instance.hypervisor])[pnode]
ins_l.Raise("Can't contact node %s" % pnode)
if instance.name in ins_l.payload:
raise errors.OpExecError("Instance is running, can't shutdown"
" block devices.")
_CheckInstanceDown(lu, instance, "cannot shutdown disks")
_ShutdownInstanceDisks(lu, instance)
......@@ -4123,20 +4132,7 @@ class LUReinstallInstance(LogicalUnit):
raise errors.OpPrereqError("Instance '%s' has no disks" %
self.op.instance_name,
errors.ECODE_INVAL)
if instance.admin_up:
raise errors.OpPrereqError("Instance '%s' is marked to be up" %
self.op.instance_name,
errors.ECODE_STATE)
remote_info = self.rpc.call_instance_info(instance.primary_node,
instance.name,
instance.hypervisor)
remote_info.Raise("Error checking node %s" % instance.primary_node,
prereq=True, ecode=errors.ECODE_ENVIRON)
if remote_info.payload:
raise errors.OpPrereqError("Instance '%s' is running on the node %s" %
(self.op.instance_name,
instance.primary_node),
errors.ECODE_STATE)
_CheckInstanceDown(self, instance, "cannot reinstall")
self.op.os_type = getattr(self.op, "os_type", None)
self.op.force_variant = getattr(self.op, "force_variant", False)
......@@ -4223,18 +4219,7 @@ class LURecreateInstanceDisks(LogicalUnit):
if instance.disk_template == constants.DT_DISKLESS:
raise errors.OpPrereqError("Instance '%s' has no disks" %
self.op.instance_name, errors.ECODE_INVAL)
if instance.admin_up:
raise errors.OpPrereqError("Instance '%s' is marked to be up" %
self.op.instance_name, errors.ECODE_STATE)
remote_info = self.rpc.call_instance_info(instance.primary_node,
instance.name,
instance.hypervisor)
remote_info.Raise("Error checking node %s" % instance.primary_node,
prereq=True, ecode=errors.ECODE_ENVIRON)
if remote_info.payload:
raise errors.OpPrereqError("Instance '%s' is running on the node %s" %
(self.op.instance_name,
instance.primary_node), errors.ECODE_STATE)
_CheckInstanceDown(self, instance, "cannot recreate disks")
if not self.op.disks:
self.op.disks = range(len(instance.disks))
......@@ -4289,19 +4274,7 @@ class LURenameInstance(LogicalUnit):
instance = self.cfg.GetInstanceInfo(self.op.instance_name)
assert instance is not None
_CheckNodeOnline(self, instance.primary_node)
if instance.admin_up:
raise errors.OpPrereqError("Instance '%s' is marked to be up" %
self.op.instance_name, errors.ECODE_STATE)
remote_info = self.rpc.call_instance_info(instance.primary_node,
instance.name,
instance.hypervisor)
remote_info.Raise("Error checking node %s" % instance.primary_node,
prereq=True, ecode=errors.ECODE_ENVIRON)
if remote_info.payload:
raise errors.OpPrereqError("Instance '%s' is running on the node %s" %
(self.op.instance_name,
instance.primary_node), errors.ECODE_STATE)
_CheckInstanceDown(self, instance, "cannot rename")
self.instance = instance
# new name verification
......@@ -8137,17 +8110,8 @@ class LUSetInstanceParams(LogicalUnit):
if disk_op == constants.DDM_REMOVE:
if len(instance.disks) == 1:
raise errors.OpPrereqError("Cannot remove the last disk of"
" an instance",
errors.ECODE_INVAL)
ins_l = self.rpc.call_instance_list([pnode], [instance.hypervisor])
ins_l = ins_l[pnode]
msg = ins_l.fail_msg
if msg:
raise errors.OpPrereqError("Can't contact node %s: %s" %
(pnode, msg), errors.ECODE_ENVIRON)
if instance.name in ins_l.payload:
raise errors.OpPrereqError("Instance is running, can't remove"
" disks.", errors.ECODE_STATE)
" an instance", errors.ECODE_INVAL)
_CheckInstanceDown(self, instance, "cannot remove disks")
if (disk_op == constants.DDM_ADD and
len(instance.nics) >= constants.MAX_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 to comment