Statistics
| Branch: | Tag: | Revision:

root / qa / qa_config.py @ 2e5eb96a

History | View | Annotate | Download (3.1 kB)

1 c68d1f43 Michael Hanselmann
#
2 c68d1f43 Michael Hanselmann
#
3 c68d1f43 Michael Hanselmann
4 3582eef6 Iustin Pop
# Copyright (C) 2007, 2011 Google Inc.
5 cec9845c Michael Hanselmann
#
6 cec9845c Michael Hanselmann
# This program is free software; you can redistribute it and/or modify
7 cec9845c Michael Hanselmann
# it under the terms of the GNU General Public License as published by
8 cec9845c Michael Hanselmann
# the Free Software Foundation; either version 2 of the License, or
9 cec9845c Michael Hanselmann
# (at your option) any later version.
10 cec9845c Michael Hanselmann
#
11 cec9845c Michael Hanselmann
# This program is distributed in the hope that it will be useful, but
12 cec9845c Michael Hanselmann
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 cec9845c Michael Hanselmann
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 cec9845c Michael Hanselmann
# General Public License for more details.
15 cec9845c Michael Hanselmann
#
16 cec9845c Michael Hanselmann
# You should have received a copy of the GNU General Public License
17 cec9845c Michael Hanselmann
# along with this program; if not, write to the Free Software
18 cec9845c Michael Hanselmann
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 cec9845c Michael Hanselmann
# 02110-1301, USA.
20 cec9845c Michael Hanselmann
21 cec9845c Michael Hanselmann
22 cec9845c Michael Hanselmann
"""QA configuration.
23 cec9845c Michael Hanselmann

24 cec9845c Michael Hanselmann
"""
25 cec9845c Michael Hanselmann
26 cec9845c Michael Hanselmann
27 a705dc05 Michael Hanselmann
from ganeti import utils
28 a705dc05 Michael Hanselmann
from ganeti import serializer
29 7d88f255 Iustin Pop
from ganeti import compat
30 cec9845c Michael Hanselmann
31 cec9845c Michael Hanselmann
import qa_error
32 cec9845c Michael Hanselmann
33 cec9845c Michael Hanselmann
34 cec9845c Michael Hanselmann
cfg = None
35 cec9845c Michael Hanselmann
options = None
36 cec9845c Michael Hanselmann
37 cec9845c Michael Hanselmann
38 cec9845c Michael Hanselmann
def Load(path):
39 cec9845c Michael Hanselmann
  """Loads the passed configuration file.
40 cec9845c Michael Hanselmann

41 cec9845c Michael Hanselmann
  """
42 3582eef6 Iustin Pop
  global cfg # pylint: disable-msg=W0603
43 cec9845c Michael Hanselmann
44 a705dc05 Michael Hanselmann
  cfg = serializer.LoadJson(utils.ReadFile(path))
45 cec9845c Michael Hanselmann
46 cec9845c Michael Hanselmann
  Validate()
47 cec9845c Michael Hanselmann
48 cec9845c Michael Hanselmann
49 cec9845c Michael Hanselmann
def Validate():
50 cec9845c Michael Hanselmann
  if len(cfg['nodes']) < 1:
51 cec9845c Michael Hanselmann
    raise qa_error.Error("Need at least one node")
52 cec9845c Michael Hanselmann
  if len(cfg['instances']) < 1:
53 cec9845c Michael Hanselmann
    raise qa_error.Error("Need at least one instance")
54 1d693311 Michael Hanselmann
  if len(cfg["disk"]) != len(cfg["disk-growth"]):
55 1d693311 Michael Hanselmann
    raise qa_error.Error("Config options 'disk' and 'disk-growth' must have"
56 1d693311 Michael Hanselmann
                         " the same number of items")
57 cec9845c Michael Hanselmann
58 cec9845c Michael Hanselmann
59 cec9845c Michael Hanselmann
def get(name, default=None):
60 cec9845c Michael Hanselmann
  return cfg.get(name, default)
61 cec9845c Michael Hanselmann
62 cec9845c Michael Hanselmann
63 7d88f255 Iustin Pop
def TestEnabled(tests):
64 7d88f255 Iustin Pop
  """Returns True if the given tests are enabled.
65 7d88f255 Iustin Pop

66 7d88f255 Iustin Pop
  @param tests: a single test, or a list of tests to check
67 1010ec70 Michael Hanselmann

68 1010ec70 Michael Hanselmann
  """
69 7d88f255 Iustin Pop
  if isinstance(tests, basestring):
70 7d88f255 Iustin Pop
    tests = [tests]
