4 # Copyright (C) 2007 Google Inc.
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.
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.
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
37 """Loads the passed configuration file.
44 cfg = simplejson.load(f)
52 if len(cfg['nodes']) < 1:
53 raise qa_error.Error("Need at least one node")
54 if len(cfg['instances']) < 1:
55 raise qa_error.Error("Need at least one instance")
56 if len(cfg["disk"]) != len(cfg["disk-growth"]):
57 raise qa_error.Error("Config options 'disk' and 'disk-growth' must have"
58 " the same number of items")
61 def get(name, default=None):
62 return cfg.get(name, default)
65 def TestEnabled(test):
66 """Returns True if the given test is enabled."""
67 return cfg.get('tests', {}).get(test, False)
71 return cfg['nodes'][0]
74 def AcquireInstance():
75 """Returns an instance which isn't in use.
78 # Filter out unwanted instances
79 tmp_flt = lambda inst: not inst.get('_used', False)
80 instances = filter(tmp_flt, cfg['instances'])
83 if len(instances) == 0:
84 raise qa_error.OutOfInstancesError("No instances left")
91 def ReleaseInstance(inst):
95 def AcquireNode(exclude=None):
96 """Returns the least used node.
99 master = GetMasterNode()
101 # Filter out unwanted nodes
102 # TODO: Maybe combine filters
104 nodes = cfg['nodes'][:]
105 elif isinstance(exclude, (list, tuple)):
106 nodes = filter(lambda node: node not in exclude, cfg['nodes'])
108 nodes = filter(lambda node: node != exclude, cfg['nodes'])
110 tmp_flt = lambda node: node.get('_added', False) or node == master
111 nodes = filter(tmp_flt, nodes)
115 raise qa_error.OutOfNodesError("No nodes left")
117 # Get node with least number of uses
119 result = cmp(a.get('_count', 0), b.get('_count', 0))
121 result = cmp(a['primary'], b['primary'])
124 nodes.sort(cmp=compare)
127 node['_count'] = node.get('_count', 0) + 1
131 def ReleaseNode(node):
132 node['_count'] = node.get('_count', 0) - 1