2013-09-23 16:37:40 +00:00
|
|
|
import logging
|
2013-10-01 03:54:12 +00:00
|
|
|
import os
|
2014-06-23 15:24:54 +00:00
|
|
|
import json
|
2013-09-23 16:37:40 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
from flask import Flask, request, Request, _request_ctx_stack
|
2013-09-20 22:38:17 +00:00
|
|
|
from flask.ext.principal import Principal
|
2015-01-17 03:41:54 +00:00
|
|
|
from flask.ext.login import LoginManager, UserMixin
|
2013-09-27 23:29:01 +00:00
|
|
|
from flask.ext.mail import Mail
|
2015-06-22 21:37:13 +00:00
|
|
|
from werkzeug.routing import BaseConverter
|
2013-10-03 20:19:01 +00:00
|
|
|
|
2014-04-03 22:47:17 +00:00
|
|
|
import features
|
|
|
|
|
2015-02-05 20:37:14 +00:00
|
|
|
from avatars.avatars import Avatar
|
2014-04-03 21:31:46 +00:00
|
|
|
from storage import Storage
|
2014-05-13 16:17:26 +00:00
|
|
|
from data import model
|
|
|
|
from data import database
|
2014-04-03 21:31:46 +00:00
|
|
|
from data.userfiles import Userfiles
|
2014-05-09 21:39:43 +00:00
|
|
|
from data.users import UserAuthentication
|
2014-04-10 19:20:16 +00:00
|
|
|
from data.billing import Billing
|
2014-05-09 22:45:11 +00:00
|
|
|
from data.buildlogs import BuildLogs
|
2014-09-08 20:43:17 +00:00
|
|
|
from data.archivedlogs import LogArchive
|
2014-05-30 18:25:29 +00:00
|
|
|
from data.userevent import UserEventsBuilderModule
|
2015-01-16 18:44:29 +00:00
|
|
|
from data.queue import WorkQueue
|
2015-08-03 19:49:10 +00:00
|
|
|
from util.saas.analytics import Analytics
|
|
|
|
from util.saas.exceptionlog import Sentry
|
2015-01-09 21:23:31 +00:00
|
|
|
from util.names import urn_generator
|
2015-08-03 19:49:10 +00:00
|
|
|
from util.config.oauth import GoogleOAuthConfig, GithubOAuthConfig, GitLabOAuthConfig
|
|
|
|
from util.security.signing import Signer
|
2015-08-11 20:39:33 +00:00
|
|
|
from util.saas.cloudwatch import send_cloudwatch
|
|
|
|
from util.saas.metricqueue import MetricQueue
|
2015-01-23 22:26:14 +00:00
|
|
|
from util.config.provider import FileConfigProvider, TestConfigProvider
|
2015-01-09 21:23:31 +00:00
|
|
|
from util.config.configutil import generate_secret_key
|
2015-01-20 17:43:11 +00:00
|
|
|
from util.config.superusermanager import SuperUserManager
|
2014-04-07 20:59:22 +00:00
|
|
|
|
2015-02-04 20:29:24 +00:00
|
|
|
OVERRIDE_CONFIG_DIRECTORY = 'conf/stack/'
|
2014-08-22 00:36:11 +00:00
|
|
|
OVERRIDE_CONFIG_YAML_FILENAME = 'conf/stack/config.yaml'
|
|
|
|
OVERRIDE_CONFIG_PY_FILENAME = 'conf/stack/config.py'
|
|
|
|
|
2014-06-23 15:24:54 +00:00
|
|
|
OVERRIDE_CONFIG_KEY = 'QUAY_OVERRIDE_CONFIG'
|
2014-04-07 20:59:22 +00:00
|
|
|
|
2015-02-09 22:28:11 +00:00
|
|
|
CONFIG_PROVIDER = FileConfigProvider(OVERRIDE_CONFIG_DIRECTORY, 'config.yaml', 'config.py')
|
2014-04-07 20:59:22 +00:00
|
|
|
|
2013-09-20 15:55:44 +00:00
|
|
|
app = Flask(__name__)
|
2013-10-03 20:19:01 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-06-22 21:37:13 +00:00
|
|
|
class RegexConverter(BaseConverter):
|
|
|
|
def __init__(self, url_map, *items):
|
|
|
|
super(RegexConverter, self).__init__(url_map)
|
|
|
|
logger.debug('Installing regex converter with regex: %s', items[0])
|
|
|
|
self.regex = items[0]
|
|
|
|
|
|
|
|
|
|
|
|
app.url_map.converters['regex'] = RegexConverter
|
|
|
|
|
2015-01-09 21:23:31 +00:00
|
|
|
# Instantiate the default configuration (for test or for normal operation).
|
2014-04-07 20:59:22 +00:00
|
|
|
if 'TEST' in os.environ:
|
2015-01-09 21:23:31 +00:00
|
|
|
CONFIG_PROVIDER = TestConfigProvider()
|
|
|
|
|
2014-04-07 20:59:22 +00:00
|
|
|
from test.testconfig import TestConfig
|
|
|
|
logger.debug('Loading test config.')
|
|
|
|
app.config.from_object(TestConfig())
|
|
|
|
else:
|
|
|
|
from config import DefaultConfig
|
|
|
|
logger.debug('Loading default config.')
|
|
|
|
app.config.from_object(DefaultConfig())
|
2015-01-09 21:23:31 +00:00
|
|
|
app.teardown_request(database.close_db_filter)
|
2014-04-03 21:31:46 +00:00
|
|
|
|
2015-01-09 21:23:31 +00:00
|
|
|
# Load the override config via the provider.
|
|
|
|
CONFIG_PROVIDER.update_app_config(app.config)
|
2013-09-27 23:29:01 +00:00
|
|
|
|
2015-01-09 21:23:31 +00:00
|
|
|
# Update any configuration found in the override environment variable.
|
|
|
|
OVERRIDE_CONFIG_KEY = 'QUAY_OVERRIDE_CONFIG'
|
2014-06-23 15:24:54 +00:00
|
|
|
|
2015-01-09 21:23:31 +00:00
|
|
|
environ_config = json.loads(os.environ.get(OVERRIDE_CONFIG_KEY, '{}'))
|
|
|
|
app.config.update(environ_config)
|
2014-10-14 17:58:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RequestWithId(Request):
|
|
|
|
request_gen = staticmethod(urn_generator(['request']))
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(RequestWithId, self).__init__(*args, **kwargs)
|
|
|
|
self.request_id = self.request_gen()
|
|
|
|
|
|
|
|
|
|
|
|
@app.before_request
|
|
|
|
def _request_start():
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Starting request: %s', request.path)
|
2014-10-14 17:58:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.after_request
|
|
|
|
def _request_end(r):
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Ending request: %s', request.path)
|
2014-10-14 17:58:08 +00:00
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
class InjectingFilter(logging.Filter):
|
|
|
|
def filter(self, record):
|
2015-02-11 19:15:18 +00:00
|
|
|
if _request_ctx_stack.top is not None:
|
|
|
|
record.msg = '[%s] %s' % (request.request_id, record.msg)
|
2014-10-14 17:58:08 +00:00
|
|
|
return True
|
|
|
|
|
2015-02-11 19:15:18 +00:00
|
|
|
# Add the request id filter to all handlers of the root logger
|
|
|
|
for handler in logging.getLogger().handlers:
|
|
|
|
handler.addFilter(InjectingFilter())
|
2014-10-14 17:58:08 +00:00
|
|
|
|
|
|
|
app.request_class = RequestWithId
|
|
|
|
|
2014-04-03 22:47:17 +00:00
|
|
|
features.import_features(app.config)
|
|
|
|
|
2014-03-17 16:01:13 +00:00
|
|
|
Principal(app, use_sessions=False)
|
2013-09-20 22:38:17 +00:00
|
|
|
|
2014-11-25 00:25:13 +00:00
|
|
|
avatar = Avatar(app)
|
2014-04-09 03:05:45 +00:00
|
|
|
login_manager = LoginManager(app)
|
|
|
|
mail = Mail(app)
|
|
|
|
storage = Storage(app)
|
2014-09-09 19:54:03 +00:00
|
|
|
userfiles = Userfiles(app, storage)
|
2014-09-11 15:18:28 +00:00
|
|
|
log_archive = LogArchive(app, storage)
|
2014-04-09 03:05:45 +00:00
|
|
|
analytics = Analytics(app)
|
2014-04-10 19:20:16 +00:00
|
|
|
billing = Billing(app)
|
2014-04-28 22:59:22 +00:00
|
|
|
sentry = Sentry(app)
|
2014-05-09 22:45:11 +00:00
|
|
|
build_logs = BuildLogs(app)
|
2015-06-12 21:58:19 +00:00
|
|
|
authentication = UserAuthentication(app, OVERRIDE_CONFIG_DIRECTORY)
|
2014-05-30 18:25:29 +00:00
|
|
|
userevents = UserEventsBuilderModule(app)
|
2015-01-20 17:43:11 +00:00
|
|
|
superusers = SuperUserManager(app)
|
2015-02-04 20:29:24 +00:00
|
|
|
signer = Signer(app, OVERRIDE_CONFIG_DIRECTORY)
|
2015-08-11 20:39:33 +00:00
|
|
|
metric_queue = MetricQueue()
|
|
|
|
send_cloudwatch(metric_queue, app)
|
2015-01-16 18:44:29 +00:00
|
|
|
|
|
|
|
tf = app.config['DB_TRANSACTION_FACTORY']
|
2014-05-21 23:50:37 +00:00
|
|
|
|
2015-01-07 21:20:51 +00:00
|
|
|
github_login = GithubOAuthConfig(app.config, 'GITHUB_LOGIN_CONFIG')
|
|
|
|
github_trigger = GithubOAuthConfig(app.config, 'GITHUB_TRIGGER_CONFIG')
|
2015-05-02 21:54:48 +00:00
|
|
|
gitlab_trigger = GitLabOAuthConfig(app.config, 'GITLAB_TRIGGER_CONFIG')
|
2015-01-07 21:20:51 +00:00
|
|
|
google_login = GoogleOAuthConfig(app.config, 'GOOGLE_LOGIN_CONFIG')
|
2015-05-02 21:54:48 +00:00
|
|
|
oauth_apps = [github_login, github_trigger, gitlab_trigger, google_login]
|
2014-11-05 21:43:37 +00:00
|
|
|
|
2014-05-22 16:13:41 +00:00
|
|
|
image_diff_queue = WorkQueue(app.config['DIFFS_QUEUE_NAME'], tf)
|
|
|
|
dockerfile_build_queue = WorkQueue(app.config['DOCKERFILE_BUILD_QUEUE_NAME'], tf,
|
2015-08-11 20:39:33 +00:00
|
|
|
metric_queue=metric_queue)
|
2014-07-18 02:51:58 +00:00
|
|
|
notification_queue = WorkQueue(app.config['NOTIFICATION_QUEUE_NAME'], tf)
|
2014-05-22 16:13:41 +00:00
|
|
|
|
2014-05-13 16:17:26 +00:00
|
|
|
database.configure(app.config)
|
|
|
|
model.config.app_config = app.config
|
|
|
|
model.config.store = storage
|
2014-07-29 17:47:54 +00:00
|
|
|
|
2015-01-05 17:31:02 +00:00
|
|
|
# Generate a secret key if none was specified.
|
|
|
|
if app.config['SECRET_KEY'] is None:
|
|
|
|
logger.debug('Generating in-memory secret key')
|
|
|
|
app.config['SECRET_KEY'] = generate_secret_key()
|
|
|
|
|
2015-01-17 03:41:54 +00:00
|
|
|
@login_manager.user_loader
|
|
|
|
def load_user(user_uuid):
|
|
|
|
logger.debug('User loader loading deferred user with uuid: %s' % user_uuid)
|
|
|
|
return LoginWrappedDBUser(user_uuid)
|
|
|
|
|
|
|
|
class LoginWrappedDBUser(UserMixin):
|
|
|
|
def __init__(self, user_uuid, db_user=None):
|
|
|
|
self._uuid = user_uuid
|
|
|
|
self._db_user = db_user
|
|
|
|
|
|
|
|
def db_user(self):
|
|
|
|
if not self._db_user:
|
2015-07-15 21:25:41 +00:00
|
|
|
self._db_user = model.user.get_user_by_uuid(self._uuid)
|
2015-01-17 03:41:54 +00:00
|
|
|
return self._db_user
|
|
|
|
|
|
|
|
def is_authenticated(self):
|
|
|
|
return self.db_user() is not None
|
|
|
|
|
|
|
|
def is_active(self):
|
|
|
|
return self.db_user().verified
|
|
|
|
|
|
|
|
def get_id(self):
|
|
|
|
return unicode(self._uuid)
|
|
|
|
|
2014-07-29 17:47:54 +00:00
|
|
|
def get_app_url():
|
|
|
|
return '%s://%s' % (app.config['PREFERRED_URL_SCHEME'], app.config['SERVER_HOSTNAME'])
|