Revision beb81ea5 lib/cmdlib.py
b/lib/cmdlib.py | ||
---|---|---|
40 | 40 |
import shutil |
41 | 41 |
import itertools |
42 | 42 |
import operator |
43 |
import ipaddr |
|
44 | 43 |
|
45 | 44 |
from ganeti import ssh |
46 | 45 |
from ganeti import utils |
... | ... | |
1315 | 1314 |
"""Wrapper over L{_ExpandItemName} for instance.""" |
1316 | 1315 |
return _ExpandItemName(cfg.ExpandInstanceName, name, "Instance") |
1317 | 1316 |
|
1318 |
def _BuildNetworkHookEnv(name, network, gateway, network6, gateway6, |
|
1317 |
|
|
1318 |
def _BuildNetworkHookEnv(name, subnet, gateway, network6, gateway6, |
|
1319 | 1319 |
network_type, mac_prefix, tags): |
1320 |
"""Builds network related env variables for hooks |
|
1321 |
|
|
1322 |
This builds the hook environment from individual variables. |
|
1323 |
|
|
1324 |
@type name: string |
|
1325 |
@param name: the name of the network |
|
1326 |
@type network: string |
|
1327 |
@param network: the ipv4 subnet |
|
1328 |
@type gateway: string |
|
1329 |
@param gateway: the ipv4 gateway |
|
1330 |
@type network6: string |
|
1331 |
@param network6: the ipv6 subnet |
|
1332 |
@type gateway6: string |
|
1333 |
@param gateway6: the ipv6 gateway |
|
1334 |
@type network_type: string |
|
1335 |
@param network_type: the type of the network |
|
1336 |
@type mac_prefix: string |
|
1337 |
@param mac_prefix: the mac_prefix |
|
1338 |
@type tags: list |
|
1339 |
@param tags: the tags of the network |
|
1340 |
|
|
1341 |
""" |
|
1320 | 1342 |
env = dict() |
1321 | 1343 |
if name: |
1322 | 1344 |
env["NETWORK_NAME"] = name |
1323 |
if network:
|
|
1324 |
env["NETWORK_SUBNET"] = network
|
|
1345 |
if subnet:
|
|
1346 |
env["NETWORK_SUBNET"] = subnet
|
|
1325 | 1347 |
if gateway: |
1326 | 1348 |
env["NETWORK_GATEWAY"] = gateway |
1327 | 1349 |
if network6: |
... | ... | |
1338 | 1360 |
return env |
1339 | 1361 |
|
1340 | 1362 |
|
1341 |
def _BuildNetworkHookEnvByObject(lu, network): |
|
1363 |
def _BuildNetworkHookEnvByObject(net): |
|
1364 |
"""Builds network related env varliables for hooks |
|
1365 |
|
|
1366 |
@type lu: L{objects.LogicalUnit} |
|
1367 |
@param lu: the corresponding LU |
|
1368 |
@type network: L{objects.Network} |
|
1369 |
@param network: the network object |
|
1370 |
|
|
1371 |
""" |
|
1342 | 1372 |
args = { |
1343 |
"name": network.name,
|
|
1344 |
"network": network.network,
|
|
1345 |
"gateway": network.gateway,
|
|
1346 |
"network6": network.network6,
|
|
1347 |
"gateway6": network.gateway6,
|
|
1348 |
"network_type": network.network_type,
|
|
1349 |
"mac_prefix": network.mac_prefix,
|
|
1350 |
"tags" : network.tags,
|
|
1373 |
"name": net.name, |
|
1374 |
"subnet": net.network,
|
|
1375 |
"gateway": net.gateway, |
|
1376 |
"network6": net.network6, |
|
1377 |
"gateway6": net.gateway6, |
|
1378 |
"network_type": net.network_type, |
|
1379 |
"mac_prefix": net.mac_prefix, |
|
1380 |
"tags": net.tags,
|
|
1351 | 1381 |
} |
1352 |
return _BuildNetworkHookEnv(**args) |
|
1382 |
|
|
1383 |
return _BuildNetworkHookEnv(**args) # pylint: disable=W0142 |
|
1353 | 1384 |
|
1354 | 1385 |
|
1355 | 1386 |
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status, |
... | ... | |
1411 | 1442 |
} |
1412 | 1443 |
if nics: |
1413 | 1444 |
nic_count = len(nics) |
1414 |
for idx, (ip, mac, mode, link, network, netinfo) in enumerate(nics):
|
|
1445 |
for idx, (ip, mac, mode, link, net, netinfo) in enumerate(nics): |
|
1415 | 1446 |
if ip is None: |
1416 | 1447 |
ip = "" |
1417 | 1448 |
env["INSTANCE_NIC%d_IP" % idx] = ip |
... | ... | |
1419 | 1450 |
env["INSTANCE_NIC%d_MODE" % idx] = mode |
1420 | 1451 |
env["INSTANCE_NIC%d_LINK" % idx] = link |
1421 | 1452 |
if network: |
1422 |
env["INSTANCE_NIC%d_NETWORK" % idx] = network
|
|
1453 |
env["INSTANCE_NIC%d_NETWORK" % idx] = net |
|
1423 | 1454 |
if netinfo: |
1424 | 1455 |
nobj = objects.Network.FromDict(netinfo) |
1425 | 1456 |
if nobj.network: |
... | ... | |
1464 | 1495 |
|
1465 | 1496 |
return env |
1466 | 1497 |
|
1498 |
|
|
1467 | 1499 |
def _NICToTuple(lu, nic): |
1468 | 1500 |
"""Build a tupple of nic information. |
1469 | 1501 |
|
... | ... | |
1473 | 1505 |
@param nic: nic to convert to hooks tuple |
1474 | 1506 |
|
1475 | 1507 |
""" |
1476 |
cluster = lu.cfg.GetClusterInfo() |
|
1477 | 1508 |
ip = nic.ip |
1478 | 1509 |
mac = nic.mac |
1510 |
cluster = lu.cfg.GetClusterInfo() |
|
1479 | 1511 |
filled_params = cluster.SimpleFillNIC(nic.nicparams) |
1480 | 1512 |
mode = filled_params[constants.NIC_MODE] |
1481 | 1513 |
link = filled_params[constants.NIC_LINK] |
1482 |
network = nic.network
|
|
1514 |
net = nic.network |
|
1483 | 1515 |
netinfo = None |
1484 |
if network:
|
|
1485 |
net_uuid = lu.cfg.LookupNetwork(network)
|
|
1516 |
if net: |
|
1517 |
net_uuid = lu.cfg.LookupNetwork(net) |
|
1486 | 1518 |
if net_uuid: |
1487 | 1519 |
nobj = lu.cfg.GetNetwork(net_uuid) |
1488 | 1520 |
netinfo = objects.Network.ToDict(nobj) |
1489 |
return (ip, mac, mode, link, network, netinfo) |
|
1521 |
return (ip, mac, mode, link, net, netinfo) |
|
1522 |
|
|
1490 | 1523 |
|
1491 | 1524 |
def _NICListToTuple(lu, nics): |
1492 | 1525 |
"""Build a list of nic information tuples. |
... | ... | |
1501 | 1534 |
|
1502 | 1535 |
""" |
1503 | 1536 |
hooks_nics = [] |
1504 |
cluster = lu.cfg.GetClusterInfo() |
|
1505 | 1537 |
for nic in nics: |
1506 | 1538 |
hooks_nics.append(_NICToTuple(lu, nic)) |
1507 | 1539 |
return hooks_nics |
1508 | 1540 |
|
1541 |
|
|
1509 | 1542 |
def _BuildInstanceHookEnvByObject(lu, instance, override=None): |
1510 | 1543 |
"""Builds instance related env variables for hooks from an object. |
1511 | 1544 |
|
... | ... | |
9476 | 9509 |
|
9477 | 9510 |
""" |
9478 | 9511 |
nics = [] |
9479 |
for idx, nic in enumerate(op.nics):
|
|
9512 |
for nic in op.nics:
|
|
9480 | 9513 |
nic_mode_req = nic.get(constants.INIC_MODE, None) |
9481 | 9514 |
nic_mode = nic_mode_req |
9482 | 9515 |
if nic_mode is None or nic_mode == constants.VALUE_AUTO: |
... | ... | |
10238 | 10271 |
(idx, netparams.values())) |
10239 | 10272 |
nic.nicparams = dict(netparams) |
10240 | 10273 |
if nic.ip is not None: |
10241 |
filled_params = cluster.SimpleFillNIC(nic.nicparams) |
|
10242 | 10274 |
if nic.ip.lower() == constants.NIC_IP_POOL: |
10243 | 10275 |
try: |
10244 | 10276 |
nic.ip = self.cfg.GenerateIp(net, self.proc.GetECId()) |
... | ... | |
12962 | 12994 |
errors.ECODE_NOTUNIQUE) |
12963 | 12995 |
elif new_ip.lower() == constants.NIC_IP_POOL: |
12964 | 12996 |
raise errors.OpPrereqError("ip=pool, but no network found", |
12965 |
ECODEE_INVAL)
|
|
12997 |
errors.ECODE_INVAL)
|
|
12966 | 12998 |
else: |
12967 | 12999 |
# new net is None |
12968 | 13000 |
if self.op.conflicts_check: |
... | ... | |
13228 | 13260 |
nic.nicparams, cluster, pnode) |
13229 | 13261 |
return None |
13230 | 13262 |
|
13231 |
def _PrepareNicRemove(_, params, private):
|
|
13263 |
def _PrepareNicRemove(_, params, __):
|
|
13232 | 13264 |
ip = params.ip |
13233 | 13265 |
net = params.network |
13234 | 13266 |
if net is not None and ip is not None: |
... | ... | |
13454 | 13486 |
""" |
13455 | 13487 |
mac = params[constants.INIC_MAC] |
13456 | 13488 |
ip = params.get(constants.INIC_IP, None) |
13457 |
network = params.get(constants.INIC_NETWORK, None)
|
|
13489 |
net = params.get(constants.INIC_NETWORK, None) |
|
13458 | 13490 |
#TODO: not private.filled?? can a nic have no nicparams?? |
13459 | 13491 |
nicparams = private.filled |
13460 | 13492 |
|
13461 |
return (objects.NIC(mac=mac, ip=ip, network=network, nicparams=nicparams), [
|
|
13493 |
return (objects.NIC(mac=mac, ip=ip, network=net, nicparams=nicparams), [ |
|
13462 | 13494 |
("nic.%d" % idx, |
13463 | 13495 |
"add:mac=%s,ip=%s,mode=%s,link=%s,network=%s" % |
13464 | 13496 |
(mac, ip, private.filled[constants.NIC_MODE], |
13465 | 13497 |
private.filled[constants.NIC_LINK], |
13466 |
network)),
|
|
13498 |
net)), |
|
13467 | 13499 |
]) |
13468 | 13500 |
|
13469 | 13501 |
@staticmethod |
... | ... | |
15571 | 15603 |
""" |
15572 | 15604 |
args = { |
15573 | 15605 |
"name": self.op.network_name, |
15574 |
"network": self.op.network,
|
|
15606 |
"subnet": self.op.network,
|
|
15575 | 15607 |
"gateway": self.op.gateway, |
15576 | 15608 |
"network6": self.op.network6, |
15577 | 15609 |
"gateway6": self.op.gateway6, |
... | ... | |
15579 | 15611 |
"network_type": self.op.network_type, |
15580 | 15612 |
"tags": self.op.tags, |
15581 | 15613 |
} |
15582 |
return _BuildNetworkHookEnv(**args) |
|
15614 |
return _BuildNetworkHookEnv(**args) # pylint: disable=W0142
|
|
15583 | 15615 |
|
15584 | 15616 |
def Exec(self, feedback_fn): |
15585 | 15617 |
"""Add the ip pool to the cluster. |
... | ... | |
15662 | 15694 |
# Verify that the network is not conncted. |
15663 | 15695 |
node_groups = [group.name |
15664 | 15696 |
for group in self.cfg.GetAllNodeGroupsInfo().values() |
15665 |
for network in group.networks.keys()
|
|
15666 |
if network == self.network_uuid]
|
|
15697 |
for net in group.networks.keys() |
|
15698 |
if net == self.network_uuid] |
|
15667 | 15699 |
|
15668 | 15700 |
if node_groups: |
15669 | 15701 |
self.LogWarning("Nework '%s' is connected to the following" |
... | ... | |
15779 | 15811 |
""" |
15780 | 15812 |
args = { |
15781 | 15813 |
"name": self.op.network_name, |
15782 |
"network": self.network.network,
|
|
15814 |
"subnet": self.network.network,
|
|
15783 | 15815 |
"gateway": self.gateway, |
15784 | 15816 |
"network6": self.network6, |
15785 | 15817 |
"gateway6": self.gateway6, |
... | ... | |
15787 | 15819 |
"network_type": self.network_type, |
15788 | 15820 |
"tags": self.tags, |
15789 | 15821 |
} |
15790 |
return _BuildNetworkHookEnv(**args) |
|
15822 |
return _BuildNetworkHookEnv(**args) # pylint: disable=W0142
|
|
15791 | 15823 |
|
15792 | 15824 |
def BuildHooksNodes(self): |
15793 | 15825 |
"""Build hooks nodes. |
... | ... | |
15892 | 15924 |
do_instances = query.NETQ_INST in self.requested_data |
15893 | 15925 |
do_groups = do_instances or (query.NETQ_GROUP in self.requested_data) |
15894 | 15926 |
do_stats = query.NETQ_STATS in self.requested_data |
15895 |
cluster = lu.cfg.GetClusterInfo() |
|
15896 | 15927 |
|
15897 | 15928 |
network_to_groups = None |
15898 | 15929 |
network_to_instances = None |
... | ... | |
15902 | 15933 |
if do_groups: |
15903 | 15934 |
all_groups = lu.cfg.GetAllNodeGroupsInfo() |
15904 | 15935 |
network_to_groups = dict((uuid, []) for uuid in self.wanted) |
15905 |
default_nicpp = cluster.nicparams[constants.PP_DEFAULT] |
|
15906 | 15936 |
|
15907 | 15937 |
if do_instances: |
15908 | 15938 |
all_instances = lu.cfg.GetAllInstancesInfo() |
... | ... | |
16015 | 16045 |
ret["GROUP_NAME"] = self.group_name |
16016 | 16046 |
ret["GROUP_NETWORK_MODE"] = self.network_mode |
16017 | 16047 |
ret["GROUP_NETWORK_LINK"] = self.network_link |
16018 |
ret.update(_BuildNetworkHookEnvByObject(self, self.network))
|
|
16048 |
ret.update(_BuildNetworkHookEnvByObject(self.network)) |
|
16019 | 16049 |
return ret |
16020 | 16050 |
|
16021 | 16051 |
def BuildHooksNodes(self): |
... | ... | |
16049 | 16079 |
instances = [(instance.name, idx, nic.ip) |
16050 | 16080 |
for instance in groupinstances |
16051 | 16081 |
for idx, nic in enumerate(instance.nics) |
16052 |
if (not nic.network and pool._Contains(nic.ip))]
|
|
16082 |
if (not nic.network and pool.Contains(nic.ip))] |
|
16053 | 16083 |
if instances: |
16054 | 16084 |
self.LogWarning("Following occurences use IPs from network %s" |
16055 | 16085 |
" that is about to connect to nodegroup %s: %s" % |
... | ... | |
16110 | 16140 |
def BuildHooksEnv(self): |
16111 | 16141 |
ret = dict() |
16112 | 16142 |
ret["GROUP_NAME"] = self.group_name |
16113 |
ret.update(_BuildNetworkHookEnvByObject(self, self.network))
|
|
16143 |
ret.update(_BuildNetworkHookEnvByObject(self.network)) |
|
16114 | 16144 |
return ret |
16115 | 16145 |
|
16116 | 16146 |
def BuildHooksNodes(self): |
... | ... | |
16192 | 16222 |
@param node: node name |
16193 | 16223 |
|
16194 | 16224 |
""" |
16195 |
(conf_net, conf_netparams) = lu.cfg.CheckIPInNodeGroup(ip, node)
|
|
16225 |
(conf_net, _) = lu.cfg.CheckIPInNodeGroup(ip, node)
|
|
16196 | 16226 |
if conf_net is not None: |
16197 | 16227 |
raise errors.OpPrereqError("Conflicting IP found:" |
16198 | 16228 |
" %s <> %s." % (ip, conf_net), |
Also available in: Unified diff