/
application.py
104 lines (96 loc) · 3.65 KB
/
application.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
#!/usr/bin/env python
# Copyright 2012 Participatory Culture Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from flask import (redirect, url_for, render_template, request, flash, session,
json)
from flask.ext import redis
from flask.ext.babel import Babel
from flask.ext.mail import Mail
from rq_dashboard import RQDashboard
import config
import messages
import utils
import time
from utils import db, accounts, queue_task, ops
from accounts.views import accounts_blueprint
from admin.views import admin_blueprint
from multiprocessing import Process
app = config.create_app()
app.register_blueprint(accounts_blueprint, url_prefix='/accounts')
app.register_blueprint(admin_blueprint, url_prefix='/admin')
babel = Babel(app)
mail = Mail(app)
redis = redis.init_redis(app)
# add exts for blueprint use
app.config['babel'] = babel
app.config['mail'] = mail
app.config['redis'] = redis
RQDashboard(app)
# check for admin user ; create if missing
if not db.get_user('admin'):
print('Creating admin user; password: launchpad')
db.create_user(username='admin', password='launchpad',
email=config.ADMIN_EMAIL, is_admin=True)
# hack to add auth for rq dashboard
@app.before_request
def rq_auth_check():
if request.path.find('/rq') > -1 and not session.get('user'):
return redirect(url_for('accounts.login'))
@app.route('/')
def index():
return redirect(url_for('admin.index'))
#github post receive hook
@app.route('/github', methods=['POST'])
def github_hook():
data = json.loads(request.form.get('payload', ''))
# make sure this came from unisubs
repo = data.get('repository', {})
if repo.get('name') == 'unisubs' and \
repo.get('url') == 'https://github.com/pculture/unisubs':
# get the branch
branch = data.get('ref').split('refs/heads/')[-1]
db.log({
'ip': request.remote_addr,
'user': 'github',
'command': 'Hook: deploy for {0}'.format(branch),
})
# spawn process to not delay the response to github
p = Process(target=deploy_demo, args=(branch,))
p.start()
return 'kthxbye'
def deploy_demo(branch=None):
"""
Runs the demo task for the specified branch
"""
result_key = str(int(time.time()))
# don't deploy these branches
ignored_branches = ['dev', 'staging', 'production']
# hack to check to make sure a demo of that branch exists
hook_key = 'github'
ops.run_fabric_task('demo:amara show_demos',
result_key=hook_key, notify=False)
demos = ops.get_fabric_log(hook_key)
demo_name = branch.replace('-', '_')
if branch not in ignored_branches and demos.find(demo_name) != -1:
task = 'demo:amara,{0} deploy'.format(branch)
job = queue_task(ops.run_fabric_task, task, result_key)
if __name__=='__main__':
from optparse import OptionParser
op = OptionParser()
op.add_option('--host', dest='host', action='store', default='127.0.0.1', \
help='Hostname/IP on which to listen')
op.add_option('--port', dest='port', action='store', type=int, \
default=5000, help='Port on which to listen')
opts, args = op.parse_args()
app.run(host=opts.host, port=opts.port, debug=True)