ifdown: Introduce _UnconfigureNIC() helper method
authorDimitris Aragiorgis <dimara@grnet.gr>
Tue, 4 Feb 2014 13:33:09 +0000 (15:33 +0200)
committerDimitris Aragiorgis <dimara@grnet.gr>
Thu, 29 May 2014 11:08:05 +0000 (14:08 +0300)
This method takes a NIC object, creates the proper invironment
and invokes ifdown script.

It takes an extra boolean argument only_local which is passed
to ifdown script as the second positional argument. It states
if the external script should wipe out every configuration
related to the NIC or should only clean up local thinks, e.g.
during a successful migration we are going to invoke ifdown
on the source node but we do not want to purge any DDNS
configuration related to instance's NICs.

Signed-off-by: Dimitris Aragiorgis <dimara@grnet.gr>

lib/hypervisor/hv_kvm.py

index 1433245..3dc5ba9 100644 (file)
@@ -1126,6 +1126,29 @@ class KVMHypervisor(hv_base.BaseHypervisor):
                                    " network configuration script output: %s" %
                                    (tap, result.fail_reason, result.output))
 
+  @classmethod
+  def _UnconfigureNic(cls, instance_name, nic, only_local=True):
+    """Run ifdown script for a specific NIC
+
+    This is executed during instance cleanup and NIC hot-unplug
+
+    @param instance_name: instance we're acting on
+    @type instance_name: string
+    @param nic: nic we're acting on
+    @type nic: nic object
+    @param only_local: whether ifdown script should reset global conf (dns)
+    @type only_local: boolean
+
+    """
+    tap = cls._GetInstanceNICTap(instance_name, nic)
+    env = cls._CreateNICEnv(instance_name, nic, tap)
+    arg2 = str(only_local).lower()
+    result = utils.RunCmd([pathutils.KVM_IFDOWN, tap, arg2], env=env)
+    if result.failed:
+      raise errors.HypervisorError("Failed to unconfigure interface %s: %s;"
+                                   " network configuration script output: %s" %
+                                   (tap, result.fail_reason, result.output))
+
   @staticmethod
   def _VerifyAffinityPackage():
     if affinity is None:
@@ -2393,6 +2416,15 @@ class KVMHypervisor(hv_base.BaseHypervisor):
       else:
         self._CallMonitorCommand(name, "system_powerdown", timeout)
 
+  @classmethod
+  def _UnconfigureInstanceNICs(cls, instance_name, info=None):
+    """Get runtime NICs of an instance and unconfigure them
+
+    """
+    _, kvm_nics, __, ___ = cls._LoadKVMRuntime(instance_name, info)
+    for nic in kvm_nics:
+      cls._UnconfigureNic(instance_name, nic)
+
   def CleanupInstance(self, instance_name):
     """Cleanup after a stopped instance