cb8e9e
From 4992f2f8f3deb837e18d111013367be5d5fff0a6 Mon Sep 17 00:00:00 2001
cb8e9e
From: Aravinda VK <avishwan@redhat.com>
cb8e9e
Date: Thu, 11 Jun 2015 18:37:16 +0530
cb8e9e
Subject: [PATCH 63/73] tools/glusterfind: Cleanup glusterfind dir after a volume delete
cb8e9e
cb8e9e
If `glusterfind delete` command was not run before volume delete, stale
cb8e9e
session directories exists in /var/lib/glusterd/glusterfind directories.
cb8e9e
cb8e9e
Also shows these sessions in `glusterfind list`
cb8e9e
cb8e9e
When Volume is deleted, Post hook will be run which cleans up the stale
cb8e9e
session directories
cb8e9e
cb8e9e
BUG: 1224880
cb8e9e
Change-Id: I54c46c30313e92c1bb4cb07918ed2029b375462c
cb8e9e
Signed-off-by: Aravinda VK <avishwan@redhat.com>
cb8e9e
Reviewed-On: http://review.gluster.org/10944
cb8e9e
Reviewed-On: http://review.gluster.org/11186
cb8e9e
Reviewed-on: https://code.engineering.redhat.com/gerrit/50541
cb8e9e
Reviewed-by: Saravanakumar Arumugam <sarumuga@redhat.com>
cb8e9e
---
cb8e9e
 extras/hook-scripts/Makefile.am                   |    7 ++-
cb8e9e
 extras/hook-scripts/S57glusterfind-delete-post.py |   60 +++++++++++++++++++++
cb8e9e
 glusterfs.spec.in                                 |    6 ++
cb8e9e
 3 files changed, 72 insertions(+), 1 deletions(-)
cb8e9e
 create mode 100755 extras/hook-scripts/S57glusterfind-delete-post.py
cb8e9e
cb8e9e
diff --git a/extras/hook-scripts/Makefile.am b/extras/hook-scripts/Makefile.am
cb8e9e
index 771b37e..b5e2ae6 100644
cb8e9e
--- a/extras/hook-scripts/Makefile.am
cb8e9e
+++ b/extras/hook-scripts/Makefile.am
cb8e9e
@@ -1,7 +1,12 @@
cb8e9e
-EXTRA_DIST = S40ufo-stop.py S56glusterd-geo-rep-create-post.sh
cb8e9e
+EXTRA_DIST = S40ufo-stop.py S56glusterd-geo-rep-create-post.sh \
cb8e9e
+	S57glusterfind-delete-post.py
cb8e9e
+
cb8e9e
 SUBDIRS = add-brick set start stop reset
cb8e9e
 
cb8e9e
 scriptsdir = $(GLUSTERD_WORKDIR)/hooks/1/gsync-create/post/
cb8e9e
 if USE_GEOREP
cb8e9e
 scripts_SCRIPTS = S56glusterd-geo-rep-create-post.sh
cb8e9e
 endif
