Merge branch 'stable-2.8' into stable-2.9
[ganeti-local] / qa / qa_config.py
index 43d3350..97ccaa7 100644 (file)
@@ -38,6 +38,7 @@ _INSTANCE_CHECK_KEY = "instance-check"
 _ENABLED_HV_KEY = "enabled-hypervisors"
 _VCLUSTER_MASTER_KEY = "vcluster-master"
 _VCLUSTER_BASEDIR_KEY = "vcluster-basedir"
+_ENABLED_DISK_TEMPLATES_KEY = "enabled-disk-templates"
 
 #: QA configuration (L{_QaConfig})
 _config = None
@@ -279,12 +280,14 @@ class _QaConfig(object):
     if not self.get("instances"):
       raise qa_error.Error("Need at least one instance")
 
-    if (self.get("disk") is None or
-        self.get("disk-growth") is None or
-        len(self.get("disk")) != len(self.get("disk-growth"))):
-      raise qa_error.Error("Config options 'disk' and 'disk-growth' must exist"
-                           " and have the same number of items")
-
+    disks = self.GetDiskOptions()
+    if disks is None:
+      raise qa_error.Error("Config option 'disks' must exist")
+    else:
+      for d in disks:
+        if d.get("size") is None or d.get("growth") is None:
+          raise qa_error.Error("Config options `size` and `growth` must exist"
+                               " for all `disks` items")
     check = self.GetInstanceCheckScript()
     if check:
       try:
@@ -321,6 +324,24 @@ class _QaConfig(object):
     """
     return self._data[name]
 
+  def __setitem__(self, key, value):
+    """Sets a configuration value.
+
+    """
+    self._data[key] = value
+
+  def __delitem__(self, key):
+    """Deletes a value from the configuration.
+
+    """
+    del(self._data[key])
+
+  def __len__(self):
+    """Return the number of configuration items.
+
+    """
+    return len(self._data)
+
   def get(self, name, default=None):
     """Returns configuration value.
 
@@ -349,28 +370,67 @@ class _QaConfig(object):
     @rtype: list
 
     """
+    return self._GetStringListParameter(
+      _ENABLED_HV_KEY,
+      [constants.DEFAULT_ENABLED_HYPERVISOR])
+
+  def GetDefaultHypervisor(self):
+    """Returns the default hypervisor to be used.
+
+    """
+    return self.GetEnabledHypervisors()[0]
+
+  def GetEnabledDiskTemplates(self):
+    """Returns the list of enabled disk templates.
+
+    @rtype: list
+
+    """
+    return self._GetStringListParameter(
+      _ENABLED_DISK_TEMPLATES_KEY,
+      constants.DEFAULT_ENABLED_DISK_TEMPLATES)
+
+  def GetEnabledStorageTypes(self):
+    """Returns the list of enabled storage types.
+
+    @rtype: list
+    @returns: the list of storage types enabled for QA
+
+    """
+    enabled_disk_templates = self.GetEnabledDiskTemplates()
+    enabled_storage_types = list(
+        set([constants.MAP_DISK_TEMPLATE_STORAGE_TYPE[dt]
+             for dt in enabled_disk_templates]))
+    # Storage type 'lvm-pv' cannot be activated via a disk template,
+    # therefore we add it if 'lvm-vg' is present.
+    if constants.ST_LVM_VG in enabled_storage_types:
+      enabled_storage_types.append(constants.ST_LVM_PV)
+    return enabled_storage_types
+
+  def GetDefaultDiskTemplate(self):
+    """Returns the default disk template to be used.
+
+    """
+    return self.GetEnabledDiskTemplates()[0]
+
+  def _GetStringListParameter(self, key, default_values):
+    """Retrieves a parameter's value that is supposed to be a list of strings.
+
+    @rtype: list
+
+    """
     try:
-      value = self._data[_ENABLED_HV_KEY]
+      value = self._data[key]
     except KeyError:
-      return [constants.DEFAULT_ENABLED_HYPERVISOR]
+      return default_values
     else:
       if value is None:
         return []
       elif isinstance(value, basestring):
-        # The configuration key ("enabled-hypervisors") implies there can be
-        # multiple values. Multiple hypervisors are comma-separated on the
-        # command line option to "gnt-cluster init", so we need to handle them
-        # equally here.
         return value.split(",")
       else:
         return value
 
-  def GetDefaultHypervisor(self):
-    """Returns the default hypervisor to be used.
-
-    """
-    return self.GetEnabledHypervisors()[0]
-
   def SetExclusiveStorage(self, value):
     """Set the expected value of the C{exclusive_storage} flag for the cluster.
 
