Esempio n. 1
0
    def GetPostOpInput(self):
        """Try to allocate multiple instances.

    @return: A dict with submitted jobs, allocatable instances and failed
             allocations

    """
        if "instances" not in self.request_body:
            raise http.HttpBadRequest(
                "Request is missing required 'instances' field"
                " in body")

        op_id = {
            "OP_ID": self.POST_OPCODE.OP_ID,  # pylint: disable=E1101
        }
        body = objects.FillDict(
            self.request_body, {
                "instances": [
                    objects.FillDict(inst, op_id)
                    for inst in self.request_body["instances"]
                ],
            })

        return (body, {
            "dry_run": self.dryRun(),
        })
Esempio n. 2
0
    def _MakeInstance(self):
        # Copy default parameters
        bep = objects.FillDict(constants.BEC_DEFAULTS, {})
        hvp = objects.FillDict(constants.HVC_DEFAULTS[self.HVNAME], {})

        # Override default VNC password file path
        if constants.HV_VNC_PASSWORD_FILE in hvp:
            hvp[constants.HV_VNC_PASSWORD_FILE] = self.vncpw_path

        disks = [
            (objects.Disk(dev_type=constants.DT_PLAIN,
                          mode=constants.DISK_RDWR),
             utils.PathJoin(self.tmpdir, "disk0"), NotImplemented),
            (objects.Disk(dev_type=constants.DT_PLAIN,
                          mode=constants.DISK_RDONLY),
             utils.PathJoin(self.tmpdir, "disk1"), NotImplemented),
        ]

        inst = objects.Instance(name="server01.example.com",
                                hvparams=hvp,
                                beparams=bep,
                                osparams={},
                                nics=[],
                                os="deb1",
                                disks=map(compat.fst, disks))
        inst.UpgradeConfig()

        return (inst, disks)
Esempio n. 3
0
def _AnnotateDParamsDRBD(disk, params):
    """Annotates just DRBD disks layouts.

  """
    (drbd_params, data_params, meta_params) = params
    assert disk.dev_type == constants.DT_DRBD8

    disk.params = objects.FillDict(drbd_params, disk.params)
    (dev_data, dev_meta) = disk.children
    dev_data.params = objects.FillDict(data_params, dev_data.params)
    dev_meta.params = objects.FillDict(meta_params, dev_meta.params)

    return disk
Esempio n. 4
0
def ConnectNetwork(opts, args):
    """Map a network to a node group.

  @param opts: the command line options selected by the user
  @type args: list
  @param args: Network, mode, physlink and node groups
  @rtype: int
  @return: the desired exit code

  """
    cl = GetClient()

    network = args[0]
    nicparams = objects.FillDict(constants.NICC_DEFAULTS, opts.nicparams)

    groups = _GetDefaultGroups(cl, args[1:])

    # TODO: Change logic to support "--submit"
    for group in groups:
        op = opcodes.OpNetworkConnect(
            group_name=group,
            network_name=network,
            network_mode=nicparams[constants.NIC_MODE],
            network_link=nicparams[constants.NIC_LINK],
            network_vlan=nicparams[constants.NIC_VLAN],
            conflicts_check=opts.conflicts_check)
        SubmitOpCode(op, opts=opts, cl=cl)
Esempio n. 5
0
    def GetPostOpInput(self):
        """Try to allocate multiple instances.

    @return: A dict with submitted jobs, allocatable instances and failed
             allocations

    """
        if "instances" not in self.request_body:
            raise http.HttpBadRequest(
                "Request is missing required 'instances' field"
                " in body")

        # Unlike most other RAPI calls, this one is composed of individual opcodes,
        # and we have to do the filling ourselves
        OPCODE_RENAME = {
            "os": "os_type",
            "name": "instance_name",
        }

        body = objects.FillDict(
            self.request_body, {
                "instances": [
                    baserlib.FillOpcode(opcodes.OpInstanceCreate,
                                        inst, {},
                                        rename=OPCODE_RENAME)
                    for inst in self.request_body["instances"]
                ],
            })

        return (body, {
            "dry_run": self.dryRun(),
        })
