diff options
author | Chris St. Pierre <chris.a.st.pierre@gmail.com> | 2013-02-07 10:00:37 -0500 |
---|---|---|
committer | Chris St. Pierre <chris.a.st.pierre@gmail.com> | 2013-02-14 15:12:20 -0500 |
commit | fd67a2735ada342251cb6baaa4e678532566e975 (patch) | |
tree | 1f3f37ccacb853cf500243cd02d7a87b27022e14 | |
parent | 9bec4d6bbab599bee72256c7e09fe214cb849a1b (diff) | |
download | bcfg2-fd67a2735ada342251cb6baaa4e678532566e975.tar.gz bcfg2-fd67a2735ada342251cb6baaa4e678532566e975.tar.bz2 bcfg2-fd67a2735ada342251cb6baaa4e678532566e975.zip |
moved common file locking code into Bcfg2.Utils
-rw-r--r-- | src/lib/Bcfg2/Client/Client.py | 10 | ||||
-rw-r--r-- | src/lib/Bcfg2/Server/Plugins/Metadata.py | 12 | ||||
-rw-r--r-- | src/lib/Bcfg2/Utils.py | 10 | ||||
-rw-r--r-- | testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestMetadata.py | 2 |
4 files changed, 17 insertions, 17 deletions
diff --git a/src/lib/Bcfg2/Client/Client.py b/src/lib/Bcfg2/Client/Client.py index e26a6d07a..6aa9ace1f 100644 --- a/src/lib/Bcfg2/Client/Client.py +++ b/src/lib/Bcfg2/Client/Client.py @@ -14,6 +14,7 @@ import Bcfg2.Client.XML import Bcfg2.Client.Proxy import Bcfg2.Client.Frame import Bcfg2.Client.Tools +from Bcfg2.Utils import locked from Bcfg2.Compat import xmlrpclib from Bcfg2.version import __version__ from subprocess import Popen, PIPE @@ -288,11 +289,7 @@ class Client(object): #check lock here try: lockfile = open(self.setup['lockfile'], 'w') - try: - fcntl.lockf(lockfile.fileno(), - fcntl.LOCK_EX | fcntl.LOCK_NB) - except IOError: - # otherwise exit and give a warning to the user + if locked(lockfile.fileno()): self.fatal_error("Another instance of Bcfg2 is running. " "If you want to bypass the check, run " "with the %s option" % @@ -301,7 +298,8 @@ class Client(object): raise except: lockfile = None - self.logger.error("Failed to open lockfile") + self.logger.error("Failed to open lockfile %s: %s" % + (self.setup['lockfile'], sys.exc_info()[1])) # execute the configuration self.tools.Execute() diff --git a/src/lib/Bcfg2/Server/Plugins/Metadata.py b/src/lib/Bcfg2/Server/Plugins/Metadata.py index b64f91ccf..d74db5eb4 100644 --- a/src/lib/Bcfg2/Server/Plugins/Metadata.py +++ b/src/lib/Bcfg2/Server/Plugins/Metadata.py @@ -15,6 +15,7 @@ import Bcfg2.Server import Bcfg2.Server.Lint import Bcfg2.Server.Plugin import Bcfg2.Server.FileMonitor +from Bcfg2.Utils import locked from Bcfg2.Compat import MutableMapping, all, wraps # pylint: disable=W0622 from Bcfg2.version import Bcfg2VersionInfo @@ -27,15 +28,6 @@ except ImportError: LOGGER = logging.getLogger(__name__) -def locked(fd): - """ Acquire a lock on a file """ - try: - fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) - except IOError: - return True - return False - - if HAS_DJANGO: class MetadataClientModel(models.Model, Bcfg2.Server.Plugin.PluginDatabaseModel): @@ -194,7 +186,7 @@ class XMLMetadataConfig(Bcfg2.Server.Plugin.XMLFileBacked): newcontents = lxml.etree.tostring(dataroot, xml_declaration=False, pretty_print=True).decode('UTF-8') - while locked(fd) == True: + while locked(fd): pass try: datafile.write(newcontents) diff --git a/src/lib/Bcfg2/Utils.py b/src/lib/Bcfg2/Utils.py index ba17e1a63..247e4f16b 100644 --- a/src/lib/Bcfg2/Utils.py +++ b/src/lib/Bcfg2/Utils.py @@ -2,6 +2,7 @@ used by both client and server. Stuff that doesn't fit anywhere else. """ +import fcntl from Bcfg2.Compat import any # pylint: disable=W0622 @@ -65,3 +66,12 @@ class PackedDigitRange(object): def __len__(self): return sum(r[1] - r[0] + 1 for r in self.ranges) + len(self.ints) + + +def locked(fd): + """ Acquire a lock on a file """ + try: + fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) + except IOError: + return True + return False diff --git a/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestMetadata.py b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestMetadata.py index 9f1e73541..2697df292 100644 --- a/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestMetadata.py +++ b/testsuite/Testsrc/Testlib/TestServer/TestPlugins/TestMetadata.py @@ -328,7 +328,7 @@ class TestXMLMetadataConfig(TestXMLFileBacked): "clients.xml"), "<test/>") - @patch('Bcfg2.Server.Plugins.Metadata.locked', Mock(return_value=False)) + @patch('Bcfg2.Utils.locked', Mock(return_value=False)) @patch('fcntl.lockf', Mock()) @patch('os.open') @patch('os.fdopen') |