From 9ac99fdae57b360468731ad92471b37843fe0347 Mon Sep 17 00:00:00 2001
From: Guido Trotter <ultrotter@google.com>
Date: Wed, 23 Apr 2008 14:51:32 +0000
Subject: [PATCH] Add gnt-backup remove functionality

This patch also fixes the LUExportInstance Prereq docstring.

Reviewed-by: iustinp
---
 lib/cmdlib.py      | 41 ++++++++++++++++++++++++++++++++++++++++-
 lib/mcpu.py        |  1 +
 lib/opcodes.py     |  4 ++++
 scripts/gnt-backup | 23 +++++++++++++++++++++++
 4 files changed, 68 insertions(+), 1 deletion(-)

diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index 73907c284..cdac3d0ed 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -4436,7 +4436,7 @@ class LUExportInstance(LogicalUnit):
   def CheckPrereq(self):
     """Check prerequisites.
 
-    This checks that the instance name is a valid one.
+    This checks that the instance and node names are valid.
 
     """
     instance_name = self.cfg.ExpandInstanceName(self.op.instance_name)
@@ -4523,6 +4523,45 @@ class LUExportInstance(LogicalUnit):
                          " on node %s" % (instance.name, node))
 
 
+class LURemoveExport(NoHooksLU):
+  """Remove exports related to the named instance.
+
+  """
+  _OP_REQP = ["instance_name"]
+
+  def CheckPrereq(self):
+    """Check prerequisites.
+    """
+    pass
+
+  def Exec(self, feedback_fn):
+    """Remove any export.
+
+    """
+    instance_name = self.cfg.ExpandInstanceName(self.op.instance_name)
+    # If the instance was not found we'll try with the name that was passed in.
+    # This will only work if it was an FQDN, though.
+    fqdn_warn = False
+    if not instance_name:
+      fqdn_warn = True
+      instance_name = self.op.instance_name
+
+    op = opcodes.OpQueryExports(nodes=[])
+    exportlist = self.proc.ChainOpCode(op)
+    found = False
+    for node in exportlist:
+      if instance_name in exportlist[node]:
+        found = True
+        if not rpc.call_export_remove(node, instance_name):
+          logger.Error("could not remove export for instance %s"
+                       " on node %s" % (instance_name, node))
+
+    if fqdn_warn and not found:
+      feedback_fn("Export not found. If trying to remove an export belonging"
+                  " to a deleted instance please use its Fully Qualified"
+                  " Domain Name.")
+
+
 class TagsLU(NoHooksLU):
   """Generic tags LU.
 
diff --git a/lib/mcpu.py b/lib/mcpu.py
index 99cd737f0..8ae71962b 100644
--- a/lib/mcpu.py
+++ b/lib/mcpu.py
@@ -80,6 +80,7 @@ class Processor(object):
     # exports lu
     opcodes.OpQueryExports: cmdlib.LUQueryExports,
     opcodes.OpExportInstance: cmdlib.LUExportInstance,
+    opcodes.OpRemoveExport: cmdlib.LURemoveExport,
     # tags lu
     opcodes.OpGetTags: cmdlib.LUGetTags,
     opcodes.OpSearchTags: cmdlib.LUSearchTags,
diff --git a/lib/opcodes.py b/lib/opcodes.py
index c629e0c91..289d971b1 100644
--- a/lib/opcodes.py
+++ b/lib/opcodes.py
@@ -395,6 +395,10 @@ class OpExportInstance(OpCode):
   OP_ID = "OP_BACKUP_EXPORT"
   __slots__ = ["instance_name", "target_node", "shutdown"]
 
+class OpRemoveExport(OpCode):
+  """Remove an instance's export."""
+  OP_ID = "OP_BACKUP_REMOVE"
+  __slots__ = ["instance_name"]
 
 # Tags opcodes
 class OpGetTags(OpCode):
diff --git a/scripts/gnt-backup b/scripts/gnt-backup
index c250e250d..538b9eca6 100755
--- a/scripts/gnt-backup
+++ b/scripts/gnt-backup
@@ -105,6 +105,25 @@ def ImportInstance(opts, args):
   return 0
 
 
+def RemoveExport(opts, args):
+  """Remove an export from the cluster.
+
+  Args:
+   opts - class with options as members
+   args - list with a single element, the exported instance to remove
+  Opts used:
+
+  Returns:
+    1 in case of error, 0 otherwise
+
+  """
+  instance = args[0]
+  op = opcodes.OpRemoveExport(instance_name=args[0])
+
+  SubmitOpCode(op)
+  return 0
+
+
 # this is defined separately due to readability only
 import_opts = [
   DEBUG_OPT,
@@ -162,6 +181,10 @@ commands = {
              "Exports an instance to an image"),
   'import': (ImportInstance, ARGS_ONE, import_opts, "[opts...] <name>",
              "Imports an instance from an exported image"),
+  'remove': (RemoveExport, ARGS_ONE,
+             [DEBUG_OPT],
+             "<name>",
+             "Remove exports of named instance from the filesystem."),
   }
 
 if __name__ == '__main__':
-- 
GitLab