2015-12-23 18:08:01 +00:00
|
|
|
""" List and lookup repository images. """
|
2018-08-20 18:37:03 +00:00
|
|
|
import json
|
2015-05-14 20:47:38 +00:00
|
|
|
|
2018-08-20 18:37:03 +00:00
|
|
|
from data.registry_model import registry_model
|
2014-03-17 17:10:12 +00:00
|
|
|
from endpoints.api import (resource, nickname, require_repo_read, RepositoryParamResource,
|
2018-08-20 18:37:03 +00:00
|
|
|
path_param, disallow_for_app_repositories, format_date)
|
2016-04-11 20:20:11 +00:00
|
|
|
from endpoints.exception import NotFound
|
2015-03-20 21:46:02 +00:00
|
|
|
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2018-08-20 20:47:38 +00:00
|
|
|
def image_dict(image, with_history=False, with_tags=False):
|
2019-02-18 18:02:46 +00:00
|
|
|
parsed_command = None
|
|
|
|
if image.command:
|
|
|
|
try:
|
|
|
|
parsed_command = json.loads(image.command)
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
parsed_command = {'error': 'Could not parse command'}
|
|
|
|
|
2018-08-20 18:37:03 +00:00
|
|
|
image_data = {
|
|
|
|
'id': image.docker_image_id,
|
|
|
|
'created': format_date(image.created),
|
|
|
|
'comment': image.comment,
|
2019-02-18 18:02:46 +00:00
|
|
|
'command': parsed_command,
|
2018-08-20 18:37:03 +00:00
|
|
|
'size': image.image_size,
|
|
|
|
'uploading': image.uploading,
|
|
|
|
'sort_index': len(image.parents),
|
|
|
|
}
|
|
|
|
|
|
|
|
if with_tags:
|
|
|
|
image_data['tags'] = [tag.name for tag in image.tags]
|
|
|
|
|
|
|
|
if with_history:
|
2018-08-24 19:27:13 +00:00
|
|
|
image_data['history'] = [image_dict(parent) for parent in image.parents]
|
2018-08-20 18:37:03 +00:00
|
|
|
|
|
|
|
# Calculate the ancestors string, with the DBID's replaced with the docker IDs.
|
|
|
|
parent_docker_ids = [parent_image.docker_image_id for parent_image in image.parents]
|
|
|
|
image_data['ancestors'] = '/{0}/'.format('/'.join(parent_docker_ids))
|
|
|
|
return image_data
|
|
|
|
|
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@resource('/v1/repository/<apirepopath: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. """
|
2017-07-10 13:46:02 +00:00
|
|
|
|
2014-03-14 17:06:58 +00:00
|
|
|
@require_repo_read
|
|
|
|
@nickname('listRepositoryImages')
|
2017-03-22 18:30:13 +00:00
|
|
|
@disallow_for_app_repositories
|
2014-03-14 17:06:58 +00:00
|
|
|
def get(self, namespace, repository):
|
|
|
|
""" List the images for the specified repository. """
|
2018-08-20 18:37:03 +00:00
|
|
|
repo_ref = registry_model.lookup_repository(namespace, repository)
|
|
|
|
if repo_ref is None:
|
2015-07-31 20:31:29 +00:00
|
|
|
raise NotFound()
|
|
|
|
|
2018-08-20 18:37:03 +00:00
|
|
|
images = registry_model.get_legacy_images(repo_ref)
|
2018-08-20 20:47:38 +00:00
|
|
|
return {'images': [image_dict(image, with_tags=True) for image in images]}
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@resource('/v1/repository/<apirepopath: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. """
|
2017-07-10 13:46:02 +00:00
|
|
|
|
2014-03-14 17:06:58 +00:00
|
|
|
@require_repo_read
|
|
|
|
@nickname('getImage')
|
2017-03-22 18:30:13 +00:00
|
|
|
@disallow_for_app_repositories
|
2014-03-14 17:06:58 +00:00
|
|
|
def get(self, namespace, repository, image_id):
|
2014-03-14 17:27:56 +00:00
|
|
|
""" Get the information available for the specified image. """
|
2018-08-20 18:37:03 +00:00
|
|
|
repo_ref = registry_model.lookup_repository(namespace, repository)
|
|
|
|
if repo_ref is None:
|
|
|
|
raise NotFound()
|
|
|
|
|
|
|
|
image = registry_model.get_legacy_image(repo_ref, image_id, include_parents=True)
|
2017-07-21 18:38:31 +00:00
|
|
|
if image is None:
|
2014-03-17 20:57:35 +00:00
|
|
|
raise NotFound()
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2018-08-20 20:47:38 +00:00
|
|
|
return image_dict(image, with_history=True)
|