Move 'ConfigureNIC' to 'hv_base'
authorJose A. Lopes <jabolopes@google.com>
Mon, 7 Apr 2014 15:08:22 +0000 (17:08 +0200)
committerJose A. Lopes <jabolopes@google.com>
Tue, 15 Apr 2014 15:34:37 +0000 (17:34 +0200)
... so it can be reused by the Xen hypervisor.

Signed-off-by: Jose A. Lopes <jabolopes@google.com>
Reviewed-by: Hrvoje Ribicic <riba@google.com>

lib/hypervisor/hv_base.py
lib/hypervisor/hv_kvm/__init__.py

index a945ef1..f7317fc 100644 (file)
@@ -42,9 +42,10 @@ import re
 import logging
 
 
+from ganeti import constants
 from ganeti import errors
+from ganeti import objects
 from ganeti import utils
-from ganeti import constants
 
 
 def _IsCpuMaskWellFormed(cpu_mask):
@@ -193,6 +194,58 @@ def GenerateTapName():
   return "gnt.com.%d" % idx
 
 
+def ConfigureNIC(cmd, instance, seq, nic, tap):
+  """Run the network configuration script for a specified NIC
+
+  @type cmd: string
+  @param cmd: command to run
+  @type instance: instance object
+  @param instance: instance we're acting on
+  @type seq: int
+  @param seq: nic sequence number
+  @type nic: nic object
+  @param nic: nic we're acting on
+  @type tap: str
+  @param tap: the host's tap interface this NIC corresponds to
+
+  """
+  env = {
+    "PATH": "%s:/sbin:/usr/sbin" % os.environ["PATH"],
+    "INSTANCE": instance.name,
+    "MAC": nic.mac,
+    "MODE": nic.nicparams[constants.NIC_MODE],
+    "INTERFACE": tap,
+    "INTERFACE_INDEX": str(seq),
+    "INTERFACE_UUID": nic.uuid,
+    "TAGS": " ".join(instance.GetTags()),
+  }
+
+  if nic.ip:
+    env["IP"] = nic.ip
+
+  if nic.name:
+    env["INTERFACE_NAME"] = nic.name
+
+  if nic.nicparams[constants.NIC_LINK]:
+    env["LINK"] = nic.nicparams[constants.NIC_LINK]
+
+  if nic.nicparams[constants.NIC_VLAN]:
+    env["VLAN"] = nic.nicparams[constants.NIC_VLAN]
+
+  if nic.network:
+    n = objects.Network.FromDict(nic.netinfo)
+    env.update(n.HooksDict())
+
+  if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
+    env["BRIDGE"] = nic.nicparams[constants.NIC_LINK]
+
+  result = utils.RunCmd(cmd, env=env)
+  if result.failed:
+    raise errors.HypervisorError("Failed to configure interface %s: %s;"
+                                 " network configuration script output: %s" %
+                                 (tap, result.fail_reason, result.output))
+
+
 class HvInstanceState(object):
   RUNNING = 0
   SHUTDOWN = 1
index 34646d5..ac99fc5 100644 (file)
@@ -664,6 +664,8 @@ class KVMHypervisor(hv_base.BaseHypervisor):
   def _ConfigureNIC(instance, seq, nic, tap):
     """Run the network configuration script for a specified NIC
 
+    See L{hv_base.ConfigureNIC}.
+
     @param instance: instance we're acting on
     @type instance: instance object
     @param seq: nic sequence number
@@ -674,41 +676,7 @@ class KVMHypervisor(hv_base.BaseHypervisor):
     @type tap: str
 
     """
-    env = {
-      "PATH": "%s:/sbin:/usr/sbin" % os.environ["PATH"],
-      "INSTANCE": instance.name,
-      "MAC": nic.mac,
-      "MODE": nic.nicparams[constants.NIC_MODE],
-      "INTERFACE": tap,
-      "INTERFACE_INDEX": str(seq),
-      "INTERFACE_UUID": nic.uuid,
-      "TAGS": " ".join(instance.GetTags()),
-    }
-
-    if nic.ip:
-      env["IP"] = nic.ip
-
-    if nic.name:
-      env["INTERFACE_NAME"] = nic.name
-
-    if nic.nicparams[constants.NIC_LINK]:
-      env["LINK"] = nic.nicparams[constants.NIC_LINK]
-
-    if nic.nicparams[constants.NIC_VLAN]:
-      env["VLAN"] = nic.nicparams[constants.NIC_VLAN]
-
-    if nic.network:
-      n = objects.Network.FromDict(nic.netinfo)
-      env.update(n.HooksDict())
-
-    if nic.nicparams[constants.NIC_MODE] == constants.NIC_MODE_BRIDGED:
-      env["BRIDGE"] = nic.nicparams[constants.NIC_LINK]
-
-    result = utils.RunCmd([pathutils.KVM_IFUP, tap], env=env)
-    if result.failed:
-      raise errors.HypervisorError("Failed to configure interface %s: %s;"
-                                   " network configuration script output: %s" %
-                                   (tap, result.fail_reason, result.output))
+    hv_base.ConfigureNIC([pathutils.KVM_IFUP, tap], instance, seq, nic, tap)
 
   @staticmethod
   def _VerifyAffinityPackage():