1 # Copyright (C) 2007 Google Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
34 """Loads the passed configuration file.
41 cfg = yaml.load(f.read())
49 if len(cfg['nodes']) < 1:
50 raise qa_error.Error("Need at least one node")
51 if len(cfg['instances']) < 1:
52 raise qa_error.Error("Need at least one instance")
55 def get(name, default=None):
56 return cfg.get(name, default)
59 def TestEnabled(test):
60 """Returns True if the given test is enabled."""
61 return cfg.get('tests', {}).get(test, False)
65 return cfg['nodes'][0]
68 def AcquireInstance():
69 """Returns an instance which isn't in use.
72 # Filter out unwanted instances
73 tmp_flt = lambda inst: not inst.get('_used', False)
74 instances = filter(tmp_flt, cfg['instances'])
77 if len(instances) == 0:
78 raise qa_error.OutOfInstancesError("No instances left")
85 def ReleaseInstance(inst):
89 def AcquireNode(exclude=None):
90 """Returns the least used node.
93 master = GetMasterNode()
95 # Filter out unwanted nodes
96 # TODO: Maybe combine filters
98 nodes = cfg['nodes'][:]
100 nodes = filter(lambda node: node != exclude, cfg['nodes'])
102 tmp_flt = lambda node: node.get('_added', False) or node == master
103 nodes = filter(tmp_flt, nodes)
107 raise qa_error.OutOfNodesError("No nodes left")
109 # Get node with least number of uses
111 result = cmp(a.get('_count', 0), b.get('_count', 0))
113 result = cmp(a['primary'], b['primary'])
116 nodes.sort(cmp=compare)
119 node['_count'] = node.get('_count', 0) + 1
123 def ReleaseNode(node):
124 node['_count'] = node.get('_count', 0) - 1