Populate image storage objects with locations everywhere.

This commit is contained in:
Jake Moshenko 2014-06-27 20:04:26 -04:00
parent e68d6a7302
commit 684c26bc1a
4 changed files with 40 additions and 49 deletions

View file

@ -845,27 +845,14 @@ def get_repository(namespace_name, repository_name):
def get_repo_image(namespace_name, repository_name, image_id): def get_repo_image(namespace_name, repository_name, image_id):
location_list = list((ImageStoragePlacement def limit_to_image_id(query):
.select(ImageStoragePlacement, Image, ImageStorage, ImageStorageLocation) return query.where(Image.docker_image_id == image_id)
.join(ImageStorageLocation)
.switch(ImageStoragePlacement)
.join(ImageStorage)
.join(Image)
.join(Repository)
.where(Repository.name == repository_name,
Repository.namespace == namespace_name,
Image.docker_image_id == image_id)))
if not location_list: images = _get_repository_images_base(namespace_name, repository_name, limit_to_image_id)
if not images:
return None return None
else:
location_names = {location.location.name for location in location_list} return images[0]
image = location_list[0].storage.image
image.storage = location_list[0].storage
image.storage.locations = location_names
return image
def repository_is_public(namespace_name, repository_name): def repository_is_public(namespace_name, repository_name):
@ -1082,8 +1069,8 @@ def set_image_metadata(docker_image_id, namespace_name, repository_name,
return fetched return fetched
def get_repository_images(namespace_name, repository_name): def _get_repository_images_base(namespace_name, repository_name, query_modifier):
location_list = list((ImageStoragePlacement query = (ImageStoragePlacement
.select(ImageStoragePlacement, Image, ImageStorage, ImageStorageLocation) .select(ImageStoragePlacement, Image, ImageStorage, ImageStorageLocation)
.join(ImageStorageLocation) .join(ImageStorageLocation)
.switch(ImageStoragePlacement) .switch(ImageStoragePlacement)
@ -1091,26 +1078,33 @@ def get_repository_images(namespace_name, repository_name):
.join(Image) .join(Image)
.join(Repository) .join(Repository)
.where(Repository.name == repository_name, .where(Repository.name == repository_name,
Repository.namespace == namespace_name))) Repository.namespace == namespace_name))
query = query_modifier(query)
location_list = list(query)
images = {} images = {}
for location in location_list: for location in location_list:
# Make sure we're always retrieving the same image object. # Make sure we're always retrieving the same image object.
image = location.storage.image image = location.storage.image
image_id = image.docker_image_id
if not image_id in images: if not image.id in images:
images[image_id] = image images[image.id] = image
image.storage.locations = set() image.storage.locations = set()
else: else:
image = images[image_id] image = images[image.id]
# Add the location to the image's locations set. # Add the location to the image's locations set.
image.storage.locations.add(location) image.storage.locations.add(location.location.name)
return images.values() return images.values()
def get_repository_images(namespace_name, repository_name):
return _get_repository_images_base(namespace_name, repository_name, lambda q: q)
def list_repository_tags(namespace_name, repository_name): def list_repository_tags(namespace_name, repository_name):
select = RepositoryTag.select(RepositoryTag, Image) select = RepositoryTag.select(RepositoryTag, Image)
with_repo = select.join(Repository) with_repo = select.join(Repository)
@ -1178,20 +1172,17 @@ def garbage_collect_repository(namespace_name, repository_name):
def get_tag_image(namespace_name, repository_name, tag_name): def get_tag_image(namespace_name, repository_name, tag_name):
query = (Image def limit_to_tag(query):
.select(Image, ImageStorage) return (query
.join(RepositoryTag) .switch(Image)
.join(Repository) .join(RepositoryTag)
.switch(Image) .where(RepositoryTag.name == tag_name))
.join(ImageStorage, JOIN_LEFT_OUTER)
.where(Repository.name == repository_name,
Repository.namespace == namespace_name,
RepositoryTag.name == tag_name))
try: images = _get_repository_images_base(namespace_name, repository_name, limit_to_tag)
return query.get() if not images:
except Image.DoesNotExist:
raise DataModelException('Unable to find image for tag.') raise DataModelException('Unable to find image for tag.')
else:
return images[0]
def get_image_by_id(namespace_name, repository_name, docker_image_id): def get_image_by_id(namespace_name, repository_name, docker_image_id):
@ -1212,7 +1203,7 @@ def get_image_by_id(namespace_name, repository_name, docker_image_id):
repository_name)) repository_name))
def get_parent_images(image_obj): def get_parent_images(namespace_name, repository_name, image_obj):
""" Returns a list of parent Image objects in chronilogical order. """ """ Returns a list of parent Image objects in chronilogical order. """
parents = image_obj.ancestors parents = image_obj.ancestors
parent_db_ids = parents.strip('/').split('/') parent_db_ids = parents.strip('/').split('/')
@ -1220,12 +1211,12 @@ def get_parent_images(image_obj):
if parent_db_ids == ['']: if parent_db_ids == ['']:
return [] return []
parent_images = (Image def filter_to_parents(query):
.select(Image, ImageStorage) return query.where(Image.id << parent_db_ids)
.join(ImageStorage, JOIN_LEFT_OUTER)
.where(Image.id << parent_db_ids))
id_to_image = {unicode(image.id): image for image in parent_images} parents = _get_repository_images_base(namespace_name, repository_name, filter_to_parents)
id_to_image = {unicode(image.id): image for image in parents}
return [id_to_image[parent_id] for parent_id in parent_db_ids] return [id_to_image[parent_id] for parent_id in parent_db_ids]

View file

@ -23,7 +23,7 @@ def image_view(image):
'ancestors': image.ancestors, 'ancestors': image.ancestors,
'dbid': image.id, 'dbid': image.id,
'size': extended_props.image_size, 'size': extended_props.image_size,
'locations': [l.location.name for l in image.storage.locations] 'locations': list(image.storage.locations),
} }

View file

@ -84,7 +84,7 @@ class RepositoryTagImages(RepositoryParamResource):
except model.DataModelException: except model.DataModelException:
raise NotFound() raise NotFound()
parent_images = model.get_parent_images(tag_image) parent_images = model.get_parent_images(namespace, repository, tag_image)
parents = list(parent_images) parents = list(parent_images)
parents.reverse() parents.reverse()

View file

@ -449,7 +449,7 @@ def process_image_changes(namespace, repository, image_id):
return image_trie_path return image_trie_path
image = model.get_image_by_id(namespace, repository, image_id) image = model.get_image_by_id(namespace, repository, image_id)
parents = model.get_parent_images(image) parents = model.get_parent_images(namespace, repository, image)
# Compute the diffs and fs for the parent first if necessary # Compute the diffs and fs for the parent first if necessary
parent_trie_path = None parent_trie_path = None