diff options
author | Oliver Poignant <oliver@poignant.se> | 2013-06-06 21:34:20 +0200 |
---|---|---|
committer | Oliver Poignant <oliver@poignant.se> | 2013-06-06 21:34:20 +0200 |
commit | b318ece8304e0a8efc520727204b0905b9be78b3 (patch) | |
tree | 5188f8e246597026f43a984a5375fec845f4df6c /GitAutoDeploy.py | |
parent | df823d19dfeb6ecd2a0affab8511d070801ffd2c (diff) | |
download | Git-Auto-Deploy-b318ece8304e0a8efc520727204b0905b9be78b3.zip Git-Auto-Deploy-b318ece8304e0a8efc520727204b0905b9be78b3.tar.gz Git-Auto-Deploy-b318ece8304e0a8efc520727204b0905b9be78b3.tar.bz2 |
GitLab support
Diffstat (limited to 'GitAutoDeploy.py')
-rwxr-xr-x | GitAutoDeploy.py | 218 |
1 files changed, 114 insertions, 104 deletions
diff --git a/GitAutoDeploy.py b/GitAutoDeploy.py index d642e95..1d43b2f 100755 --- a/GitAutoDeploy.py +++ b/GitAutoDeploy.py @@ -6,111 +6,121 @@ from subprocess import call class GitAutoDeploy(BaseHTTPRequestHandler): - CONFIG_FILEPATH = './GitAutoDeploy.conf.json' - config = None - quiet = False - daemon = False - - @classmethod - def getConfig(myClass): - if(myClass.config == None): - try: - configString = open(myClass.CONFIG_FILEPATH).read() - except: - sys.exit('Could not load ' + myClass.CONFIG_FILEPATH + ' file') - - try: - myClass.config = json.loads(configString) - except: - sys.exit(myClass.CONFIG_FILEPATH + ' file is not valid json') - - for repository in myClass.config['repositories']: - if(not os.path.isdir(repository['path'])): - sys.exit('Directory ' + repository['path'] + ' not found') - if(not os.path.isdir(repository['path'] + '/.git')): - sys.exit('Directory ' + repository['path'] + ' is not a Git repository') - - return myClass.config - - def do_POST(self): - urls = self.parseRequest() - for url in urls: - paths = self.getMatchingPaths(url) - for path in paths: - self.pull(path) - self.deploy(path) - - def parseRequest(self): - length = int(self.headers.getheader('content-length')) - body = self.rfile.read(length) - post = urlparse.parse_qs(body) - items = [] - for itemString in post['payload']: - item = json.loads(itemString) - items.append(item['repository']['url']) - return items - - def getMatchingPaths(self, repoUrl): - res = [] - config = self.getConfig() - for repository in config['repositories']: - if(repository['url'] == repoUrl): - res.append(repository['path']) - return res - - def respond(self): - self.send_response(200) - self.send_header('Content-type', 'text/plain') - self.end_headers() - - def pull(self, path): - if(not self.quiet): - print "\nPost push request received" - print 'Updating ' + path - call(['cd "' + path + '" && git pull'], shell=True) - - def deploy(self, path): - config = self.getConfig() - for repository in config['repositories']: - if(repository['path'] == path): - if 'deploy' in repository: - if(not self.quiet): - print 'Executing deploy command' - call(['cd "' + path + '" && ' + repository['deploy']], shell=True) - break + CONFIG_FILEPATH = './GitAutoDeploy.conf.json' + config = None + quiet = False + daemon = False + + @classmethod + def getConfig(myClass): + if(myClass.config == None): + try: + configString = open(myClass.CONFIG_FILEPATH).read() + except: + sys.exit('Could not load ' + myClass.CONFIG_FILEPATH + ' file') + + try: + myClass.config = json.loads(configString) + except: + sys.exit(myClass.CONFIG_FILEPATH + ' file is not valid json') + + for repository in myClass.config['repositories']: + if(not os.path.isdir(repository['path'])): + sys.exit('Directory ' + repository['path'] + ' not found') + if(not os.path.isdir(repository['path'] + '/.git')): + sys.exit('Directory ' + repository['path'] + ' is not a Git repository') + + return myClass.config + + def do_POST(self): + urls = self.parseRequest() + for url in urls: + paths = self.getMatchingPaths(url) + for path in paths: + self.pull(path) + self.deploy(path) + + def parseRequest(self): + length = int(self.headers.getheader('content-length')) + body = self.rfile.read(length) + post = urlparse.parse_qs(body) + items = [] + + # If payload is missing, we assume gitlab syntax. + if not 'payload' in post: + response = json.loads(body) + if 'repository' in body: + items.append(response['repository']['url']) + + # Otherwise, we assume github syntax. + else: + for itemString in post['payload']: + item = json.loads(itemString) + items.append(item['repository']['url']) + + return items + + def getMatchingPaths(self, repoUrl): + res = [] + config = self.getConfig() + for repository in config['repositories']: + if(repository['url'] == repoUrl): + res.append(repository['path']) + return res + + def respond(self): + self.send_response(200) + self.send_header('Content-type', 'text/plain') + self.end_headers() + + def pull(self, path): + if(not self.quiet): + print "\nPost push request received" + print 'Updating ' + path + call(['cd "' + path + '" && git pull'], shell=True) + + def deploy(self, path): + config = self.getConfig() + for repository in config['repositories']: + if(repository['path'] == path): + if 'deploy' in repository: + if(not self.quiet): + print 'Executing deploy command' + call(['cd "' + path + '" && ' + repository['deploy']], shell=True) + break def main(): - try: - server = None - for arg in sys.argv: - if(arg == '-d' or arg == '--daemon-mode'): - GitAutoDeploy.daemon = True - GitAutoDeploy.quiet = True - if(arg == '-q' or arg == '--quiet'): - GitAutoDeploy.quiet = True - - if(GitAutoDeploy.daemon): - pid = os.fork() - if(pid != 0): - sys.exit() - os.setsid() - - if(not GitAutoDeploy.quiet): - print 'Github Autodeploy Service v 0.1 started' - else: - print 'Github Autodeploy Service v 0.1 started in daemon mode' - - server = HTTPServer(('', GitAutoDeploy.getConfig()['port']), GitAutoDeploy) - server.serve_forever() - except (KeyboardInterrupt, SystemExit) as e: - if(e): # wtf, why is this creating a new line? - print >> sys.stderr, e - - if(not server is None): - server.socket.close() - - if(not GitAutoDeploy.quiet): - print 'Goodbye' + try: + server = None + for arg in sys.argv: + if(arg == '-d' or arg == '--daemon-mode'): + GitAutoDeploy.daemon = True + GitAutoDeploy.quiet = True + if(arg == '-q' or arg == '--quiet'): + GitAutoDeploy.quiet = True + + if(GitAutoDeploy.daemon): + pid = os.fork() + if(pid != 0): + sys.exit() + os.setsid() + + if(not GitAutoDeploy.quiet): + print 'Github & Gitlab Autodeploy Service v 0.1 started' + else: + print 'Github & Gitlab Autodeploy Service v 0.1 started in daemon mode' + + server = HTTPServer(('', GitAutoDeploy.getConfig()['port']), GitAutoDeploy) + server.serve_forever() + except (KeyboardInterrupt, SystemExit) as e: + if(e): # wtf, why is this creating a new line? + print >> sys.stderr, e + + if(not server is None): + server.socket.close() + + if(not GitAutoDeploy.quiet): + print 'Goodbye' if __name__ == '__main__': - main() + main() |