build_chroot: hard-code the version of blaze-builder
[ganeti-local] / lib / query.py
index ef52d08..9bf5983 100644 (file)
@@ -1,7 +1,7 @@
 #
 #
 
-# Copyright (C) 2010, 2011, 2012 Google Inc.
+# Copyright (C) 2010, 2011, 2012, 2013 Google Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -90,7 +90,8 @@ from ganeti.constants import (QFT_UNKNOWN, QFT_TEXT, QFT_BOOL, QFT_NUMBER,
  IQ_LIVE,
  IQ_DISKUSAGE,
  IQ_CONSOLE,
- IQ_NODES) = range(100, 105)
+ IQ_NODES,
+ IQ_NETWORKS) = range(100, 106)
 
 (LQ_MODE,
  LQ_OWNER,
@@ -976,6 +977,23 @@ def _GetItemAttr(attr):
   return lambda _, item: getter(item)
 
 
+def _GetItemMaybeAttr(attr):
+  """Returns a field function to return a not-None attribute of the item.
+
+  If the value is None, then C{_FS_UNAVAIL} will be returned instead.
+
+  @param attr: Attribute name
+
+  """
+  def _helper(_, obj):
+    val = getattr(obj, attr)
+    if val is None:
+      return _FS_UNAVAIL
+    else:
+      return val
+  return _helper
+
+
 def _GetNDParam(name):
   """Return a field function to return an ND parameter out of the context.
 
@@ -1344,7 +1362,7 @@ def _BuildNodeFields():
     return lambda ctx, node: len(getter(ctx)[node.name])
 
   def _GetList(getter):
-    return lambda ctx, node: list(getter(ctx)[node.name])
+    return lambda ctx, node: utils.NiceSort(list(getter(ctx)[node.name]))
 
   # Add fields operating on instance lists
   for prefix, titleprefix, docword, getter in \
@@ -1383,7 +1401,7 @@ class InstanceQueryData:
 
   """
   def __init__(self, instances, cluster, disk_usage, offline_nodes, bad_nodes,
-               live_data, wrongnode_inst, console, nodes, groups):
+               live_data, wrongnode_inst, console, nodes, groups, networks):
     """Initializes this class.
 
     @param instances: List of instance objects
@@ -1402,6 +1420,8 @@ class InstanceQueryData:
     @param console: Per-instance console information
     @type nodes: dict; node name as key
     @param nodes: Node objects
+    @type networks: dict; net_uuid as key
+    @param networks: Network objects
 
     """
     assert len(set(bad_nodes) & set(offline_nodes)) == len(offline_nodes), \
@@ -1419,6 +1439,7 @@ class InstanceQueryData:
     self.console = console
     self.nodes = nodes
     self.groups = groups
+    self.networks = networks
 
     # Used for individual rows
     self.inst_hvparams = None
@@ -1520,28 +1541,75 @@ def _GetInstStatus(ctx, inst):
   return constants.INSTST_ADMINOFFLINE
 
 
-def _GetInstDiskSize(index):
-  """Build function for retrieving disk size.
+def _GetInstDisk(index, cb):
+  """Build function for calling another function with an instance Disk.
 
   @type index: int
   @param index: Disk index
+  @type cb: callable
+  @param cb: Callback
 
   """
-  def fn(_, inst):
-    """Get size of a disk.
+  def fn(ctx, inst):
+    """Call helper function with instance Disk.
 
+    @type ctx: L{InstanceQueryData}
     @type inst: L{objects.Instance}
     @param inst: Instance object
 
     """
     try:
-      return inst.disks[index].size
+      nic = inst.disks[index]
     except IndexError:
       return _FS_UNAVAIL
 
+    return cb(ctx, index, nic)
+
   return fn
 
 
+def _GetInstDiskSize(ctx, _, disk): # pylint: disable=W0613
+  """Get a Disk's size.
+
+  @type ctx: L{InstanceQueryData}
+  @type disk: L{objects.Disk}
+  @param disk: The Disk object
+
+  """
+  if disk.size is None:
+    return _FS_UNAVAIL
+  else:
+    return disk.size
+
+
+def _GetInstDeviceName(ctx, _, device): # pylint: disable=W0613
+  """Get a Device's Name.
+
+  @type ctx: L{InstanceQueryData}
+  @type device: L{objects.NIC} or L{objects.Disk}
+  @param device: The NIC or Disk object
+
+  """
+  if device.name is None:
+    return _FS_UNAVAIL
+  else:
+    return device.name
+
+
+def _GetInstDeviceUUID(ctx, _, device): # pylint: disable=W0613
+  """Get a Device's UUID.
+
+  @type ctx: L{InstanceQueryData}
+  @type device: L{objects.NIC} or L{objects.Disk}
+  @param device: The NIC or Disk object
+
+  """
+  if device.uuid is None:
+    return _FS_UNAVAIL
+  else:
+    return device.uuid
+
+
 def _GetInstNic(index, cb):
   """Build function for calling another function with an instance NIC.
 
@@ -1569,6 +1637,20 @@ def _GetInstNic(index, cb):
   return fn
 
 
+def _GetInstNicNetworkName(ctx, _, nic): # pylint: disable=W0613
+  """Get a NIC's Network.
+
+  @type ctx: L{InstanceQueryData}
+  @type nic: L{objects.NIC}
+  @param nic: NIC object
+
+  """
+  if nic.network is None:
+    return _FS_UNAVAIL
+  else:
+    return ctx.networks[nic.network].name
+
+
 def _GetInstNicNetwork(ctx, _, nic): # pylint: disable=W0613
   """Get a NIC's Network.
 