Esempio n. 6
0
def CheckHVParams(lu, node_uuids, hvname, hvparams):
    """Hypervisor parameter validation.

  This function abstract the hypervisor parameter validation to be
  used in both instance create and instance modify.

  @type lu: L{LogicalUnit}
  @param lu: the logical unit for which we check
  @type node_uuids: list
  @param node_uuids: the list of nodes on which we should check
  @type hvname: string
  @param hvname: the name of the hypervisor we should use
  @type hvparams: dict
  @param hvparams: the parameters which we need to check
  @raise errors.OpPrereqError: if the parameters are not valid

  """
    node_uuids = _FilterVmNodes(lu, node_uuids)

    cluster = lu.cfg.GetClusterInfo()
    hvfull = objects.FillDict(cluster.hvparams.get(hvname, {}), hvparams)

    hvinfo = lu.rpc.call_hypervisor_validate_params(node_uuids, hvname, hvfull)
    for node_uuid in node_uuids:
        info = hvinfo[node_uuid]
        if info.offline:
            continue
        info.Raise("Hypervisor parameter validation failed on node %s" %
                   lu.cfg.GetNodeName(node_uuid))
Esempio n. 7
0
def _AnnotateDParamsGeneric(disk, params):
    """Generic disk parameter annotation routine.

  """
    assert disk.dev_type != constants.DT_DRBD8

    disk.params = objects.FillDict(params[0], disk.params)

    return disk
Esempio n. 8
0
    def CheckPrereq(self):
        """Check prerequisites.

    """
        owned_instance_names = frozenset(
            self.owned_locks(locking.LEVEL_INSTANCE))

        # Check if locked instances are still correct
        CheckNodeGroupInstances(self.cfg, self.group_uuid,
                                owned_instance_names)

        self.group = self.cfg.GetNodeGroup(self.group_uuid)
        cluster = self.cfg.GetClusterInfo()

        if self.group is None:
            raise errors.OpExecError(
                "Could not retrieve group '%s' (UUID: %s)" %
                (self.op.group_name, self.group_uuid))

        if self.op.ndparams:
            new_ndparams = GetUpdatedParams(self.group.ndparams,
                                            self.op.ndparams)
            utils.ForceDictType(new_ndparams, constants.NDS_PARAMETER_TYPES)
            self.new_ndparams = new_ndparams

        if self.op.diskparams:
            diskparams = self.group.diskparams
            uavdp = self._UpdateAndVerifyDiskParams
            # For each disktemplate subdict update and verify the values
            new_diskparams = dict(
                (dt, uavdp(diskparams.get(dt, {}), self.op.diskparams[dt]))
                for dt in constants.DISK_TEMPLATES if dt in self.op.diskparams)
            # As we've all subdicts of diskparams ready, lets merge the actual
            # dict with all updated subdicts
            self.new_diskparams = objects.FillDict(diskparams, new_diskparams)

            try:
                utils.VerifyDictOptions(self.new_diskparams,
                                        constants.DISK_DT_DEFAULTS)
                CheckDiskAccessModeConsistency(self.new_diskparams,
                                               self.cfg,
                                               group=self.group)
            except errors.OpPrereqError as err:
                raise errors.OpPrereqError(
                    "While verify diskparams options: %s" % err,
                    errors.ECODE_INVAL)

        if self.op.hv_state:
            self.new_hv_state = MergeAndVerifyHvState(
                self.op.hv_state, self.group.hv_state_static)

        if self.op.disk_state:
            self.new_disk_state = \
              MergeAndVerifyDiskState(self.op.disk_state,
                                      self.group.disk_state_static)

        self._CheckIpolicy(cluster, owned_instance_names)