@@ -389,8 +449,29 @@ class _QaConfig(object):
     """Is the given disk template supported by the current configuration?
 
     """
-    return (not self.GetExclusiveStorage() or
-            templ in constants.DTS_EXCL_STORAGE)
+    enabled = templ in self.GetEnabledDiskTemplates()
+    return enabled and (not self.GetExclusiveStorage() or
+                        templ in constants.DTS_EXCL_STORAGE)
+
+  def IsStorageTypeSupported(self, storage_type):
+    """Is the given storage type supported by the current configuration?
+
+    This is determined by looking if at least one of the disk templates
+    which is associated with the storage type is enabled in the configuration.
+
+    """
+    enabled_disk_templates = self.GetEnabledDiskTemplates()
+    if storage_type == constants.ST_LVM_PV:
+      disk_templates = utils.GetDiskTemplatesOfStorageType(constants.ST_LVM_VG)
+    else:
+      disk_templates = utils.GetDiskTemplatesOfStorageType(storage_type)
+    return bool(set(enabled_disk_templates).intersection(set(disk_templates)))
+
+  def AreSpindlesSupported(self):
+    """Are spindles supported by the current configuration?
+
+    """
+    return self.GetExclusiveStorage()
 
   def GetVclusterSettings(self):
     """Returns settings for virtual cluster.
@@ -401,6 +482,32 @@ class _QaConfig(object):
 
     return (master, basedir)
 
+  def GetDiskOptions(self):
+    """Return options for the disks of the instances.
+
+    Get 'disks' parameter from the configuration data. If 'disks' is missing,
+    try to create it from the legacy 'disk' and 'disk-growth' parameters.
+
+    """
+    try:
+      return self._data["disks"]
+    except KeyError:
+      pass
+
+    # Legacy interface
+    sizes = self._data.get("disk")
+    growths = self._data.get("disk-growth")
+    if sizes or growths:
+      if (sizes is None or growths is None or len(sizes) != len(growths)):
+        raise qa_error.Error("Config options 'disk' and 'disk-growth' must"
+                             " exist and have the same number of items")
+      disks = []
+      for (size, growth) in zip(sizes, growths):
+        disks.append({"size": size, "growth": growth})
+      return disks
+    else:
+      return None
+
 
 def Load(path):
   """Loads the passed configuration file.
@@ -475,6 +582,8 @@ def _TestEnabledInner(check_fn, names, fn):
       value = _TestEnabledInner(check_fn, name.tests, compat.any)
     elif isinstance(name, (list, tuple)):
       value = _TestEnabledInner(check_fn, name, compat.all)
+    elif callable(name):
+      value = name()
     else:
       value = check_fn(name)
 
@@ -526,6 +635,27 @@ def GetDefaultHypervisor(*args):
   return GetConfig().GetDefaultHypervisor(*args)
 
 
+def GetEnabledDiskTemplates(*args):
+  """Wrapper for L{_QaConfig.GetEnabledDiskTemplates}.
+
+  """
+  return GetConfig().GetEnabledDiskTemplates(*args)
+
+
+def GetEnabledStorageTypes(*args):
+  """Wrapper for L{_QaConfig.GetEnabledStorageTypes}.
+
+  """
+  return GetConfig().GetEnabledStorageTypes(*args)
+
+
+def GetDefaultDiskTemplate(*args):
+  """Wrapper for L{_QaConfig.GetDefaultDiskTemplate}.
+
+  """
+  return GetConfig().GetDefaultDiskTemplate(*args)
+
+
 def GetMasterNode():
   """Wrapper for L{_QaConfig.GetMasterNode}.
 
@@ -569,12 +699,26 @@ def GetExclusiveStorage():
 
 
 def IsTemplateSupported(templ):
-  """Wrapper for L{_QaConfig.GetExclusiveStorage}.
+  """Wrapper for L{_QaConfig.IsTemplateSupported}.
 
   """
   return GetConfig().IsTemplateSupported(templ)
 
 
+def IsStorageTypeSupported(storage_type):
+  """Wrapper for L{_QaConfig.IsTemplateSupported}.
+
+  """
+  return GetConfig().IsStorageTypeSupported(storage_type)
+
+
+def AreSpindlesSupported():
+  """Wrapper for L{_QaConfig.AreSpindlesSupported}.
+
+  """
+  return GetConfig().AreSpindlesSupported()
+
+
 def _NodeSortKey(node):
   """Returns sort key for a node.
 
@@ -678,3 +822,10 @@ def NoVirtualCluster():
 
   """
   return not UseVirtualCluster()
+
+
+def GetDiskOptions():
+  """Wrapper for L{_QaConfig.GetDiskOptions}.
+
+  """
+  return GetConfig().GetDiskOptions()