Package ganeti :: Module query
[hide private]
[frames] | no frames]

Module query

source code

Module for query operations

How it works:


Attention: Retrieval functions must be idempotent. They can be called multiple times, in any order and any number of times.

Classes [hide private]
  _FilterHints
Class for filter analytics.
  _FilterCompilerHelper
Converts a query filter to a callable usable for filtering.
  Query
  NodeQueryData
Data container for node data queries.
  InstanceQueryData
Data container for instance data queries.
  LockQueryData
Data container for lock data queries.
  GroupQueryData
Data container for node group data queries.
  OsInfo
  ClusterQueryData
Functions [hide private]
 
_GetUnknownField(ctx, item)
Gets the contents of an unknown field.
source code
 
_GetQueryFields(fielddefs, selected)
Calculates the internal list of selected fields.
source code
list of objects.QueryFieldDefinition
GetAllFields(fielddefs)
Extract objects.QueryFieldDefinition from field definitions.
source code
 
_WrapLogicOp(op_fn, sentences, ctx, item)
Wrapper for logic operator functions.
source code
 
_WrapUnaryOp(op_fn, inner, ctx, item)
Wrapper for unary operator functions.
source code
 
_WrapBinaryOp(op_fn, retrieval_fn, value, ctx, item)
Wrapper for binary operator functions.
source code
 
_WrapNot(fn, lhs, rhs)
Negates the result of a wrapped function.
source code
 
_PrepareRegex(pattern)
Compiles a regular expression.
source code
callable
_CompileFilter(fields, hints, qfilter)
Converts a query filter into a callable function.
source code
 
_ProcessResult(value)
Converts result values into externally-visible ones.
source code
 
_VerifyResultRow(fields, row)
Verifies the contents of a query result row.
source code
 
_FieldDictKey((fdef, _, flags, fn))
Generates key for field dictionary.
source code
dict
_PrepareFieldList(fields, aliases)
Prepares field list for use by Query.
source code
 
GetQueryResponse(query, ctx, sort_by_name=True)
Prepares the response for a query.
source code
 
QueryFields(fielddefs, selected)
Returns list of available fields.
source code
 
_MakeField(name, title, kind, doc)
Wrapper for creating objects.QueryFieldDefinition instances.
source code
 
_StaticValueInner(value, ctx, _)
Returns a static value.
source code
 
_StaticValue(value)
Prepares a function to return a static value.
source code
 
_GetNodeRole(node, master_name)
Determine node role.
source code
 
_GetItemAttr(attr)
Returns a field function to return an attribute of the item.
source code
 
_GetNDParam(name)
Return a field function to return an ND parameter out of the context.
source code
 
_BuildNDFields(is_group)
Builds all the ndparam fields.
source code
 
_ConvWrapInner(convert, fn, ctx, item)
Wrapper for converting values.
source code
 
_ConvWrap(convert, fn)
Convenience wrapper for _ConvWrapInner.
source code
 
_GetItemTimestamp(getter)
Returns function for getting timestamp of item.
source code
 
_GetItemTimestampFields(datatype)
Returns common timestamp fields.
source code
 
_GetGroup(cb)
Build function for calling another function with an node group.
source code
 
_GetNodeGroup(ctx, node, ng)
Returns the name of a node's group.
source code
 
_GetNodePower(ctx, node)
Returns the node powered state
source code
 
_GetNdParams(ctx, node, ng)
Returns the ndparams for this node.
source code
 
_GetLiveNodeField(field, kind, ctx, node)
Gets the value of a "live" field from NodeQueryData.
source code
 
_GetNodeHvState(_, node)
Converts node's hypervisor state for query result.
source code
 
_GetNodeDiskState(_, node)
Converts node's disk state for query result.
source code
 
_BuildNodeFields()
Builds list of fields for node queries.
source code
 
_GetInstOperState(ctx, inst)
Get instance's operational status.
source code
 
_GetInstLiveData(name)
Build function for retrieving live data.
source code
 
_GetInstStatus(ctx, inst)
Get instance status.
source code
 
_GetInstDiskSize(index)
Build function for retrieving disk size.
source code
 
_GetInstNic(index, cb)
Build function for calling another function with an instance NIC.
source code
 
_GetInstNicIp(ctx, _, nic)
Get a NIC's IP address.
source code
 
