Revision 27579978

b/doc/examples/dumb-allocator
74 74
  request = data["request"]
75 75
  req_type = request["type"]
76 76
  if req_type != "allocate":
77
    print >> sys.stderr, "Unsupported allocator mode '%s'" % req_type
78
    return 1
79

  
80
  npri = SelectNode(nodes, request, [])
81
  if npri is None:
82
    return OutputError("Can't find a suitable primary node", exit_code=0)
83

  
84
  result_nodes = [npri]
85
  if request["disk_template"] == "drbd":
86
    nsec = SelectNode(nodes, request, result_nodes)
87
    if nsec is None:
88
      return OutputError("Can't find a suitable secondary node (%s selected"
89
                         " as primary)" % npri, exit_code=0)
90
    result_nodes.append(nsec)
77
    return OutputError("Unsupported allocator mode '%s'" % req_type)
78

  
79
  result_nodes = []
80
  while len(result_nodes) < request["required_nodes"]:
81
    new_selection = SelectNode(nodes, request, result_nodes)
82
    if new_selection is None:
83
      return OutputError("Can't find a suitable node for position %s"
84
                         " (already selected: %s)" %
85
                         (len(result_nodes) + 1, ", ".join(result_nodes)),
86
                         exit_code=0)
87
    result_nodes.append(new_selection)
91 88

  
92 89
  result = {
93 90
          "success": True,
b/lib/cmdlib.py
3159 3159
      raise errors.OpPrereqError("Can't compute nodes using"
3160 3160
                                 " iallocator '%s': %s" % (self.op.iallocator,
3161 3161
                                                           ial.info))
3162
    req_nodes = 1
3163
    if self.op.disk_template in constants.DTS_NET_MIRROR:
3164
      req_nodes += 1
3165

  
3166
    if len(ial.nodes) != req_nodes:
3162
    if len(ial.nodes) != ial.required_nodes:
3167 3163
      raise errors.OpPrereqError("iallocator '%s' returned invalid number"
3168 3164
                                 " of nodes (%s), required %s" %
3169
                                 (len(ial.nodes), req_nodes))
3165
                                 (len(ial.nodes), ial.required_nodes))
3170 3166
    self.op.pnode = ial.nodes[0]
3171 3167
    logger.ToStdout("Selected nodes for the instance: %s" %
3172 3168
                    (", ".join(ial.nodes),))
3173 3169
    logger.Info("Selected nodes for instance %s via iallocator %s: %s" %
3174 3170
                (self.op.instance_name, self.op.iallocator, ial.nodes))
3175
    if req_nodes == 2:
3171
    if ial.required_nodes == 2:
3176 3172
      self.op.snode = ial.nodes[1]
3177 3173

  
3178 3174
  def BuildHooksEnv(self):
......
4742 4738
    self.mode = self.name = None
4743 4739
    self.mem_size = self.disks = self.disk_template = None
4744 4740
    self.os = self.tags = self.nics = self.vcpus = None
4741
    # computed fields
4742
    self.required_nodes = None
4745 4743
    # init result fields
4746 4744
    self.success = self.info = self.nodes = None
4747 4745
    for key in kwargs:
......
4842 4840
    disk_space = _ComputeDiskSize(self.disk_template,
4843 4841
                                  self.disks[0]["size"], self.disks[1]["size"])
4844 4842

  
4843
    if self.disk_template in constants.DTS_NET_MIRROR:
4844
      self.required_nodes = 2
4845
    else:
4846
      self.required_nodes = 1
4845 4847
    request = {
4846 4848
      "type": "allocate",
4847 4849
      "name": self.name,
......
4853 4855
      "disks": self.disks,
4854 4856
      "disk_space_total": disk_space,
4855 4857
      "nics": self.nics,
4858
      "required_nodes": self.required_nodes,
4856 4859
      }
4857 4860
    data["request"] = request
4858 4861

  
......
4866 4869
    done.
4867 4870

  
4868 4871
    """
4869
    data = self.in_data
4872
    instance = self.cfg.GetInstanceInfo(self.name)
4873
    if instance is None:
4874
      raise errors.ProgrammerError("Unknown instance '%s' passed to"
4875
                                   " IAllocator" % self.name)
4876

  
4877
    if instance.disk_template not in constants.DTS_NET_MIRROR:
4878
      raise errors.OpPrereqError("Can't relocate non-mirrored instances")
4879

  
4880
    self.required_nodes = 1
4881

  
4882
    disk_space = _ComputeDiskSize(instance.disk_template,
4883
                                  instance.disks[0].size,
4884
                                  instance.disks[1].size)
4885

  
4870 4886
    request = {
4871 4887
      "type": "replace_secondary",
4872 4888
      "name": self.name,
4889
      "disk_space_total": disk_space,
4890
      "required_nodes": self.required_nodes,
4873 4891
      }
4874
    data["request"] = request
4892
    self.in_data["request"] = request
4875 4893

  
4876 4894
  def _BuildInputData(self):
4877 4895
    """Build input data structures.
......
4905 4923
      if result.failed:
4906 4924
        raise errors.OpExecError("Instance allocator call failed: %s,"
4907 4925
                                 " output: %s" %
4908
                                 (result.fail_reason, result.stdout))
4926
                                 (result.fail_reason, result.output))
4909 4927
    finally:
4910 4928
      os.unlink(fin_name)
4911 4929
    self.out_text = result.stdout

Also available in: Unified diff