summaryrefslogtreecommitdiffstats
path: root/jenkins_jobs/builder.py
diff options
context:
space:
mode:
authorYolanda Robla <yolanda.robla-mota@hp.com>2016-03-14 15:19:18 +0100
committerYolanda Robla <yolanda.robla-mota@hp.com>2016-03-14 17:28:39 +0100
commitd1df3359b3c9fd968e8e35c51dc36b0b90ffa216 (patch)
tree575dcb10b3570a79512735d254666a23febbd684 /jenkins_jobs/builder.py
parentccf682934d14f4a2395c7b9f6d463b9667b938c8 (diff)
downloadpython-jenkins-job-builder-d1df3359b3c9fd968e8e35c51dc36b0b90ffa216.tar.gz
python-jenkins-job-builder-d1df3359b3c9fd968e8e35c51dc36b0b90ffa216.tar.xz
python-jenkins-job-builder-d1df3359b3c9fd968e8e35c51dc36b0b90ffa216.zip
Allow using lockfile per jenkins master
When a jjb run is thrown when another jjb is already running, it can cause corruption of cache. Start using a lockfile to ensure this won't be happening and run securely on automated systems. Change-Id: I3ac37e738b3bb87c04a47afb8adb3e25f8fb4ea8
Diffstat (limited to 'jenkins_jobs/builder.py')
-rw-r--r--jenkins_jobs/builder.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/jenkins_jobs/builder.py b/jenkins_jobs/builder.py
index 6e257bba..35a32185 100644
--- a/jenkins_jobs/builder.py
+++ b/jenkins_jobs/builder.py
@@ -16,6 +16,7 @@
# Manage jobs in Jenkins server
import errno
+import fcntl
import hashlib
import io
import logging
@@ -47,6 +48,7 @@ class CacheStorage(object):
# removed global module references during teardown.
_yaml = yaml
_logger = logger
+ _fcntl = fcntl
def __init__(self, jenkins_url, flush=False):
cache_dir = self.get_cache_dir()
@@ -54,6 +56,11 @@ class CacheStorage(object):
host_vary = re.sub('[^A-Za-z0-9\-\~]', '_', jenkins_url)
self.cachefilename = os.path.join(
cache_dir, 'cache-host-jobs-' + host_vary + '.yml')
+
+ # generate named lockfile if not exists, and lock it
+ while not self._lock(cache_dir, host_vary):
+ time.sleep(1)
+
if flush or not os.path.isfile(self.cachefilename):
self.data = {}
else:
@@ -61,6 +68,25 @@ class CacheStorage(object):
self.data = yaml.load(yfile)
logger.debug("Using cache: '{0}'".format(self.cachefilename))
+ def _lock(self, cache_dir, jenkins_master):
+ path = os.path.join(cache_dir, "lock-jjb.%s" % jenkins_master)
+ self.lockfile = open(path, 'w')
+
+ try:
+ self._fcntl.lockf(self.lockfile,
+ self._fcntl.LOCK_EX | self._fcntl.LOCK_NB)
+ except IOError:
+ return False
+ return True
+
+ def _unlock(self):
+ if getattr(self, 'lockfile', None) is not None:
+ try:
+ self._fcntl.lockf(self.lockfile, self._fcntl.LOCK_UN)
+ self.lockfile.close()
+ except IOError:
+ pass
+
@staticmethod
def get_cache_dir():
home = os.path.expanduser('~')
@@ -111,6 +137,7 @@ class CacheStorage(object):
self._logger.info("Cache saved")
self._logger.debug("Cache written out to '%s'" %
self.cachefilename)
+ self._unlock()
def __del__(self):
self.save()