26 |
26 |
from ganeti import locking
|
27 |
27 |
from ganeti import network
|
28 |
28 |
from ganeti import objects
|
29 |
|
from ganeti import qlang
|
30 |
29 |
from ganeti import query
|
31 |
30 |
from ganeti import utils
|
32 |
31 |
from ganeti.cmdlib.base import LogicalUnit, NoHooksLU, QueryBase
|
33 |
|
from ganeti.cmdlib.common import ShareAll, CheckNodeGroupInstances
|
|
32 |
from ganeti.cmdlib.common import CheckNodeGroupInstances
|
34 |
33 |
|
35 |
34 |
|
36 |
35 |
def _BuildNetworkHookEnv(name, subnet, gateway, network6, gateway6,
|
... | ... | |
403 |
402 |
FIELDS = query.NETWORK_FIELDS
|
404 |
403 |
|
405 |
404 |
def ExpandNames(self, lu):
|
406 |
|
lu.needed_locks = {}
|
407 |
|
lu.share_locks = ShareAll()
|
408 |
|
|
409 |
|
self.do_locking = self.use_locking
|
410 |
|
|
411 |
|
all_networks = lu.cfg.GetAllNetworksInfo()
|
412 |
|
name_to_uuid = dict((n.name, n.uuid) for n in all_networks.values())
|
413 |
|
|
414 |
|
if self.names:
|
415 |
|
missing = []
|
416 |
|
self.wanted = []
|
417 |
|
|
418 |
|
for name in self.names:
|
419 |
|
if name in name_to_uuid:
|
420 |
|
self.wanted.append(name_to_uuid[name])
|
421 |
|
else:
|
422 |
|
missing.append(name)
|
423 |
|
|
424 |
|
if missing:
|
425 |
|
raise errors.OpPrereqError("Some networks do not exist: %s" % missing,
|
426 |
|
errors.ECODE_NOENT)
|
427 |
|
else:
|
428 |
|
self.wanted = locking.ALL_SET
|
429 |
|
|
430 |
|
if self.do_locking:
|
431 |
|
lu.needed_locks[locking.LEVEL_NETWORK] = self.wanted
|
432 |
|
if query.NETQ_INST in self.requested_data:
|
433 |
|
lu.needed_locks[locking.LEVEL_INSTANCE] = locking.ALL_SET
|
434 |
|
if query.NETQ_GROUP in self.requested_data:
|
435 |
|
lu.needed_locks[locking.LEVEL_NODEGROUP] = locking.ALL_SET
|
|
405 |
raise NotImplementedError
|
436 |
406 |
|
437 |
407 |
def DeclareLocks(self, lu, level):
|
438 |
|
pass
|
|
408 |
raise NotImplementedError
|
439 |
409 |
|
440 |
410 |
def _GetQueryData(self, lu):
|
441 |
|
"""Computes the list of networks and their attributes.
|
442 |
|
|
443 |
|
"""
|
444 |
|
all_networks = lu.cfg.GetAllNetworksInfo()
|
445 |
|
|
446 |
|
network_uuids = self._GetNames(lu, all_networks.keys(),
|
447 |
|
locking.LEVEL_NETWORK)
|
448 |
|
|
449 |
|
do_instances = query.NETQ_INST in self.requested_data
|
450 |
|
do_groups = query.NETQ_GROUP in self.requested_data
|
451 |
|
|
452 |
|
network_to_instances = None
|
453 |
|
network_to_groups = None
|
454 |
|
|
455 |
|
# For NETQ_GROUP, we need to map network->[groups]
|
456 |
|
if do_groups:
|
457 |
|
all_groups = lu.cfg.GetAllNodeGroupsInfo()
|
458 |
|
network_to_groups = dict((uuid, []) for uuid in network_uuids)
|
459 |
|
for _, group in all_groups.iteritems():
|
460 |
|
for net_uuid in network_uuids:
|
461 |
|
netparams = group.networks.get(net_uuid, None)
|
462 |
|
if netparams:
|
463 |
|
info = (group.name, netparams[constants.NIC_MODE],
|
464 |
|
netparams[constants.NIC_LINK])
|
465 |
|
|
466 |
|
network_to_groups[net_uuid].append(info)
|
467 |
|
|
468 |
|
if do_instances:
|
469 |
|
all_instances = lu.cfg.GetAllInstancesInfo()
|
470 |
|
network_to_instances = dict((uuid, []) for uuid in network_uuids)
|
471 |
|
for instance in all_instances.values():
|
472 |
|
for nic in instance.nics:
|
473 |
|
if nic.network in network_uuids:
|
474 |
|
network_to_instances[nic.network].append(instance.uuid)
|
475 |
|
break
|
476 |
|
|
477 |
|
if query.NETQ_STATS in self.requested_data:
|
478 |
|
stats = \
|
479 |
|
dict((uuid,
|
480 |
|
self._GetStats(network.AddressPool(all_networks[uuid])))
|
481 |
|
for uuid in network_uuids)
|
482 |
|
else:
|
483 |
|
stats = None
|
484 |
|
|
485 |
|
return query.NetworkQueryData([all_networks[uuid]
|
486 |
|
for uuid in network_uuids],
|
487 |
|
network_to_groups,
|
488 |
|
network_to_instances,
|
489 |
|
stats)
|
|
411 |
raise NotImplementedError
|
490 |
412 |
|
491 |
413 |
@staticmethod
|
492 |
414 |
def _GetStats(pool):
|
493 |
|
"""Returns statistics for a network address pool.
|
494 |
|
|
495 |
|
"""
|
496 |
|
return {
|
497 |
|
"free_count": pool.GetFreeCount(),
|
498 |
|
"reserved_count": pool.GetReservedCount(),
|
499 |
|
"map": pool.GetMap(),
|
500 |
|
"external_reservations":
|
501 |
|
utils.CommaJoin(pool.GetExternalReservations()),
|
502 |
|
}
|
|
415 |
raise NotImplementedError
|
503 |
416 |
|
504 |
417 |
|
505 |
418 |
class LUNetworkQuery(NoHooksLU):
|
... | ... | |
509 |
422 |
REQ_BGL = False
|
510 |
423 |
|
511 |
424 |
def CheckArguments(self):
|
512 |
|
self.nq = NetworkQuery(qlang.MakeSimpleFilter("name", self.op.names),
|
513 |
|
self.op.output_fields, self.op.use_locking)
|
|
425 |
raise NotImplementedError
|
514 |
426 |
|
515 |
427 |
def ExpandNames(self):
|
516 |
|
self.nq.ExpandNames(self)
|
|
428 |
raise NotImplementedError
|
517 |
429 |
|
518 |
430 |
def Exec(self, feedback_fn):
|
519 |
|
return self.nq.OldStyleQuery(self)
|
|
431 |
raise NotImplementedError
|
520 |
432 |
|
521 |
433 |
|
522 |
434 |
def _FmtNetworkConflict(details):
|