2013-12-27 22:19:14 +00:00
|
|
|
import logging
|
2014-02-26 00:39:43 +00:00
|
|
|
import urlparse
|
|
|
|
import json
|
2014-03-26 22:36:59 +00:00
|
|
|
import string
|
2014-09-04 18:24:20 +00:00
|
|
|
import datetime
|
2013-12-27 22:19:14 +00:00
|
|
|
|
2015-01-17 03:41:54 +00:00
|
|
|
# Register the various exceptions via decorators.
|
|
|
|
import endpoints.decorated
|
|
|
|
|
2014-09-04 18:24:20 +00:00
|
|
|
from flask import make_response, render_template, request, abort, session
|
2015-01-17 03:41:54 +00:00
|
|
|
from flask.ext.login import login_user
|
2013-12-27 22:19:14 +00:00
|
|
|
from flask.ext.principal import identity_changed
|
2014-03-26 22:36:59 +00:00
|
|
|
from random import SystemRandom
|
2013-12-27 22:19:14 +00:00
|
|
|
|
|
|
|
from data import model
|
2014-10-22 19:20:53 +00:00
|
|
|
from data.database import db
|
2015-01-17 03:41:54 +00:00
|
|
|
from app import app, oauth_apps, dockerfile_build_queue, LoginWrappedDBUser
|
2014-11-05 21:43:37 +00:00
|
|
|
|
2013-12-27 23:01:44 +00:00
|
|
|
from auth.permissions import QuayDeferredPermissionUser
|
2014-03-19 22:21:58 +00:00
|
|
|
from auth import scopes
|
2014-03-17 16:01:13 +00:00
|
|
|
from endpoints.api.discovery import swagger_route_data
|
2014-03-11 18:30:00 +00:00
|
|
|
from werkzeug.routing import BaseConverter
|
2014-04-03 23:32:09 +00:00
|
|
|
from functools import wraps
|
2014-04-08 23:14:24 +00:00
|
|
|
from config import getFrontendVisibleConfig
|
2014-05-09 22:49:33 +00:00
|
|
|
from external_libraries import get_external_javascript, get_external_css
|
2014-07-18 20:34:52 +00:00
|
|
|
from endpoints.notificationhelper import spawn_notification
|
2013-12-27 22:19:14 +00:00
|
|
|
|
2014-04-05 03:26:10 +00:00
|
|
|
import features
|
|
|
|
|
2013-12-27 22:19:14 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2014-07-18 19:58:18 +00:00
|
|
|
profile = logging.getLogger('application.profiler')
|
2013-12-27 22:19:14 +00:00
|
|
|
|
2014-02-18 20:50:15 +00:00
|
|
|
route_data = None
|
|
|
|
|
2014-03-11 18:30:00 +00:00
|
|
|
class RepoPathConverter(BaseConverter):
|
2014-03-11 18:42:53 +00:00
|
|
|
regex = '[\.a-zA-Z0-9_\-]+/[\.a-zA-Z0-9_\-]+'
|
2014-03-11 18:30:00 +00:00
|
|
|
weight = 200
|
|
|
|
|
|
|
|
app.url_map.converters['repopath'] = RepoPathConverter
|
|
|
|
|
2014-04-03 23:32:09 +00:00
|
|
|
def route_show_if(value):
|
|
|
|
def decorator(f):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
if not value:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
return decorated_function
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
def route_hide_if(value):
|
|
|
|
def decorator(f):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
if value:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
return decorated_function
|
|
|
|
return decorator
|
|
|
|
|
2014-03-11 18:30:00 +00:00
|
|
|
|
2014-02-18 20:50:15 +00:00
|
|
|
def get_route_data():
|
|
|
|
global route_data
|
|
|
|
if route_data:
|
|
|
|
return route_data
|
|
|
|
|
2014-03-15 03:40:41 +00:00
|
|
|
route_data = swagger_route_data(include_internal=True, compact=True)
|
2014-02-18 20:50:15 +00:00
|
|
|
return route_data
|
|
|
|
|
|
|
|
|
2014-02-16 23:59:24 +00:00
|
|
|
def truthy_param(param):
|
|
|
|
return param not in {False, 'false', 'False', '0', 'FALSE', '', 'null'}
|
|
|
|
|
|
|
|
|
2014-07-21 19:09:31 +00:00
|
|
|
def param_required(param_name):
|
|
|
|
def wrapper(wrapped):
|
|
|
|
@wraps(wrapped)
|
|
|
|
def decorated(*args, **kwargs):
|
|
|
|
if param_name not in request.args:
|
|
|
|
abort(make_response('Required param: %s' % param_name, 400))
|
|
|
|
return wrapped(*args, **kwargs)
|
|
|
|
return decorated
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2013-12-27 22:19:14 +00:00
|
|
|
def common_login(db_user):
|
2015-01-17 03:41:54 +00:00
|
|
|
if login_user(LoginWrappedDBUser(db_user.uuid, db_user)):
|
2014-11-11 22:22:37 +00:00
|
|
|
logger.debug('Successfully signed in as: %s (%s)' % (db_user.username, db_user.uuid))
|
2014-11-20 23:44:36 +00:00
|
|
|
new_identity = QuayDeferredPermissionUser(db_user.uuid, 'user_uuid', {scopes.DIRECT_LOGIN})
|
2013-12-27 22:19:14 +00:00
|
|
|
identity_changed.send(app, identity=new_identity)
|
2014-09-04 18:24:20 +00:00
|
|
|
session['login_time'] = datetime.datetime.now()
|
2013-12-27 22:19:14 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
logger.debug('User could not be logged in, inactive?.')
|
|
|
|
return False
|
2013-12-28 19:07:44 +00:00
|
|
|
|
2014-03-26 22:36:59 +00:00
|
|
|
def random_string():
|
|
|
|
random = SystemRandom()
|
|
|
|
return ''.join([random.choice(string.ascii_uppercase + string.digits) for _ in range(8)])
|
|
|
|
|
2014-04-15 20:35:15 +00:00
|
|
|
def list_files(path, extension):
|
|
|
|
import os
|
|
|
|
def matches(f):
|
|
|
|
return os.path.splitext(f)[1] == '.' + extension
|
|
|
|
|
|
|
|
def join_path(dp, f):
|
|
|
|
# Remove the static/ prefix. It is added in the template.
|
|
|
|
return os.path.join(dp, f)[len('static/'):]
|
|
|
|
|
|
|
|
filepath = 'static/' + path
|
|
|
|
return [join_path(dp, f) for dp, dn, files in os.walk(filepath) for f in files if matches(f)]
|
2014-04-15 19:51:32 +00:00
|
|
|
|
2014-07-30 00:54:16 +00:00
|
|
|
SAVED_CACHE_STRING = random_string()
|
|
|
|
|
2014-04-15 20:35:15 +00:00
|
|
|
def render_page_template(name, **kwargs):
|
|
|
|
if app.config.get('DEBUGGING', False):
|
|
|
|
# If DEBUGGING is enabled, then we load the full set of individual JS and CSS files
|
|
|
|
# from the file system.
|
|
|
|
library_styles = list_files('lib', 'css')
|
|
|
|
main_styles = list_files('css', 'css')
|
|
|
|
library_scripts = list_files('lib', 'js')
|
|
|
|
main_scripts = list_files('js', 'js')
|
|
|
|
cache_buster = 'debugging'
|
2014-04-16 18:23:22 +00:00
|
|
|
|
|
|
|
file_lists = [library_styles, main_styles, library_scripts, main_scripts]
|
|
|
|
for file_list in file_lists:
|
|
|
|
file_list.sort()
|
2014-04-15 20:35:15 +00:00
|
|
|
else:
|
|
|
|
library_styles = []
|
|
|
|
main_styles = ['dist/quay-frontend.css']
|
|
|
|
library_scripts = []
|
|
|
|
main_scripts = ['dist/quay-frontend.min.js']
|
2014-07-30 00:54:16 +00:00
|
|
|
cache_buster = SAVED_CACHE_STRING
|
2014-04-15 19:51:32 +00:00
|
|
|
|
2014-11-25 20:32:10 +00:00
|
|
|
use_cdn = app.config.get('USE_CDN', True)
|
|
|
|
if request.args.get('use_cdn') is not None:
|
|
|
|
use_cdn = request.args.get('use_cdn') == 'true'
|
|
|
|
|
|
|
|
external_styles = get_external_css(local=not use_cdn)
|
|
|
|
external_scripts = get_external_javascript(local=not use_cdn)
|
2014-05-09 22:49:33 +00:00
|
|
|
|
2014-11-05 21:43:37 +00:00
|
|
|
def get_oauth_config():
|
|
|
|
oauth_config = {}
|
|
|
|
for oauth_app in oauth_apps:
|
2014-11-07 01:35:52 +00:00
|
|
|
oauth_config[oauth_app.key_name] = oauth_app.get_public_config()
|
2014-11-05 21:43:37 +00:00
|
|
|
|
|
|
|
return oauth_config
|
|
|
|
|
2014-10-22 18:49:33 +00:00
|
|
|
contact_href = None
|
|
|
|
if len(app.config.get('CONTACT_INFO', [])) == 1:
|
|
|
|
contact_href = app.config['CONTACT_INFO'][0]
|
|
|
|
|
2014-03-26 22:36:59 +00:00
|
|
|
resp = make_response(render_template(name, route_data=json.dumps(get_route_data()),
|
2014-05-09 22:49:33 +00:00
|
|
|
external_styles=external_styles,
|
|
|
|
external_scripts=external_scripts,
|
2014-04-15 19:51:32 +00:00
|
|
|
main_styles=main_styles,
|
|
|
|
library_styles=library_styles,
|
|
|
|
main_scripts=main_scripts,
|
|
|
|
library_scripts=library_scripts,
|
2014-04-05 03:26:10 +00:00
|
|
|
feature_set=json.dumps(features.get_features()),
|
2014-04-08 23:14:24 +00:00
|
|
|
config_set=json.dumps(getFrontendVisibleConfig(app.config)),
|
2014-11-05 21:43:37 +00:00
|
|
|
oauth_set=json.dumps(get_oauth_config()),
|
2014-11-17 19:54:07 +00:00
|
|
|
scope_set=json.dumps(scopes.ALL_SCOPES),
|
2014-04-08 23:14:24 +00:00
|
|
|
mixpanel_key=app.config.get('MIXPANEL_KEY', ''),
|
2014-08-08 00:44:59 +00:00
|
|
|
google_analytics_key=app.config.get('GOOGLE_ANALYTICS_KEY', ''),
|
2014-04-28 22:59:22 +00:00
|
|
|
sentry_public_dsn=app.config.get('SENTRY_PUBLIC_DSN', ''),
|
2014-04-08 23:14:24 +00:00
|
|
|
is_debug=str(app.config.get('DEBUGGING', False)).lower(),
|
2014-04-09 00:33:20 +00:00
|
|
|
show_chat=features.OLARK_CHAT,
|
2014-04-15 19:51:32 +00:00
|
|
|
cache_buster=cache_buster,
|
2014-05-28 19:22:36 +00:00
|
|
|
has_billing=features.BILLING,
|
2014-10-22 18:49:33 +00:00
|
|
|
contact_href=contact_href,
|
2014-04-05 03:26:10 +00:00
|
|
|
**kwargs))
|
|
|
|
|
2014-02-18 20:50:15 +00:00
|
|
|
resp.headers['X-FRAME-OPTIONS'] = 'DENY'
|
2014-02-21 19:52:40 +00:00
|
|
|
return resp
|
2014-02-26 00:39:43 +00:00
|
|
|
|
|
|
|
|
2014-03-12 23:19:39 +00:00
|
|
|
def check_repository_usage(user_or_org, plan_found):
|
|
|
|
private_repos = model.get_private_repo_count(user_or_org.username)
|
|
|
|
repos_allowed = plan_found['privateRepos']
|
2014-03-12 04:49:03 +00:00
|
|
|
|
2014-03-12 23:19:39 +00:00
|
|
|
if private_repos > repos_allowed:
|
|
|
|
model.create_notification('over_private_usage', user_or_org, {'namespace': user_or_org.username})
|
|
|
|
else:
|
|
|
|
model.delete_notifications_by_kind(user_or_org, 'over_private_usage')
|
2014-03-12 04:49:03 +00:00
|
|
|
|
|
|
|
|
2014-02-26 00:39:43 +00:00
|
|
|
def start_build(repository, dockerfile_id, tags, build_name, subdir, manual,
|
2014-04-02 01:49:06 +00:00
|
|
|
trigger=None, pull_robot_name=None):
|
2014-02-26 00:39:43 +00:00
|
|
|
host = urlparse.urlparse(request.url).netloc
|
2014-09-24 22:01:35 +00:00
|
|
|
repo_path = '%s/%s/%s' % (host, repository.namespace_user.username, repository.name)
|
2014-02-26 00:39:43 +00:00
|
|
|
|
|
|
|
token = model.create_access_token(repository, 'write')
|
|
|
|
logger.debug('Creating build %s with repo %s tags %s and dockerfile_id %s',
|
|
|
|
build_name, repo_path, tags, dockerfile_id)
|
|
|
|
|
|
|
|
job_config = {
|
|
|
|
'docker_tags': tags,
|
2014-10-01 18:23:15 +00:00
|
|
|
'registry': host,
|
2014-04-02 01:49:06 +00:00
|
|
|
'build_subdir': subdir
|
2014-02-26 00:39:43 +00:00
|
|
|
}
|
2014-03-27 22:33:13 +00:00
|
|
|
|
2014-10-22 19:20:53 +00:00
|
|
|
with app.config['DB_TRANSACTION_FACTORY'](db):
|
|
|
|
build_request = model.create_repository_build(repository, token, job_config,
|
|
|
|
dockerfile_id, build_name,
|
|
|
|
trigger, pull_robot_name=pull_robot_name)
|
2014-02-26 00:39:43 +00:00
|
|
|
|
2014-11-20 20:36:39 +00:00
|
|
|
dockerfile_build_queue.put([repository.namespace_user.username, repository.name], json.dumps({
|
2014-10-22 19:20:53 +00:00
|
|
|
'build_uuid': build_request.uuid,
|
|
|
|
'pull_credentials': model.get_pull_credentials(pull_robot_name) if pull_robot_name else None
|
2015-02-03 21:29:47 +00:00
|
|
|
}), retries_remaining=3)
|
2014-02-26 00:39:43 +00:00
|
|
|
|
2014-07-18 19:58:18 +00:00
|
|
|
# Add the build to the repo's log.
|
2014-02-26 00:39:43 +00:00
|
|
|
metadata = {
|
|
|
|
'repo': repository.name,
|
2014-09-24 22:01:35 +00:00
|
|
|
'namespace': repository.namespace_user.username,
|
2014-02-26 00:39:43 +00:00
|
|
|
'fileid': dockerfile_id,
|
|
|
|
'manual': manual,
|
|
|
|
}
|
|
|
|
|
|
|
|
if trigger:
|
|
|
|
metadata['trigger_id'] = trigger.uuid
|
|
|
|
metadata['config'] = json.loads(trigger.config)
|
|
|
|
metadata['service'] = trigger.service.name
|
|
|
|
|
2014-09-24 22:01:35 +00:00
|
|
|
model.log_action('build_dockerfile', repository.namespace_user.username, ip=request.remote_addr,
|
|
|
|
metadata=metadata, repository=repository)
|
2014-02-26 00:39:43 +00:00
|
|
|
|
2014-07-18 19:58:18 +00:00
|
|
|
# Add notifications for the build queue.
|
|
|
|
profile.debug('Adding notifications for repository')
|
|
|
|
event_data = {
|
|
|
|
'build_id': build_request.uuid,
|
|
|
|
'build_name': build_name,
|
|
|
|
'docker_tags': tags,
|
2014-07-18 20:34:52 +00:00
|
|
|
'is_manual': manual
|
2014-07-18 19:58:18 +00:00
|
|
|
}
|
|
|
|
|
2014-07-18 20:34:52 +00:00
|
|
|
if trigger:
|
|
|
|
event_data['trigger_id'] = trigger.uuid
|
|
|
|
event_data['trigger_kind'] = trigger.service.name
|
|
|
|
|
2014-07-18 19:58:18 +00:00
|
|
|
spawn_notification(repository, 'build_queued', event_data,
|
2014-07-18 20:34:52 +00:00
|
|
|
subpage='build?current=%s' % build_request.uuid,
|
2014-07-18 19:58:18 +00:00
|
|
|
pathargs=['build', build_request.uuid])
|
2014-11-11 22:22:37 +00:00
|
|
|
return build_request
|