Esempio n. 9
0
    def _InstDict(self, node, instance, hvp=None, bep=None, osp=None):
        """Convert the given instance to a dict.

    This is done via the instance's ToDict() method and additionally
    we fill the hvparams with the cluster defaults.

    @type instance: L{objects.Instance}
    @param instance: an Instance object
    @type hvp: dict or None
    @param hvp: a dictionary with overridden hypervisor parameters
    @type bep: dict or None
    @param bep: a dictionary with overridden backend parameters
    @type osp: dict or None
    @param osp: a dictionary with overridden os parameters
    @rtype: dict
    @return: the instance dict, with the hvparams filled with the
        cluster defaults

    """
        idict = instance.ToDict()
        cluster = self._cfg.GetClusterInfo()
        idict["hvparams"] = cluster.FillHV(instance)
        idict["secondary_nodes"] = \
          self._cfg.GetInstanceSecondaryNodes(instance.uuid)
        if hvp is not None:
            idict["hvparams"].update(hvp)
        idict["beparams"] = cluster.FillBE(instance)
        if bep is not None:
            idict["beparams"].update(bep)
        idict["osparams"] = cluster.SimpleFillOS(instance.os,
                                                 instance.osparams)
        if osp is not None:
            idict["osparams"].update(osp)
        disks = self._cfg.GetInstanceDisks(instance.uuid)
        idict["disks_info"] = self._DisksDictDP(node, (disks, instance))
        for nic in idict["nics"]:
            nic["nicparams"] = objects.FillDict(
                cluster.nicparams[constants.PP_DEFAULT], nic["nicparams"])
            network = nic.get("network", None)
            if network:
                net_uuid = self._cfg.LookupNetwork(network)
                if net_uuid:
                    nobj = self._cfg.GetNetwork(net_uuid)
                    nic["netinfo"] = objects.Network.ToDict(nobj)
        return idict
Esempio n. 10
0
    def CheckPrereq(self):
        """Check prerequisite.

    """
        if self.op.iallocator:
            cluster = self.cfg.GetClusterInfo()
            default_vg = self.cfg.GetVGName()
            ec_id = self.proc.GetECId()

            if self.op.opportunistic_locking:
                # Only consider nodes for which a lock is held
                node_whitelist = self.cfg.GetNodeNames(
                    set(self.owned_locks(locking.LEVEL_NODE))
                    & set(self.owned_locks(locking.LEVEL_NODE_RES)))
            else:
                node_whitelist = None

            insts = [
                CreateInstanceAllocRequest(
                    op, ComputeDisks(op.disks, op.disk_template, default_vg),
                    ComputeNics(op, cluster, None, self.cfg, ec_id),
                    ComputeFullBeParams(op, cluster), node_whitelist)
                for op in self.op.instances
            ]

            req = iallocator.IAReqMultiInstanceAlloc(instances=insts)
            ial = iallocator.IAllocator(self.cfg, self.rpc, req)

            ial.Run(self.op.iallocator)

            if not ial.success:
                raise errors.OpPrereqError(
                    "Can't compute nodes using"
                    " iallocator '%s': %s" % (self.op.iallocator, ial.info),
                    errors.ECODE_NORES)

            self.ia_result = ial.result

        if self.op.dry_run:
            self.dry_run_result = objects.FillDict(
                self._ConstructPartialResult(), {
                    constants.JOB_IDS_KEY: [],
                })
Esempio n. 11
0
    ret = utils.JoinDisjointDicts(hv_info, {"bootid": bootid})

    _AddSpindlesToLegacyNodeInfo(ret, space_info)
    _AddStorageInfoToLegacyNodeInfoByTemplate(ret, space_info, disk_template)

    return ret


def _AnnotateDParamsDRBD(disk, (drbd_params, data_params, meta_params)):
    """Annotates just DRBD disks layouts.

  """
    assert disk.dev_type == constants.DT_DRBD8

    disk.params = objects.FillDict(drbd_params, disk.params)
    (dev_data, dev_meta) = disk.children
    dev_data.params = objects.FillDict(data_params, dev_data.params)
    dev_meta.params = objects.FillDict(meta_params, dev_meta.params)

    return disk


def _AnnotateDParamsGeneric(disk, (params, )):
    """Generic disk parameter annotation routine.

  """
    assert disk.dev_type != constants.DT_DRBD8

    disk.params = objects.FillDict(params, disk.params)
Esempio n. 12
0
 def testFillDict(self):
     pDict = serializer.PrivateDict()
     pDict["bar"] = "egg"
     self.assertEqual(pDict, objects.FillDict({}, pDict))