Pass netinfo in rpcs
authorDimitris Aragiorgis <dimara@grnet.gr>
Wed, 6 Jun 2012 12:57:37 +0000 (15:57 +0300)
committerDimitris Aragiorgis <dimara@grnet.gr>
Wed, 22 Aug 2012 14:11:18 +0000 (17:11 +0300)
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
lib/hypervisor/hv_kvm.py
lib/objects.py
lib/rpc.py
lib/rpc_defs.py

index 5ceda12..296b441 100644 (file)
@@ -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))
index 2c04a5b..5fba763 100644 (file)
@@ -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]
index 2b02f9b..f684b86 100644 (file)
@@ -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):
index 136532f..3164e5d 100644 (file)
@@ -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)):
index 2e8841b..93644d6 100644 (file)
@@ -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):