Move the creation of images to when the JSON is uploaded.

This commit is contained in:
Jake Moshenko 2015-02-13 16:28:45 -05:00
parent 3cae6609a7
commit 59b794dd61
3 changed files with 12 additions and 33 deletions

View file

@ -1628,8 +1628,6 @@ def garbage_collect_repository(namespace_name, repository_name):
logger.info('Garbage collecting storage for images: %s', to_remove)
_garbage_collect_storage(storage_id_whitelist)
return len(to_remove)
def _garbage_collect_storage(storage_id_whitelist):
if len(storage_id_whitelist) == 0:

View file

@ -222,32 +222,6 @@ def create_repository(namespace, repository):
repo = model.create_repository(namespace, repository,
get_authenticated_user())
logger.debug('Determining already added images')
added_images = OrderedDict([(desc['id'], desc) for desc in image_descriptions])
new_repo_images = dict(added_images)
# Optimization: Lookup any existing images in the repository with matching docker IDs and
# remove them from the added dict, so we don't need to look them up one-by-one.
def chunks(l, n):
for i in xrange(0, len(l), n):
yield l[i:i+n]
# Note: We do this in chunks in an effort to not hit the SQL query size limit.
for chunk in chunks(new_repo_images.keys(), 50):
existing_images = model.lookup_repository_images(namespace, repository, chunk)
for existing in existing_images:
added_images.pop(existing.docker_image_id)
logger.debug('Creating/Linking necessary images')
username = get_authenticated_user() and get_authenticated_user().username
translations = {}
for image_description in added_images.values():
model.find_create_or_link_image(image_description['id'], repo, username,
translations, storage.preferred_locations[0])
logger.debug('Created images')
track_and_log('push_repo', repo)
return make_response('Created', 201)
@ -280,7 +254,7 @@ def update_images(namespace, repository):
event.publish_event_data('docker-cli', user_data)
logger.debug('GCing repository')
num_removed = model.garbage_collect_repository(namespace, repository)
model.garbage_collect_repository(namespace, repository)
# Generate a job for each notification that has been added to this repo
logger.debug('Adding notifications for repository')
@ -288,8 +262,8 @@ def update_images(namespace, repository):
updated_tags = session.get('pushed_tags', {})
event_data = {
'updated_tags': updated_tags,
'pruned_image_count': num_removed
}
track_and_log('push_repo', repo)
spawn_notification(repo, 'repo_push', event_data)
return make_response('Updated', 204)

View file

@ -9,6 +9,7 @@ from time import time
from app import storage as store, image_diff_queue, app
from auth.auth import process_auth, extract_namespace_repo_from_session
from auth.auth_context import get_authenticated_user
from util import checksums, changes
from util.http import abort, exact_abort
from auth.permissions import (ReadRepositoryPermission,
@ -456,9 +457,15 @@ def put_image_json(namespace, repository, image_id):
logger.debug('Looking up repo image')
repo_image = model.get_repo_image_extended(namespace, repository, image_id)
if not repo_image:
logger.debug('Image not found')
abort(404, 'Image %(image_id)s not found', issue='unknown-image',
image_id=image_id)
logger.debug('Image not found, creating image')
repo = model.get_repository(namespace, repository)
if repo is None:
abort(404, 'Repository does not exist: %(namespace)s/%(repository)s', issue='no-repo',
namespace=namespace, repository=repository)
username = get_authenticated_user() and get_authenticated_user().username
repo_image = model.find_create_or_link_image(image_id, repo, username, {},
store.preferred_locations[0])
uuid = repo_image.storage.uuid