diff --git a/GitAutoDeploy.py b/GitAutoDeploy.py index d642e95..c6559e1 100755 --- a/GitAutoDeploy.py +++ b/GitAutoDeploy.py @@ -4,33 +4,50 @@ from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer from subprocess import call -class GitAutoDeploy(BaseHTTPRequestHandler): +class GitAutoDeploy(BaseHTTPRequestHandler): CONFIG_FILEPATH = './GitAutoDeploy.conf.json' config = None quiet = False daemon = False + isGetAvailable = False @classmethod - def getConfig(myClass): - if(myClass.config == None): + def getConfig(cls): + if cls.config is None: try: - configString = open(myClass.CONFIG_FILEPATH).read() - except: - sys.exit('Could not load ' + myClass.CONFIG_FILEPATH + ' file') + configString = open(cls.CONFIG_FILEPATH).read() + except IOError: + sys.exit('Could not load ' + cls.CONFIG_FILEPATH + ' file') try: - myClass.config = json.loads(configString) + cls.config = json.loads(configString) except: - sys.exit(myClass.CONFIG_FILEPATH + ' file is not valid json') + sys.exit(cls.CONFIG_FILEPATH + ' file is not valid json') - for repository in myClass.config['repositories']: - if(not os.path.isdir(repository['path'])): + for repository in cls.config['repositories']: + if not os.path.isdir(repository['path']): sys.exit('Directory ' + repository['path'] + ' not found') - if(not os.path.isdir(repository['path'] + '/.git')): + if not os.path.isdir(repository['path'] + '/.git'): sys.exit('Directory ' + repository['path'] + ' is not a Git repository') - return myClass.config + return cls.config + + def do_GET(self): + if GitAutoDeploy.isGetAvailable: + paths = [repository['path'] for repository in self.getConfig()['repositories']] + for path in paths: + self.pull(path) + self.deploy(path) + self.send_response(200) + self.send_header("Content-type", "text/html") + self.end_headers() + self.wfile.write("") + self.wfile.write("
Ok, updated.
") + self.wfile.write("") + else: + self.send_response(500) def do_POST(self): urls = self.parseRequest() @@ -54,7 +71,7 @@ def getMatchingPaths(self, repoUrl): res = [] config = self.getConfig() for repository in config['repositories']: - if(repository['url'] == repoUrl): + if repository['url'] == repoUrl: res.append(repository['path']) return res @@ -64,7 +81,7 @@ def respond(self): self.end_headers() def pull(self, path): - if(not self.quiet): + if not self.quiet: print "\nPost push request received" print 'Updating ' + path call(['cd "' + path + '" && git pull'], shell=True) @@ -72,45 +89,50 @@ def pull(self, path): def deploy(self, path): config = self.getConfig() for repository in config['repositories']: - if(repository['path'] == path): + if repository['path'] == path: if 'deploy' in repository: - if(not self.quiet): - print 'Executing deploy command' - call(['cd "' + path + '" && ' + repository['deploy']], shell=True) + if not self.quiet: + print 'Executing deploy command' + call(['cd "' + path + '" && ' + repository['deploy']], shell=True) break + def main(): + global server try: server = None - for arg in sys.argv: - if(arg == '-d' or arg == '--daemon-mode'): + for arg in sys.argv: + if arg == '-d' or arg == '--daemon-mode': GitAutoDeploy.daemon = True GitAutoDeploy.quiet = True - if(arg == '-q' or arg == '--quiet'): + if arg == '-q' or arg == '--quiet': GitAutoDeploy.quiet = True - - if(GitAutoDeploy.daemon): + if arg == '-g' or arg == '--get-to-pull': + GitAutoDeploy.isGetAvailable = True + + if GitAutoDeploy.daemon: pid = os.fork() - if(pid != 0): + if pid != 0: sys.exit() os.setsid() - if(not GitAutoDeploy.quiet): - print 'Github Autodeploy Service v 0.1 started' + if not GitAutoDeploy.quiet: + print 'Github Autodeploy Service v 0.2 started' else: - print 'Github Autodeploy Service v 0.1 started in daemon mode' - + print 'Github Autodeploy Service v 0.2 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? + if e: # wtf, why is this creating a new line? print >> sys.stderr, e - if(not server is None): + if not server is None: server.socket.close() - if(not GitAutoDeploy.quiet): + if not GitAutoDeploy.quiet: print 'Goodbye' + if __name__ == '__main__': - main() + main() diff --git a/README.textile b/README.textile index 10e549e..f07be16 100644 --- a/README.textile +++ b/README.textile @@ -11,6 +11,7 @@ To set it up, do the following: * enter the matching for your project(s) in the GitAutoDeploy.conf.json file * start the server by typing "python GitAutoDeploy.py" * to run it as a daemon add ==--daemon-mode== +* to trigger the pull and deploy via get add ==--get-to-pull== * On the Github page go to a repository, then "Admin", "Service Hooks", "Post-Receive URLs" and add the url of your machine + port (e.g. http://example.com:8001).