root / qa / qa_config.py @ a0c3e726
History | View | Annotate | Download (4.5 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2007, 2011 Google Inc.
|
5 |
#
|
6 |
# This program is free software; you can redistribute it and/or modify
|
7 |
# it under the terms of the GNU General Public License as published by
|
8 |
# the Free Software Foundation; either version 2 of the License, or
|
9 |
# (at your option) any later version.
|
10 |
#
|
11 |
# This program is distributed in the hope that it will be useful, but
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
# General Public License for more details.
|
15 |
#
|
16 |
# You should have received a copy of the GNU General Public License
|
17 |
# along with this program; if not, write to the Free Software
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""QA configuration.
|
23 |
|
24 |
"""
|
25 |
|
26 |
|
27 |
from ganeti import utils |
28 |
from ganeti import serializer |
29 |
from ganeti import compat |
30 |
|
31 |
import qa_error |
32 |
|
33 |
|
34 |
cfg = None
|
35 |
options = None
|
36 |
|
37 |
|
38 |
def Load(path): |
39 |
"""Loads the passed configuration file.
|
40 |
|
41 |
"""
|
42 |
global cfg # pylint: disable=W0603 |
43 |
|
44 |
cfg = serializer.LoadJson(utils.ReadFile(path)) |
45 |
|
46 |
Validate() |
47 |
|
48 |
|
49 |
def Validate(): |
50 |
if len(cfg["nodes"]) < 1: |
51 |
raise qa_error.Error("Need at least one node") |
52 |
if len(cfg["instances"]) < 1: |
53 |
raise qa_error.Error("Need at least one instance") |
54 |
if len(cfg["disk"]) != len(cfg["disk-growth"]): |
55 |
raise qa_error.Error("Config options 'disk' and 'disk-growth' must have" |
56 |
" the same number of items")
|
57 |
|
58 |
|
59 |
def get(name, default=None): |
60 |
return cfg.get(name, default)
|
61 |
|
62 |
|
63 |
class Either: |
64 |
def __init__(self, tests): |
65 |
"""Initializes this class.
|
66 |
|
67 |
@type tests: list or string
|
68 |
@param tests: List of test names
|
69 |
@see: L{TestEnabled} for details
|
70 |
|
71 |
"""
|
72 |
self.tests = tests
|
73 |
|
74 |
|
75 |
def _MakeSequence(value): |
76 |
"""Make sequence of single argument.
|
77 |
|
78 |
If the single argument is not already a list or tuple, a list with the
|
79 |
argument as a single item is returned.
|
80 |
|
81 |
"""
|
82 |
if isinstance(value, (list, tuple)): |
83 |
return value
|
84 |
else:
|
85 |
return [value]
|
86 |
|
87 |
|
88 |
def _TestEnabledInner(check_fn, names, fn): |
89 |
"""Evaluate test conditions.
|
90 |
|
91 |
@type check_fn: callable
|
92 |
@param check_fn: Callback to check whether a test is enabled
|
93 |
@type names: sequence or string
|
94 |
@param names: Test name(s)
|
95 |
@type fn: callable
|
96 |
@param fn: Aggregation function
|
97 |
@rtype: bool
|
98 |
@return: Whether test is enabled
|
99 |
|
100 |
"""
|
101 |
names = _MakeSequence(names) |
102 |
|
103 |
result = [] |
104 |
|
105 |
for name in names: |
106 |
if isinstance(name, Either): |
107 |
value = _TestEnabledInner(check_fn, name.tests, compat.any) |
108 |
elif isinstance(name, (list, tuple)): |
109 |
value = _TestEnabledInner(check_fn, name, compat.all) |
110 |
else:
|
111 |
value = check_fn(name) |
112 |
|
113 |
result.append(value) |
114 |
|
115 |
return fn(result)
|
116 |
|
117 |
|
118 |
def TestEnabled(tests, _cfg=None): |
119 |
"""Returns True if the given tests are enabled.
|
120 |
|
121 |
@param tests: A single test as a string, or a list of tests to check; can
|
122 |
contain L{Either} for OR conditions, AND is default
|
123 |
|
124 |
"""
|
125 |
if _cfg is None: |
126 |
_cfg = cfg |
127 |
|
128 |
# Get settings for all tests
|
129 |
cfg_tests = _cfg.get("tests", {})
|
130 |
|
131 |
# Get default setting
|
132 |
default = cfg_tests.get("default", True) |
133 |
|
134 |
return _TestEnabledInner(lambda name: cfg_tests.get(name, default), |
135 |
tests, compat.all) |
136 |
|
137 |
|
138 |
def GetMasterNode(): |
139 |
return cfg["nodes"][0] |
140 |
|
141 |
|
142 |
def AcquireInstance(): |
143 |
"""Returns an instance which isn't in use.
|
144 |
|
145 |
"""
|
146 |
# Filter out unwanted instances
|
147 |
tmp_flt = lambda inst: not inst.get("_used", False) |
148 |
instances = filter(tmp_flt, cfg["instances"]) |
149 |
del tmp_flt
|
150 |
|
151 |
if len(instances) == 0: |
152 |
raise qa_error.OutOfInstancesError("No instances left") |
153 |
|
154 |
inst = instances[0]
|
155 |
inst["_used"] = True |
156 |
return inst
|
157 |
|
158 |
|
159 |
def ReleaseInstance(inst): |
160 |
inst["_used"] = False |
161 |
|
162 |
|
163 |
def AcquireNode(exclude=None): |
164 |
"""Returns the least used node.
|
165 |
|
166 |
"""
|
167 |
master = GetMasterNode() |
168 |
|
169 |
# Filter out unwanted nodes
|
170 |
# TODO: Maybe combine filters
|
171 |
if exclude is None: |
172 |
nodes = cfg["nodes"][:]
|
173 |
elif isinstance(exclude, (list, tuple)): |
174 |
nodes = filter(lambda node: node not in exclude, cfg["nodes"]) |
175 |
else:
|
176 |
nodes = filter(lambda node: node != exclude, cfg["nodes"]) |
177 |
|
178 |
tmp_flt = lambda node: node.get("_added", False) or node == master |
179 |
nodes = filter(tmp_flt, nodes)
|
180 |
del tmp_flt
|
181 |
|
182 |
if len(nodes) == 0: |
183 |
raise qa_error.OutOfNodesError("No nodes left") |
184 |
|
185 |
# Get node with least number of uses
|
186 |
def compare(a, b): |
187 |
result = cmp(a.get("_count", 0), b.get("_count", 0)) |
188 |
if result == 0: |
189 |
result = cmp(a["primary"], b["primary"]) |
190 |
return result
|
191 |
|
192 |
nodes.sort(cmp=compare) |
193 |
|
194 |
node = nodes[0]
|
195 |
node["_count"] = node.get("_count", 0) + 1 |
196 |
return node
|
197 |
|
198 |
|
199 |
def ReleaseNode(node): |
200 |
node["_count"] = node.get("_count", 0) - 1 |