blob: 8ce07cecdf051f48e4137767d7b9aaaec1ef9af2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
import json, urlparse, sys, os
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from subprocess import call
class GitAutoDeploy(BaseHTTPRequestHandler):
CONFIG_FILEPATH = './GitAutoDeploy.conf.json'
config = None
deamonMode = False
@classmethod
def getConfig(myClass):
if(myClass.config == None):
try:
configString = open(myClass.CONFIG_FILEPATH).read()
except:
print 'Could not load ' + myClass.CONFIG_FILEPATH + ' file'
sys.exit()
try:
myClass.config = json.loads(configString)
except:
print myClass.CONFIG_FILEPATH + ' file is not valid json'
sys.exit()
for repository in myClass.config['repositories']:
if(not os.path.isdir(repository['path'])):
print 'Directory ' + repository['path'] + ' not found'
sys.exit()
if(not os.path.isdir(repository['path'] + '/.git')):
print 'Directory ' + repository['path'] + ' is not a Git repository'
sys.exit()
return myClass.config
def do_POST(self):
urls = self.parseRequest()
for url in urls:
path = self.getMatchingPath(url)
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 getMatchingPath(self, repoUrl):
config = self.getConfig()
for repository in config['repositories']:
if(repository['url'] == repoUrl):
return repository['path']
def respond(self):
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
def pull(self, path):
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:
print "Executing deploy command"
call(['cd "' + path + '";' + repository['deploy']], shell=True)
break
def main():
try:
print "Github Autodeploy Service v 0.1 started"
server = HTTPServer(('', GitAutoDeploy.getConfig()['port']), GitAutoDeploy)
server.serve_forever()
except KeyboardInterrupt:
print "Closing"
server.socket.close()
if __name__ == '__main__':
main()
|