Commit eb0f0ce0 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

Move watcher's LockFile function to utils

Reviewed-by: iustinp
parent 307149a8
...@@ -90,18 +90,6 @@ def DoCmd(cmd): ...@@ -90,18 +90,6 @@ def DoCmd(cmd):
return res 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): class WatcherState(object):
"""Interface to a state file recording restart attempts. """Interface to a state file recording restart attempts.
...@@ -118,7 +106,7 @@ class WatcherState(object): ...@@ -118,7 +106,7 @@ class WatcherState(object):
fd = os.open(constants.WATCHER_STATEFILE, os.O_RDWR | os.O_CREAT) fd = os.open(constants.WATCHER_STATEFILE, os.O_RDWR | os.O_CREAT)
self.statefile = os.fdopen(fd, 'w+') self.statefile = os.fdopen(fd, 'w+')
LockFile(self.statefile.fileno()) utils.LockFile(self.statefile.fileno())
try: try:
self._data = serializer.Load(self.statefile.read()) self._data = serializer.Load(self.statefile.read())
...@@ -151,7 +139,7 @@ class WatcherState(object): ...@@ -151,7 +139,7 @@ class WatcherState(object):
# starting ganeti-watcher again at the same time will create a conflict. # starting ganeti-watcher again at the same time will create a conflict.
fd = utils.WriteFile(constants.WATCHER_STATEFILE, fd = utils.WriteFile(constants.WATCHER_STATEFILE,
data=serialized_form, data=serialized_form,
prewrite=LockFile, close=False) prewrite=utils.LockFile, close=False)
self.statefile = os.fdopen(fd, 'w+') self.statefile = os.fdopen(fd, 'w+')
def Close(self): def Close(self):
......
...@@ -1082,3 +1082,15 @@ def LockedMethod(fn): ...@@ -1082,3 +1082,15 @@ def LockedMethod(fn):
lock.release() lock.release()
return result return result
return wrapper 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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment