finish v1 registry refactor

This commit is contained in:
Jimmy Zelinskie 2016-07-12 13:48:44 -04:00
parent c14437e54a
commit 8435c254c3
3 changed files with 65 additions and 42 deletions

View file

@ -5,14 +5,14 @@ from util.morecollections import AttrDict
# TODO(jzelinskie): implement all of these methods using both legacy and new models.
def blob_placement_locations_docker_v1(namespace_name, repo_name, image_id):
def placement_locations_docker_v1(namespace_name, repo_name, image_id):
repo_image = model.image.get_repo_image_extended(namespace_name, repo_name, image_id)
if repo_image is None:
return None
return repo_image.storage.locations
def blob_placement_locations_and_path_docker_v1(namespace_name, repo_name, image_id):
def placement_locations_and_path_docker_v1(namespace_name, repo_name, image_id):
repo_image = model.image.get_repo_image_extended(namespace_name, repo_name, image_id)
if not repo_image:
return None, None
@ -73,7 +73,7 @@ def update_image_uploading(namespace_name, repo_name, image_id, is_uploading):
pass
def update_image_size(namespace_name, repo_name, image_id, size, uncompressed_size):
def update_image_sizes(namespace_name, repo_name, image_id, size, uncompressed_size):
model.storage.set_image_storage_metadata(
image_id,
namespace_name,
@ -83,7 +83,7 @@ def update_image_size(namespace_name, repo_name, image_id, size, uncompressed_si
)
def image_size(namespace_name, repo_name, image_id):
def get_image_size(namespace_name, repo_name, image_id):
return repo_image.storage.image_size
@ -124,3 +124,23 @@ def create_temp_hidden_tag(namespace_name, repo_name, expiration):
# model.tag.create_temporary_hidden_tag(repo, repo_image,
# app.config['PUSH_TEMP_TAG_EXPIRATION_SEC'])
pass
def list_tags(namespace_name, repo_name):
return model.tag.list_repository_tags(namespace_name, repo_name)
def create_or_update_tag(namespace_name, repo_name, image_id, tag_name):
model.tag.create_or_update_tag(namespace_name, repo_name, tag_name, image_id)
def find_image_id_by_tag(namespace_name, repo_name, tag_name):
try:
tag_image = model.tag.get_tag_image(namespace_name, repo_name, tag_name)
except model.DataModelException:
return None
return tag_image.docker_image_id
def delete_tag(namespace_name, repo_name, tag_name):
model.tag.delete_tag(namespace_name, repo_name, tag_name)

View file

@ -14,6 +14,7 @@ from auth.permissions import (ReadRepositoryPermission,
ModifyRepositoryPermission)
from auth.registry_jwt_auth import get_granted_username
from data import model, database
from data.model import v1
from digest import checksums
from endpoints.v1 import v1_bp
from endpoints.decorators import anon_protect
@ -29,7 +30,7 @@ logger = logging.getLogger(__name__)
def _finish_image(namespace, repository, image_id):
# Checksum is ok, we remove the marker
update_image_uploading(namespace, repository, image_id, False)
v1.update_image_uploading(namespace, repository, image_id, False)
# Send a job to the work queue to replicate the image layer.
# TODO(jzelinskie): make this not use imagestorage
@ -41,7 +42,7 @@ def require_completion(f):
@wraps(f)
def wrapper(namespace, repository, *args, **kwargs):
image_id = kwargs['image_id']
if is_image_uploading(namespace, repository, image_id):
if v1.is_image_uploading(namespace, repository, image_id):
abort(400, 'Image %(image_id)s is being uploaded, retry later',
issue='upload-in-progress', image_id=image_id)
return f(namespace, repository, *args, **kwargs)
@ -83,8 +84,8 @@ def head_image_layer(namespace, repository, image_id, headers):
logger.debug('Checking repo permissions')
if permission.can() or model.repository.repository_is_public(namespace, repository):
logger.debug('Looking up blob placement locations')
locations = blob_placement_locations_docker_v1(namespace, repository, image_id)
logger.debug('Looking up placement locations')
locations = v1.placement_locations_docker_v1(namespace, repository, image_id)
if locations is None:
logger.debug('Could not find any blob placement locations')
abort(404, 'Image %(image_id)s not found', issue='unknown-image',
@ -116,8 +117,10 @@ def get_image_layer(namespace, repository, image_id, headers):
logger.debug('Checking repo permissions')
if permission.can() or model.repository.repository_is_public(namespace, repository):
logger.debug('Looking up blob placement locations and path')
locations, path = blob_placement_locations_and_path_docker_v1(namespace, repository, image_id)
logger.debug('Looking up placement locations and path')
locations, path = v1.placement_locations_and_path_docker_v1(namespace,
repository,
image_id)
if not locations or not path:
abort(404, 'Image %(image_id)s not found', issue='unknown-image',
image_id=image_id)
@ -152,7 +155,7 @@ def put_image_layer(namespace, repository, image_id):
abort(403)
logger.debug('Retrieving image')
if storage_exists_docker_v1(namespace, repository, image_id):
if v1.storage_exists(namespace, repository, image_id):
exact_abort(409, 'Image already exists')
logger.debug('Storing layer data')
@ -182,7 +185,7 @@ def put_image_layer(namespace, repository, image_id):
sr.add_handler(piece_hasher.update)
# Add a handler which computes the checksum.
v1_metadata = docker_v1_metadata(namespace, repository, image_id)
v1_metadata = v1.docker_v1_metadata(namespace, repository, image_id)
h, sum_hndlr = checksums.simple_checksum_handler(v1_metadata.compat_json)
sr.add_handler(sum_hndlr)
@ -191,7 +194,7 @@ def put_image_layer(namespace, repository, image_id):
sr.add_handler(content_sum_hndlr)
# Stream write the data to storage.
locations, path = blob_placement_locations_and_path_docker_v1(namespace, repository, image_id)
locations, path = v1.placement_locations_and_path_docker_v1(namespace, repository, image_id)
with database.CloseForLongOperation(app.config):
try:
store.stream_write(locations, path, sr)
@ -200,10 +203,11 @@ def put_image_layer(namespace, repository, image_id):
abort(520, 'Image %(image_id)s could not be written. Please try again.', image_id=image_id)
# Save the size of the image.
update_image_size(namespace, repository, image_id, size_info.compressed_size, size_info.uncompressed_size)
v1.update_image_sizes(namespace, repository, image_id, size_info.compressed_size,
size_info.uncompressed_size)
# Save the BitTorrent pieces.
create_bittorrent_pieces(namespace, repository, image_id, piece_hasher.final_piece_hashes())
v1.create_bittorrent_pieces(namespace, repository, image_id, piece_hasher.final_piece_hashes())
# Append the computed checksum.
csums = []
@ -217,7 +221,6 @@ def put_image_layer(namespace, repository, image_id):
except (IOError, checksums.TarError) as exc:
logger.debug('put_image_layer: Error when computing tarsum %s', exc)
v1_metadata = docker_v1_metadata(namespace, repository, image_id)
if v1_metadata.checksum is None:
# We don't have a checksum stored yet, that's fine skipping the check.
# Not removing the mark though, image is not downloadable yet.
@ -269,7 +272,7 @@ def put_image_checksum(namespace, repository, image_id):
issue='missing-checksum-cookie', image_id=image_id)
logger.debug('Looking up repo image')
v1_metadata = docker_v1_metadata(namespace_name, repo_name, image_id)
v1_metadata = v1.docker_v1_metadata(namespace, repository, image_id)
if not v1_metadata:
abort(404, 'Image not found: %(image_id)s', issue='unknown-image', image_id=image_id)
@ -278,7 +281,7 @@ def put_image_checksum(namespace, repository, image_id):
abort(404, 'Image not found: %(image_id)s', issue='unknown-image', image_id=image_id)
logger.debug('Marking image path')
if not is_image_uploading(namespace, repository, image_id):
if not v1.is_image_uploading(namespace, repository, image_id):
abort(409, 'Cannot set checksum for image %(image_id)s',
issue='image-write-error', image_id=image_id)
@ -289,7 +292,7 @@ def put_image_checksum(namespace, repository, image_id):
if len(checksum_parts) != 2:
abort(400, 'Invalid checksum format')
store_docker_v1_checksum(namespace, repository, image_id, checksum, content_checksum)
v1.store_docker_v1_checksum(namespace, repository, image_id, checksum, content_checksum)
if checksum not in session.get('checksum', []):
logger.debug('session checksums: %s', session.get('checksum', []))
@ -317,12 +320,12 @@ def get_image_json(namespace, repository, image_id, headers):
abort(403)
logger.debug('Looking up repo image')
v1_metadata = docker_v1_metadata(namespace_name, repo_name, image_id)
v1_metadata = v1.docker_v1_metadata(namespace, repository, image_id)
if v1_metadata is None:
flask_abort(404)
logger.debug('Looking up repo layer size')
size = image_size(namespace_name, repo_name, image_id)
size = v1.get_image_size(namespace, repository, image_id)
if size is not None:
# Note: X-Docker-Size is optional and we *can* end up with a NULL image_size,
# so handle this case rather than failing.
@ -345,7 +348,7 @@ def get_image_ancestry(namespace, repository, image_id, headers):
if not permission.can() and not model.repository.repository_is_public(namespace, repository):
abort(403)
ancestry_docker_ids = image_ancestry(namespace, repository, image_id)
ancestry_docker_ids = v1.image_ancestry(namespace, repository, image_id)
if ancestry_docker_ids is None:
abort(404, 'Image %(image_id)s not found', issue='unknown-image', image_id=image_id)
@ -386,36 +389,36 @@ def put_image_json(namespace, repository, image_id):
logger.debug('Looking up repo image')
if not repository_exists(namespace, repository):
if not v1.repository_exists(namespace, repository):
abort(404, 'Repository does not exist: %(namespace)s/%(repository)s', issue='no-repo',
namespace=namespace, repository=repository)
v1_metadata = docker_v1_metadata(namespace, repository, image_id)
v1_metadata = v1.docker_v1_metadata(namespace, repository, image_id)
if v1_metadata is None:
username = get_authenticated_user() and get_authenticated_user().username
if not username:
username = get_granted_username()
logger.debug('Image not found, creating or linking image with initiating user context: %s', username)
create_or_link_image(username, repository, image_id, store.preferred_locations[0])
v1_metadata = docker_v1_metadata(namespace, repository, image_id)
v1.create_or_link_image(username, repository, image_id, store.preferred_locations[0])
v1_metadata = v1.docker_v1_metadata(namespace, repository, image_id)
# Create a temporary tag to prevent this image from getting garbage collected while the push
# is in progress.
create_temp_hidden_tag(namespace_name, repo_name, app.config['PUSH_TEMP_TAG_EXPIRATION_SEC'])
v1.create_temp_hidden_tag(namespace, repository, app.config['PUSH_TEMP_TAG_EXPIRATION_SEC'])
parent_id = data.get('parent', None)
if parent_id:
logger.debug('Looking up parent image')
if docker_v1_metadata(namespace, repository, parent_id) is None:
if v1.docker_v1_metadata(namespace, repository, parent_id) is None:
abort(400, 'Image %(image_id)s depends on non existing parent image %(parent_id)s',
issue='invalid-request', image_id=image_id, parent_id=parent_id)
logger.debug('Checking if image already exists')
if v1_metadata and not is_image_uploading(namespace, repository, image_id):
if v1_metadata and not v1.is_image_uploading(namespace, repository, image_id):
exact_abort(409, 'Image already exists')
update_image_uploading(namespace, repository, image_id, True)
v1.update_image_uploading(namespace, repository, image_id, True)
# If we reach that point, it means that this is a new image or a retry
# on a failed push, save the metadata
@ -423,8 +426,7 @@ def put_image_json(namespace, repository, image_id):
command = json.dumps(command_list) if command_list else None
logger.debug('Setting image metadata')
update_docker_v1_metadata(namespace, repository, image_id, data.get('created'),
data.get('comment'), command, uploaded_metadata, parent_image)
v1.update_docker_v1_metadata(namespace, repository, image_id, data.get('created'),
data.get('comment'), command, uploaded_metadata, parent_id)
return make_response('true', 200)

View file

@ -3,11 +3,13 @@ import json
from flask import abort, request, jsonify, make_response, session
from util.names import TAG_ERROR, TAG_REGEX
from auth.auth import process_auth
from auth.permissions import (ReadRepositoryPermission,
ModifyRepositoryPermission)
from data import model
from data.model import v1
from endpoints.common import parse_repository_name
from endpoints.decorators import anon_protect
from endpoints.v1 import v1_bp
@ -25,7 +27,7 @@ def get_tags(namespace_name, repo_name):
permission = ReadRepositoryPermission(namespace_name, repo_name)
if permission.can() or model.repository.repository_is_public(namespace_name, repo_name):
tags = model.tag.list_repository_tags(namespace_name, repo_name)
tags = v1.list_tags(namespace_name, repo_name)
tag_map = {tag.name: tag.image.docker_image_id for tag in tags}
return jsonify(tag_map)
@ -40,12 +42,11 @@ def get_tag(namespace_name, repo_name, tag):
permission = ReadRepositoryPermission(namespace_name, repo_name)
if permission.can() or model.repository.repository_is_public(namespace_name, repo_name):
try:
tag_image = model.tag.get_tag_image(namespace_name, repo_name, tag)
except model.DataModelException:
image_id = v1.find_image_id_by_tag(namespace_name, repo_name, tag)
if image_id is None:
abort(404)
resp = make_response('"%s"' % tag_image.docker_image_id)
resp = make_response('"%s"' % image_id)
resp.headers['Content-Type'] = 'application/json'
return resp
@ -63,14 +64,14 @@ def put_tag(namespace_name, repo_name, tag):
if not TAG_REGEX.match(tag):
abort(400, TAG_ERROR)
docker_image_id = json.loads(request.data)
model.tag.create_or_update_tag(namespace_name, repo_name, tag, docker_image_id)
image_id = json.loads(request.data)
v1.create_or_update_tag(namespace_name, repo_name, image_id, tag)
# Store the updated tag.
if 'pushed_tags' not in session:
session['pushed_tags'] = {}
session['pushed_tags'][tag] = docker_image_id
session['pushed_tags'][tag] = image_id
return make_response('Created', 200)
@ -85,7 +86,7 @@ def delete_tag(namespace_name, repo_name, tag):
permission = ModifyRepositoryPermission(namespace_name, repo_name)
if permission.can():
model.tag.delete_tag(namespace_name, repo_name, tag)
v1.delete_tag(namespace_name, repo_name, tag)
track_and_log('delete_tag', model.repository.get_repository(namespace_name, repo_name),
tag=tag)
return make_response('Deleted', 200)