Revision 298fe380 lib/cmdlib.py

b/lib/cmdlib.py
4650 4650
                                   " result: %s" % (node, node_result))
4651 4651

  
4652 4652

  
4653
def _AllocatorGetClusterData(cfg, sstore):
4653
def _IAllocatorGetClusterData(cfg, sstore):
4654 4654
  """Compute the generic allocator input data.
4655 4655

  
4656 4656
  This is the data that is independent of the actual operation.
......
4720 4720
  return data
4721 4721

  
4722 4722

  
4723
def _AllocatorAddNewInstance(data, op):
4723
def _IAllocatorAddNewInstance(data, op):
4724 4724
  """Add new instance data to allocator structure.
4725 4725

  
4726 4726
  This in combination with _AllocatorGetClusterData will create the
......
4730 4730
  done.
4731 4731

  
4732 4732
  """
4733
  if len(op.disks) != 2:
4734
    raise errors.OpExecError("Only two-disk configurations supported")
4735

  
4736
  disk_space = _ComputeDiskSize(op.disk_template,
4737
                                op.disks[0]["size"], op.disks[1]["size"])
4738

  
4733 4739
  request = {
4734 4740
    "type": "allocate",
4735 4741
    "name": op.name,
......
4739 4745
    "vcpus": op.vcpus,
4740 4746
    "memory": op.mem_size,
4741 4747
    "disks": op.disks,
4748
    "disk_space_total": disk_space,
4742 4749
    "nics": op.nics,
4743 4750
    }
4744 4751
  data["request"] = request
4745 4752

  
4746 4753

  
4747
def _AllocatorAddRelocateInstance(data, op):
4754
def _IAllocatorAddRelocateInstance(data, op):
4748 4755
  """Add relocate instance data to allocator structure.
4749 4756

  
4750
  This in combination with _AllocatorGetClusterData will create the
4757
  This in combination with _IAllocatorGetClusterData will create the
4751 4758
  correct structure needed as input for the allocator.
4752 4759

  
4753 4760
  The checks for the completeness of the opcode must have already been
......
4761 4768
  data["request"] = request
4762 4769

  
4763 4770

  
4771
def _IAllocatorRun(name, data):
4772
  """Run an instance allocator and return the results.
4773

  
4774
  """
4775
  alloc_script = utils.FindFile(name, constants.IALLOCATOR_SEARCH_PATH,
4776
                                os.path.isfile)
4777
  if alloc_script is None:
4778
    raise errors.OpExecError("Can't find allocator")
4779

  
4780
  fd, fin_name = tempfile.mkstemp(prefix="ganeti-iallocator.")
4781
  try:
4782
    os.write(fd, data)
4783
    os.close(fd)
4784
    result = utils.RunCmd([alloc_script, fin_name])
4785
    if result.failed:
4786
      raise errors.OpExecError("Instance allocator call failed: %s,"
4787
                               " output: %s" %
4788
                               (result.fail_reason, result.stdout))
4789
  finally:
4790
    os.unlink(fin_name)
4791
  return result.stdout
4792

  
4793

  
4764 4794
class LUTestAllocator(NoHooksLU):
4765 4795
  """Run allocator tests.
4766 4796

  
......
4775 4805
    This checks the opcode parameters depending on the director and mode test.
4776 4806

  
4777 4807
    """
4778
    if self.op.mode == constants.ALF_MODE_ALLOC:
4808
    if self.op.mode == constants.IALLOCATOR_MODE_ALLOC:
4779 4809
      for attr in ["name", "mem_size", "disks", "disk_template",
4780 4810
                   "os", "tags", "nics", "vcpus"]:
4781 4811
        if not hasattr(self.op, attr):
......
4796 4826
                                     " 'nics' parameter")
4797 4827
      if not isinstance(self.op.disks, list):
4798 4828
        raise errors.OpPrereqError("Invalid parameter 'disks'")
4829
      if len(self.op.disks) != 2:
4830
        raise errors.OpPrereqError("Only two-disk configurations supported")
4799 4831
      for row in self.op.disks:
4800 4832
        if (not isinstance(row, dict) or
4801 4833
            "size" not in row or
......
4804 4836
            row["mode"] not in ['r', 'w']):
4805 4837
          raise errors.OpPrereqError("Invalid contents of the"
4806 4838
                                     " 'disks' parameter")
4807
    elif self.op.mode == constants.ALF_MODE_RELOC:
4839
    elif self.op.mode == constants.IALLOCATOR_MODE_RELOC:
4808 4840
      if not hasattr(self.op, "name"):
4809 4841
        raise errors.OpPrereqError("Missing attribute 'name' on opcode input")
4810 4842
      fname = self.cfg.ExpandInstanceName(self.op.name)
......
4816 4848
      raise errors.OpPrereqError("Invalid test allocator mode '%s'" %
4817 4849
                                 self.op.mode)
4818 4850

  
4819
    if self.op.direction == constants.ALF_DIR_OUT:
4820
      if not hasattr(self.op, "allocator"):
4851
    if self.op.direction == constants.IALLOCATOR_DIR_OUT:
4852
      if not hasattr(self.op, "allocator") or self.op.allocator is None:
4821 4853
        raise errors.OpPrereqError("Missing allocator name")
4822
      raise errors.OpPrereqError("Allocator out mode not supported yet")
4823
    elif self.op.direction != constants.ALF_DIR_IN:
4854
    elif self.op.direction != constants.IALLOCATOR_DIR_IN:
4824 4855
      raise errors.OpPrereqError("Wrong allocator test '%s'" %
4825 4856
                                 self.op.direction)
4826 4857

  
......
4828 4859
    """Run the allocator test.
4829 4860

  
4830 4861
    """
4831
    data = _AllocatorGetClusterData(self.cfg, self.sstore)
4832
    if self.op.mode == constants.ALF_MODE_ALLOC:
4833
      _AllocatorAddNewInstance(data, self.op)
4862
    data = _IAllocatorGetClusterData(self.cfg, self.sstore)
4863
    if self.op.mode == constants.IALLOCATOR_MODE_ALLOC:
4864
      _IAllocatorAddNewInstance(data, self.op)
4834 4865
    else:
4835
      _AllocatorAddRelocateInstance(data, self.op)
4866
      _IAllocatorAddRelocateInstance(data, self.op)
4836 4867

  
4837 4868
    if _JSON_INDENT is None:
4838 4869
      text = simplejson.dumps(data)
4839 4870
    else:
4840 4871
      text = simplejson.dumps(data, indent=_JSON_INDENT)
4841
    return text
4872
    if self.op.direction == constants.IALLOCATOR_DIR_IN:
4873
      result = text
4874
    else:
4875
      result = _IAllocatorRun(self.op.allocator, text)
4876
    return result

Also available in: Unified diff