From 24fc781f163237c835ef2a1600a3c93543944e0f Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Fri, 8 Aug 2008 11:23:17 +0000
Subject: [PATCH] Don't always remove queue lock when queue is purged

The lock should only be removed if ganeti-noded is going to quit.
Otherwise it needs to be kept to prevent another process from creating
it again while we're still holding the (removed) lock. This is due to
POSIX filesystem semantics.

Reviewed-by: iustinp
---
 lib/backend.py | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/lib/backend.py b/lib/backend.py
index ec3cb7ab6..68cfe5280 100644
--- a/lib/backend.py
+++ b/lib/backend.py
@@ -69,6 +69,19 @@ def _CleanDirectory(path, exclude=[]):
       utils.RemoveFile(full_name)
 
 
+def _JobQueuePurge(keep_lock):
+  """Removes job queue files and archived jobs
+
+  """
+  if keep_lock:
+    exclude = [constants.JOB_QUEUE_LOCK_FILE]
+  else:
+    exclude = []
+
+  _CleanDirectory(constants.QUEUE_DIR, exclude=exclude)
+  _CleanDirectory(constants.JOB_QUEUE_ARCHIVE_DIR)
+
+
 def _GetMasterInfo():
   """Return the master ip and netdev.
 
@@ -192,7 +205,8 @@ def LeaveCluster():
   """
   _CleanDirectory(constants.DATA_DIR)
 
-  JobQueuePurge()
+  # The lock can be removed because we're going to quit anyway.
+  _JobQueuePurge(keep_lock=False)
 
   try:
     priv_key, pub_key, auth_keys = ssh.GetUserFiles(constants.GANETI_RUNAS)
@@ -1698,8 +1712,9 @@ def JobQueuePurge():
   """Removes job queue files and archived jobs
 
   """
-  _CleanDirectory(constants.QUEUE_DIR)
-  _CleanDirectory(constants.JOB_QUEUE_ARCHIVE_DIR)
+  # The lock must not be removed, otherwise another process could create
+  # it again.
+  return _JobQueuePurge(keep_lock=True)
 
 
 def JobQueueRename(old, new):
-- 
GitLab