Commit 2a02d6fe authored by Helga Velroyen's avatar Helga Velroyen
Browse files

Remove instance query python code



This patch removes the python code for the instance
queries. So far, it replaces it by 'NotImplemented'
exceptions. In a later patch of this series, the
remaining part is remove completely.
Signed-off-by: default avatarHelga Velroyen <helgav@google.com>
Reviewed-by: default avatarHrvoje Ribicic <riba@google.com>
parent 20fb929a
......@@ -22,180 +22,27 @@
"""Logical units for querying instances."""
import itertools
import logging
import operator
from ganeti import compat
from ganeti import constants
from ganeti import locking
from ganeti import qlang
from ganeti import query
from ganeti.cmdlib.base import QueryBase, NoHooksLU
from ganeti.cmdlib.common import ShareAll, GetWantedInstances, \
CheckInstanceNodeGroups, CheckInstancesNodeGroups, AnnotateDiskParams
from ganeti.cmdlib.instance_operation import GetInstanceConsole
CheckInstancesNodeGroups, AnnotateDiskParams
from ganeti.cmdlib.instance_utils import NICListToTuple
from ganeti.hypervisor import hv_base
import ganeti.masterd.instance
class InstanceQuery(QueryBase):
FIELDS = query.INSTANCE_FIELDS
def ExpandNames(self, lu):
lu.needed_locks = {}
lu.share_locks = ShareAll()
if self.names:
(_, self.wanted) = GetWantedInstances(lu, self.names)
else:
self.wanted = locking.ALL_SET
self.do_locking = (self.use_locking and
query.IQ_LIVE in self.requested_data)
if self.do_locking:
lu.needed_locks[locking.LEVEL_INSTANCE] = self.wanted
lu.needed_locks[locking.LEVEL_NODEGROUP] = []
lu.needed_locks[locking.LEVEL_NODE] = []
lu.needed_locks[locking.LEVEL_NETWORK] = []
lu.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
self.do_grouplocks = (self.do_locking and
query.IQ_NODES in self.requested_data)
raise NotImplementedError
def DeclareLocks(self, lu, level):
if self.do_locking:
if level == locking.LEVEL_NODEGROUP and self.do_grouplocks:
assert not lu.needed_locks[locking.LEVEL_NODEGROUP]
# Lock all groups used by instances optimistically; this requires going
# via the node before it's locked, requiring verification later on
lu.needed_locks[locking.LEVEL_NODEGROUP] = \
set(group_uuid
for instance_name in lu.owned_locks(locking.LEVEL_INSTANCE)
for group_uuid in
lu.cfg.GetInstanceNodeGroups(
lu.cfg.GetInstanceInfoByName(instance_name).uuid))
elif level == locking.LEVEL_NODE:
lu._LockInstancesNodes() # pylint: disable=W0212
elif level == locking.LEVEL_NETWORK:
lu.needed_locks[locking.LEVEL_NETWORK] = \
frozenset(net_uuid
for instance_name in lu.owned_locks(locking.LEVEL_INSTANCE)
for net_uuid in
lu.cfg.GetInstanceNetworks(
lu.cfg.GetInstanceInfoByName(instance_name).uuid))
@staticmethod
def _CheckGroupLocks(lu):
owned_instance_names = frozenset(lu.owned_locks(locking.LEVEL_INSTANCE))
owned_groups = frozenset(lu.owned_locks(locking.LEVEL_NODEGROUP))
# Check if node groups for locked instances are still correct
for instance_name in owned_instance_names:
instance = lu.cfg.GetInstanceInfoByName(instance_name)
CheckInstanceNodeGroups(lu.cfg, instance.uuid, owned_groups)
def _GetQueryData(self, lu):
"""Computes the list of instances and their attributes.
"""
if self.do_grouplocks:
self._CheckGroupLocks(lu)
cluster = lu.cfg.GetClusterInfo()
insts_by_name = dict((inst.name, inst) for
inst in lu.cfg.GetAllInstancesInfo().values())
instance_names = self._GetNames(lu, insts_by_name.keys(),
locking.LEVEL_INSTANCE)
instance_list = [insts_by_name[node] for node in instance_names]
node_uuids = frozenset(itertools.chain(*(inst.all_nodes
for inst in instance_list)))
hv_list = list(set([inst.hypervisor for inst in instance_list]))
bad_node_uuids = []
offline_node_uuids = []
wrongnode_inst_uuids = set()
# Gather data as requested
if self.requested_data & set([query.IQ_LIVE, query.IQ_CONSOLE]):
live_data = {}
node_data = lu.rpc.call_all_instances_info(node_uuids, hv_list,
cluster.hvparams)
for node_uuid in node_uuids:
result = node_data[node_uuid]
if result.offline:
# offline nodes will be in both lists
assert result.fail_msg
offline_node_uuids.append(node_uuid)
if result.fail_msg:
bad_node_uuids.append(node_uuid)
elif result.payload:
for inst_name in result.payload:
if inst_name in insts_by_name:
instance = insts_by_name[inst_name]
if instance.primary_node == node_uuid:
for iname in result.payload:
live_data[insts_by_name[iname].uuid] = result.payload[iname]
else:
wrongnode_inst_uuids.add(instance.uuid)
else:
# orphan instance; we don't list it here as we don't
# handle this case yet in the output of instance listing
logging.warning("Orphan instance '%s' found on node %s",
inst_name, lu.cfg.GetNodeName(node_uuid))
# else no instance is alive
else:
live_data = {}
if query.IQ_DISKUSAGE in self.requested_data:
gmi = ganeti.masterd.instance
disk_usage = dict((inst.uuid,
gmi.ComputeDiskSize(inst.disk_template,
[{constants.IDISK_SIZE: disk.size}
for disk in inst.disks]))
for inst in instance_list)
else:
disk_usage = None
if query.IQ_CONSOLE in self.requested_data:
consinfo = {}
for inst in instance_list:
if inst.uuid in live_data:
# Instance is running
node = lu.cfg.GetNodeInfo(inst.primary_node)
group = lu.cfg.GetNodeGroup(node.group)
consinfo[inst.uuid] = \
GetInstanceConsole(cluster, inst, node, group)
else:
consinfo[inst.uuid] = None
else:
consinfo = None
if query.IQ_NODES in self.requested_data:
nodes = dict(lu.cfg.GetMultiNodeInfo(node_uuids))
groups = dict((uuid, lu.cfg.GetNodeGroup(uuid))
for uuid in set(map(operator.attrgetter("group"),
nodes.values())))
else:
nodes = None
groups = None
if query.IQ_NETWORKS in self.requested_data:
net_uuids = itertools.chain(*(lu.cfg.GetInstanceNetworks(i.uuid)
for i in instance_list))
networks = dict((uuid, lu.cfg.GetNetwork(uuid)) for uuid in net_uuids)
else:
networks = None
return query.InstanceQueryData(instance_list, lu.cfg.GetClusterInfo(),
disk_usage, offline_node_uuids,
bad_node_uuids, live_data,
wrongnode_inst_uuids, consinfo, nodes,
groups, networks)
raise NotImplementedError
class LUInstanceQuery(NoHooksLU):
......@@ -206,17 +53,16 @@ class LUInstanceQuery(NoHooksLU):
REQ_BGL = False
def CheckArguments(self):
self.iq = InstanceQuery(qlang.MakeSimpleFilter("name", self.op.names),
self.op.output_fields, self.op.use_locking)
raise NotImplementedError
def ExpandNames(self):
self.iq.ExpandNames(self)
raise NotImplementedError
def DeclareLocks(self, level):
self.iq.DeclareLocks(self, level)
raise NotImplementedError
def Exec(self, feedback_fn):
return self.iq.OldStyleQuery(self)
raise NotImplementedError
class LUInstanceQueryData(NoHooksLU):
......
......@@ -26,7 +26,6 @@ from ganeti import errors
from ganeti import query
from ganeti.cmdlib.base import NoHooksLU
from ganeti.cmdlib.cluster import ClusterQuery
from ganeti.cmdlib.instance_query import InstanceQuery
from ganeti.cmdlib.misc import ExtStorageQuery
from ganeti.cmdlib.operating_system import OsQuery
......@@ -34,7 +33,6 @@ from ganeti.cmdlib.operating_system import OsQuery
#: Query type implementations
_QUERY_IMPL = {
constants.QR_CLUSTER: ClusterQuery,
constants.QR_INSTANCE: InstanceQuery,
constants.QR_OS: OsQuery,
constants.QR_EXTSTORAGE: ExtStorageQuery,
}
......
......@@ -3337,7 +3337,6 @@ qrOs = "os"
qrViaOp :: FrozenSet String
qrViaOp =
ConstantUtils.mkSet [qrCluster,
qrInstance,
qrOs,
qrExtstorage]
......@@ -3345,6 +3344,7 @@ qrViaOp =
qrViaLuxi :: FrozenSet String
qrViaLuxi = ConstantUtils.mkSet [qrGroup,
qrExport,
qrInstance,
qrJob,
qrLock,
qrNetwork,
......
......@@ -137,7 +137,7 @@ class TestLUQuery(unittest.TestCase):
sorted(constants.QR_VIA_OP))
assert constants.QR_NODE in constants.QR_VIA_LUXI
assert constants.QR_INSTANCE in constants.QR_VIA_OP
assert constants.QR_INSTANCE in constants.QR_VIA_LUXI
for i in constants.QR_VIA_OP:
self.assert_(query._GetQueryImplementation(i))
......
......@@ -23,46 +23,10 @@
"""
from ganeti import opcodes
from ganeti import query
from testsupport import *
import testutils
class TestLUInstanceQuery(CmdlibTestCase):
def setUp(self):
super(TestLUInstanceQuery, self).setUp()
self.inst = self.cfg.AddNewInstance()
self.fields = query._BuildInstanceFields().keys()
def testInvalidInstance(self):
op = opcodes.OpInstanceQuery(names=["does_not_exist"],
output_fields=self.fields)
self.ExecOpCodeExpectOpPrereqError(
op, "Instance 'does_not_exist' not known")
def testQueryInstance(self):
op = opcodes.OpInstanceQuery(output_fields=self.fields)
self.ExecOpCode(op)
class TestLUInstanceQueryData(CmdlibTestCase):
def setUp(self):
super(TestLUInstanceQueryData, self).setUp()
self.inst = self.cfg.AddNewInstance()
def testQueryInstanceData(self):
op = opcodes.OpInstanceQueryData()
self.ExecOpCode(op)
def testQueryStaticInstanceData(self):
op = opcodes.OpInstanceQueryData(static=True)
self.ExecOpCode(op)
if __name__ == "__main__":
testutils.GanetiTestProgram()
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