Revision 22f50b1d

b/lib/cmdlib.py
6088 6088
    """
6089 6089
    self.needed_locks = {}
6090 6090

  
6091
    # cheap checks, mostly valid constants given
6092

  
6093
    if self.op.hypervisor is None:
6094
      self.op.hypervisor = self.cfg.GetHypervisorType()
6095

  
6096
    cluster = self.cfg.GetClusterInfo()
6097
    enabled_hvs = cluster.enabled_hypervisors
6098
    if self.op.hypervisor not in enabled_hvs:
6099
      raise errors.OpPrereqError("Selected hypervisor (%s) not enabled in the"
6100
                                 " cluster (%s)" % (self.op.hypervisor,
6101
                                  ",".join(enabled_hvs)),
6102
                                 errors.ECODE_STATE)
6103

  
6104
    # check hypervisor parameter syntax (locally)
6105
    utils.ForceDictType(self.op.hvparams, constants.HVS_PARAMETER_TYPES)
6106
    filled_hvp = objects.FillDict(cluster.hvparams[self.op.hypervisor],
6107
                                  self.op.hvparams)
6108
    hv_type = hypervisor.GetHypervisor(self.op.hypervisor)
6109
    hv_type.CheckParameterSyntax(filled_hvp)
6110
    self.hv_full = filled_hvp
6111
    # check that we don't specify global parameters on an instance
6112
    _CheckGlobalHvParams(self.op.hvparams)
6113

  
6114
    # fill and remember the beparams dict
6115
    utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
6116
    self.be_full = objects.FillDict(cluster.beparams[constants.PP_DEFAULT],
6117
                                    self.op.beparams)
6118

  
6119
    #### instance parameters check
6120

  
6121 6091
    instance_name = self.op.instance_name
6122 6092
    # this is just a preventive check, but someone might still add this
6123 6093
    # instance in the meantime, and creation will fail at lock-add time
......
6127 6097

  
6128 6098
    self.add_locks[locking.LEVEL_INSTANCE] = instance_name
6129 6099

  
6130
    # NIC buildup
6131
    self.nics = []
6132
    for idx, nic in enumerate(self.op.nics):
6133
      nic_mode_req = nic.get("mode", None)
6134
      nic_mode = nic_mode_req
6135
      if nic_mode is None:
6136
        nic_mode = cluster.nicparams[constants.PP_DEFAULT][constants.NIC_MODE]
6137

  
6138
      # in routed mode, for the first nic, the default ip is 'auto'
6139
      if nic_mode == constants.NIC_MODE_ROUTED and idx == 0:
6140
        default_ip_mode = constants.VALUE_AUTO
6141
      else:
6142
        default_ip_mode = constants.VALUE_NONE
6143

  
6144
      # ip validity checks
6145
      ip = nic.get("ip", default_ip_mode)
6146
      if ip is None or ip.lower() == constants.VALUE_NONE:
6147
        nic_ip = None
6148
      elif ip.lower() == constants.VALUE_AUTO:
6149
        if not self.op.name_check:
6150
          raise errors.OpPrereqError("IP address set to auto but name checks"
6151
                                     " have been skipped. Aborting.",
6152
                                     errors.ECODE_INVAL)
6153
        nic_ip = self.hostname1.ip
6154
      else:
6155
        if not utils.IsValidIP(ip):
6156
          raise errors.OpPrereqError("Given IP address '%s' doesn't look"
6157
                                     " like a valid IP" % ip,
6158
                                     errors.ECODE_INVAL)
6159
        nic_ip = ip
6160

  
6161
      # TODO: check the ip address for uniqueness
6162
      if nic_mode == constants.NIC_MODE_ROUTED and not nic_ip:
6163
        raise errors.OpPrereqError("Routed nic mode requires an ip address",
6164
                                   errors.ECODE_INVAL)
6165

  
6166
      # MAC address verification
6167
      mac = nic.get("mac", constants.VALUE_AUTO)
6168
      if mac not in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
6169
        mac = utils.NormalizeAndValidateMac(mac)
6170

  
6171
        try:
6172
          self.cfg.ReserveMAC(mac, self.proc.GetECId())
6173
        except errors.ReservationError:
6174
          raise errors.OpPrereqError("MAC address %s already in use"
6175
                                     " in cluster" % mac,
6176
                                     errors.ECODE_NOTUNIQUE)
6177

  
6178
      # bridge verification
6179
      bridge = nic.get("bridge", None)
6180
      link = nic.get("link", None)
6181
      if bridge and link:
6182
        raise errors.OpPrereqError("Cannot pass 'bridge' and 'link'"
6183
                                   " at the same time", errors.ECODE_INVAL)
6184
      elif bridge and nic_mode == constants.NIC_MODE_ROUTED:
6185
        raise errors.OpPrereqError("Cannot pass 'bridge' on a routed nic",
6186
                                   errors.ECODE_INVAL)
6187
      elif bridge:
6188
        link = bridge
6189

  
6190
      nicparams = {}
6191
      if nic_mode_req:
6192
        nicparams[constants.NIC_MODE] = nic_mode_req
6193
      if link:
6194
        nicparams[constants.NIC_LINK] = link
6195

  
6196
      check_params = objects.FillDict(cluster.nicparams[constants.PP_DEFAULT],
6197
                                      nicparams)
6198
      objects.NIC.CheckParameterSyntax(check_params)
6199
      self.nics.append(objects.NIC(mac=mac, ip=nic_ip, nicparams=nicparams))
6200

  
6201
    # disk checks/pre-build
6202
    self.disks = []
6203
    for disk in self.op.disks:
6204
      mode = disk.get("mode", constants.DISK_RDWR)
6205
      if mode not in constants.DISK_ACCESS_SET:
6206
        raise errors.OpPrereqError("Invalid disk access mode '%s'" %
6207
                                   mode, errors.ECODE_INVAL)
6208
      size = disk.get("size", None)
6209
      if size is None:
6210
        raise errors.OpPrereqError("Missing disk size", errors.ECODE_INVAL)
6211
      try:
6212
        size = int(size)
6213
      except (TypeError, ValueError):
6214
        raise errors.OpPrereqError("Invalid disk size '%s'" % size,
6215
                                   errors.ECODE_INVAL)
6216
      new_disk = {"size": size, "mode": mode}
6217
      if "adopt" in disk:
6218
        new_disk["adopt"] = disk["adopt"]
6219
      self.disks.append(new_disk)
6220

  
6221 6100
    if self.op.iallocator:
6222 6101
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
6223 6102
    else:
......
6332 6211
      raise errors.OpPrereqError("Cluster does not support lvm-based"
6333 6212
                                 " instances", errors.ECODE_STATE)
6334 6213

  
6214
    if self.op.hypervisor is None:
6215
      self.op.hypervisor = self.cfg.GetHypervisorType()
6216

  
6217
    cluster = self.cfg.GetClusterInfo()
6218
    enabled_hvs = cluster.enabled_hypervisors
6219
    if self.op.hypervisor not in enabled_hvs:
6220
      raise errors.OpPrereqError("Selected hypervisor (%s) not enabled in the"
6221
                                 " cluster (%s)" % (self.op.hypervisor,
6222
                                  ",".join(enabled_hvs)),
6223
                                 errors.ECODE_STATE)
6224

  
6225
    # check hypervisor parameter syntax (locally)
6226
    utils.ForceDictType(self.op.hvparams, constants.HVS_PARAMETER_TYPES)
6227
    filled_hvp = objects.FillDict(cluster.hvparams[self.op.hypervisor],
6228
                                  self.op.hvparams)
6229
    hv_type = hypervisor.GetHypervisor(self.op.hypervisor)
6230
    hv_type.CheckParameterSyntax(filled_hvp)
6231
    self.hv_full = filled_hvp
6232
    # check that we don't specify global parameters on an instance
6233
    _CheckGlobalHvParams(self.op.hvparams)
6234

  
6235
    # fill and remember the beparams dict
6236
    utils.ForceDictType(self.op.beparams, constants.BES_PARAMETER_TYPES)
6237
    self.be_full = objects.FillDict(cluster.beparams[constants.PP_DEFAULT],
6238
                                    self.op.beparams)
6239

  
6240
    # NIC buildup
6241
    self.nics = []
6242
    for idx, nic in enumerate(self.op.nics):
6243
      nic_mode_req = nic.get("mode", None)
6244
      nic_mode = nic_mode_req
6245
      if nic_mode is None:
6246
        nic_mode = cluster.nicparams[constants.PP_DEFAULT][constants.NIC_MODE]
6247

  
6248
      # in routed mode, for the first nic, the default ip is 'auto'
6249
      if nic_mode == constants.NIC_MODE_ROUTED and idx == 0:
6250
        default_ip_mode = constants.VALUE_AUTO
6251
      else:
6252
        default_ip_mode = constants.VALUE_NONE
6253

  
6254
      # ip validity checks
6255
      ip = nic.get("ip", default_ip_mode)
6256
      if ip is None or ip.lower() == constants.VALUE_NONE:
6257
        nic_ip = None
6258
      elif ip.lower() == constants.VALUE_AUTO:
6259
        if not self.op.name_check:
6260
          raise errors.OpPrereqError("IP address set to auto but name checks"
6261
                                     " have been skipped. Aborting.",
6262
                                     errors.ECODE_INVAL)
6263
        nic_ip = self.hostname1.ip
6264
      else:
6265
        if not utils.IsValidIP(ip):
6266
          raise errors.OpPrereqError("Given IP address '%s' doesn't look"
6267
                                     " like a valid IP" % ip,
6268
                                     errors.ECODE_INVAL)
6269
        nic_ip = ip
6270

  
6271
      # TODO: check the ip address for uniqueness
6272
      if nic_mode == constants.NIC_MODE_ROUTED and not nic_ip:
6273
        raise errors.OpPrereqError("Routed nic mode requires an ip address",
6274
                                   errors.ECODE_INVAL)
6275

  
6276
      # MAC address verification
6277
      mac = nic.get("mac", constants.VALUE_AUTO)
6278
      if mac not in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
6279
        mac = utils.NormalizeAndValidateMac(mac)
6280

  
6281
        try:
6282
          self.cfg.ReserveMAC(mac, self.proc.GetECId())
6283
        except errors.ReservationError:
6284
          raise errors.OpPrereqError("MAC address %s already in use"
6285
                                     " in cluster" % mac,
6286
                                     errors.ECODE_NOTUNIQUE)
6287

  
6288
      # bridge verification
6289
      bridge = nic.get("bridge", None)
6290
      link = nic.get("link", None)
6291
      if bridge and link:
6292
        raise errors.OpPrereqError("Cannot pass 'bridge' and 'link'"
6293
                                   " at the same time", errors.ECODE_INVAL)
6294
      elif bridge and nic_mode == constants.NIC_MODE_ROUTED:
6295
        raise errors.OpPrereqError("Cannot pass 'bridge' on a routed nic",
6296
                                   errors.ECODE_INVAL)
6297
      elif bridge:
6298
        link = bridge
6299

  
6300
      nicparams = {}
6301
      if nic_mode_req:
6302
        nicparams[constants.NIC_MODE] = nic_mode_req
6303
      if link:
6304
        nicparams[constants.NIC_LINK] = link
6305

  
6306
      check_params = objects.FillDict(cluster.nicparams[constants.PP_DEFAULT],
6307
                                      nicparams)
6308
      objects.NIC.CheckParameterSyntax(check_params)
6309
      self.nics.append(objects.NIC(mac=mac, ip=nic_ip, nicparams=nicparams))
6310

  
6311
    # disk checks/pre-build
6312
    self.disks = []
6313
    for disk in self.op.disks:
6314
      mode = disk.get("mode", constants.DISK_RDWR)
6315
      if mode not in constants.DISK_ACCESS_SET:
6316
        raise errors.OpPrereqError("Invalid disk access mode '%s'" %
6317
                                   mode, errors.ECODE_INVAL)
6318
      size = disk.get("size", None)
6319
      if size is None:
6320
        raise errors.OpPrereqError("Missing disk size", errors.ECODE_INVAL)
6321
      try:
6322
        size = int(size)
6323
      except (TypeError, ValueError):
6324
        raise errors.OpPrereqError("Invalid disk size '%s'" % size,
6325
                                   errors.ECODE_INVAL)
6326
      new_disk = {"size": size, "mode": mode}
6327
      if "adopt" in disk:
6328
        new_disk["adopt"] = disk["adopt"]
6329
      self.disks.append(new_disk)
6330

  
6335 6331
    if self.op.mode == constants.INSTANCE_IMPORT:
6336 6332
      src_node = self.op.src_node
6337 6333
      src_path = self.op.src_path

Also available in: Unified diff