2015-05-14 20:47:38 +00:00
|
|
|
""" List and lookup repository images, and download image diffs. """
|
|
|
|
|
2014-03-14 17:06:58 +00:00
|
|
|
import json
|
|
|
|
|
|
|
|
from collections import defaultdict
|
|
|
|
|
2014-04-03 21:31:46 +00:00
|
|
|
from app import storage as store
|
2014-03-17 17:10:12 +00:00
|
|
|
from endpoints.api import (resource, nickname, require_repo_read, RepositoryParamResource,
|
2014-08-19 23:05:28 +00:00
|
|
|
format_date, NotFound, path_param)
|
2014-03-14 17:06:58 +00:00
|
|
|
from data import model
|
2014-03-14 22:39:31 +00:00
|
|
|
from util.cache import cache_control_flask_restful
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
|
2015-07-31 20:31:29 +00:00
|
|
|
def image_view(image, image_map, include_ancestors=True):
|
2014-03-14 17:06:58 +00:00
|
|
|
extended_props = image
|
|
|
|
if image.storage and image.storage.id:
|
|
|
|
extended_props = image.storage
|
|
|
|
|
|
|
|
command = extended_props.command
|
2014-09-08 19:02:26 +00:00
|
|
|
|
2014-09-18 21:16:10 +00:00
|
|
|
def docker_id(aid):
|
2015-03-18 18:47:53 +00:00
|
|
|
if not aid or not aid in image_map:
|
2014-09-08 19:02:26 +00:00
|
|
|
return ''
|
|
|
|
|
2015-03-20 21:46:02 +00:00
|
|
|
return image_map[aid].docker_image_id
|
2014-09-08 19:02:26 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
image_data = {
|
2014-03-14 17:06:58 +00:00
|
|
|
'id': image.docker_image_id,
|
2014-03-17 17:10:12 +00:00
|
|
|
'created': format_date(extended_props.created),
|
2014-03-14 17:06:58 +00:00
|
|
|
'comment': extended_props.comment,
|
|
|
|
'command': json.loads(command) if command else None,
|
|
|
|
'size': extended_props.image_size,
|
2014-08-13 21:54:15 +00:00
|
|
|
'uploading': image.storage.uploading,
|
2015-03-20 21:46:02 +00:00
|
|
|
'sort_index': len(image.ancestors),
|
2014-03-14 17:06:58 +00:00
|
|
|
}
|
|
|
|
|
2015-03-20 21:46:02 +00:00
|
|
|
if include_ancestors:
|
|
|
|
# Calculate the ancestors string, with the DBID's replaced with the docker IDs.
|
|
|
|
ancestors = [docker_id(a) for a in image.ancestors.split('/')]
|
|
|
|
image_data['ancestors'] = '/'.join(ancestors)
|
|
|
|
|
|
|
|
return image_data
|
|
|
|
|
|
|
|
|
|
|
|
def historical_image_view(image, image_map):
|
|
|
|
ancestors = [image_map[a] for a in image.ancestors.split('/')[1:-1]]
|
|
|
|
normal_view = image_view(image, image_map)
|
2015-07-31 20:31:29 +00:00
|
|
|
normal_view['history'] = [image_view(parent, image_map, False) for parent in ancestors]
|
2015-03-20 21:46:02 +00:00
|
|
|
return normal_view
|
|
|
|
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2014-03-19 19:39:44 +00:00
|
|
|
@resource('/v1/repository/<repopath:repository>/image/')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
2014-03-14 17:06:58 +00:00
|
|
|
class RepositoryImageList(RepositoryParamResource):
|
|
|
|
""" Resource for listing repository images. """
|
|
|
|
@require_repo_read
|
|
|
|
@nickname('listRepositoryImages')
|
|
|
|
def get(self, namespace, repository):
|
|
|
|
""" List the images for the specified repository. """
|
2015-07-31 20:31:29 +00:00
|
|
|
repo = model.repository.get_repository(namespace, repository)
|
|
|
|
if not repo:
|
|
|
|
raise NotFound()
|
|
|
|
|
|
|
|
all_images = model.image.get_repository_images_without_placements(repo)
|
2015-07-15 21:25:41 +00:00
|
|
|
all_tags = model.tag.list_repository_tags(namespace, repository)
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
tags_by_image_id = defaultdict(list)
|
2015-03-18 18:47:53 +00:00
|
|
|
found_image_ids = set()
|
|
|
|
|
2014-03-14 17:06:58 +00:00
|
|
|
for tag in all_tags:
|
|
|
|
tags_by_image_id[tag.image.docker_image_id].append(tag.name)
|
2015-03-18 18:47:53 +00:00
|
|
|
found_image_ids.add(str(tag.image.id))
|
|
|
|
found_image_ids.update(tag.image.ancestors.split('/')[1:-1])
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2014-09-18 21:16:10 +00:00
|
|
|
image_map = {}
|
2015-03-18 18:47:53 +00:00
|
|
|
filtered_images = []
|
2014-09-18 21:16:10 +00:00
|
|
|
for image in all_images:
|
2015-03-18 18:47:53 +00:00
|
|
|
if str(image.id) in found_image_ids:
|
2015-03-20 21:46:02 +00:00
|
|
|
image_map[str(image.id)] = image
|
2015-03-18 18:47:53 +00:00
|
|
|
filtered_images.append(image)
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
def add_tags(image_json):
|
|
|
|
image_json['tags'] = tags_by_image_id[image_json['id']]
|
|
|
|
return image_json
|
|
|
|
|
|
|
|
return {
|
2015-03-18 18:47:53 +00:00
|
|
|
'images': [add_tags(image_view(image, image_map)) for image in filtered_images]
|
2014-03-14 17:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-19 19:39:44 +00:00
|
|
|
@resource('/v1/repository/<repopath:repository>/image/<image_id>')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
@path_param('image_id', 'The Docker image ID')
|
2014-03-14 17:06:58 +00:00
|
|
|
class RepositoryImage(RepositoryParamResource):
|
|
|
|
""" Resource for handling repository images. """
|
|
|
|
@require_repo_read
|
|
|
|
@nickname('getImage')
|
|
|
|
def get(self, namespace, repository, image_id):
|
2014-03-14 17:27:56 +00:00
|
|
|
""" Get the information available for the specified image. """
|
2015-07-15 21:25:41 +00:00
|
|
|
image = model.image.get_repo_image_extended(namespace, repository, image_id)
|
2014-03-14 17:06:58 +00:00
|
|
|
if not image:
|
2014-03-17 20:57:35 +00:00
|
|
|
raise NotFound()
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2014-09-18 21:16:10 +00:00
|
|
|
# Lookup all the ancestor images for the image.
|
|
|
|
image_map = {}
|
2015-07-15 21:25:41 +00:00
|
|
|
for current_image in model.image.get_parent_images(namespace, repository, image):
|
2015-03-20 21:46:02 +00:00
|
|
|
image_map[str(current_image.id)] = current_image
|
2014-09-18 21:16:10 +00:00
|
|
|
|
2015-03-20 21:46:02 +00:00
|
|
|
return historical_image_view(image, image_map)
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
|
2014-03-19 19:39:44 +00:00
|
|
|
@resource('/v1/repository/<repopath:repository>/image/<image_id>/changes')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
@path_param('image_id', 'The Docker image ID')
|
2014-03-14 17:06:58 +00:00
|
|
|
class RepositoryImageChanges(RepositoryParamResource):
|
|
|
|
""" Resource for handling repository image change lists. """
|
|
|
|
|
2014-03-14 22:39:31 +00:00
|
|
|
@cache_control_flask_restful(max_age=60*60) # Cache for one hour
|
2014-03-14 17:06:58 +00:00
|
|
|
@require_repo_read
|
|
|
|
@nickname('getImageChanges')
|
|
|
|
def get(self, namespace, repository, image_id):
|
2014-03-14 17:27:56 +00:00
|
|
|
""" Get the list of changes for the specified image. """
|
2015-07-15 21:25:41 +00:00
|
|
|
image = model.image.get_repo_image_extended(namespace, repository, image_id)
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
if not image:
|
2014-03-17 20:57:35 +00:00
|
|
|
raise NotFound()
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2014-06-11 19:37:45 +00:00
|
|
|
diffs_path = store.image_file_diffs_path(image.storage.uuid)
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
try:
|
2014-06-17 20:03:43 +00:00
|
|
|
response_json = json.loads(store.get_content(image.storage.locations, diffs_path))
|
2014-03-14 17:06:58 +00:00
|
|
|
return response_json
|
|
|
|
except IOError:
|
2014-03-17 20:57:35 +00:00
|
|
|
raise NotFound()
|