2015-05-14 20:47:38 +00:00
|
|
|
""" Manage the tags of a repository. """
|
|
|
|
|
2015-04-15 19:21:09 +00:00
|
|
|
from flask import request, abort
|
2014-03-26 23:42:29 +00:00
|
|
|
|
|
|
|
from endpoints.api import (resource, nickname, require_repo_read, require_repo_write,
|
2016-04-11 20:20:11 +00:00
|
|
|
RepositoryParamResource, log_action, validate_json_request,
|
2015-11-03 23:15:23 +00:00
|
|
|
path_param, parse_args, query_param, truthy_bool)
|
2016-04-11 20:20:11 +00:00
|
|
|
from endpoints.exception import NotFound
|
2014-03-14 17:06:58 +00:00
|
|
|
from endpoints.api.image import image_view
|
|
|
|
from data import model
|
|
|
|
from auth.auth_context import get_authenticated_user
|
2015-10-05 20:36:33 +00:00
|
|
|
from util.names import TAG_ERROR, TAG_REGEX
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2015-04-15 19:21:09 +00:00
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@resource('/v1/repository/<apirepopath:repository>/tag/')
|
2015-04-15 19:21:09 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
class ListRepositoryTags(RepositoryParamResource):
|
2015-06-30 08:38:58 +00:00
|
|
|
""" Resource for listing full repository tag history, alive *and dead*. """
|
2015-04-15 19:21:09 +00:00
|
|
|
|
2015-08-05 21:07:35 +00:00
|
|
|
@require_repo_read
|
2016-01-26 21:27:36 +00:00
|
|
|
@parse_args()
|
2015-04-16 21:18:00 +00:00
|
|
|
@query_param('specificTag', 'Filters the tags to the specific tag.', type=str, default='')
|
2015-06-30 08:38:58 +00:00
|
|
|
@query_param('limit', 'Limit to the number of results to return per page. Max 100.', type=int, default=50)
|
|
|
|
@query_param('page', 'Page index for the results. Default 1.', type=int, default=1)
|
2015-04-15 19:21:09 +00:00
|
|
|
@nickname('listRepoTags')
|
2016-01-26 21:27:36 +00:00
|
|
|
def get(self, namespace, repository, parsed_args):
|
2015-07-15 21:25:41 +00:00
|
|
|
repo = model.repository.get_repository(namespace, repository)
|
2015-04-15 19:21:09 +00:00
|
|
|
if not repo:
|
2015-07-14 08:34:45 +00:00
|
|
|
raise NotFound()
|
2015-04-15 19:21:09 +00:00
|
|
|
|
|
|
|
def tag_view(tag):
|
|
|
|
tag_info = {
|
|
|
|
'name': tag.name,
|
|
|
|
'docker_image_id': tag.image.docker_image_id,
|
2015-04-16 21:18:00 +00:00
|
|
|
'reversion': tag.reversion,
|
2015-04-15 19:21:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if tag.lifetime_start_ts > 0:
|
|
|
|
tag_info['start_ts'] = tag.lifetime_start_ts
|
|
|
|
|
|
|
|
if tag.lifetime_end_ts > 0:
|
|
|
|
tag_info['end_ts'] = tag.lifetime_end_ts
|
|
|
|
|
2017-03-01 23:22:40 +00:00
|
|
|
if tag.id in manifest_map:
|
|
|
|
tag_info['manifest_digest'] = manifest_map[tag.id].digest
|
|
|
|
|
2015-04-15 19:21:09 +00:00
|
|
|
return tag_info
|
|
|
|
|
2016-01-26 21:27:36 +00:00
|
|
|
specific_tag = parsed_args.get('specificTag') or None
|
2015-06-30 08:38:58 +00:00
|
|
|
|
2016-01-26 21:27:36 +00:00
|
|
|
page = max(1, parsed_args.get('page', 1))
|
|
|
|
limit = min(100, max(1, parsed_args.get('limit', 50)))
|
2017-03-01 23:22:40 +00:00
|
|
|
tags, manifest_map, more = model.tag.list_repository_tag_history(repo, page=page, size=limit,
|
|
|
|
specific_tag=specific_tag)
|
2015-06-30 08:38:58 +00:00
|
|
|
|
|
|
|
return {
|
2016-08-02 18:17:33 +00:00
|
|
|
'tags': [tag_view(tag) for tag in tags],
|
2015-06-30 08:38:58 +00:00
|
|
|
'page': page,
|
2017-03-01 23:22:40 +00:00
|
|
|
'has_additional': more,
|
2015-06-30 08:38:58 +00:00
|
|
|
}
|
2015-04-15 19:21:09 +00:00
|
|
|
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@resource('/v1/repository/<apirepopath:repository>/tag/<tag>')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
@path_param('tag', 'The name of the tag')
|
2014-03-14 17:06:58 +00:00
|
|
|
class RepositoryTag(RepositoryParamResource):
|
|
|
|
""" Resource for managing repository tags. """
|
2014-03-26 23:42:29 +00:00
|
|
|
schemas = {
|
|
|
|
'MoveTag': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Description of to which image a new or existing tag should point',
|
|
|
|
'required': [
|
|
|
|
'image',
|
|
|
|
],
|
|
|
|
'properties': {
|
|
|
|
'image': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'Image identifier to which the tag should point',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@require_repo_write
|
|
|
|
@nickname('changeTagImage')
|
|
|
|
@validate_json_request('MoveTag')
|
|
|
|
def put(self, namespace, repository, tag):
|
|
|
|
""" Change which image a tag points to or create a new tag."""
|
2015-10-05 20:36:33 +00:00
|
|
|
|
|
|
|
if not TAG_REGEX.match(tag):
|
|
|
|
abort(400, TAG_ERROR)
|
|
|
|
|
2014-03-26 23:42:29 +00:00
|
|
|
image_id = request.get_json()['image']
|
2015-07-15 21:25:41 +00:00
|
|
|
image = model.image.get_repo_image(namespace, repository, image_id)
|
2014-03-26 23:42:29 +00:00
|
|
|
if not image:
|
|
|
|
raise NotFound()
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-03-26 23:42:29 +00:00
|
|
|
original_image_id = None
|
|
|
|
try:
|
2016-06-02 20:36:38 +00:00
|
|
|
original_tag_image = model.tag.get_repo_tag_image(image.repository, tag)
|
2014-03-26 23:42:29 +00:00
|
|
|
if original_tag_image:
|
|
|
|
original_image_id = original_tag_image.docker_image_id
|
|
|
|
except model.DataModelException:
|
|
|
|
# This is a new tag.
|
|
|
|
pass
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
model.tag.create_or_update_tag(namespace, repository, tag, image_id)
|
2014-03-26 23:42:29 +00:00
|
|
|
|
|
|
|
username = get_authenticated_user().username
|
|
|
|
log_action('move_tag' if original_image_id else 'create_tag', namespace,
|
2015-02-11 16:54:30 +00:00
|
|
|
{'username': username, 'repo': repository, 'tag': tag,
|
2017-02-14 19:55:24 +00:00
|
|
|
'namespace': namespace, 'image': image_id,
|
|
|
|
'original_image': original_image_id},
|
2015-07-15 21:25:41 +00:00
|
|
|
repo=model.repository.get_repository(namespace, repository))
|
2014-03-26 23:42:29 +00:00
|
|
|
|
|
|
|
return 'Updated', 201
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2014-03-26 23:42:29 +00:00
|
|
|
@require_repo_write
|
2014-03-14 17:06:58 +00:00
|
|
|
@nickname('deleteFullTag')
|
|
|
|
def delete(self, namespace, repository, tag):
|
|
|
|
""" Delete the specified repository tag. """
|
2015-07-15 21:25:41 +00:00
|
|
|
model.tag.delete_tag(namespace, repository, tag)
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
username = get_authenticated_user().username
|
|
|
|
log_action('delete_tag', namespace,
|
2017-02-14 19:55:24 +00:00
|
|
|
{'username': username, 'repo': repository, 'namespace': namespace, 'tag': tag},
|
2015-07-15 21:25:41 +00:00
|
|
|
repo=model.repository.get_repository(namespace, repository))
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2016-12-06 21:26:28 +00:00
|
|
|
return '', 204
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@resource('/v1/repository/<apirepopath:repository>/tag/<tag>/images')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
@path_param('tag', 'The name of the tag')
|
2014-03-14 17:06:58 +00:00
|
|
|
class RepositoryTagImages(RepositoryParamResource):
|
|
|
|
""" Resource for listing the images in a specific repository tag. """
|
|
|
|
@require_repo_read
|
|
|
|
@nickname('listTagImages')
|
2016-01-26 21:27:36 +00:00
|
|
|
@parse_args()
|
2015-11-03 23:15:23 +00:00
|
|
|
@query_param('owned', 'If specified, only images wholely owned by this tag are returned.',
|
|
|
|
type=truthy_bool, default=False)
|
2016-01-26 21:27:36 +00:00
|
|
|
def get(self, namespace, repository, tag, parsed_args):
|
2014-03-14 17:06:58 +00:00
|
|
|
""" List the images for the specified repository tag. """
|
|
|
|
try:
|
2015-07-15 21:25:41 +00:00
|
|
|
tag_image = model.tag.get_tag_image(namespace, repository, tag)
|
2014-03-14 17:06:58 +00:00
|
|
|
except model.DataModelException:
|
2014-03-17 20:57:35 +00:00
|
|
|
raise NotFound()
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
parent_images = model.image.get_parent_images(namespace, repository, tag_image)
|
2014-09-18 21:16:10 +00:00
|
|
|
image_map = {}
|
2015-11-03 23:15:23 +00:00
|
|
|
|
|
|
|
image_map[str(tag_image.id)] = tag_image
|
|
|
|
|
2014-09-18 21:16:10 +00:00
|
|
|
for image in parent_images:
|
2015-03-20 21:46:02 +00:00
|
|
|
image_map[str(image.id)] = image
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2015-11-03 23:15:23 +00:00
|
|
|
image_map_all = dict(image_map)
|
2015-11-06 22:47:08 +00:00
|
|
|
all_images = [tag_image] + list(parent_images)
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2015-11-03 23:15:23 +00:00
|
|
|
# Filter the images returned to those not found in the ancestry of any of the other tags in
|
|
|
|
# the repository.
|
2016-01-26 21:27:36 +00:00
|
|
|
if parsed_args['owned']:
|
2015-11-03 23:15:23 +00:00
|
|
|
all_tags = model.tag.list_repository_tags(namespace, repository)
|
|
|
|
for current_tag in all_tags:
|
|
|
|
if current_tag.name == tag:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Remove the tag's image ID.
|
|
|
|
tag_image_id = str(current_tag.image_id)
|
|
|
|
image_map.pop(tag_image_id, None)
|
|
|
|
|
|
|
|
# Remove any ancestors:
|
|
|
|
for ancestor_id in current_tag.image.ancestors.split('/'):
|
|
|
|
image_map.pop(ancestor_id, None)
|
|
|
|
|
2014-03-14 17:06:58 +00:00
|
|
|
return {
|
2015-11-03 23:15:23 +00:00
|
|
|
'images': [image_view(image, image_map_all) for image in all_images
|
2016-01-26 21:27:36 +00:00
|
|
|
if not parsed_args['owned'] or (str(image.id) in image_map)]
|
2014-03-14 17:06:58 +00:00
|
|
|
}
|
2015-04-16 21:18:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-03-03 22:23:23 +00:00
|
|
|
@resource('/v1/repository/<apirepopath:repository>/tag/<tag>/restore')
|
2015-04-16 21:18:00 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
@path_param('tag', 'The name of the tag')
|
2017-03-03 22:23:23 +00:00
|
|
|
class RestoreTag(RepositoryParamResource):
|
|
|
|
""" Resource for restoring a repository tag back to a previous image. """
|
2015-04-16 21:18:00 +00:00
|
|
|
schemas = {
|
2017-03-03 22:23:23 +00:00
|
|
|
'RestoreTag': {
|
2015-04-16 21:18:00 +00:00
|
|
|
'type': 'object',
|
2017-03-03 22:23:23 +00:00
|
|
|
'description': 'Restores a tag to a specific image',
|
2015-04-16 21:18:00 +00:00
|
|
|
'required': [
|
|
|
|
'image',
|
|
|
|
],
|
|
|
|
'properties': {
|
|
|
|
'image': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'Image identifier to which the tag should point',
|
|
|
|
},
|
2017-03-03 22:23:23 +00:00
|
|
|
'manifest_digest': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'If specified, the manifest digest that should be used',
|
|
|
|
},
|
2015-04-16 21:18:00 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@require_repo_write
|
2017-03-03 22:23:23 +00:00
|
|
|
@nickname('restoreTag')
|
|
|
|
@validate_json_request('RestoreTag')
|
2015-04-16 21:18:00 +00:00
|
|
|
def post(self, namespace, repository, tag):
|
2017-03-03 22:23:23 +00:00
|
|
|
""" Restores a repository tag back to a previous image in the repository. """
|
|
|
|
repo = model.repository.get_repository(namespace, repository)
|
2015-04-16 21:18:00 +00:00
|
|
|
|
2017-03-03 22:23:23 +00:00
|
|
|
# Restore the tag back to the previous image.
|
2015-04-16 21:18:00 +00:00
|
|
|
image_id = request.get_json()['image']
|
2017-03-03 22:23:23 +00:00
|
|
|
manifest_digest = request.get_json().get('manifest_digest', None)
|
|
|
|
if manifest_digest is not None:
|
|
|
|
existing_image = model.tag.restore_tag_to_manifest(repo, tag, manifest_digest)
|
|
|
|
else:
|
|
|
|
existing_image = model.tag.restore_tag_to_image(repo, tag, image_id)
|
2015-04-16 21:18:00 +00:00
|
|
|
|
2017-03-03 22:23:23 +00:00
|
|
|
# Log the reversion/restoration.
|
2015-04-16 21:18:00 +00:00
|
|
|
username = get_authenticated_user().username
|
2017-03-03 22:23:23 +00:00
|
|
|
log_data = {
|
|
|
|
'username': username,
|
|
|
|
'repo': repository,
|
|
|
|
'tag': tag,
|
|
|
|
'image': image_id,
|
|
|
|
}
|
|
|
|
|
|
|
|
if existing_image is not None:
|
|
|
|
log_data['original_image'] = existing_image.docker_image_id
|
|
|
|
|
2015-04-16 21:18:00 +00:00
|
|
|
log_action('revert_tag', namespace,
|
2017-03-03 22:23:23 +00:00
|
|
|
log_data, repo=model.repository.get_repository(namespace, repository))
|
2015-04-16 21:18:00 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
'image_id': image_id,
|
2017-03-03 22:23:23 +00:00
|
|
|
'original_image_id': existing_image.docker_image_id if existing_image else None,
|
2015-04-16 21:18:00 +00:00
|
|
|
}
|