#
#
-# Copyright (C) 2007, 2011 Google Inc.
+# Copyright (C) 2007, 2011, 2012 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
"""
+import os
+from ganeti import constants
from ganeti import utils
from ganeti import serializer
from ganeti import compat
import qa_error
+_INSTANCE_CHECK_KEY = "instance-check"
+_ENABLED_HV_KEY = "enabled-hypervisors"
+
+
cfg = None
options = None
raise qa_error.Error("Config options 'disk' and 'disk-growth' must have"
" the same number of items")
+ check = GetInstanceCheckScript()
+ if check:
+ try:
+ os.stat(check)
+ except EnvironmentError, err:
+ raise qa_error.Error("Can't find instance check script '%s': %s" %
+ (check, err))
+
+ enabled_hv = frozenset(GetEnabledHypervisors())
+ if not enabled_hv:
+ raise qa_error.Error("No hypervisor is enabled")
+
+ difference = enabled_hv - constants.HYPER_TYPES
+ if difference:
+ raise qa_error.Error("Unknown hypervisor(s) enabled: %s" %
+ utils.CommaJoin(difference))
+
def get(name, default=None):
- return cfg.get(name, default)
+ return cfg.get(name, default) # pylint: disable=E1103
+
+
+class Either:
+ def __init__(self, tests):
+ """Initializes this class.
+
+ @type tests: list or string
+ @param tests: List of test names
+ @see: L{TestEnabled} for details
+
+ """
+ self.tests = tests
+
+
+def _MakeSequence(value):
+ """Make sequence of single argument.
+
+ If the single argument is not already a list or tuple, a list with the
+ argument as a single item is returned.
+
+ """
+ if isinstance(value, (list, tuple)):
+ return value
+ else:
+ return [value]
+
+
+def _TestEnabledInner(check_fn, names, fn):
+ """Evaluate test conditions.
+
+ @type check_fn: callable
+ @param check_fn: Callback to check whether a test is enabled
+ @type names: sequence or string
+ @param names: Test name(s)
+ @type fn: callable
+ @param fn: Aggregation function
+ @rtype: bool
+ @return: Whether test is enabled
+
+ """
+ names = _MakeSequence(names)
+
+ result = []
+ for name in names:
+ if isinstance(name, Either):
+ value = _TestEnabledInner(check_fn, name.tests, compat.any)
+ elif isinstance(name, (list, tuple)):
+ value = _TestEnabledInner(check_fn, name, compat.all)
+ else:
+ value = check_fn(name)
-def TestEnabled(tests):
+ result.append(value)
+
+ return fn(result)
+
+
+def TestEnabled(tests, _cfg=None):
"""Returns True if the given tests are enabled.
- @param tests: a single test, or a list of tests to check
+ @param tests: A single test as a string, or a list of tests to check; can
+ contain L{Either} for OR conditions, AND is default
"""
- if isinstance(tests, basestring):
- tests = [tests]
+ if _cfg is None:
+ _cfg = cfg
# Get settings for all tests
- all_tests = cfg.get("tests", {})
+ cfg_tests = _cfg.get("tests", {}) # pylint: disable=E1103
# Get default setting
- default = all_tests.get("default", True)
+ default = cfg_tests.get("default", True)
+
+ return _TestEnabledInner(lambda name: cfg_tests.get(name, default),
+ tests, compat.all)
+
+
+def GetInstanceCheckScript():
+ """Returns path to instance check script or C{None}.
+
+ """
+ return cfg.get(_INSTANCE_CHECK_KEY, None) # pylint: disable=E1103
+
+
+def GetEnabledHypervisors():
+ """Returns list of enabled hypervisors.
+
+ @rtype: list
- return compat.all(all_tests.get(name, default) for name in tests)
+ """
+ try:
+ value = cfg[_ENABLED_HV_KEY]
+ except KeyError:
+ return [constants.DEFAULT_ENABLED_HYPERVISOR]
+ else:
+ if 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():
+ """Returns the default hypervisor to be used.
+
+ """
+ return GetEnabledHypervisors()[0]
+
+
+def GetInstanceNicMac(inst, default=None):
+ """Returns MAC address for instance's network interface.
+
+ """
+ return inst.get("nic.mac/0", default)
def GetMasterNode():