115 lines
No EOL
2.9 KiB
Python
115 lines
No EOL
2.9 KiB
Python
import docker
|
|
import logging
|
|
import shutil
|
|
import os
|
|
import re
|
|
|
|
from flask import Flask, request, send_file, make_response, jsonify
|
|
from zipfile import ZipFile
|
|
from tempfile import TemporaryFile, mkdtemp
|
|
|
|
|
|
BUFFER_SIZE = 8 * 1024
|
|
LOG_FORMAT = '%(asctime)-15s - %(levelname)s - %(pathname)s - ' + \
|
|
'%(funcName)s - %(message)s'
|
|
|
|
app = Flask(__name__)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def count_steps(dockerfileobj):
|
|
steps = 0
|
|
for line in dockerfileobj.readlines():
|
|
stripped = line.strip()
|
|
if stripped and stripped[0] is not '#':
|
|
steps += 1
|
|
return steps
|
|
|
|
@app.route('/')
|
|
def index():
|
|
return send_file('test.html')
|
|
|
|
|
|
@app.route('/start/zip/<path:tag_name>', methods=['POST'])
|
|
def start_build(tag_name):
|
|
docker_zip = request.files['dockerfile']
|
|
|
|
build_dir = mkdtemp(prefix='docker-build-')
|
|
|
|
# Save the zip file to temp somewhere
|
|
with TemporaryFile() as zip_file:
|
|
docker_zip.save(zip_file)
|
|
to_extract = ZipFile(zip_file)
|
|
to_extract.extractall(build_dir)
|
|
|
|
docker_cl = docker.Client(version='1.5')
|
|
|
|
build_status = docker_cl.build(path=build_dir, tag=tag_name)
|
|
|
|
dockerfile_path = os.path.join(build_dir, "Dockerfile")
|
|
with open(dockerfile_path, 'r') as dockerfileobj:
|
|
num_steps = count_steps(dockerfileobj)
|
|
logger.debug('Dockerfile had %s steps' % num_steps)
|
|
|
|
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))
|
|
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)
|
|
continue
|
|
|
|
shutil.rmtree(build_dir)
|
|
|
|
# Get the image count
|
|
if not built_image:
|
|
abort(500)
|
|
|
|
history = docker_cl.history(built_image)
|
|
num_images = len(history)
|
|
|
|
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)
|
|
if match:
|
|
current_image += 1
|
|
image_progress = 0
|
|
logger.debug('Now pushing image %s/%s' % (current_image, num_images))
|
|
continue
|
|
|
|
if 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))
|
|
continue
|
|
|
|
return jsonify({
|
|
'images_pushed': num_images,
|
|
'commands_run': num_steps,
|
|
})
|
|
|
|
|
|
@app.route('/status')
|
|
def get_status():
|
|
return 'building'
|
|
|
|
|
|
if __name__ == '__main__':
|
|
logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT)
|
|
app.run(host='0.0.0.0', port=5002, debug=True) |