_GetInstNicBridge(ctx, index, _)
Get a NIC's bridge.
source code
 
_GetInstAllNicBridges(ctx, inst)
Get all network bridges for an instance.
source code
 
_GetInstNicParam(name)
Build function for retrieving a NIC parameter.
source code
 
_GetInstanceNetworkFields()
Get instance fields involving network interfaces.
source code
 
_GetInstDiskUsage(ctx, inst)
Get disk usage for an instance.
source code
 
_GetInstanceConsole(ctx, inst)
Get console information for instance.
source code
 
_GetInstanceDiskFields()
Get instance fields involving disks.
source code
 
_GetInstanceParameterFields()
Get instance fields involving parameters.
source code
 
_GetInstNodeGroup(ctx, default, node_name)
Gets group UUID of an instance node.
source code
 
_GetInstNodeGroupName(ctx, default, node_name)
Gets group name of an instance node.
source code
 
_BuildInstanceFields()
Builds list of fields for instance queries.
source code
 
_GetLockOwners(_, data)
Returns a sorted list of a lock's current owners.
source code
 
_GetLockPending(_, data)
Returns a sorted list of a lock's pending acquires.
source code
 
_BuildLockFields()
Builds list of fields for lock queries.
source code
 
_BuildGroupFields()
Builds list of fields for node group queries.
source code
 
_BuildOsFields()
Builds list of fields for operating system queries.
source code
 
_JobUnavailInner(fn, ctx, (job_id, job))
Return _FS_UNAVAIL if job is None.
source code
 
_JobUnavail(inner)
Wrapper for _JobUnavailInner.
source code
 
_PerJobOpInner(fn, job)
Executes a function per opcode in a job.
source code
 
_PerJobOp(fn)
Wrapper for _PerJobOpInner.
source code
 
_JobTimestampInner(fn, job)
Converts unavailable timestamp to _FS_UNAVAIL.
source code
 
_JobTimestamp(fn)
Wrapper for _JobTimestampInner.
source code
 
_BuildJobFields()
Builds list of fields for job queries.
source code
 
_GetExportName(_, (node_name, expname))
Returns an export name if available.
source code
 
_BuildExportFields()
Builds list of fields for exports.
source code
 
_ClusterWatcherPause(ctx, _)
Returns until when watcher is paused (if available).
source code
 