@@ -1615,6 +1697,27 @@ def _GetInstNicBridge(ctx, index, _):
     return _FS_UNAVAIL
 
 
+def _GetInstAllNicNetworkNames(ctx, inst):
+  """Get all network names for an instance.
+
+  @type ctx: L{InstanceQueryData}
+  @type inst: L{objects.Instance}
+  @param inst: Instance object
+
+  """
+  result = []
+
+  for nic in inst.nics:
+    name = None
+    if nic.network:
+      name = ctx.networks[nic.network].name
+    result.append(name)
+
+  assert len(result) == len(inst.nics)
+
+  return result
+
+
 def _GetInstAllNicBridges(ctx, inst):
   """Get all network bridges for an instance.
 
@@ -1683,6 +1786,12 @@ def _GetInstanceNetworkFields():
     (_MakeField("nic.ips", "NIC_IPs", QFT_OTHER,
                 "List containing each network interface's IP address"),
      IQ_CONFIG, 0, lambda ctx, inst: [nic.ip for nic in inst.nics]),
+    (_MakeField("nic.names", "NIC_Names", QFT_OTHER,
+                "List containing each network interface's name"),
+     IQ_CONFIG, 0, lambda ctx, inst: [nic.name for nic in inst.nics]),
+    (_MakeField("nic.uuids", "NIC_UUIDs", QFT_OTHER,
+                "List containing each network interface's UUID"),
+     IQ_CONFIG, 0, lambda ctx, inst: [nic.uuid for nic in inst.nics]),
     (_MakeField("nic.modes", "NIC_modes", QFT_OTHER,
                 "List containing each network interface's mode"), IQ_CONFIG, 0,
      lambda ctx, inst: [nicp[constants.NIC_MODE]
@@ -1697,6 +1806,9 @@ def _GetInstanceNetworkFields():
     (_MakeField("nic.networks", "NIC_networks", QFT_OTHER,
                 "List containing each interface's network"), IQ_CONFIG, 0,
      lambda ctx, inst: [nic.network for nic in inst.nics]),
+    (_MakeField("nic.networks.names", "NIC_networks_names", QFT_OTHER,
+                "List containing each interface's network"),
+     IQ_NETWORKS, 0, _GetInstAllNicNetworkNames)
     ]
 
   # NICs by number
@@ -1709,6 +1821,12 @@ def _GetInstanceNetworkFields():
       (_MakeField("nic.mac/%s" % i, "NicMAC/%s" % i, QFT_TEXT,
                   "MAC address of %s network interface" % numtext),
        IQ_CONFIG, 0, _GetInstNic(i, nic_mac_fn)),
+      (_MakeField("nic.name/%s" % i, "NicName/%s" % i, QFT_TEXT,
+                  "Name address of %s network interface" % numtext),
+       IQ_CONFIG, 0, _GetInstNic(i, _GetInstDeviceName)),
+      (_MakeField("nic.uuid/%s" % i, "NicUUID/%s" % i, QFT_TEXT,
+                  "UUID address of %s network interface" % numtext),
+       IQ_CONFIG, 0, _GetInstNic(i, _GetInstDeviceUUID)),
       (_MakeField("nic.mode/%s" % i, "NicMode/%s" % i, QFT_TEXT,
                   "Mode of %s network interface" % numtext),
        IQ_CONFIG, 0, _GetInstNic(i, nic_mode_fn)),
@@ -1721,6 +1839,9 @@ def _GetInstanceNetworkFields():
       (_MakeField("nic.network/%s" % i, "NicNetwork/%s" % i, QFT_TEXT,
                   "Network of %s network interface" % numtext),
        IQ_CONFIG, 0, _GetInstNic(i, _GetInstNicNetwork)),
+      (_MakeField("nic.network.name/%s" % i, "NicNetworkName/%s" % i, QFT_TEXT,
+                  "Network name of %s network interface" % numtext),
+       IQ_NETWORKS, 0, _GetInstNic(i, _GetInstNicNetworkName)),
       ])
 
   aliases = [
@@ -1784,14 +1905,25 @@ def _GetInstanceDiskFields():
      IQ_CONFIG, 0, lambda ctx, inst: len(inst.disks)),
     (_MakeField("disk.sizes", "Disk_sizes", QFT_OTHER, "List of disk sizes"),
      IQ_CONFIG, 0, lambda ctx, inst: [disk.size for disk in inst.disks]),
+    (_MakeField("disk.names", "Disk_names", QFT_OTHER, "List of disk names"),
+     IQ_CONFIG, 0, lambda ctx, inst: [disk.name for disk in inst.disks]),
+    (_MakeField("disk.uuids", "Disk_UUIDs", QFT_OTHER, "List of disk UUIDs"),
+     IQ_CONFIG, 0, lambda ctx, inst: [disk.uuid for disk in inst.disks]),
     ]
 
   # Disks by number
-  fields.extend([
-    (_MakeField("disk.size/%s" % i, "Disk/%s" % i, QFT_UNIT,
-                "Disk size of %s disk" % utils.FormatOrdinal(i + 1)),
-     IQ_CONFIG, 0, _GetInstDiskSize(i))
-    for i in range(constants.MAX_DISKS)])
+  for i in range(constants.MAX_DISKS):
+    numtext = utils.FormatOrdinal(i + 1)
+    fields.extend([
+        (_MakeField("disk.size/%s" % i, "Disk/%s" % i, QFT_UNIT,
+                    "Disk size of %s disk" % numtext),
+        IQ_CONFIG, 0, _GetInstDisk(i, _GetInstDiskSize)),
+        (_MakeField("disk.name/%s" % i, "DiskName/%s" % i, QFT_TEXT,
+                    "Name of %s disk" % numtext),
+        IQ_CONFIG, 0, _GetInstDisk(i, _GetInstDeviceName)),
+        (_MakeField("disk.uuid/%s" % i, "DiskUUID/%s" % i, QFT_TEXT,
+                    "UUID of %s disk" % numtext),
+        IQ_CONFIG, 0, _GetInstDisk(i, _GetInstDeviceUUID))])
 
   return fields
 
@@ -1943,6 +2075,9 @@ def _BuildInstanceFields():
     (_MakeField("admin_up", "Autostart", QFT_BOOL,
                 "Desired state of instance"),
      IQ_CONFIG, 0, lambda ctx, inst: inst.admin_state == constants.ADMINST_UP),
+    (_MakeField("disks_active", "DisksActive", QFT_BOOL,
+                "Desired state of instance disks"),
+     IQ_CONFIG, 0, _GetItemAttr("disks_active")),
     (_MakeField("tags", "Tags", QFT_OTHER, "Tags"), IQ_CONFIG, 0,
      lambda ctx, inst: list(inst.GetTags())),
     (_MakeField("console", "Console", QFT_OTHER,
@@ -2383,7 +2518,7 @@ def _GetExportName(_, (node_name, expname)): # pylint: disable=W0613
 
   """
   if expname is None:
