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,
|
2014-08-19 23:05:28 +00:00
|
|
|
RepositoryParamResource, log_action, NotFound, validate_json_request,
|
2015-07-15 21:25:41 +00:00
|
|
|
path_param, parse_args, query_param)
|
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-04-15 19:21:09 +00:00
|
|
|
|
|
|
|
@resource('/v1/repository/<repopath:repository>/tag/')
|
|
|
|
@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
|
2015-04-16 21:18:00 +00:00
|
|
|
@parse_args
|
|
|
|
@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')
|
2015-04-16 21:18:00 +00:00
|
|
|
def get(self, args, namespace, repository):
|
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
|
|
|
|
|
|
|
|
return tag_info
|
|
|
|
|
2015-04-16 21:18:00 +00:00
|
|
|
specific_tag = args.get('specificTag') or None
|
2015-06-30 08:38:58 +00:00
|
|
|
|
|
|
|
page = min(1, args.get('start', 1))
|
2015-04-16 21:18:00 +00:00
|
|
|
limit = min(100, max(1, args.get('limit', 50)))
|
2015-06-30 08:38:58 +00:00
|
|
|
|
|
|
|
# Note: We ask for limit+1 here, so we can check to see if there are
|
|
|
|
# additional pages of results.
|
2015-07-15 21:25:41 +00:00
|
|
|
tags = model.tag.list_repository_tag_history(repo, page=page, size=limit+1,
|
|
|
|
specific_tag=specific_tag)
|
2015-06-30 08:38:58 +00:00
|
|
|
|
|
|
|
tags = list(tags)
|
|
|
|
return {
|
|
|
|
'tags': [tag_view(tag) for tag in tags[0:limit]],
|
|
|
|
'page': page,
|
|
|
|
'has_additional': len(tags) >= limit
|
|
|
|
}
|
2015-04-15 19:21:09 +00:00
|
|
|
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2014-03-19 19:39:44 +00:00
|
|
|
@resource('/v1/repository/<repopath: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."""
|
|
|
|
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:
|
2015-07-15 21:25:41 +00:00
|
|
|
original_tag_image = model.tag.get_tag_image(namespace, 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)
|
|
|
|
model.repository.garbage_collect_repository(namespace, repository)
|
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,
|
|
|
|
'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)
|
|
|
|
model.repository.garbage_collect_repository(namespace, repository)
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
username = get_authenticated_user().username
|
|
|
|
log_action('delete_tag', namespace,
|
|
|
|
{'username': username, 'repo': repository, '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
|
|
|
|
|
|
|
return 'Deleted', 204
|
|
|
|
|
|
|
|
|
2014-03-19 19:39:44 +00:00
|
|
|
@resource('/v1/repository/<repopath: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')
|
|
|
|
def get(self, namespace, repository, tag):
|
|
|
|
""" 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 = {}
|
|
|
|
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
|
|
|
|
|
|
|
parents = list(parent_images)
|
|
|
|
parents.reverse()
|
|
|
|
all_images = [tag_image] + parents
|
|
|
|
|
|
|
|
return {
|
2014-09-18 21:16:10 +00:00
|
|
|
'images': [image_view(image, image_map) for image in all_images]
|
2014-03-14 17:06:58 +00:00
|
|
|
}
|
2015-04-16 21:18:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/repository/<repopath:repository>/tag/<tag>/revert')
|
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
@path_param('tag', 'The name of the tag')
|
|
|
|
class RevertTag(RepositoryParamResource):
|
|
|
|
""" Resource for reverting a repository tag back to a previous image. """
|
|
|
|
schemas = {
|
|
|
|
'RevertTag': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Reverts a tag to a specific image',
|
|
|
|
'required': [
|
|
|
|
'image',
|
|
|
|
],
|
|
|
|
'properties': {
|
|
|
|
'image': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'Image identifier to which the tag should point',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@require_repo_write
|
|
|
|
@nickname('revertTag')
|
|
|
|
@validate_json_request('RevertTag')
|
|
|
|
def post(self, namespace, repository, tag):
|
|
|
|
""" Reverts a repository tag back to a previous image in the repository. """
|
|
|
|
try:
|
2015-07-15 21:25:41 +00:00
|
|
|
tag_image = model.tag.get_tag_image(namespace, repository, tag)
|
2015-04-16 21:18:00 +00:00
|
|
|
except model.DataModelException:
|
|
|
|
raise NotFound()
|
|
|
|
|
|
|
|
# Revert the tag back to the previous image.
|
|
|
|
image_id = request.get_json()['image']
|
2015-07-15 21:25:41 +00:00
|
|
|
model.tag.revert_tag(tag_image.repository, tag, image_id)
|
|
|
|
model.repository.garbage_collect_repository(namespace, repository)
|
2015-04-16 21:18:00 +00:00
|
|
|
|
|
|
|
# Log the reversion.
|
|
|
|
username = get_authenticated_user().username
|
|
|
|
log_action('revert_tag', namespace,
|
|
|
|
{'username': username, 'repo': repository, 'tag': tag,
|
|
|
|
'image': image_id, 'original_image': tag_image.docker_image_id},
|
2015-07-15 21:25:41 +00:00
|
|
|
repo=model.repository.get_repository(namespace, repository))
|
2015-04-16 21:18:00 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
'image_id': image_id,
|
|
|
|
'original_image_id': tag_image.docker_image_id
|
|
|
|
}
|