-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy_server.py
154 lines (133 loc) · 5.2 KB
/
deploy_server.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
#!/usr/bin/env python
# inspired Github-Auto-Deploy by logsol
# https://github.com/logsol/Github-Auto-Deploy/blob/master/GitAutoDeploy.py
import urlparse
import sys
import os
import json
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import settings
import logging
import logging.handlers
import urllib2
from netaddr import IPNetwork, IPAddress
from fabfile import deploy
from fabric.tasks import execute
logger = logging.getLogger(__name__)
class GitAutoDeploy(BaseHTTPRequestHandler):
"""
simple web server that receive post from github
"""
config = None
quiet = False
daemon = False
git_refs = None
git_repo_name = None
git_commit_url = None
request_ip = None
def getConfig(self):
for repository in self.config['repositories']:
if not os.path.isdir(repository['path']):
sys.exit('Directory ' + repository['path'] + ' not found')
if not os.path.isdir(repository['path'] + '/.git'):
sys.exit('Directory ' + repository['path'] + ' is not a Git repository')
return self.config
def do_GET(self):
"""
this shows test page.
quick server up test
"""
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write("<html><head><title>Hello World</title>")
self.wfile.write("<link rel='stylesheet' href='//cdn.jsdelivr.net/foundation/5.0.2/css/foundation.min.css'>")
self.wfile.write("</head>")
self.wfile.write("<body style='padding:10%;'><h1>Hello World</h1>")
self.wfile.write("</body></html>")
self.wfile.close()
def do_POST(self):
"""
receive post from github
"""
content_len = int(self.headers.getheader('content-length'))
post_body = self.rfile.read(content_len)
self.set_repo_info(post_body)
logger.info("received post receive hook payload from github")
self.request_ip = self.client_address[0]
logger.info("repo url: %(url)s, ref: %(ref)s" %
{"url": self.git_repo_url, "ref": self.git_refs})
if self.is_valid_request() and self.git_refs == "refs/heads/master":
branch = self.git_refs.split("/")[-1]
execute(deploy, repo_name=self.git_repo_name, branch=branch)
self.send_response(200)
self.send_header('Content-type', 'text/plain')
else:
self.send_response(404)
self.end_headers()
def set_repo_info(self, post_body):
"""
parse payload info
"""
post = urlparse.parse_qs(post_body)
for payload in post['payload']:
item = json.loads(payload)
self.git_repo_url = item['repository']['url']
self.git_repo_name = item['repository']['name']
self.git_refs = item['ref']
self.git_commit_url = item['commits'][0]['url']
def is_valid_request(self):
"""
Security: for now, we will check ip to validate request
later we should use HTTPS and basic authentication to verify
ie: hook address to be: https://yourUser:yourSecret@yoursite.net/path
"""
valid = False
# get web hook ip
response = urllib2.urlopen("https://api.github.com/meta")
data = json.load(response)
github_cdir = data['hooks']
logging.info("request ip: %s" % self.request_ip)
logging.info("github cdir: %s" % github_cdir)
if IPAddress(self.request_ip) in IPNetwork(github_cdir[0]) or self.request_ip == '127.0.0.1':
valid = True
return valid
def main():
try:
server = None
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 GitAutoDeploy.daemon:
pid = os.fork()
if pid != 0:
sys.exit()
os.setsid()
if not GitAutoDeploy.quiet:
msg = 'github auto deploy service v 0.1 started at port {!s}'.format(settings.PORT)
else:
msg = 'github auto deploy service v 0.1 started at port {!s} in daemon mode'.format(settings.PORT)
print msg
logger.info(msg)
server = HTTPServer(('', settings.PORT), GitAutoDeploy)
server.serve_forever()
except (KeyboardInterrupt, SystemExit) as e:
if e: # wtf, why is this creating a new line?
print >> sys.stderr, e
if not server is None:
server.socket.close()
if not GitAutoDeploy.quiet:
print 'Goodbye'
if __name__ == '__main__':
logging.basicConfig(format=settings.FORMAT,
level=logging.INFO,
filename=settings.LOG_FILE,
filemode='a')
handler = logging.handlers.RotatingFileHandler(settings.LOG_FILE,
mode='a',
maxBytes=1000000, # approx 1mb
backupCount=1)
main()