diff --git a/daemons/ganeti-watcher b/daemons/ganeti-watcher index 4dfc0822c90391ea46b42cfa537309a9a858dadf..b88e387489b68d4ae5a5381ddc3fe781fd613c02 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 33bf83f3be51f9c19913db3f43693babdf42f86b..ceacc9b5ea51f76738835b4f03e183c457427573 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