2013-10-24 04:05:58 +00:00
|
|
|
import docker
|
|
|
|
import logging
|
|
|
|
import shutil
|
|
|
|
import os
|
|
|
|
import re
|
2013-10-25 05:14:38 +00:00
|
|
|
import requests
|
2013-10-24 04:05:58 +00:00
|
|
|
|
2013-10-24 05:29:34 +00:00
|
|
|
from flask import Flask, request, send_file, jsonify, redirect, url_for, abort
|
2013-10-24 04:05:58 +00:00
|
|
|
from zipfile import ZipFile
|
|
|
|
from tempfile import TemporaryFile, mkdtemp
|
2013-10-24 05:29:34 +00:00
|
|
|
from uuid import uuid4
|
|
|
|
from multiprocessing.pool import ThreadPool
|
2013-10-24 04:05:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
BUFFER_SIZE = 8 * 1024
|
|
|
|
LOG_FORMAT = '%(asctime)-15s - %(levelname)s - %(pathname)s - ' + \
|
|
|
|
'%(funcName)s - %(message)s'
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
def index():
|
|
|
|
return send_file('test.html')
|
|
|
|
|
|
|
|
|
2013-10-24 05:29:34 +00:00
|
|
|
def count_steps(dockerfile_path):
|
|
|
|
with open(dockerfile_path, 'r') as dockerfileobj:
|
|
|
|
steps = 0
|
|
|
|
for line in dockerfileobj.readlines():
|
|
|
|
stripped = line.strip()
|
|
|
|
if stripped and stripped[0] is not '#':
|
|
|
|
steps += 1
|
|
|
|
return steps
|
|
|
|
|
2013-10-24 04:05:58 +00:00
|
|
|
|
2013-10-24 05:29:34 +00:00
|
|
|
def prepare_zip(request_file):
|
2013-10-24 04:05:58 +00:00
|
|
|
build_dir = mkdtemp(prefix='docker-build-')
|
|
|
|
|
|
|
|
# Save the zip file to temp somewhere
|
|
|
|
with TemporaryFile() as zip_file:
|
2013-10-25 05:14:38 +00:00
|
|
|
zip_file.write(request_file.content)
|
2013-10-24 04:05:58 +00:00
|
|
|
to_extract = ZipFile(zip_file)
|
|
|
|
to_extract.extractall(build_dir)
|
|
|
|
|
2013-10-24 05:29:34 +00:00
|
|
|
return build_dir
|
2013-10-24 04:05:58 +00:00
|
|
|
|
|
|
|
|
2013-10-24 05:29:34 +00:00
|
|
|
def prepare_dockerfile(request_file):
|
|
|
|
build_dir = mkdtemp(prefix='docker-build-')
|
2013-10-24 04:05:58 +00:00
|
|
|
dockerfile_path = os.path.join(build_dir, "Dockerfile")
|
2013-10-25 05:14:38 +00:00
|
|
|
with open(dockerfile_path, 'w') as dockerfile:
|
|
|
|
dockerfile.write(request_file.content)
|
2013-10-24 05:29:34 +00:00
|
|
|
|
|
|
|
return build_dir
|
|
|
|
|
|
|
|
|
|
|
|
def build_image(build_dir, tag_name, num_steps, result_object):
|
|
|
|
try:
|
|
|
|
logger.debug('Does this show up?')
|
|
|
|
docker_cl = docker.Client(version='1.5')
|
|
|
|
result_object['status'] = 'building'
|
|
|
|
build_status = docker_cl.build(path=build_dir, tag=tag_name)
|
|
|
|
|
|
|
|
current_step = 0
|
|
|
|
built_image = None
|
|
|
|
for status in build_status:
|
|
|
|
step_increment = re.search(r'Step ([0-9]+) :', status)
|
|
|
|
if step_increment:
|
|
|
|
current_step = int(step_increment.group(1))
|
|
|
|
logger.debug('Step now: %s/%s' % (current_step, num_steps))
|
|
|
|
result_object['current_command'] = current_step
|
|
|
|
continue
|
|
|
|
|
|
|
|
complete = re.match(r'Successfully built ([a-z0-9]+)$', status)
|
|
|
|
if complete:
|
|
|
|
built_image = complete.group(1)
|
|
|
|
logger.debug('Final image ID is: %s' % built_image)
|
2013-10-24 04:05:58 +00:00
|
|
|
continue
|
|
|
|
|
2013-10-24 05:29:34 +00:00
|
|
|
shutil.rmtree(build_dir)
|
|
|
|
|
|
|
|
# Get the image count
|
|
|
|
if not built_image:
|
|
|
|
result_object['status'] = 'error'
|
|
|
|
result_object['message'] = 'Unable to build dockerfile.'
|
|
|
|
return
|
|
|
|
|
|
|
|
history = docker_cl.history(built_image)
|
|
|
|
num_images = len(history)
|
|
|
|
result_object['total_images'] = num_images
|
|
|
|
|
|
|
|
result_object['status'] = 'pushing'
|
|
|
|
logger.debug('Pushing to tag name: %s' % tag_name)
|
|
|
|
resp = docker_cl.push(tag_name)
|
|
|
|
|
|
|
|
current_image = 0
|
|
|
|
image_progress = 0
|
|
|
|
for status in resp:
|
|
|
|
if u'status' in status:
|
|
|
|
status_msg = status[u'status']
|
|
|
|
next_image = r'(Pushing|Image) [a-z0-9]+( already pushed, skipping)?$'
|
|
|
|
match = re.match(next_image, status_msg)
|
2013-10-24 04:05:58 +00:00
|
|
|
if match:
|
2013-10-24 05:29:34 +00:00
|
|
|
current_image += 1
|
|
|
|
image_progress = 0
|
|
|
|
logger.debug('Now pushing image %s/%s' %
|
|
|
|
(current_image, num_images))
|
|
|
|
|
|
|
|
elif status_msg == u'Pushing' and u'progress' in status:
|
|
|
|
percent = r'\(([0-9]+)%\)'
|
|
|
|
match = re.search(percent, status[u'progress'])
|
|
|
|
if match:
|
|
|
|
image_progress = int(match.group(1))
|
2013-10-24 04:05:58 +00:00
|
|
|
|
2013-10-24 05:29:34 +00:00
|
|
|
result_object['current_image'] = current_image
|
|
|
|
result_object['image_completion_percent'] = image_progress
|
2013-10-24 04:05:58 +00:00
|
|
|
|
2013-10-24 05:29:34 +00:00
|
|
|
elif u'errorDetail' in status:
|
|
|
|
result_object['status'] = 'error'
|
|
|
|
if u'message' in status[u'errorDetail']:
|
|
|
|
result_object['message'] = status[u'errorDetail'][u'message']
|
|
|
|
return
|
2013-10-24 04:05:58 +00:00
|
|
|
|
2013-10-24 05:29:34 +00:00
|
|
|
result_object['status'] = 'complete'
|
|
|
|
except Exception as e:
|
|
|
|
logger.exception('Exception when processing request.')
|
|
|
|
result_object['status'] = 'error'
|
|
|
|
result_object['message'] = e.message
|
|
|
|
|
|
|
|
|
2013-10-24 18:57:05 +00:00
|
|
|
MIME_PROCESSORS = {
|
|
|
|
'application/zip': prepare_zip,
|
|
|
|
'text/plain': prepare_dockerfile,
|
|
|
|
'application/octet-stream': prepare_dockerfile,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
builds = {}
|
|
|
|
pool = ThreadPool(1)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/build/', methods=['POST'])
|
2013-10-24 05:29:34 +00:00
|
|
|
def start_build():
|
2013-10-25 05:14:38 +00:00
|
|
|
resource_url = request.values['resource_url']
|
2013-10-24 05:29:34 +00:00
|
|
|
tag_name = request.values['tag']
|
|
|
|
|
2013-10-25 05:14:38 +00:00
|
|
|
download_resource = requests.get(resource_url)
|
|
|
|
download_resource.get()
|
|
|
|
|
2013-10-24 05:29:34 +00:00
|
|
|
logger.info('Request to build file of type: %s with tag: %s' %
|
|
|
|
(c_type, tag_name))
|
|
|
|
|
|
|
|
if c_type not in MIME_PROCESSORS:
|
|
|
|
logger.error('Invalid dockerfile content type: %s' % c_type)
|
|
|
|
abort(400)
|
|
|
|
|
|
|
|
build_dir = MIME_PROCESSORS[c_type](docker_input)
|
|
|
|
|
|
|
|
dockerfile_path = os.path.join(build_dir, "Dockerfile")
|
|
|
|
num_steps = count_steps(dockerfile_path)
|
|
|
|
logger.debug('Dockerfile had %s steps' % num_steps)
|
|
|
|
|
|
|
|
job_id = str(uuid4())
|
|
|
|
logger.info('Sending job to builder pool: %s' % job_id)
|
|
|
|
|
|
|
|
result_object = {
|
|
|
|
'id': job_id,
|
|
|
|
'total_commands': num_steps,
|
|
|
|
'total_images': None,
|
|
|
|
'current_command': 0,
|
|
|
|
'current_image': 0,
|
|
|
|
'image_completion_percent': 0,
|
|
|
|
'status': 'waiting',
|
|
|
|
'message': None,
|
|
|
|
}
|
|
|
|
builds[job_id] = result_object
|
|
|
|
pool.apply_async(build_image, [build_dir, tag_name, num_steps,
|
|
|
|
result_object])
|
|
|
|
|
2013-10-25 05:14:38 +00:00
|
|
|
resp = make_response('Created', 201)
|
|
|
|
resp.headers['Location'] = url_for('get_status', job_id=job_id)
|
|
|
|
return resp
|
2013-10-24 05:29:34 +00:00
|
|
|
|
|
|
|
|
2013-10-24 18:57:05 +00:00
|
|
|
@app.route('/build/<job_id>')
|
2013-10-24 05:29:34 +00:00
|
|
|
def get_status(job_id):
|
|
|
|
if job_id not in builds:
|
|
|
|
abort(400)
|
|
|
|
|
|
|
|
return jsonify(builds[job_id])
|
2013-10-24 04:05:58 +00:00
|
|
|
|
|
|
|
|
2013-10-24 18:57:05 +00:00
|
|
|
@app.route('/build/', methods=['GET'])
|
|
|
|
def get_all_status():
|
|
|
|
return jsonify({
|
|
|
|
'builds': builds,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2013-10-24 04:05:58 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT)
|
2013-10-24 20:37:03 +00:00
|
|
|
app.run(host='0.0.0.0', port=5002, debug=True)
|