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-03-17 20:57:35 +00:00
|
|
|
format_date, NotFound)
|
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
|
|
|
|
|
|
|
|
|
|
|
def image_view(image):
|
|
|
|
extended_props = image
|
|
|
|
if image.storage and image.storage.id:
|
|
|
|
extended_props = image.storage
|
|
|
|
|
|
|
|
command = extended_props.command
|
|
|
|
return {
|
|
|
|
'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,
|
|
|
|
'ancestors': image.ancestors,
|
|
|
|
'dbid': image.id,
|
|
|
|
'size': extended_props.image_size,
|
2014-06-28 00:04:26 +00:00
|
|
|
'locations': list(image.storage.locations),
|
2014-08-13 21:54:15 +00:00
|
|
|
'uploading': image.storage.uploading,
|
2014-03-14 17:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-19 19:39:44 +00:00
|
|
|
@resource('/v1/repository/<repopath:repository>/image/')
|
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. """
|
|
|
|
all_images = model.get_repository_images(namespace, repository)
|
|
|
|
all_tags = model.list_repository_tags(namespace, repository)
|
|
|
|
|
|
|
|
tags_by_image_id = defaultdict(list)
|
|
|
|
for tag in all_tags:
|
|
|
|
tags_by_image_id[tag.image.docker_image_id].append(tag.name)
|
|
|
|
|
|
|
|
|
|
|
|
def add_tags(image_json):
|
|
|
|
image_json['tags'] = tags_by_image_id[image_json['id']]
|
|
|
|
return image_json
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
'images': [add_tags(image_view(image)) for image in all_images]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-19 19:39:44 +00:00
|
|
|
@resource('/v1/repository/<repopath:repository>/image/<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. """
|
2014-03-14 17:06:58 +00:00
|
|
|
image = model.get_repo_image(namespace, repository, image_id)
|
|
|
|
if not image:
|
2014-03-17 20:57:35 +00:00
|
|
|
raise NotFound()
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
return image_view(image)
|
|
|
|
|
|
|
|
|
2014-03-19 19:39:44 +00:00
|
|
|
@resource('/v1/repository/<repopath:repository>/image/<image_id>/changes')
|
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. """
|
2014-03-14 17:06:58 +00:00
|
|
|
image = model.get_repo_image(namespace, repository, image_id)
|
|
|
|
|
|
|
|
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()
|