root / doc / examples / dumb-allocator @ 298fe380
History | View | Annotate | Download (2.6 kB)
1 |
#!/usr/bin/python |
---|---|
2 |
# |
3 |
|
4 |
# Copyright (C) 2008 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 |
"""Simple first-fit allocator for ganeti instance allocation framework. |
22 |
|
23 |
This allocator just iterates over the nodes and selects the first one |
24 |
that fits in both memory and disk space, without any consideration for |
25 |
equal spread or VCPU oversubscription. |
26 |
|
27 |
""" |
28 |
import simplejson |
29 |
import sys |
30 |
|
31 |
|
32 |
def SelectNode(nodes, request, to_skip): |
33 |
"""Select a node for the given instance |
34 |
|
35 |
""" |
36 |
disk_size = request["disk_space_total"] |
37 |
selected = None |
38 |
for nname, ninfo in nodes.iteritems(): |
39 |
if nname in to_skip: |
40 |
continue |
41 |
if request["memory"] > ninfo["free_memory"]: |
42 |
continue |
43 |
if disk_size > ninfo["free_disk"]: |
44 |
continue |
45 |
selected = nname |
46 |
break |
47 |
return selected |
48 |
|
49 |
|
50 |
def OutputError(text): |
51 |
"""Builds an error response with a given info message. |
52 |
|
53 |
""" |
54 |
error = { |
55 |
"success": False, |
56 |
"info": text, |
57 |
} |
58 |
print simplejson.dumps(error, indent=2) |
59 |
return 1 |
60 |
|
61 |
|
62 |
def main(): |
63 |
"""Main function. |
64 |
|
65 |
""" |
66 |
if len(sys.argv) < 2: |
67 |
print >> sys.stderr, "Usage: %s cluster.json" % (sys.argv[0]) |
68 |
return 1 |
69 |
|
70 |
data = simplejson.load(open(sys.argv[1])) |
71 |
|
72 |
nodes = data["nodes"] |
73 |
request = data["request"] |
74 |
req_type = request["type"] |
75 |
if req_type != "allocate": |
76 |
print >> sys.stderr, "Unsupported allocator mode '%s'" % req_type |
77 |
return 1 |
78 |
|
79 |
npri = SelectNode(nodes, request, []) |
80 |
if npri is None: |
81 |
return OutputError("Can't find a suitable primary node") |
82 |
|
83 |
result_nodes = [npri] |
84 |
if request["disk_template"] == "drbd": |
85 |
nsec = SelectNode(nodes, request, result_nodes) |
86 |
if nsec is None: |
87 |
return OutputError("Can't find a suitable secondary node (%s selected" |
88 |
" as primary)" % npri) |
89 |
result_nodes.append(nsec) |
90 |
|
91 |
result = { |
92 |
"success": True, |
93 |
"info": "Allocation successful", |
94 |
"nodes": result_nodes, |
95 |
} |
96 |
print simplejson.dumps(result, indent=2) |
97 |
return 0 |
98 |
|
99 |
if __name__ == "__main__": |
100 |
sys.exit(main()) |