Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
136 lines (93 sloc) 3.2 KB
# -*- coding: utf-8 -*-
"""
springcreek.core
~~~~~~~~~~~~~~~~
This module contains the main application of SpringCreek.
"""
import os
import tempfile
import heroku
import envoy
from flask import Flask, render_template, request, redirect, url_for
from flask.views import MethodView
from flask_debugtoolbar import DebugToolbarExtension
from flask_heroku import Heroku
from flask_googlefed import GoogleAuth
from raven.contrib.flask import Sentry
from flask.ext.celery import Celery
from .models import db, BuildRequest, BuildResult
app = Flask(__name__)
app.secret_key = 'some-secret-key'
# Use gevent workers for celery.
app.config['CELERYD_POOL'] = 'gevent'
app.config['CELERYD_CONCURRENCY'] = 1000
# Heroku API Key.
app.config['HEROKU_API_KEY'] = os.environ.get('HEROKU_API_KEY')
app.config['DEBUG_TB_ENABLED'] = True
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
# Bootstrap Heroku environment variables.
heroku_env = Heroku(app)
# Intialize databse configuration.
db.init_app(app)
sentry = Sentry(app)
auth = GoogleAuth(app)
toolbar = DebugToolbarExtension(app)
celery = Celery(app)
heroku = heroku.from_key(app.config['HEROKU_API_KEY'] )
@celery.task
def build_task(request):
app.logger.info('Starting build for {0}.'.format(request))
# Replace # w/ @
_split_url = request.application_url.split('@')
clone_url = _split_url[0]
clone_ref = _split_url[1] if len(_split_url) > 1 else None
h_app = heroku.apps.create(stack=cedar)
h_app.config['BUILDPACK_URL'] = request.buildpack_url
app.logger.info('Heroku app {0} created for {1}.'.format(h_app, request))
pwd = tempfile.mkdtemp(prefix='springcreek')
app.logger.info('Temp dir {0} created for {1}.'.format(pwd, request))
os.chdir(pwd)
app.logger.info('Cloning {0} created for {1}.'.format(application_url, request))
c = envoy.run('git clone {0} repo'.format(application_url))
os.chdir('repo')
app.logger.info('Pushing app to {0} for {1}.'.format(h_app, request))
# c = envoy.run('git push git@heroku.com:{h_app.name}.git')
# git clone {{ application_url }} repo
# cd repo
# git push {{ clone_ref }}:master
@app.route('/')
@auth.required
def landing_page():
return render_template('index.html')
class Builds(MethodView):
@auth.required
def get(self):
return str(len(BuildRequest.query.all()))
@auth.required
def post(self):
"""Create a new BuildRequest."""
r = BuildRequest(
buildpack_url=request.form.get('buildpack_url'),
application_url=request.form.get('application_url'),
keep=('keep' in request.form)
)
db.session.add(r)
db.session.commit()
# Send the build task off to work.
app.logger.info('Sending off a task...')
build_task.delay(r)
return redirect(r.url)
app.add_url_rule('/builds', view_func=Builds.as_view('builds'))
@app.route('/builds/<id>')
@auth.required
def view_build(id):
b_request = BuildRequest.query.filter_by(id=id).first()
context = dict(
request=b_request
)
if b_request:
return render_template('build.html', **context)
else:
return redirect(url_for('builds'))
if __name__ == '__main__':
app.run()