From eb0f0ce076ff4a47fa3cb3c61efa70a4d9181b9a Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Thu, 10 Jul 2008 12:38:06 +0000
Subject: [PATCH] Move watcher's LockFile function to utils

Reviewed-by: iustinp
---
 daemons/ganeti-watcher | 16 ++--------------
 lib/utils.py           | 12 ++++++++++++
 2 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/daemons/ganeti-watcher b/daemons/ganeti-watcher
index 4dfc0822c..b88e38748 100755
--- a/daemons/ganeti-watcher
+++ b/daemons/ganeti-watcher
@@ -90,18 +90,6 @@ def DoCmd(cmd):
   return res
 
 
-def LockFile(fd):
-  """Locks a file using POSIX locks.
-
-  """
-  try:
-    fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
-  except IOError, err:
-    if err.errno == errno.EAGAIN:
-      raise errors.LockError("File already locked")
-    raise
-
-
 class WatcherState(object):
   """Interface to a state file recording restart attempts.
 
@@ -118,7 +106,7 @@ class WatcherState(object):
     fd = os.open(constants.WATCHER_STATEFILE, os.O_RDWR | os.O_CREAT)
     self.statefile = os.fdopen(fd, 'w+')
 
-    LockFile(self.statefile.fileno())
+    utils.LockFile(self.statefile.fileno())
 
     try:
       self._data = serializer.Load(self.statefile.read())
@@ -151,7 +139,7 @@ class WatcherState(object):
     # starting ganeti-watcher again at the same time will create a conflict.
     fd = utils.WriteFile(constants.WATCHER_STATEFILE,
                          data=serialized_form,
-                         prewrite=LockFile, close=False)
+                         prewrite=utils.LockFile, close=False)
     self.statefile = os.fdopen(fd, 'w+')
 
   def Close(self):
diff --git a/lib/utils.py b/lib/utils.py
index 33bf83f3b..ceacc9b5e 100644
--- a/lib/utils.py
+++ b/lib/utils.py
@@ -1082,3 +1082,15 @@ def LockedMethod(fn):
       lock.release()
     return result
   return wrapper
+
+
+def LockFile(fd):
+  """Locks a file using POSIX locks.
+
+  """
+  try:
+    fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
+  except IOError, err:
+    if err.errno == errno.EAGAIN:
+      raise errors.LockError("File already locked")
+    raise
-- 
GitLab