LUSetInstanceParams: abstract _GetUpdatedParams
[ganeti-local] / doc / examples / dumb-allocator
index 53b9cd2..c59d75f 100755 (executable)
@@ -25,6 +25,7 @@ that fits in both memory and disk space, without any consideration for
 equal spread or VCPU oversubscription.
 
 """
+
 import simplejson
 import sys
 
@@ -73,12 +74,22 @@ def main():
   nodes =  data["nodes"]
   request = data["request"]
   req_type = request["type"]
-  if req_type != "allocate":
+  offline_nodes = [name for name in nodes if nodes[name]["offline"]]
+  drained_nodes = [name for name in nodes if nodes[name]["offline"]]
+  if req_type == "allocate":
+    forbidden_nodes = offline_nodes + drained_nodes
+    inst_data = request
+  elif req_type == "relocate":
+    idict = data["instances"][request["name"]]
+    forbidden_nodes = idict["nodes"] + offline_nodes + drained_nodes
+    inst_data = idict
+    inst_data["disk_space_total"] = request["disk_space_total"]
+  else:
     return OutputError("Unsupported allocator mode '%s'" % req_type)
 
   result_nodes = []
   while len(result_nodes) < request["required_nodes"]:
-    new_selection = SelectNode(nodes, request, result_nodes)
+    new_selection = SelectNode(nodes, inst_data, result_nodes+forbidden_nodes)
     if new_selection is None:
       return OutputError("Can't find a suitable node for position %s"
                          " (already selected: %s)" %
@@ -87,12 +98,13 @@ def main():
     result_nodes.append(new_selection)
 
   result = {
-          "success": True,
-          "info": "Allocation successful",
-          "nodes": result_nodes,
-          }
+    "success": True,
+    "info": "Allocation successful",
+    "nodes": result_nodes,
+    }
   print simplejson.dumps(result, indent=2)
   return 0
 
+
 if __name__ == "__main__":
     sys.exit(main())