2014-09-16 04:18:57 +00:00
|
|
|
import logging
|
|
|
|
import json
|
2014-09-19 16:22:54 +00:00
|
|
|
import hashlib
|
2014-09-16 04:18:57 +00:00
|
|
|
|
2014-10-29 19:42:44 +00:00
|
|
|
from flask import redirect, Blueprint, abort, send_file, request
|
2014-09-16 04:18:57 +00:00
|
|
|
|
2014-10-08 20:54:03 +00:00
|
|
|
from app import app
|
2014-09-16 04:18:57 +00:00
|
|
|
from auth.auth import process_auth
|
2014-10-29 19:42:44 +00:00
|
|
|
from auth.auth_context import get_authenticated_user
|
2014-09-16 04:18:57 +00:00
|
|
|
from auth.permissions import ReadRepositoryPermission
|
|
|
|
from data import model
|
2014-09-18 21:26:40 +00:00
|
|
|
from data import database
|
2014-10-30 16:49:51 +00:00
|
|
|
from endpoints.trackhelper import track_and_log
|
2014-10-08 20:54:03 +00:00
|
|
|
from storage import Storage
|
2014-09-16 04:18:57 +00:00
|
|
|
|
2014-09-17 02:43:19 +00:00
|
|
|
from util.queuefile import QueueFile
|
|
|
|
from util.queueprocess import QueueProcess
|
2014-09-16 15:53:54 +00:00
|
|
|
from util.gzipwrap import GzipWrap
|
2014-09-19 16:22:54 +00:00
|
|
|
from util.dockerloadformat import build_docker_load_stream
|
2014-09-16 04:18:57 +00:00
|
|
|
|
|
|
|
verbs = Blueprint('verbs', __name__)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2014-11-06 22:50:48 +00:00
|
|
|
def _open_stream(namespace, repository, tag, synthetic_image_id, image_json, image_id_list):
|
2014-10-08 20:54:03 +00:00
|
|
|
store = Storage(app)
|
|
|
|
|
2014-11-06 22:50:48 +00:00
|
|
|
# For performance reasons, we load the full image list here, cache it, then disconnect from
|
|
|
|
# the database.
|
|
|
|
with database.UseThenDisconnect(app.config):
|
|
|
|
image_list = model.get_matching_repository_images(namespace, repository, image_id_list)
|
2014-11-05 17:27:38 +00:00
|
|
|
|
2014-09-19 16:22:54 +00:00
|
|
|
def get_next_image():
|
2014-11-06 22:50:48 +00:00
|
|
|
for current_image in image_list:
|
|
|
|
yield current_image
|
2014-09-19 16:22:54 +00:00
|
|
|
|
2014-09-17 02:43:19 +00:00
|
|
|
def get_next_layer():
|
2014-11-06 22:50:48 +00:00
|
|
|
for current_image_entry in image_list:
|
2014-09-17 02:43:19 +00:00
|
|
|
current_image_path = store.image_layer_path(current_image_entry.storage.uuid)
|
|
|
|
current_image_stream = store.stream_read_file(current_image_entry.storage.locations,
|
|
|
|
current_image_path)
|
|
|
|
|
2014-11-06 22:50:48 +00:00
|
|
|
current_image_id = current_image_entry.id
|
2014-09-17 02:43:19 +00:00
|
|
|
logger.debug('Returning image layer %s: %s' % (current_image_id, current_image_path))
|
|
|
|
yield current_image_stream
|
|
|
|
|
2014-09-19 16:22:54 +00:00
|
|
|
stream = build_docker_load_stream(namespace, repository, tag, synthetic_image_id, image_json,
|
|
|
|
get_next_image, get_next_layer)
|
|
|
|
|
2014-09-17 02:43:19 +00:00
|
|
|
return stream.read
|
|
|
|
|
2014-09-18 21:26:40 +00:00
|
|
|
|
|
|
|
def _write_synthetic_image_to_storage(linked_storage_uuid, linked_locations, queue_file):
|
2014-10-08 20:54:03 +00:00
|
|
|
store = Storage(app)
|
2014-10-08 17:43:12 +00:00
|
|
|
|
|
|
|
def handle_exception(ex):
|
2014-10-16 16:54:16 +00:00
|
|
|
logger.debug('Exception when building squashed image %s: %s', linked_storage_uuid, ex)
|
2014-11-06 22:50:48 +00:00
|
|
|
|
|
|
|
with database.UseThenDisconnect(app.config):
|
|
|
|
model.delete_derived_storage_by_uuid(linked_storage_uuid)
|
2014-10-08 17:43:12 +00:00
|
|
|
|
|
|
|
queue_file.add_exception_handler(handle_exception)
|
|
|
|
|
2014-09-18 21:26:40 +00:00
|
|
|
image_path = store.image_layer_path(linked_storage_uuid)
|
|
|
|
store.stream_write(linked_locations, image_path, queue_file)
|
2014-09-17 02:43:19 +00:00
|
|
|
queue_file.close()
|
2014-09-16 04:18:57 +00:00
|
|
|
|
2014-10-08 17:43:12 +00:00
|
|
|
if not queue_file.raised_exception:
|
2014-11-06 22:50:48 +00:00
|
|
|
with database.UseThenDisconnect(app.config):
|
|
|
|
done_uploading = model.get_storage_by_uuid(linked_storage_uuid)
|
|
|
|
done_uploading.uploading = False
|
|
|
|
done_uploading.save()
|
2014-09-18 21:26:40 +00:00
|
|
|
|
|
|
|
|
2014-09-19 16:54:52 +00:00
|
|
|
@verbs.route('/squash/<namespace>/<repository>/<tag>', methods=['GET'])
|
2014-09-16 04:18:57 +00:00
|
|
|
@process_auth
|
2014-09-18 21:26:40 +00:00
|
|
|
def get_squashed_tag(namespace, repository, tag):
|
2014-09-16 04:18:57 +00:00
|
|
|
permission = ReadRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can() or model.repository_is_public(namespace, repository):
|
|
|
|
# Lookup the requested tag.
|
2014-10-15 01:40:02 +00:00
|
|
|
try:
|
|
|
|
tag_image = model.get_tag_image(namespace, repository, tag)
|
|
|
|
except model.DataModelException:
|
2014-09-16 04:18:57 +00:00
|
|
|
abort(404)
|
|
|
|
|
|
|
|
# Lookup the tag's image and storage.
|
2014-11-10 18:44:36 +00:00
|
|
|
repo_image = model.get_repo_image_extended(namespace, repository, tag_image.docker_image_id)
|
2014-09-16 04:18:57 +00:00
|
|
|
if not repo_image:
|
|
|
|
abort(404)
|
2014-09-18 21:26:40 +00:00
|
|
|
|
2014-10-29 19:42:44 +00:00
|
|
|
# Log the action.
|
|
|
|
track_and_log('repo_verb', repo_image.repository, tag=tag, verb='squash')
|
|
|
|
|
2014-10-08 20:54:03 +00:00
|
|
|
store = Storage(app)
|
2014-09-18 21:26:40 +00:00
|
|
|
derived = model.find_or_create_derived_storage(repo_image.storage, 'squash',
|
|
|
|
store.preferred_locations[0])
|
|
|
|
if not derived.uploading:
|
|
|
|
logger.debug('Derived image %s exists in storage', derived.uuid)
|
|
|
|
derived_layer_path = store.image_layer_path(derived.uuid)
|
|
|
|
download_url = store.get_direct_download_url(derived.locations, derived_layer_path)
|
2014-09-17 02:43:19 +00:00
|
|
|
if download_url:
|
2014-09-18 21:26:40 +00:00
|
|
|
logger.debug('Redirecting to download URL for derived image %s', derived.uuid)
|
|
|
|
return redirect(download_url)
|
2014-09-17 02:43:19 +00:00
|
|
|
|
2014-11-06 23:00:52 +00:00
|
|
|
# Close the database handle here for this process before we send the long download.
|
|
|
|
database.close_db_filter(None)
|
|
|
|
|
2014-09-18 21:26:40 +00:00
|
|
|
logger.debug('Sending cached derived image %s', derived.uuid)
|
|
|
|
return send_file(store.stream_read_file(derived.locations, derived_layer_path))
|
2014-09-17 02:43:19 +00:00
|
|
|
|
2014-09-16 04:18:57 +00:00
|
|
|
# Load the ancestry for the image.
|
2014-09-18 21:26:40 +00:00
|
|
|
logger.debug('Building and returning derived image %s', derived.uuid)
|
2014-09-16 04:18:57 +00:00
|
|
|
uuid = repo_image.storage.uuid
|
|
|
|
ancestry_data = store.get_content(repo_image.storage.locations, store.image_ancestry_path(uuid))
|
|
|
|
full_image_list = json.loads(ancestry_data)
|
2014-09-18 21:26:40 +00:00
|
|
|
|
2014-09-19 16:22:54 +00:00
|
|
|
# Load the image's JSON layer.
|
|
|
|
image_json_data = store.get_content(repo_image.storage.locations, store.image_json_path(uuid))
|
|
|
|
image_json = json.loads(image_json_data)
|
|
|
|
|
|
|
|
# Calculate a synthetic image ID.
|
|
|
|
synthetic_image_id = hashlib.sha256(tag_image.docker_image_id + ':squash').hexdigest()
|
|
|
|
|
2014-09-17 02:43:19 +00:00
|
|
|
# Create a queue process to generate the data. The queue files will read from the process
|
|
|
|
# and send the results to the client and storage.
|
2014-10-21 21:40:57 +00:00
|
|
|
def _cleanup():
|
|
|
|
# Close any existing DB connection once the process has exited.
|
|
|
|
database.close_db_filter(None)
|
|
|
|
|
2014-09-19 16:22:54 +00:00
|
|
|
args = (namespace, repository, tag, synthetic_image_id, image_json, full_image_list)
|
2014-10-21 21:40:57 +00:00
|
|
|
queue_process = QueueProcess(_open_stream,
|
|
|
|
8 * 1024, 10 * 1024 * 1024, # 8K/10M chunk/max
|
|
|
|
args, finished=_cleanup)
|
2014-09-17 02:43:19 +00:00
|
|
|
|
|
|
|
client_queue_file = QueueFile(queue_process.create_queue(), 'client')
|
|
|
|
storage_queue_file = QueueFile(queue_process.create_queue(), 'storage')
|
2014-09-18 21:26:40 +00:00
|
|
|
|
2014-09-17 02:43:19 +00:00
|
|
|
# Start building.
|
|
|
|
queue_process.run()
|
|
|
|
|
|
|
|
# Start the storage saving.
|
2014-09-18 21:26:40 +00:00
|
|
|
storage_args = (derived.uuid, derived.locations, storage_queue_file)
|
2014-10-21 21:40:57 +00:00
|
|
|
QueueProcess.run_process(_write_synthetic_image_to_storage, storage_args, finished=_cleanup)
|
2014-09-16 04:18:57 +00:00
|
|
|
|
2014-11-06 22:50:48 +00:00
|
|
|
# Close the database handle here for this process before we send the long download.
|
|
|
|
database.close_db_filter(None)
|
2014-11-05 17:27:38 +00:00
|
|
|
|
2014-09-17 02:43:19 +00:00
|
|
|
# Return the client's data.
|
|
|
|
return send_file(client_queue_file)
|
2014-09-16 04:18:57 +00:00
|
|
|
|
|
|
|
abort(403)
|