Add a test endpoint that allows us to manually create builds. Add access tokens to the build data.

This commit is contained in:
yackob03 2013-10-25 18:17:43 -04:00
parent c5ff08e57d
commit 78d2d6cad0
7 changed files with 99 additions and 14 deletions

View file

@ -152,7 +152,9 @@ class RepositoryTag(BaseModel):
class RepositoryBuild(BaseModel):
repository = ForeignKeyField(Repository)
access_token = ForeignKeyField(AccessToken)
resource_key = CharField()
tag = CharField()
build_node_id = IntegerField(null=True)
phase = CharField(default='waiting')
status_url = CharField(null=True)

View file

@ -569,5 +569,6 @@ def list_repository_builds(namespace_name, repository_name):
return fetched
def create_repository_build(repo, resource_key):
return RepositoryBuild.create(repository=repo, resource_key=resource_key)
def create_repository_build(repo, access_token, resource_key, tag):
return RepositoryBuild.create(repository=repo, access_token=access_token,
resource_key=resource_key, tag=tag)

View file

@ -2,6 +2,8 @@ import logging
import stripe
import re
import requests
import urlparse
import json
from flask import request, make_response, jsonify, abort, url_for
from flask.ext.login import login_required, current_user, logout_user
@ -190,14 +192,6 @@ def create_repo_api():
repo = model.create_repository(namespace_name, repository_name, owner,
visibility)
if request.values['initialize']:
logger.debug('User requested repository initialization.')
dockerfile_source = request.files['initializedata']
dockerfile_id = user_files.store_file(dockerfile_source)
build_request = model.create_repository_build(repo, dockerfile_id)
dockerfile_build_queue.put(json.dumps({'request_id': build_request.id}))
resp = make_response('Created', 201)
resp.headers['Location'] = url_for('get_repo_api', namespace=namespace_name,
repository=repository_name)
@ -371,7 +365,7 @@ def get_repo_api(namespace, repository):
@app.route('/api/repository/<path:repository>/build/', methods=['GET'])
@parse_repository_name
def get_repo_builds(namespace, repository):
permission = AdministerRepositoryPermission(namespace, repository)
permission = ModifyRepositoryPermission(namespace, repository)
if permission.can():
def build_view(build_obj):
if build_obj.status_url:
@ -400,6 +394,29 @@ def get_repo_builds(namespace, repository):
abort(403) # Permissions denied
@app.route('/api/repository/<path:repository>/build/', methods=['POST'])
@parse_repository_name
def request_repo_build(namespace, repository):
permission = ModifyRepositoryPermission(namespace, repository)
if permission.can():
logger.debug('User requested repository initialization.')
dockerfile_source = request.files['initializedata']
dockerfile_id = user_files.store_file(dockerfile_source)
repo = model.get_repository(namespace, repository)
token = model.create_access_token(repo, 'write')
host = urlparse.urlparse(request.url).netloc
tag = '%s/%s/%s' % (host, repo.namespace, repo.name)
build_request = model.create_repository_build(repo, token, dockerfile_id,
tag)
dockerfile_build_queue.put(json.dumps({'request_id': build_request.id}))
return make_response('Created', 201)
abort(403) # Permissions denied
def role_view(repo_perm_obj):
return {
'role': repo_perm_obj.role.name

53
endpoints/test.py Normal file
View file

@ -0,0 +1,53 @@
from random import SystemRandom
from flask import jsonify, send_file
from app import app
@app.route('/test/build/status', methods=['GET'])
def generate_random_build_status():
response = {
'id': 1,
'total_commands': None,
'total_images': None,
'current_command': None,
'current_image': None,
'image_completion_percent': None,
'status': None,
'message': None,
}
random = SystemRandom()
phases = {
'waiting': {},
'starting': {
'total_commands': 7,
'current_command': 0,
},
'initializing': {},
'error': {
'message': 'Oops!'
},
'complete': {},
'building': {
'total_commands': 7,
'current_command': random.randint(1, 7),
},
'pushing': {
'total_commands': 7,
'current_command': 7,
'total_images': 11,
'current_image': random.randint(1, 11),
'image_completion_percent': random.randint(0, 100),
},
}
phase = random.choice(phases.keys())
response['status'] = phase
response.update(phases[phase])
return jsonify(response)
@app.route('/test/build', methods=['GET'])
def create_build():
return send_file('templates/testbuild.html')

View file

@ -148,7 +148,10 @@ if __name__ == '__main__':
'Empty repository which is building.',
False, [], (0, [], None))
build = model.create_repository_build(building, '123-45-6789')
token = model.create_access_token(building, 'write')
tag = 'ci.devtable.com:5000/%s/%s' % (building.namespace, building.name)
build = model.create_repository_build(building, token, '123-45-6789', tag)
build.build_node_id = 1
build.phase = 'building'
build.status_url = 'http://localhost:5000/test/build/status'

8
templates/testbuild.html Normal file
View file

@ -0,0 +1,8 @@
<html>
<body>
<form enctype='multipart/form-data' method="post" action="/api/repository/devtable/complex/build/">
<input type="file" name="initializedata">
<button type="submit">Send</button>
</form>
</body>
</html>

View file

@ -108,8 +108,9 @@ def babysit_builder(request):
repo = repository_build.repository
payload = {
'tag': 'quay.io/%s/%s' % (repo.namespace, repo.name),
'tag': repository_build.tag,
'resource_url': user_files.get_file_url(repository_build.resource_key),
'token': repository_build.access_token.code,
}
start_build = requests.post(build_endpoint, data=payload)
@ -146,7 +147,7 @@ def babysit_builder(request):
def process_work_items(pool):
logger.debug('Getting work item from queue.')
item = dockerfile_build_queue.get()
item = dockerfile_build_queue.get(processing_time=60*60) # allow 1 hr
while item:
logger.debug('Queue gave us some work: %s' % item.body)