summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOliver Poignant <oliver@poignant.se>2015-05-17 21:08:29 +0200
committerOliver Poignant <oliver@poignant.se>2015-05-17 21:08:29 +0200
commitc88ae14185b8d829eeb6d099f49867d37f7723d5 (patch)
tree1f32e046ec3af1f27ac253cd814bd5196362ada0
parent72f40fc96e50452b5fa1201c2e5dbf03f84e891e (diff)
parent9393a4d0d7b3edc24eea30882f49172bf1d37b88 (diff)
downloadGit-Auto-Deploy-c88ae14185b8d829eeb6d099f49867d37f7723d5.zip
Git-Auto-Deploy-c88ae14185b8d829eeb6d099f49867d37f7723d5.tar.gz
Git-Auto-Deploy-c88ae14185b8d829eeb6d099f49867d37f7723d5.tar.bz2
Merge pull request #25 from quizzz-and-chiv/lock
Wait while previous deploy request is finished or just exit if some other request is waiting
-rwxr-xr-xGitAutoDeploy.py28
-rw-r--r--clear_lock.sh2
-rw-r--r--lock.sh27
-rw-r--r--unlock.sh2
4 files changed, 56 insertions, 3 deletions
diff --git a/GitAutoDeploy.py b/GitAutoDeploy.py
index 9f8b26e..6a6afc8 100755
--- a/GitAutoDeploy.py
+++ b/GitAutoDeploy.py
@@ -40,6 +40,7 @@ class GitAutoDeploy(BaseHTTPRequestHandler):
if(not os.path.isdir(repository['path'] + '/.git')):
print "Directory %s is not a Git repository" % repository['path']
sys.exit(2)
+ myClass.clearLock(repository['path'])
return myClass.config
@@ -52,8 +53,17 @@ class GitAutoDeploy(BaseHTTPRequestHandler):
for url in urls:
repos = self.getMatchingPaths(url)
for repo in repos:
- self.pull(repo['path'], repo['branch'])
- self.deploy(repo['path'])
+ if self.lock(repo['path']):
+ try:
+ n = 4
+ while 0 < n and 0 != self.pull(repo['path'], repo['branch']):
+ --n
+ if 0 < n:
+ self.deploy(repo['path'])
+ except:
+ call(['echo "Error during \'pull\' or \'deploy\' operation on path: ' + repo['path'] + '"'], shell=True)
+ finally:
+ self.unlock(repo['path'])
def parseRequest(self):
contenttype = self.headers.getheader('content-type')
@@ -116,11 +126,23 @@ class GitAutoDeploy(BaseHTTPRequestHandler):
self.send_header('Content-type', 'text/plain')
self.end_headers()
+ def lock(self, path):
+ return 0 == call(['sh lock.sh "' + path + '"'], shell=True)
+
+ def unlock(self, path):
+ call(['sh unlock.sh "' + path + '"'], shell=True)
+
+ @classmethod
+ def clearLock(myClass, path):
+ call(['sh clear_lock.sh "' + path + '"'], shell=True)
+
def pull(self, path, branch):
if(not self.quiet):
print "\nPost push request received"
print 'Updating ' + path
- call(['cd "' + path + '" && git fetch origin ; git update-index --refresh &> /dev/null ; git reset --hard origin/' + branch], shell=True)
+ res = call(['sleep 5; cd "' + path + '" && git fetch origin ; git update-index --refresh &> /dev/null ; git reset --hard origin/' + branch], shell=True)
+ call(['echo "Pull result: ' + str(res) + '"'], shell=True)
+ return res
def deploy(self, path):
config = self.getConfig()
diff --git a/clear_lock.sh b/clear_lock.sh
new file mode 100644
index 0000000..601a51e
--- /dev/null
+++ b/clear_lock.sh
@@ -0,0 +1,2 @@
+#!/bin/sh
+cd $1 && rm status_waiting status_running; cd -
diff --git a/lock.sh b/lock.sh
new file mode 100644
index 0000000..a67e019
--- /dev/null
+++ b/lock.sh
@@ -0,0 +1,27 @@
+#!/bin/sh
+cd $1
+
+set -o noclobber
+{ > status_waiting; }
+if [ "$?" != "0" ]
+then
+ echo "Some other thread is alreay waiting. Exit."
+
+ set +o noclobber
+ cd -
+ exit 1
+else
+
+ { > status_running; }
+ while [ "$?" != "0" ]
+ do
+ echo "Some other thread is already building. Waiting 5 sec!"
+ sleep 5
+ { > status_running; }
+ done
+ rm status_waiting
+
+ set +o noclobber
+ cd -
+ exit 0
+fi
diff --git a/unlock.sh b/unlock.sh
new file mode 100644
index 0000000..0ff55c4
--- /dev/null
+++ b/unlock.sh
@@ -0,0 +1,2 @@
+#!/bin/sh
+cd $1 && rm status_running; cd -