-    return _FS_UNAVAIL
+    return _FS_NODATA
   else:
     return expname
 
@@ -2414,6 +2549,8 @@ _CLUSTER_VERSION_FIELDS = {
                      "API version for OS template scripts"),
   "export_version": ("ExportVersion", QFT_NUMBER, constants.EXPORT_VERSION,
                      "Import/export file format version"),
+  "vcs_version": ("VCSVersion", QFT_TEXT, constants.VCS_VERSION,
+                     "VCS version"),
   }
 
 
@@ -2569,7 +2706,7 @@ def _BuildNetworkFields():
   # Add simple fields
   fields.extend([
     (_MakeField(name, title, kind, doc),
-     NETQ_CONFIG, 0, _GetItemAttr(name))
+     NETQ_CONFIG, 0, _GetItemMaybeAttr(name))
      for (name, (title, kind, _, doc)) in _NETWORK_SIMPLE_FIELDS.items()])
 
   def _GetLength(getter):
@@ -2604,6 +2741,9 @@ def _BuildNetworkFields():
     compat.partial(_GetNetworkStatsField, name, kind))
     for (name, (title, kind, _, doc)) in _NETWORK_STATS_FIELDS.items()])
 
+  # Add timestamps
+  fields.extend(_GetItemTimestampFields(IQ_NETWORKS))
+
   return _PrepareFieldList(fields, [])
 
 #: Fields for cluster information