This repository has been archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.py
executable file
·85 lines (70 loc) · 2.58 KB
/
index.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
#!/usr/bin/env python
# Copied from [email protected]:Door43/flask-github-webhook.git
import os
import sys
import json
import requests
import ipaddress
from flask import Flask, request, abort
sys.path.append('/var/www/vhosts/door43.org/tools/general_tools')
try:
from git_wrapper import *
except:
print "Please verify that"
print "/var/www/vhosts/door43.org/tools/general_tools exists."
sys.exit(1)
app = Flask(__name__)
reposfile = '/var/www/vhosts/unfoldingword.org/repos.json'
# Store the IP address blocks that github uses for hook requests.
try:
hook_blocks = requests.get('https://api.github.com/meta').json()['hooks']
except KeyError:
hook_blocks = [ u'192.30.252.0/22', u'127.0.0.1/32']
hook_blocks.append(u'127.0.0.1/32')
if os.path.exists(reposfile):
repos = json.loads(open(reposfile, 'r').read())
else:
repos = {}
@app.route("/", methods=['GET', 'POST'])
def index():
if request.method == 'GET':
return 'OK'
elif request.method == 'POST':
# Check if the POST request if from github.com
for block in hook_blocks:
ip = ipaddress.ip_address(u'%s' % request.remote_addr)
if ipaddress.ip_address(ip) in ipaddress.ip_network(block):
break #the remote_addr is within the network range of github
else:
abort(403)
if request.headers.get('X-GitHub-Event') == "ping":
return json.dumps({'msg': 'Hi!'})
if request.headers.get('X-GitHub-Event') != "push":
return json.dumps({'msg': "wrong event type"})
payload = json.loads(request.data)
repo_name = payload['repository']['full_name']
if repo_name in repos:
local_path = repos[repo_name]
else:
abort(404)
if not os.path.exists(local_path):
os.makedirs(local_path, 0755)
gitClone(local_path, '[email protected]:{0}.git'.format(repo_name))
return 'Cloned repo'
gitPull(local_path)
if os.path.exists('{0}/publish'.format(local_path)):
out, ret = runCommand('{0}/s3_push.sh'.format(local_path))
if ret > 0:
return 'Publish failed'
return 'Publish succeeded'
return 'OK'
if __name__ == "__main__":
try:
port_number = int(sys.argv[1])
except:
port_number = 80
is_dev = os.environ.get('ENV', None) == 'dev'
if os.environ.get('USE_PROXYFIX', None) == 'true':
from werkzeug.contrib.fixers import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app)
app.run(host='0.0.0.0', port=port_number, debug=is_dev)