"""IP pool related commands"""
-# pylint: disable-msg=W0401,W0614
+# pylint: disable=W0401,W0614
# W0401: Wildcard import ganeti.cli
# W0614: Unused import %s from wildcard import (since we need cli)
network6=opts.network6,
mac_prefix=opts.mac_prefix,
network_type=opts.network_type,
- add_reserved_ips=_HandleReservedIPs(opts.add_reserved_ips),
+ add_reserved_ips=\
+ _HandleReservedIPs(opts.add_reserved_ips),
tags=tags)
SubmitOpCode(op, opts=opts)
not opts.no_headers)
-def ShowNetworkConfig(opts, args):
+def ShowNetworkConfig(_, args):
"""Show network information.
@param opts: the command line options selected by the user
for (name, network, gateway, network6, gateway6,
mac_prefix, network_type, free_count, reserved_count,
- map, group_list, instances, ext_res) in result:
+ mapping, group_list, instances, ext_res) in result:
size = free_count + reserved_count
ToStdout("Network name: %s", name)
ToStdout(" subnet: %s", network)
100 * float(free_count)/float(size))
ToStdout(" usage map:")
idx = 0
- for line in wrap(map, width=64):
+ for line in wrap(mapping, width=64):
ToStdout(" %s %s %d", str(idx).rjust(3), line.ljust(64), idx + 63)
idx += 64
ToStdout(" (X) used (.) free")
["nic.ips", "nic.networks"],
use_locking=False)
- l = lambda value: ", ".join(`idx`+":"+str(ip)
+ l = lambda value: ", ".join(str(idx)+":"+str(ip)
for idx, (ip, net) in enumerate(value)
if net == name)
- ToStdout(" %s : %s", inst, l(zip(ips,networks)))
+ ToStdout(" %s : %s", inst, l(zip(ips, networks)))
else:
ToStdout(" not used by any instances")
ToStderr("Please give at least one of the parameters.")
return 1
- op = opcodes.OpNetworkSetParams(network_name=args[0],
- # pylint: disable-msg=W0142
- **all_changes)
+ # pylint: disable=W0142
+ op = opcodes.OpNetworkSetParams(network_name=args[0], **all_changes)
# TODO: add feedback to user, e.g. list the modifications
SubmitOrSend(op, opts)
import shutil
import itertools
import operator
-import ipaddr
from ganeti import ssh
from ganeti import utils
"""Wrapper over L{_ExpandItemName} for instance."""
return _ExpandItemName(cfg.ExpandInstanceName, name, "Instance")
-def _BuildNetworkHookEnv(name, network, gateway, network6, gateway6,
+
+def _BuildNetworkHookEnv(name, subnet, gateway, network6, gateway6,
network_type, mac_prefix, tags):
+ """Builds network related env variables for hooks
+
+ This builds the hook environment from individual variables.
+
+ @type name: string
+ @param name: the name of the network
+ @type network: string
+ @param network: the ipv4 subnet
+ @type gateway: string
+ @param gateway: the ipv4 gateway
+ @type network6: string
+ @param network6: the ipv6 subnet
+ @type gateway6: string
+ @param gateway6: the ipv6 gateway
+ @type network_type: string
+ @param network_type: the type of the network
+ @type mac_prefix: string
+ @param mac_prefix: the mac_prefix
+ @type tags: list
+ @param tags: the tags of the network
+
+ """
env = dict()
if name:
env["NETWORK_NAME"] = name
- if network:
- env["NETWORK_SUBNET"] = network
+ if subnet:
+ env["NETWORK_SUBNET"] = subnet
if gateway:
env["NETWORK_GATEWAY"] = gateway
if network6:
return env
-def _BuildNetworkHookEnvByObject(lu, network):
+def _BuildNetworkHookEnvByObject(net):
+ """Builds network related env varliables for hooks
+
+ @type lu: L{objects.LogicalUnit}
+ @param lu: the corresponding LU
+ @type network: L{objects.Network}
+ @param network: the network object
+
+ """
args = {
- "name": network.name,
- "network": network.network,
- "gateway": network.gateway,
- "network6": network.network6,
- "gateway6": network.gateway6,
- "network_type": network.network_type,
- "mac_prefix": network.mac_prefix,
- "tags" : network.tags,
+ "name": net.name,
+ "subnet": net.network,
+ "gateway": net.gateway,
+ "network6": net.network6,
+ "gateway6": net.gateway6,
+ "network_type": net.network_type,
+ "mac_prefix": net.mac_prefix,
+ "tags": net.tags,
}
- return _BuildNetworkHookEnv(**args)
+
+ return _BuildNetworkHookEnv(**args) # pylint: disable=W0142
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
}
if nics:
nic_count = len(nics)
- for idx, (ip, mac, mode, link, network, netinfo) in enumerate(nics):
+ for idx, (ip, mac, mode, link, net, netinfo) in enumerate(nics):
if ip is None:
ip = ""
env["INSTANCE_NIC%d_IP" % idx] = ip
env["INSTANCE_NIC%d_MODE" % idx] = mode
env["INSTANCE_NIC%d_LINK" % idx] = link
if network:
- env["INSTANCE_NIC%d_NETWORK" % idx] = network
+ env["INSTANCE_NIC%d_NETWORK" % idx] = net
if netinfo:
nobj = objects.Network.FromDict(netinfo)
if nobj.network:
return env
+
def _NICToTuple(lu, nic):
"""Build a tupple of nic information.
@param nic: nic to convert to hooks tuple
"""
- cluster = lu.cfg.GetClusterInfo()
ip = nic.ip
mac = nic.mac
+ cluster = lu.cfg.GetClusterInfo()
filled_params = cluster.SimpleFillNIC(nic.nicparams)
mode = filled_params[constants.NIC_MODE]
link = filled_params[constants.NIC_LINK]
- network = nic.network
+ net = nic.network
netinfo = None
- if network:
- net_uuid = lu.cfg.LookupNetwork(network)
+ if net:
+ net_uuid = lu.cfg.LookupNetwork(net)
if net_uuid:
nobj = lu.cfg.GetNetwork(net_uuid)
netinfo = objects.Network.ToDict(nobj)
- return (ip, mac, mode, link, network, netinfo)
+ return (ip, mac, mode, link, net, netinfo)
+
def _NICListToTuple(lu, nics):
"""Build a list of nic information tuples.
"""
hooks_nics = []
- cluster = lu.cfg.GetClusterInfo()
for nic in nics:
hooks_nics.append(_NICToTuple(lu, nic))
return hooks_nics
+
def _BuildInstanceHookEnvByObject(lu, instance, override=None):
"""Builds instance related env variables for hooks from an object.
"""
nics = []
- for idx, nic in enumerate(op.nics):
+ for nic in op.nics:
nic_mode_req = nic.get(constants.INIC_MODE, None)
nic_mode = nic_mode_req
if nic_mode is None or nic_mode == constants.VALUE_AUTO:
(idx, netparams.values()))
nic.nicparams = dict(netparams)
if nic.ip is not None:
- filled_params = cluster.SimpleFillNIC(nic.nicparams)
if nic.ip.lower() == constants.NIC_IP_POOL:
try:
nic.ip = self.cfg.GenerateIp(net, self.proc.GetECId())
errors.ECODE_NOTUNIQUE)
elif new_ip.lower() == constants.NIC_IP_POOL:
raise errors.OpPrereqError("ip=pool, but no network found",
- ECODEE_INVAL)
+ errors.ECODE_INVAL)
else:
# new net is None
if self.op.conflicts_check:
nic.nicparams, cluster, pnode)
return None
- def _PrepareNicRemove(_, params, private):
+ def _PrepareNicRemove(_, params, __):
ip = params.ip
net = params.network
if net is not None and ip is not None:
"""
mac = params[constants.INIC_MAC]
ip = params.get(constants.INIC_IP, None)
- network = params.get(constants.INIC_NETWORK, None)
+ net = params.get(constants.INIC_NETWORK, None)
#TODO: not private.filled?? can a nic have no nicparams??
nicparams = private.filled
- return (objects.NIC(mac=mac, ip=ip, network=network, nicparams=nicparams), [
+ return (objects.NIC(mac=mac, ip=ip, network=net, nicparams=nicparams), [
("nic.%d" % idx,
"add:mac=%s,ip=%s,mode=%s,link=%s,network=%s" %
(mac, ip, private.filled[constants.NIC_MODE],
private.filled[constants.NIC_LINK],
- network)),
+ net)),
])
@staticmethod
"""
args = {
"name": self.op.network_name,
- "network": self.op.network,
+ "subnet": self.op.network,
"gateway": self.op.gateway,
"network6": self.op.network6,
"gateway6": self.op.gateway6,
"network_type": self.op.network_type,
"tags": self.op.tags,
}
- return _BuildNetworkHookEnv(**args)
+ return _BuildNetworkHookEnv(**args) # pylint: disable=W0142
def Exec(self, feedback_fn):
"""Add the ip pool to the cluster.
# Verify that the network is not conncted.
node_groups = [group.name
for group in self.cfg.GetAllNodeGroupsInfo().values()
- for network in group.networks.keys()
- if network == self.network_uuid]
+ for net in group.networks.keys()
+ if net == self.network_uuid]
if node_groups:
self.LogWarning("Nework '%s' is connected to the following"
"""
args = {
"name": self.op.network_name,
- "network": self.network.network,
+ "subnet": self.network.network,
"gateway": self.gateway,
"network6": self.network6,
"gateway6": self.gateway6,
"network_type": self.network_type,
"tags": self.tags,
}
- return _BuildNetworkHookEnv(**args)
+ return _BuildNetworkHookEnv(**args) # pylint: disable=W0142
def BuildHooksNodes(self):
"""Build hooks nodes.
do_instances = query.NETQ_INST in self.requested_data
do_groups = do_instances or (query.NETQ_GROUP in self.requested_data)
do_stats = query.NETQ_STATS in self.requested_data
- cluster = lu.cfg.GetClusterInfo()
network_to_groups = None
network_to_instances = None
if do_groups:
all_groups = lu.cfg.GetAllNodeGroupsInfo()
network_to_groups = dict((uuid, []) for uuid in self.wanted)
- default_nicpp = cluster.nicparams[constants.PP_DEFAULT]
if do_instances:
all_instances = lu.cfg.GetAllInstancesInfo()
ret["GROUP_NAME"] = self.group_name
ret["GROUP_NETWORK_MODE"] = self.network_mode
ret["GROUP_NETWORK_LINK"] = self.network_link
- ret.update(_BuildNetworkHookEnvByObject(self, self.network))
+ ret.update(_BuildNetworkHookEnvByObject(self.network))
return ret
def BuildHooksNodes(self):
instances = [(instance.name, idx, nic.ip)
for instance in groupinstances
for idx, nic in enumerate(instance.nics)
- if (not nic.network and pool._Contains(nic.ip))]
+ if (not nic.network and pool.Contains(nic.ip))]
if instances:
self.LogWarning("Following occurences use IPs from network %s"
" that is about to connect to nodegroup %s: %s" %
def BuildHooksEnv(self):
ret = dict()
ret["GROUP_NAME"] = self.group_name
- ret.update(_BuildNetworkHookEnvByObject(self, self.network))
+ ret.update(_BuildNetworkHookEnvByObject(self.network))
return ret
def BuildHooksNodes(self):
@param node: node name
"""
- (conf_net, conf_netparams) = lu.cfg.CheckIPInNodeGroup(ip, node)
+ (conf_net, _) = lu.cfg.CheckIPInNodeGroup(ip, node)
if conf_net is not None:
raise errors.OpPrereqError("Conflicting IP found:"
" %s <> %s." % (ip, conf_net),
return result
+def _GenerateMACSuffix():
+ """Generate one mac address
+
+ """
+ byte1 = random.randrange(0, 256)
+ byte2 = random.randrange(0, 256)
+ byte3 = random.randrange(0, 256)
+ suffix = "%02x:%02x:%02x" % (byte1, byte2, byte3)
+ return suffix
+
class ConfigWriter:
"""The interface to the cluster configuration.
return wraps(view_func)(_decorator)
return _get_mac_prefix
- def _GenerateMACSuffix(self):
- """Generate one mac address
-
- """
- byte1 = random.randrange(0, 256)
- byte2 = random.randrange(0, 256)
- byte3 = random.randrange(0, 256)
- suffix = "%02x:%02x:%02x" % (byte1, byte2, byte3)
- return suffix
-
@locking.ssynchronized(_config_lock, shared=1)
def GetNdParams(self, node):
"""Get the node params populated with cluster defaults.
"""
existing = self._AllMACs()
- gen_mac = self._GenerateMACPrefix(net)(self._GenerateMACSuffix)
+ gen_mac = self._GenerateMACPrefix(net)(_GenerateMACSuffix)
return self._temporary_ids.Generate(existing, gen_mac, ec_id)
@locking.ssynchronized(_config_lock, shared=1)
as reserved.
"""
- nobj = self._UnlockedGetNetwork(net_uuid)
- pool = network.AddressPool(nobj)
self._temporary_ips.Reserve(ec_id, ('release', address, net_uuid))
@locking.ssynchronized(_config_lock, shared=1)
- def ReleaseIp(self, network, address, ec_id):
+ def ReleaseIp(self, net, address, ec_id):
"""Give a specified IP address back to an IP pool.
This is just a wrapper around _UnlockedReleaseIp.
"""
- net_uuid = self._UnlockedLookupNetwork(network)
+ net_uuid = self._UnlockedLookupNetwork(net)
if net_uuid:
self._UnlockedReleaseIp(net_uuid, address, ec_id)
raise errors.ReservationError("Cannot generate IP. Network is full")
return ("reserve", ip, net_uuid)
- _ ,address, _ = self._temporary_ips.Generate([], gen_one, ec_id)
+ _, address, _ = self._temporary_ips.Generate([], gen_one, ec_id)
return address
def _UnlockedReserveIp(self, net_uuid, address, ec_id):
"""Get a list of network names
"""
- names = [network.name
- for network in self._config_data.networks.values()]
+ names = [net.name
+ for net in self._config_data.networks.values()]
return names
def _UnlockedGetNetwork(self, uuid):
for net_uuid in nodegroup_info.networks.keys():
net_info = self._UnlockedGetNetwork(net_uuid)
pool = network.AddressPool(net_info)
- if pool._Contains(ip):
+ if pool.Contains(ip):
return (net_info.name, nodegroup_info.networks[net_uuid])
return (None, None)
_NETWORK_STATS_FIELDS = {
"free_count": ("FreeCount", QFT_NUMBER, 0, "How many addresses are free"),
- "reserved_count": ("ReservedCount", QFT_NUMBER, 0, "How many addresses are reserved"),
+ "reserved_count": ("ReservedCount", QFT_NUMBER, 0,
+ "How many addresses are reserved"),
"map": ("Map", QFT_TEXT, 0, "The actual mapping"),
- "external_reservations": ("ExternalReservations", QFT_TEXT, 0, "The external reservations"),
+ "external_reservations": ("ExternalReservations", QFT_TEXT, 0,
+ "The external reservations"),
}
-
-def _GetNetworkStatsField(field, kind, ctx, net):
+def _GetNetworkStatsField(field, kind, ctx):
"""Gets the value of a "stats" field from L{NetworkQueryData}.
@param field: Field name
fields.extend([
(_MakeField(name, title, kind, doc),
NETQ_CONFIG, 0, _GetItemAttr(name))
- for (name, (title, kind, flags, doc)) in _NETWORK_SIMPLE_FIELDS.items()
+ for (name, (title, kind, _, doc)) in _NETWORK_SIMPLE_FIELDS.items()
])
def _GetLength(getter):
fields.extend([
(_MakeField(name, title, kind, doc), NETQ_STATS, 0,
compat.partial(_GetNetworkStatsField, name, kind))
- for (name, (title, kind, flags, doc)) in _NETWORK_STATS_FIELDS.items()
+ for (name, (title, kind, _, doc)) in _NETWORK_STATS_FIELDS.items()
])
return _PrepareFieldList(fields, [])