Revision a807cc69

b/lib/cmdlib.py
1313 1313
  """Wrapper over L{_ExpandItemName} for instance."""
1314 1314
  return _ExpandItemName(cfg.ExpandInstanceName, name, "Instance")
1315 1315

  
1316
def _BuildNetworkHookEnv(name, network, gateway, network6, gateway6,
1317
                         network_type, mac_prefix):
1318
  env = dict()
1319
  if name:
1320
    env["NETWORK_NAME"] = name
1321
  if network:
1322
    env["NETWORK_SUBNET"] = network
1323
  if gateway:
1324
    env["NETWORK_GATEWAY"] = gateway
1325
  if network6:
1326
    env["NETWORK_SUBNET6"] = network6
1327
  if gateway6:
1328
    env["NETWORK_GATEWAY6"] = gateway6
1329
  if mac_prefix:
1330
    env["NETWORK_MAC_PREFIX"] = mac_prefix
1331
  if network_type:
1332
    env["NETWORK_TYPE"] = network_type
1333

  
1334
  return env
1335

  
1336

  
1337
def _BuildNetworkHookEnvByObject(lu, network):
1338
  args = {
1339
    "name": network.name,
1340
    "network": network.network,
1341
    "gateway": network.gateway,
1342
    "network6": network.network6,
1343
    "gateway6": network.gateway6,
1344
    "network_type": network.network_type,
1345
    "mac_prefix": network.mac_prefix,
1346
  }
1347
  return _BuildNetworkHookEnv(**args)
1348

  
1316 1349

  
1317 1350
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
1318 1351
                          minmem, maxmem, vcpus, nics, disk_template, disks,
......
1373 1406
  }
1374 1407
  if nics:
1375 1408
    nic_count = len(nics)
1376
    for idx, (ip, mac, mode, link, network) in enumerate(nics):
1409
    for idx, (ip, mac, mode, link, network, netinfo) in enumerate(nics):
1377 1410
      if ip is None:
1378 1411
        ip = ""
1379 1412
      env["INSTANCE_NIC%d_IP" % idx] = ip
1380 1413
      env["INSTANCE_NIC%d_MAC" % idx] = mac
1381 1414
      env["INSTANCE_NIC%d_MODE" % idx] = mode
1382 1415
      env["INSTANCE_NIC%d_LINK" % idx] = link
1383
      env["INSTANCE_NIC%d_NETWORK" % idx] = network
1416
      if network:
1417
        env["INSTANCE_NIC%d_NETWORK" % idx] = network
1418
        if netinfo:
1419
          nobj = objects.Network.FromDict(netinfo)
1420
          if nobj.network:
1421
            env["INSTANCE_NIC%d_NETWORK_SUBNET" % idx] = nobj.network
1422
          if nobj.gateway:
1423
            env["INSTANCE_NIC%d_NETWORK_GATEWAY" % idx] = nobj.gateway
1424
          if nobj.network6:
1425
            env["INSTANCE_NIC%d_NETWORK_SUBNET6" % idx] = nobj.network6
1426
          if nobj.gateway6:
1427
            env["INSTANCE_NIC%d_NETWORK_GATEWAY6" % idx] = nobj.gateway6
1428
          if nobj.mac_prefix:
1429
            env["INSTANCE_NIC%d_NETWORK_MAC_PREFIX" % idx] = nobj.mac_prefix
1430
          if nobj.network_type:
1431
            env["INSTANCE_NIC%d_NETWORK_TYPE" % idx] = nobj.network_type
1384 1432
      if mode == constants.NIC_MODE_BRIDGED:
1385 1433
        env["INSTANCE_NIC%d_BRIDGE" % idx] = link
1386 1434
  else:
......
1409 1457

  
1410 1458
  return env
1411 1459

  
1460
def _NICToTuple(lu, nic):
1461
  """Build a tupple of nic information.
1462

  
1463
  @type lu:  L{LogicalUnit}
1464
  @param lu: the logical unit on whose behalf we execute
1465
  @type nic: L{objects.NIC}
1466
  @param nic: nic to convert to hooks tuple
1467

  
1468
  """
1469
  cluster = lu.cfg.GetClusterInfo()
1470
  ip = nic.ip
1471
  mac = nic.mac
1472
  filled_params = cluster.SimpleFillNIC(nic.nicparams)
1473
  mode = filled_params[constants.NIC_MODE]
1474
  link = filled_params[constants.NIC_LINK]
1475
  network = nic.network
1476
  netinfo = None
1477
  if network:
1478
    net_uuid = lu.cfg.LookupNetwork(network)
1479
    if net_uuid:
1480
      nobj = lu.cfg.GetNetwork(net_uuid)
1481
      netinfo = objects.Network.ToDict(nobj)
1482
  return (ip, mac, mode, link, network, netinfo)
