forked from olipo186/Git-Auto-Deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GitAutoDeploy.py
executable file
·278 lines (220 loc) · 7.27 KB
/
GitAutoDeploy.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python
import json, urlparse, sys, os, signal, socket
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from subprocess import call
from threading import Timer
class GitAutoDeploy(BaseHTTPRequestHandler):
CONFIG_FILEPATH = './GitAutoDeploy.conf.json'
config = None
debug = True
quiet = False
daemon = False
@classmethod
def getConfig(myClass):
if(myClass.config == None):
try:
configString = open(myClass.CONFIG_FILEPATH).read()
except:
print "Could not load %s file" % myClass.CONFIG_FILEPATH
sys.exit(2)
try:
myClass.config = json.loads(configString)
except:
print "%s file is not valid JSON" % myClass.CONFIG_FILEPATH
sys.exit(2)
for repository in myClass.config['repositories']:
if(not os.path.isdir(repository['path'])):
print "Directory %s not found" % repository['path']
sys.exit(2)
if not os.path.isdir(os.path.join(repository['path'], '.git')) and not os.path.isdir(os.path.join(repository['path'], 'objects')):
print "Directory %s is not a Git repository" % repository['path']
sys.exit(2)
return myClass.config
def do_POST(self):
urls = self.parseRequest()
self.respond()
Timer(1.0, self.do_process, [urls]).start()
def do_process(self, urls):
for url in urls:
paths = self.getMatchingPaths(url)
for path in paths:
self.pull(path)
self.deploy(path)
def parseRequest(self):
contenttype = self.headers.getheader('content-type')
length = int(self.headers.getheader('content-length'))
body = self.rfile.read(length)
items = []
try:
if contenttype == "application/json" or contenttype == "application/x-www-form-urlencoded":
post = urlparse.parse_qs(body)
# If payload is missing, we assume gitlab syntax.
if contenttype == "application/json" and "payload" not in post:
mode = "github"
# If x-www-form-urlencoded, we assume bitbucket syntax.
elif contenttype == "application/x-www-form-urlencoded":
mode = "bitbucket"
# Oh Gitlab, dear Gitlab...
else:
mode = "gitlab"
if mode == "github":
response = json.loads(body)
items.append(response['repository']['url'])
elif mode == "bitbucket":
for itemString in post['payload']:
item = json.loads(itemString)
items.append("ssh://hg@bitbucket.org" + item['repository']['absolute_url'][0:-1])
# Otherwise, we assume github/bitbucket syntax.
elif mode == "gitlab":
for itemString in post['payload']:
item = json.loads(itemString)
items.append(item['repository']['url'])
# WTF?!
else:
pass
except Exception:
pass
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 fetch origin ; git update-index --refresh &> /dev/null ; git reset --hard origin/master'], shell=True)
def deploy(self, path):
config = self.getConfig()
for repository in config['repositories']:
if(repository['path'] == path):
cmds = []
if 'deploy' in repository:
cmds.append(repository['deploy'])
gd = config['global_deploy']
if len(gd[0]) is not 0:
cmds.insert(0, gd[0])
if len(gd[1]) is not 0:
cmds.append(gd[1])
if(not self.quiet):
print 'Executing deploy command(s)'
for cmd in cmds:
call(['cd "' + path + '" && ' + cmd], shell=True)
break
class GitAutoDeployMain:
server = None
def run(self):
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(arg == '--force'):
print '[KILLER MODE] Warning: The --force option will try to kill any process ' \
'using %s port. USE AT YOUR OWN RISK' %GitAutoDeploy.getConfig()['port']
self.kill_them_all()
if(GitAutoDeploy.daemon):
pid = os.fork()
if(pid > 0):
sys.exit(0)
os.setsid()
self.create_pidfile()
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'
try:
self.server = HTTPServer(('', GitAutoDeploy.getConfig()['port']), GitAutoDeploy)
self.server.serve_forever()
except socket.error, e:
if(not GitAutoDeploy.quiet and not GitAutoDeploy.daemon):
print "Error on socket: %s" % e
self.debug_diagnosis()
sys.exit(1)
def kill_them_all(self):
pid = self.get_pid_on_port(GitAutoDeploy.getConfig()['port'])
if pid == False:
print '[KILLER MODE] I don\'t know the number of pid that is using my configured port\n ' \
'[KILLER MODE] Maybe no one? Please, use --force option carefully'
return False
os.kill(pid, signal.SIGKILL)
return True
def create_pidfile(self):
with open(GitAutoDeploy.getConfig()['pidfilepath'], 'w') as f:
f.write(str(os.getpid()))
def read_pidfile(self):
with open(GitAutoDeploy.getConfig()['pidfilepath'],'r') as f:
return f.readlines()
def remove_pidfile(self):
os.remove(GitAutoDeploy.getConfig()['pidfilepath'])
def debug_diagnosis(self):
if GitAutoDeploy.debug == False:
return
port = GitAutoDeploy.getConfig()['port']
pid = self.get_pid_on_port(port)
if pid == False:
print 'I don\'t know the number of pid that is using my configured port'
return
print 'Process with pid number %s is using port %s' % (pid, port)
with open("/proc/%s/cmdline" % pid) as f:
cmdline = f.readlines()
print 'cmdline ->', cmdline[0].replace('\x00', ' ')
def get_pid_on_port(self,port):
with open("/proc/net/tcp",'r') as f:
filecontent = f.readlines()[1:]
pids = [int(x) for x in os.listdir('/proc') if x.isdigit()]
conf_port = str(GitAutoDeploy.getConfig()['port'])
mpid = False
for line in filecontent:
if mpid != False:
break
_, laddr, _, _, _, _, _, _, _, inode = line.split()[:10]
decport = str(int(laddr.split(':')[1], 16))
if decport != conf_port:
continue
for pid in pids:
try:
path = "/proc/%s/fd" % pid
if os.access(path, os.R_OK) is False:
continue
for fd in os.listdir(path):
cinode = os.readlink("/proc/%s/fd/%s" % (pid, fd))
minode = cinode.split(":")
if len(minode) == 2 and minode[1][1:-1] == inode:
mpid = pid
except Exception as e:
pass
return mpid
def stop(self):
if(self.server is not None):
self.server.socket.close()
def exit(self):
if(not GitAutoDeploy.quiet):
print '\nGoodbye'
self.remove_pidfile()
sys.exit(0)
def signal_handler(self, signum, frame):
self.stop()
if(signum == 1):
self.run()
return
elif(signum == 2):
print '\nKeyboard Interrupt!!!'
elif(signum == 6):
print 'Requested close by SIGABRT (process abort signal). Code 6.'
self.exit()
if __name__ == '__main__':
gadm = GitAutoDeployMain()
signal.signal(signal.SIGHUP, gadm.signal_handler)
signal.signal(signal.SIGINT, gadm.signal_handler)
signal.signal(signal.SIGABRT, gadm.signal_handler)
signal.signal(signal.SIGPIPE, signal.SIG_IGN)
gadm.run()