cb8e9e
+
cb8e9e
+deletehookscriptsdir = $(GLUSTERD_WORKDIR)/hooks/1/delete/post/
cb8e9e
+deletehookscripts_SCRIPTS = S57glusterfind-delete-post.py
cb8e9e
diff --git a/extras/hook-scripts/S57glusterfind-delete-post.py b/extras/hook-scripts/S57glusterfind-delete-post.py
cb8e9e
new file mode 100755
cb8e9e
index 0000000..70edb56
cb8e9e
--- /dev/null
cb8e9e
+++ b/extras/hook-scripts/S57glusterfind-delete-post.py
cb8e9e
@@ -0,0 +1,60 @@
cb8e9e
+#!/usr/bin/python
cb8e9e
+import os
cb8e9e
+import shutil
cb8e9e
+from errno import ENOENT
cb8e9e
+from subprocess import Popen, PIPE
cb8e9e
+from argparse import ArgumentParser
cb8e9e
+
cb8e9e
+
cb8e9e
+DEFAULT_GLUSTERD_WORKDIR = "/var/lib/glusterd"
cb8e9e
+
cb8e9e
+
cb8e9e
+def handle_rm_error(func, path, exc_info):
cb8e9e
+    if exc_info[1].errno == ENOENT:
cb8e9e
+        return
cb8e9e
+
cb8e9e
+    raise exc_info[1]
cb8e9e
+
cb8e9e
+
cb8e9e
+def get_glusterd_workdir():
cb8e9e
+    p = Popen(["gluster", "system::", "getwd"],
cb8e9e
+              stdout=PIPE, stderr=PIPE)
cb8e9e
+
cb8e9e
+    out, _ = p.communicate()
cb8e9e
+
cb8e9e
+    if p.returncode == 0:
cb8e9e
+        return out.strip()
cb8e9e
+    else:
cb8e9e
+        return DEFAULT_GLUSTERD_WORKDIR
cb8e9e
+
cb8e9e
+
cb8e9e
+def get_args():
cb8e9e
+    parser = ArgumentParser(description="Volume delete post hook script")
cb8e9e
+    parser.add_argument("--volname")
cb8e9e
+    return parser.parse_args()
cb8e9e
+
cb8e9e
+
cb8e9e
+def main():
cb8e9e
+    args = get_args()
cb8e9e
+    glusterfind_dir = os.path.join(get_glusterd_workdir(), "glusterfind")
cb8e9e
+
cb8e9e
+    # Check all session directories, if any directory found for
cb8e9e
+    # the deleted volume, cleanup all the session directories
cb8e9e
+    for session in os.listdir(glusterfind_dir):
cb8e9e
+        # Possible session directory
cb8e9e
+        volume_session_path = os.path.join(glusterfind_dir,
cb8e9e
+                                           session,
cb8e9e
+                                           args.volname)
cb8e9e
+        if os.path.exists(volume_session_path):
cb8e9e
+            shutil.rmtree(volume_session_path, onerror=handle_rm_error)
cb8e9e
+
cb8e9e
+        # Try to Remove directory, if any other dir exists for different
cb8e9e
+        # volume, then rmdir will fail with ENOTEMPTY which is fine
cb8e9e
+        try:
cb8e9e
+            os.rmdir(os.path.join(glusterfind_dir, session))
cb8e9e
+        except (OSError, IOError):
cb8e9e
+            pass
cb8e9e
+
cb8e9e
+
cb8e9e
+if __name__ == "__main__":
cb8e9e
+    main()
cb8e9e
diff --git a/glusterfs.spec.in b/glusterfs.spec.in
cb8e9e
index 4b02f85..aadfe70 100644
cb8e9e
--- a/glusterfs.spec.in
cb8e9e
+++ b/glusterfs.spec.in
cb8e9e
@@ -991,6 +991,9 @@ fi
cb8e9e
 %exclude %{_libexecdir}/glusterfs/glusterfind
cb8e9e
 %exclude %{_bindir}/glusterfind
cb8e9e
 %exclude %{_libexecdir}/glusterfs/peer_add_secret_pub
cb8e9e
+%{_sharedstatedir}/glusterd/hooks/1/delete/post/S57glusterfind-delete-post.py
cb8e9e
+%exclude %{_sharedstatedir}/glusterd/hooks/1/delete/post/S57glusterfind-delete-post.pyc
cb8e9e
+%exclude %{_sharedstatedir}/glusterd/hooks/1/delete/post/S57glusterfind-delete-post.pyo
cb8e9e
 # exclude server files
cb8e9e
 %exclude %{_sharedstatedir}/glusterd/*
cb8e9e
 %exclude %{_sysconfdir}/glusterfs
cb8e9e
@@ -1789,6 +1792,9 @@ end
cb8e9e
 %endif
cb8e9e
 
cb8e9e
 %changelog
cb8e9e
+* Thu Jun 11 2015 Aravinda VK <avishwan@redhat.com>
cb8e9e
+- Added post hook for volume delete as part of glusterfind (#1225551)
cb8e9e
+
cb8e9e
 * Thu Jun 11 2015 Atin Mukherjee <amukherj@redhat.com>
cb8e9e
 - Security hardening flags inclusion (#1200815)
cb8e9e
 
cb8e9e
-- 
cb8e9e
1.7.1
cb8e9e