Commit 4986c93b authored by Hrvoje Ribicic's avatar Hrvoje Ribicic
Browse files

Factor out and alter the instance running check

Examining whether an instance is running is useful outside the instance
state check, especially as the check relies on the admin_state recorded
within the configuration, which the code treats as the desired rather
than the actual state. This patch pulls the check out into a separate
function. It also updates the function to account for possible user
shutdowns, if the right option is passed.
Signed-off-by: default avatarHrvoje Ribicic <>
Reviewed-by: default avatarJose A. Lopes <>
parent d6e1e696
......@@ -32,8 +32,8 @@ from ganeti import locking
from ganeti import objects
from ganeti import opcodes
from ganeti import pathutils
from ganeti.serializer import Private
import ganeti.rpc.node as rpc
from ganeti.serializer import Private
from ganeti import ssconf
from ganeti import utils
......@@ -1008,6 +1008,40 @@ def IsExclusiveStorageEnabledNode(cfg, node):
return cfg.GetNdParams(node)[constants.ND_EXCLUSIVE_STORAGE]
def IsInstanceRunning(lu, instance, check_user_shutdown=False):
""" Given an instance object, checks if the instance is running.
pnode_uuid = instance.primary_node
# We assume that the instance is offline if the node is offline
if lu.cfg.GetNodeInfo(pnode_uuid).offline:
return False
all_hvparams = lu.cfg.GetClusterInfo().hvparams
instance_list = lu.rpc.call_instance_list([pnode_uuid], [instance.hypervisor],
instance_list.Raise("Can't contact node %s for instance information" %
prereq=True, ecode=errors.ECODE_ENVIRON)
if not in instance_list.payload:
return False
if check_user_shutdown:
# One more check to be made - whether the instance was shutdown by the user
full_hvparams = lu.cfg.GetClusterInfo().FillHV(lu.instance)
inst_info = lu.rpc.call_instance_info(pnode_uuid,,
instance.hypervisor, full_hvparams)
inst_info.Raise("Can't retrieve instance information for instance %s" %, prereq=True, ecode=errors.ECODE_ENVIRON)
return inst_info.payload["state"] != \
return True
def CheckInstanceState(lu, instance, req_states, msg=None):
"""Ensure that an instance is in one of the required states.
......@@ -1027,14 +1061,9 @@ def CheckInstanceState(lu, instance, req_states, msg=None):
if constants.ADMINST_UP not in req_states:
pnode_uuid = instance.primary_node
# Replicating the offline check
if not lu.cfg.GetNodeInfo(pnode_uuid).offline:
all_hvparams = lu.cfg.GetClusterInfo().hvparams
ins_l = lu.rpc.call_instance_list(
[pnode_uuid], [instance.hypervisor], all_hvparams)[pnode_uuid]
ins_l.Raise("Can't contact node %s for instance information" %
prereq=True, ecode=errors.ECODE_ENVIRON)
if in ins_l.payload:
if IsInstanceRunning(lu, instance):
raise errors.OpPrereqError("Instance %s is running, %s" %
(, msg), errors.ECODE_STATE)
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