1412 1483

  
1413 1484
def _NICListToTuple(lu, nics):
1414 1485
  """Build a list of nic information tuples.
......
1425 1496
  hooks_nics = []
1426 1497
  cluster = lu.cfg.GetClusterInfo()
1427 1498
  for nic in nics:
1428
    ip = nic.ip
1429
    mac = nic.mac
1430
    filled_params = cluster.SimpleFillNIC(nic.nicparams)
1431
    mode = filled_params[constants.NIC_MODE]
1432
    link = filled_params[constants.NIC_LINK]
1433
    network = nic.network
1434
    hooks_nics.append((ip, mac, mode, link, network))
1499
    hooks_nics.append(_NICToTuple(lu, nic))
1435 1500
  return hooks_nics
1436 1501

  
1437

  
1438 1502
def _BuildInstanceHookEnvByObject(lu, instance, override=None):
1439 1503
  """Builds instance related env variables for hooks from an object.
1440 1504

  
......
12355 12419
      nics = []
12356 12420

  
12357 12421
      for nic in self._new_nics:
12358
        nicparams = self.cluster.SimpleFillNIC(nic.nicparams)
12359
        mode = nicparams[constants.NIC_MODE]
12360
        link = nicparams[constants.NIC_LINK]
12361
        nics.append((nic.ip, nic.mac, mode, link, nic.network))
12422
        n = copy.deepcopy(nic)
12423
        nicparams = self.cluster.SimpleFillNIC(n.nicparams)
12424
        n.nicparams = nicparams
12425
        nics.append(_NICToTuple(self, n))
12362 12426

  
12363 12427
      args["nics"] = nics
12364 12428

  
......
15535 15599
    """Build hooks env.
15536 15600

  
15537 15601
    """
15538
    env = {
15539
      "NETWORK_NAME": self.op.network_name,
15540
      "NETWORK_SUBNET": self.op.network,
15541
      "NETWORK_GATEWAY": self.op.gateway,
15542
      "NETWORK_SUBNET6": self.op.network6,
15543
      "NETWORK_GATEWAY6": self.op.gateway6,
15544
      "NETWORK_MAC_PREFIX": self.mac_prefix,
15545
      "NETWORK_TYPE": self.op.network_type,
15602
    args = {
15603
      "name": self.op.network_name,
15604
      "network": self.op.network,
15605
      "gateway": self.op.gateway,
15606
      "network6": self.op.network6,
15607
      "gateway6": self.op.gateway6,
15608
      "mac_prefix": self.mac_prefix,
15609
      "network_type": self.op.network_type,
15546 15610
      }
15547
    return env
15611
    return _BuildNetworkHookEnv(**args)
15548 15612

  
15549 15613
  def Exec(self, feedback_fn):
15550 15614
    """Add the ip pool to the cluster.
......
15741 15805
    """Build hooks env.
15742 15806

  
15743 15807
    """
15744
    env = {
15745
      "NETWORK_NAME": self.op.network_name,
15746
      "NETWORK_SUBNET": self.network.network,
15747
      "NETWORK_GATEWAY": self.gateway,
15748
      "NETWORK_SUBNET6": self.network6,
15749
      "NETWORK_GATEWAY6": self.gateway6,
15750
      "NETWORK_MAC_PREFIX": self.mac_prefix,
15751
      "NETWORK_TYPE": self.network_type,
15808
    args = {
15809
      "name": self.op.network_name,
15810
      "network": self.network.network,
15811
      "gateway": self.gateway,
15812
      "network6": self.network6,
15813
      "gateway6": self.gateway6,
15814
      "mac_prefix": self.mac_prefix,
15815
      "network_type": self.network_type,
15752 15816
      }
15753
    return env
15817
    return _BuildNetworkHookEnv(**args)
15754 15818

  
15755 15819
  def BuildHooksNodes(self):
15756 15820
    """Build hooks nodes.
......
15985 16049
  def BuildHooksEnv(self):
15986 16050
    ret = dict()
15987 16051
    ret["GROUP_NAME"] = self.group_name
15988
    ret["GROUP_NETWORK_NAME"] = self.network_name
15989 16052
    ret["GROUP_NETWORK_MODE"] = self.network_mode
15990 16053
    ret["GROUP_NETWORK_LINK"] = self.network_link
16054
    ret.update(_BuildNetworkHookEnvByObject(self, self.network))
15991 16055
    return ret
15992 16056

  
15993 16057
  def BuildHooksNodes(self):
......
16110 16174
  def BuildHooksEnv(self):
16111 16175
    ret = dict()
16112 16176
    ret["GROUP_NAME"] = self.group_name
16113
    ret["GROUP_NETWORK_NAME"] = self.network_name
16177
    ret.update(_BuildNetworkHookEnvByObject(self, self.network))
16114 16178
    return ret
16115 16179

  
16116 16180
  def BuildHooksNodes(self):

Also available in: Unified diff