71 7d88f255 Iustin Pop
  return compat.all(cfg.get("tests", {}).get(t, True) for t in tests)
72 cec9845c Michael Hanselmann
73 cec9845c Michael Hanselmann
74 cec9845c Michael Hanselmann
def GetMasterNode():
75 cec9845c Michael Hanselmann
  return cfg['nodes'][0]
76 cec9845c Michael Hanselmann
77 cec9845c Michael Hanselmann
78 cec9845c Michael Hanselmann
def AcquireInstance():
79 cec9845c Michael Hanselmann
  """Returns an instance which isn't in use.
80 cec9845c Michael Hanselmann

81 cec9845c Michael Hanselmann
  """
82 cec9845c Michael Hanselmann
  # Filter out unwanted instances
83 cec9845c Michael Hanselmann
  tmp_flt = lambda inst: not inst.get('_used', False)
84 cec9845c Michael Hanselmann
  instances = filter(tmp_flt, cfg['instances'])
85 cec9845c Michael Hanselmann
  del tmp_flt
86 cec9845c Michael Hanselmann
87 cec9845c Michael Hanselmann
  if len(instances) == 0:
88 cec9845c Michael Hanselmann
    raise qa_error.OutOfInstancesError("No instances left")
89 cec9845c Michael Hanselmann
90 cec9845c Michael Hanselmann
  inst = instances[0]
91 cec9845c Michael Hanselmann
  inst['_used'] = True
92 cec9845c Michael Hanselmann
  return inst
93 cec9845c Michael Hanselmann
94 cec9845c Michael Hanselmann
95 cec9845c Michael Hanselmann
def ReleaseInstance(inst):
96 cec9845c Michael Hanselmann
  inst['_used'] = False
97 cec9845c Michael Hanselmann
98 cec9845c Michael Hanselmann
99 cec9845c Michael Hanselmann
def AcquireNode(exclude=None):
100 cec9845c Michael Hanselmann
  """Returns the least used node.
101 cec9845c Michael Hanselmann

102 cec9845c Michael Hanselmann
  """
103 cec9845c Michael Hanselmann
  master = GetMasterNode()
104 cec9845c Michael Hanselmann
105 cec9845c Michael Hanselmann
  # Filter out unwanted nodes
106 cec9845c Michael Hanselmann
  # TODO: Maybe combine filters
107 cec9845c Michael Hanselmann
  if exclude is None:
108 cec9845c Michael Hanselmann
    nodes = cfg['nodes'][:]
109 4b62db14 Michael Hanselmann
  elif isinstance(exclude, (list, tuple)):
110 4b62db14 Michael Hanselmann
    nodes = filter(lambda node: node not in exclude, cfg['nodes'])
111 cec9845c Michael Hanselmann
  else:
112 cec9845c Michael Hanselmann
    nodes = filter(lambda node: node != exclude, cfg['nodes'])
113 cec9845c Michael Hanselmann
114 cec9845c Michael Hanselmann
  tmp_flt = lambda node: node.get('_added', False) or node == master
115 cec9845c Michael Hanselmann
  nodes = filter(tmp_flt, nodes)
116 cec9845c Michael Hanselmann
  del tmp_flt
117 cec9845c Michael Hanselmann
118 cec9845c Michael Hanselmann
  if len(nodes) == 0:
119 cec9845c Michael Hanselmann
    raise qa_error.OutOfNodesError("No nodes left")
120 cec9845c Michael Hanselmann
121 cec9845c Michael Hanselmann
  # Get node with least number of uses
122 cec9845c Michael Hanselmann
  def compare(a, b):
123 cec9845c Michael Hanselmann
    result = cmp(a.get('_count', 0), b.get('_count', 0))
124 cec9845c Michael Hanselmann
    if result == 0:
125 cec9845c Michael Hanselmann
      result = cmp(a['primary'], b['primary'])
126 cec9845c Michael Hanselmann
    return result
127 cec9845c Michael Hanselmann
128 cec9845c Michael Hanselmann
  nodes.sort(cmp=compare)
129 cec9845c Michael Hanselmann
130 cec9845c Michael Hanselmann
  node = nodes[0]
131 cec9845c Michael Hanselmann
  node['_count'] = node.get('_count', 0) + 1
132 cec9845c Michael Hanselmann
  return node
133 cec9845c Michael Hanselmann
134 cec9845c Michael Hanselmann
135 cec9845c Michael Hanselmann
def ReleaseNode(node):
136 cec9845c Michael Hanselmann
  node['_count'] = node.get('_count', 0) - 1