_BuildClusterFields()
Builds list of fields for cluster information.
source code
Variables [hide private]
  QFF_HOSTNAME = 0x01
  QFF_IP_ADDRESS = 0x02
  QFF_ALL = QFF_HOSTNAME | QFF_IP_ADDRESS
  FIELD_NAME_RE = re.compile(r"^[a-z0-9/._]+$")
  TITLE_RE = re.compile(r"^[^\s]+$")
  DOC_RE = re.compile(r"^[A-Z].*[^.,?!]$")
  _VERIFY_FN = {QFT_UNKNOWN: ht.TNone, QFT_TEXT: ht.TString, QFT...
Verification function for each field type
  _FS_UNKNOWN = object()
  _FS_NODATA = object()
  _FS_UNAVAIL = object()
  _FS_OFFLINE = object()
  _FS_ALL = frozenset([_FS_UNKNOWN, _FS_NODATA, _FS_UNAVAIL, _FS...
List of all special status
  _VTToQFT = {constants.VTYPE_STRING: QFT_OTHER, constants.VTYPE...
VType to QFT mapping
  _SERIAL_NO_DOC = "%s object serial number, incremented on each...
  NDP_TITLE = {constants.ND_OOB_PROGRAM: "OutOfBandProgram", con...
  _NODE_SIMPLE_FIELDS = {"drained":("Drained", QFT_BOOL, 0, "Whe...
Fields that are direct attributes of an objects.Node object
  _NODE_LIVE_FIELDS = {"bootid":("BootID", QFT_TEXT, "bootid", "...
Fields requiring talking to the node
  _INST_SIMPLE_FIELDS = {"disk_template":("Disk_template", QFT_T...
  _GROUP_SIMPLE_FIELDS = {"alloc_policy":("AllocPolicy", QFT_TEX...
  _CLUSTER_VERSION_FIELDS = {"software_version":("SoftwareVersio...
  _CLUSTER_SIMPLE_FIELDS = {"cluster_name":("Name", QFT_TEXT, QF...
  CLUSTER_FIELDS = _BuildClusterFields()
Fields for cluster information
  NODE_FIELDS = _BuildNodeFields()
Fields available for node queries
  INSTANCE_FIELDS = _BuildInstanceFields()
Fields available for instance queries
  LOCK_FIELDS = _BuildLockFields()
Fields available for lock queries
  GROUP_FIELDS = _BuildGroupFields()
Fields available for node group queries
  OS_FIELDS = _BuildOsFields()
Fields available for operating system queries
  JOB_FIELDS = _BuildJobFields()
Fields available for job queries
  EXPORT_FIELDS = _BuildExportFields()
Fields available for exports
  ALL_FIELDS = {constants.QR_CLUSTER: CLUSTER_FIELDS, constants....
All available resources
  ALL_FIELD_LISTS = ALL_FIELDS.values()
All available field lists

Imports: logging, operator, re, constants, errors, utils, compat, objects, ht, runtime, qlang, QFT_UNKNOWN, QFT_TEXT, QFT_BOOL, QFT_NUMBER, QFT_UNIT, QFT_TIMESTAMP, QFT_OTHER, RS_NORMAL, RS_UNKNOWN, RS_NODATA, RS_UNAVAIL, RS_OFFLINE


Function Details [hide private]

_GetQueryFields(fielddefs, selected)

source code 

Calculates the internal list of selected fields.

Unknown fields are returned as constants.QFT_UNKNOWN.

Parameters:
  • fielddefs (dict) - Field definitions
  • selected (list of strings) - List of selected fields

_CompileFilter(fields, hints, qfilter)

source code 

Converts a query filter into a callable function.

See _FilterCompilerHelper for details.

Returns: callable

_VerifyResultRow(fields, row)

source code 

Verifies the contents of a query result row.

Parameters:
  • fields (list) - Field definitions for result
  • row (list of tuples) - Row data

_PrepareFieldList(fields, aliases)

source code 

Prepares field list for use by Query.

Converts the list to a dictionary and does some verification.

Parameters:
  • fields (list of tuples; (objects.QueryFieldDefinition, data kind, retrieval function)) - List of fields, see Query.__init__ for a better description
  • aliases (list of tuples; (alias, target)) - list of tuples containing aliases; for each alias/target pair, a duplicate will be created in the field list
Returns: dict
Field dictionary for Query

GetQueryResponse(query, ctx, sort_by_name=True)

source code 

Prepares the response for a query.

Parameters:
  • ctx - Data container, see Query.Query
  • sort_by_name (boolean) - Whether to sort by name or keep the input data's ordering
  • query (Query)

QueryFields(fielddefs, selected)

source code 

Returns list of available fields.

Parameters:
  • fielddefs (dict) - Field definitions
  • selected (list of strings) - List of selected fields
Returns:
List of objects.QueryFieldDefinition

_MakeField(name, title, kind, doc)

source code 

Wrapper for creating objects.QueryFieldDefinition instances.

Parameters:
  • name - Field name as a regular expression
  • title - Human-readable title
  • kind - Field type
  • doc - Human-readable description

_GetNodeRole(node, master_name)

source code 

Determine node role.

Parameters:
  • node (objects.Node) - Node object
  • master_name (string) - Master node name

_GetItemAttr(attr)

source code 

Returns a field function to return an attribute of the item.

Parameters:
  • attr - Attribute name

_BuildNDFields(is_group)

source code 

Builds all the ndparam fields.

Parameters:
  • is_group - whether this is called at group or node level

_ConvWrapInner(convert, fn, ctx, item)

source code 

Wrapper for converting values.

Parameters:
  • convert - Conversion function receiving value as single parameter
  • fn - Retrieval function

_ConvWrap(convert, fn)

source code 

Convenience wrapper for _ConvWrapInner.

Parameters:
  • convert - Conversion function receiving value as single parameter
  • fn - Retrieval function

_GetItemTimestamp(getter)

source code 

Returns function for getting timestamp of item.

Parameters:
  • getter (callable) - Function to retrieve timestamp attribute

_GetItemTimestampFields(datatype)

source code 

Returns common timestamp fields.

Parameters:

_GetGroup(cb)

source code 

Build function for calling another function with an node group.

Parameters:
  • cb - The callback to be called with the nodegroup

_GetNodeGroup(ctx, node, ng)

source code 

Returns the name of a node's group.

Parameters:

_GetNodePower(ctx, node)

source code 

Returns the node powered state

Parameters:

_GetNdParams(ctx, node, ng)

source code 

Returns the ndparams for this node.

Parameters:

_GetLiveNodeField(field, kind, ctx, node)

source code 

Gets the value of a "live" field from NodeQueryData.

Parameters:

_GetInstOperState(ctx, inst)

source code 

Get instance's operational status.

Parameters:

_GetInstLiveData(name)

source code 

Build function for retrieving live data.

Parameters:
  • name (string) - Live data field name

_GetInstStatus(ctx, inst)

source code 

Get instance status.

Parameters:

_GetInstDiskSize(index)

source code 

Build function for retrieving disk size.

Parameters:
  • index (int) - Disk index

_GetInstNic(index, cb)

source code 

Build function for calling another function with an instance NIC.

Parameters:
  • index (int) - NIC index
  • cb (callable) - Callback

_GetInstNicIp(ctx, _, nic)

source code 

Get a NIC's IP address.

Parameters:

_GetInstNicBridge(ctx, index, _)

source code 

Get a NIC's bridge.

Parameters:

_GetInstAllNicBridges(ctx, inst)

source code 

Get all network bridges for an instance.

Parameters:

_GetInstNicParam(name)

source code 

Build function for retrieving a NIC parameter.

Parameters:
  • name (string) - Parameter name

_GetInstanceNetworkFields()

source code 

Get instance fields involving network interfaces.

Returns:
Tuple containing list of field definitions used as input for _PrepareFieldList and a list of aliases

_GetInstDiskUsage(ctx, inst)

source code 

Get disk usage for an instance.

Parameters:

_GetInstanceConsole(ctx, inst)

source code 

Get console information for instance.

Parameters:

_GetInstanceDiskFields()

source code 

Get instance fields involving disks.

Returns:
List of field definitions used as input for _PrepareFieldList

_GetInstanceParameterFields()

source code 

Get instance fields involving parameters.

Returns:
List of field definitions used as input for _PrepareFieldList

_GetInstNodeGroup(ctx, default, node_name)

source code 

Gets group UUID of an instance node.

Parameters:

_GetInstNodeGroupName(ctx, default, node_name)

source code 

Gets group name of an instance node.

Parameters:

_JobUnavailInner(fn, ctx, (job_id, job))

source code 

Return _FS_UNAVAIL if job is None.

When listing specifc jobs (e.g. "gnt-job list 1 2 3"), a job may not be found, in which case this function converts it to _FS_UNAVAIL.


Variables Details [hide private]

_VERIFY_FN

Verification function for each field type

Value:
{QFT_UNKNOWN: ht.TNone, QFT_TEXT: ht.TString, QFT_BOOL: ht.TBool, QFT_\
NUMBER: ht.TInt, QFT_UNIT: ht.TInt, QFT_TIMESTAMP: ht.TNumber, QFT_OTH\
ER: lambda _: True,}

_FS_ALL

List of all special status

Value:
frozenset([_FS_UNKNOWN, _FS_NODATA, _FS_UNAVAIL, _FS_OFFLINE])

_VTToQFT

VType to QFT mapping

Value:
{constants.VTYPE_STRING: QFT_OTHER, constants.VTYPE_MAYBE_STRING: QFT_\
OTHER, constants.VTYPE_BOOL: QFT_BOOL, constants.VTYPE_SIZE: QFT_UNIT,\
 constants.VTYPE_INT: QFT_NUMBER,}

_SERIAL_NO_DOC

Value:
"%s object serial number, incremented on each modification"

NDP_TITLE

Value:
{constants.ND_OOB_PROGRAM: "OutOfBandProgram", constants.ND_SPINDLE_CO\
UNT: "SpindleCount",}

_NODE_SIMPLE_FIELDS

Fields that are direct attributes of an objects.Node object

Value:
{"drained":("Drained", QFT_BOOL, 0, "Whether node is drained"), "maste\
r_candidate":("MasterC", QFT_BOOL, 0, "Whether node is a master candid\
ate"), "master_capable":("MasterCapable", QFT_BOOL, 0, "Whether node c\
an become a master candidate"), "name":("Node", QFT_TEXT, QFF_HOSTNAME\
, "Node name"), "offline":("Offline", QFT_BOOL, 0, "Whether node is ma\
rked offline"), "serial_no":("SerialNo", QFT_NUMBER, 0, _SERIAL_NO_DOC\
 % "Node"), "uuid":("UUID", QFT_TEXT, 0, "Node UUID"), "vm_capable":("\
VMCapable", QFT_BOOL, 0, "Whether node can host instances"),}

_NODE_LIVE_FIELDS

Fields requiring talking to the node

Value:
{"bootid":("BootID", QFT_TEXT, "bootid", "Random UUID renewed for each\
 system reboot, can be used" " for detecting reboots by tracking chang\
es"), "cnodes":("CNodes", QFT_NUMBER, "cpu_nodes", "Number of NUMA dom\
ains on node (if exported by hypervisor)"), "csockets":("CSockets", QF\
T_NUMBER, "cpu_sockets", "Number of physical CPU sockets (if exported \
by hypervisor)"), "ctotal":("CTotal", QFT_NUMBER, "cpu_total", "Number\
 of logical processors"), "dfree":("DFree", QFT_UNIT, "vg_free", "Avai\
lable disk space in volume group"), "dtotal":("DTotal", QFT_UNIT, "vg_\
...

_INST_SIMPLE_FIELDS

Value:
{"disk_template":("Disk_template", QFT_TEXT, 0, "Instance disk templat\
e"), "hypervisor":("Hypervisor", QFT_TEXT, 0, "Hypervisor name"), "nam\
e":("Instance", QFT_TEXT, QFF_HOSTNAME, "Instance name"), "network_por\
t":("Network_port", QFT_OTHER, 0, "Instance network port if available \
(e.g. for VNC console)"), "os":("OS", QFT_TEXT, 0, "Operating system")\
, "serial_no":("SerialNo", QFT_NUMBER, 0, _SERIAL_NO_DOC % "Instance")\
, "uuid":("UUID", QFT_TEXT, 0, "Instance UUID"),}

_GROUP_SIMPLE_FIELDS

Value:
{"alloc_policy":("AllocPolicy", QFT_TEXT, "Allocation policy for group\
"), "name":("Group", QFT_TEXT, "Group name"), "serial_no":("SerialNo",\
 QFT_NUMBER, _SERIAL_NO_DOC % "Group"), "uuid":("UUID", QFT_TEXT, "Gro\
up UUID"),}

_CLUSTER_VERSION_FIELDS

Value:
{"software_version":("SoftwareVersion", QFT_TEXT, constants.RELEASE_VE\
RSION, "Software version"), "protocol_version":("ProtocolVersion", QFT\
_NUMBER, constants.PROTOCOL_VERSION, "RPC protocol version"), "config_\
version":("ConfigVersion", QFT_NUMBER, constants.CONFIG_VERSION, "Conf\
iguration format version"), "os_api_version":("OsApiVersion", QFT_NUMB\
ER, max(constants.OS_API_VERSIONS), "API version for OS template scrip\
ts"), "export_version":("ExportVersion", QFT_NUMBER, constants.EXPORT_\
VERSION, "Import/export file format version"),}

_CLUSTER_SIMPLE_FIELDS

Value:
{"cluster_name":("Name", QFT_TEXT, QFF_HOSTNAME, "Cluster name"), "mas\
ter_node":("Master", QFT_TEXT, QFF_HOSTNAME, "Master node name"), "vol\
ume_group_name":("VgName", QFT_TEXT, 0, "LVM volume group name"),}

ALL_FIELDS

All available resources

Value:
{constants.QR_CLUSTER: CLUSTER_FIELDS, constants.QR_INSTANCE: INSTANCE\
_FIELDS, constants.QR_NODE: NODE_FIELDS, constants.QR_LOCK: LOCK_FIELD\
S, constants.QR_GROUP: GROUP_FIELDS, constants.QR_OS: OS_FIELDS, const\
ants.QR_JOB: JOB_FIELDS, constants.QR_EXPORT: EXPORT_FIELDS,}