Repository navigation
Expand file tree
/
Copy pathGitOSCAutoDeployWithLog.py
More file actions
215 lines (182 loc) · 7.18 KB
/
Copy pathGitOSCAutoDeployWithLog.py
File metadata and controls
215 lines (182 loc) · 7.18 KB
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
#!/usr/bin/env python
import json
import os
import sys
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from subprocess import call
import re
# import logging
class GitOSCAutoDeploy(BaseHTTPRequestHandler):
CONFIG_FILEPATH = './GitOSCAutoDeploy.conf.json'
config = None
quiet = False
daemon = False
branch = None
url = None
path = '/'
@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']):
try:
os.mkdir(repository['path'])
except:
sys.exit('Can NOT make a repository directory' + repository['path'])
# sys.exit('Directory ' + repository['path'] + ' not found')
# Check for a repository with a local or a remote GIT_WORK_DIR
if not os.path.isdir(os.path.join(repository['path'], '.git')) \
and not os.path.isdir(os.path.join(repository['path'], 'objects')):
try:
call(['git clone' + " " + repository['url'] + " " + repository['path']], shell=True)
except:
sys.exit('Can NOT clone repository directory into ' + os.path.isdir(repository['path']))
# sys.exit('Directory ' + repository['path'] + ' is not a Git repository')
return myClass.config
def do_GET(self):
""" Handle GET Request"""
if self.path == "/" or self.path == "/favicon.ico":
self.path = "/invoke.json"
# Prepend the file name with current dir path.
path = os.getcwd() + self.path
# Check if file exists there.
if os.path.isfile(path):
# Read the file and return its contents.
with open(path) as fileHandle:
# Send headers.
self.send_header('Content-type', 'text/json')
self.send_response(200)
self.end_headers()
# Send file content.
self.wfile.write(fileHandle.read().encode())
# Fail with 404 File Not Found error.
else:
# Send headers with error.
self.send_header('Content-type', 'text/json')
self.send_response(404, 'File Not Found')
self.end_headers()
# try:
# buf = 'It works'
# self.wfile.write(buf)
# except IOError:
# self.send_error(400, 'Bad Request')
def do_POST(self):
""" Handle POST Request"""
event = self.headers.getheader('X-Github-Event')
if event == 'ping':
if not self.quiet:
print 'Ping event received'
self.respond(204)
return
self.respond(204)
urls = self.parseRequest()
for url in urls:
paths = self.getMatchingPaths(url)
for path in paths:
self.fetch(path)
self.deploy(path)
def validateURL(self):
regex = re.compile(
r'^(?:http|ftp)s?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...
r'localhost|' # localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
try:
# TODO
# There must a bug on way
validatedurl = re.match(regex, self.url, 0)
except Exception:
sys.exit('Validate URL failed!')
return validatedurl
def parseRequest(self):
length = int(self.headers.getheader('content-length'))
if length <= 0:
sys.exit('Get content-length from headers failed!')
body = self.rfile.read(length)
try:
json.loads(body)
except Exception:
# do http decode
import urllib
body = urllib.unquote(body)
try:
json.loads(body)
except Exception:
# do remove 5 character
body = body[5:]
payload = json.loads(body)
self.branch = payload['push_data']['ref']
for url in [payload['push_data']['repository']['url']]:
self.url = url
self.validateURL()
return [payload['push_data']['repository']['url']]
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, code):
self.send_response(code)
self.send_header('Content-type', 'text/plain')
self.end_headers()
def fetch(self, path):
if not self.quiet:
print "\nPost push request received"
print 'Updating ' + path
call(['cd "' + path + '" && git fetch'], shell=True)
def deploy(self, path):
config = self.getConfig()
for repository in config['repositories']:
if repository['path'] == path:
if 'deploy' in repository:
branch = None
if 'branch' in repository:
branch = repository['branch']
if branch is None or branch == self.branch:
if not self.quiet:
print 'Executing deploy command'
call(['cd "' + path + '" && ' + repository['deploy']], shell=True)
elif not self.quiet:
print 'Push to different branch (%s != %s), not deploying' % (branch, self.branch)
break
def main():
server = None
try:
for arg in sys.argv:
if arg == '-d' or arg == '--daemon-mode':
GitOSCAutoDeploy.daemon = True
GitOSCAutoDeploy.quiet = True
if arg == '-q' or arg == '--quiet':
GitOSCAutoDeploy.quiet = True
if GitOSCAutoDeploy.daemon:
pid = os.fork()
if pid != 0:
sys.exit()
os.setsid()
if not GitOSCAutoDeploy.quiet:
print 'Github AutoDeploy Service v0.2 started'
else:
print 'Github AutoDeploy Service v 0.2 started in daemon mode'
server = HTTPServer(('', GitOSCAutoDeploy.getConfig()['port']), GitOSCAutoDeploy)
server.serve_forever()
except (KeyboardInterrupt, SystemExit) as e:
if e: # wtf, why is this creating a new line?
print >> sys.stderr, e
if server is not None:
server.socket.close()
if not GitOSCAutoDeploy.quiet:
print 'Goodbye'
if __name__ == '__main__':
main()