From 8c4771b16672f1e97c29545b2f75dca24b6bbe96 Mon Sep 17 00:00:00 2001 From: Dimitris Aragiorgis <dimara@grnet.gr> Date: Wed, 6 Jun 2012 15:57:37 +0300 Subject: [PATCH] Pass netinfo in rpcs If a nic has a network field then encapsulate a network object in netinfo slot for every rpc. This is needed to pass network info to scripts managing nics (kvm-vif-bridge). Introduce _BuildNetworkEnv() Signed-off-by: Dimitris Aragiorgis <dimara@grnet.gr> --- lib/client/gnt_instance.py | 2 +- lib/hypervisor/hv_kvm.py | 27 ++++++++++++++++++++++++++- lib/objects.py | 2 +- lib/rpc.py | 20 ++++++++++++++++++++ lib/rpc_defs.py | 3 ++- 5 files changed, 50 insertions(+), 4 deletions(-) diff --git a/lib/client/gnt_instance.py b/lib/client/gnt_instance.py index 5ceda1209..296b4413e 100644 --- a/lib/client/gnt_instance.py +++ b/lib/client/gnt_instance.py @@ -1258,7 +1258,7 @@ def ShowInstanceConfig(opts, args): FormatParameterDict(buf, instance["be_instance"], be_actual, level=2) # TODO(ganeti 2.7) rework the NICs as well buf.write(" - NICs:\n") - for idx, (ip, mac, mode, link, network) in enumerate(instance["nics"]): + for idx, (ip, mac, mode, link, network, _) in enumerate(instance["nics"]): buf.write(" - nic/%d: MAC: %s, IP: %s," " mode: %s, link: %s, network: %s\n" % (idx, mac, ip, mode, link, network)) diff --git a/lib/hypervisor/hv_kvm.py b/lib/hypervisor/hv_kvm.py index 2c04a5bdf..5fba7635b 100644 --- a/lib/hypervisor/hv_kvm.py +++ b/lib/hypervisor/hv_kvm.py @@ -773,8 +773,33 @@ class KVMHypervisor(hv_base.BaseHypervisor): if nic.nicparams[constants.NIC_LINK]: env["LINK"] = nic.nicparams[constants.NIC_LINK] + def _BuildNetworkEnv(name, network, gateway, network6, gateway6, + network_type, mac_prefix, tags, env): + if name: + env["NETWORK_NAME"] = name + if network: + env["NETWORK_SUBNET"] = network + if gateway: + env["NETWORK_GATEWAY"] = gateway + if network6: + env["NETWORK_SUBNET6"] = network6 + if gateway6: + env["NETWORK_GATEWAY6"] = gateway6 + if mac_prefix: + env["NETWORK_MAC_PREFIX"] = mac_prefix + if network_type: + env["NETWORK_TYPE"] = network_type + if tags: + env["NETWORK_TAGS"] = " ".join(tags) + + return env + + if nic.network: - env["NETWORK"] = nic.network + n = objects.Network.FromDict(nic.netinfo) + _BuildNetworkEnv(nic.network, n.network, n.gateway, + n.network6, n.gateway6, n.network_type, + n.mac_prefix, n.tags, env) if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED: env["BRIDGE"] = nic.nicparams[constants.NIC_LINK] diff --git a/lib/objects.py b/lib/objects.py index 2b02f9b33..f684b860e 100644 --- a/lib/objects.py +++ b/lib/objects.py @@ -510,7 +510,7 @@ class ConfigData(ConfigObject): class NIC(ConfigObject): """Config object representing a network card.""" - __slots__ = ["mac", "ip", "network", "nicparams"] + __slots__ = ["mac", "ip", "network", "nicparams", "netinfo"] @classmethod def CheckParameterSyntax(cls, nicparams): diff --git a/lib/rpc.py b/lib/rpc.py index 136532ff5..3164e5d5f 100644 --- a/lib/rpc.py +++ b/lib/rpc.py @@ -35,6 +35,7 @@ import zlib import base64 import pycurl import threading +import copy from ganeti import utils from ganeti import objects @@ -663,6 +664,7 @@ class RpcRunner(_RpcClientBase, rpc_defs.ED_INST_DICT: self._InstDict, rpc_defs.ED_INST_DICT_HVP_BEP: self._InstDictHvpBep, rpc_defs.ED_INST_DICT_OSP_DP: self._InstDictOspDp, + rpc_defs.ED_NIC_DICT: self._NicDict, # Encoders annotating disk parameters rpc_defs.ED_DISKS_DICT_DP: self._DisksDictDP, @@ -688,6 +690,18 @@ class RpcRunner(_RpcClientBase, _generated_rpc.RpcClientDnsOnly.__init__(self) _generated_rpc.RpcClientDefault.__init__(self) + def _NicDict(self, nic): + """Convert the given nic to a dict and encapsulate netinfo + + """ + n = copy.deepcopy(nic) + if n.network: + net_uuid = self._cfg.LookupNetwork(n.network) + if net_uuid: + nobj = self._cfg.GetNetwork(net_uuid) + n.netinfo = objects.Network.ToDict(nobj) + return n.ToDict() + def _InstDict(self, instance, hvp=None, bep=None, osp=None): """Convert the given instance to a dict. @@ -722,6 +736,12 @@ class RpcRunner(_RpcClientBase, nic["nicparams"] = objects.FillDict( cluster.nicparams[constants.PP_DEFAULT], nic["nicparams"]) + network = nic.get("network", None) + if network: + net_uuid = self._cfg.LookupNetwork(network) + if net_uuid: + nobj = self._cfg.GetNetwork(net_uuid) + nic["netinfo"] = objects.Network.ToDict(nobj) return idict def _InstDictHvpBep(self, (instance, hvp, bep)): diff --git a/lib/rpc_defs.py b/lib/rpc_defs.py index 2e8841b43..93644d627 100644 --- a/lib/rpc_defs.py +++ b/lib/rpc_defs.py @@ -73,7 +73,8 @@ ACCEPT_OFFLINE_NODE = object() ED_COMPRESS, ED_BLOCKDEV_RENAME, ED_DISKS_DICT_DP, - ED_SINGLE_DISK_DICT_DP) = range(1, 14) + ED_SINGLE_DISK_DICT_DP, + ED_NIC_DICT) = range(1, 15) def _Prepare(calls): -- GitLab