Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added pull on GET Request #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 54 additions & 32 deletions GitAutoDeploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("<html>")
self.wfile.write("<head><title>Github Autodeploy</title></head>")
self.wfile.write("<body><p>Ok, updated.</p></body>")
self.wfile.write("</html>")
else:
self.send_response(500)

def do_POST(self):
urls = self.parseRequest()
Expand All @@ -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

Expand All @@ -64,53 +81,58 @@ 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)

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()
1 change: 1 addition & 0 deletions README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down