-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·63 lines (48 loc) · 1.61 KB
/
app.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
#!/usr/bin/env python
import hashlib
import hmac
import os
import subprocess
from flask import Flask, request, abort
app = Flask(__name__, instance_relative_config=True)
app.config.from_pyfile('application.cfg')
class ChecksumCalcStream(object):
def __init__(self, stream, key):
self._stream = stream
self._hash = hmac.new(key, digestmod=hashlib.sha1)
def read(self, bytes):
rv = self._stream.read(bytes)
self._hash.update(rv)
return rv
def readline(self, size_hint):
rv = self._stream.readline(size_hint)
self._hash.update(rv)
return rv
def generate_checksum(request, key=None):
env = request.environ
stream = ChecksumCalcStream(env['wsgi.input'], key=key)
env['wsgi.input'] = stream
return stream._hash
def run_command():
path = '/usr/lib/myawesomeapp'
python_path = '/'.join([path, 'bin/python'])
ext_path = '/'.join([path, 'src/myawesomeapp'])
git_pull = 'git pull origin master'
install = '{0} setup.py develop'.format(python_path)
restart = 'sudo service apache2 reload'
os.chdir(ext_path)
subprocess.call(git_pull.split(' '))
subprocess.call(install.split(' '))
subprocess.call(restart.split(' '))
@app.route("/", methods=['POST'])
def deploy():
hash = generate_checksum(request, key=app.config['GITHUB_SECRET'])
# Accessing json just to parse the stream
request.json
headers = request.headers
if 'sha1={0}'.format(hash.hexdigest()) == headers.get('X-Hub-Signature'):
run_command()
return 'OK'
abort(404)
if __name__ == "__main__":
app.run(debug=True)