create interfaces for v1 and v2 data model

This commit is contained in:
Jimmy Zelinskie 2016-08-30 15:05:15 -04:00
parent b775458d4b
commit c06d395f96
14 changed files with 1048 additions and 732 deletions

View file

@ -1,263 +1,435 @@
from collections import namedtuple
from app import app, storage as store
from data import model
from data.model import db_transaction
from util.morecollections import AttrDict
from data.interfaces.common import repository_for_repo
def placement_locations_docker_v1(namespace_name, repo_name, image_id):
""" Returns all the placements for the image with the given V1 Docker ID, found under the
given repository or None if no image was found.
class Repository(namedtuple('Repository', ['id', 'name', 'namespace_name', 'description',
'is_public'])):
"""
repo_image = model.image.get_repo_image_and_storage(namespace_name, repo_name, image_id)
if repo_image is None or repo_image.storage is None:
return None
return repo_image.storage.locations
def placement_locations_and_path_docker_v1(namespace_name, repo_name, image_id):
""" Returns a tuple of the placements and storage path location for the image with the
given V1 Docker ID, found under the given repository or None if no image was found.
Repository represents a namespaced collection of tags.
"""
repo_image = model.image.get_repo_image_extended(namespace_name, repo_name, image_id)
if not repo_image or repo_image.storage is None:
return None, None
return repo_image.storage.locations, model.storage.get_layer_path(repo_image.storage)
def docker_v1_metadata(namespace_name, repo_name, image_id):
""" Returns various pieces of metadata associated with an image with the given V1 Docker ID,
including the checksum and its V1 JSON metadata.
def _repository_for_repo(repo):
"""
repo_image = model.image.get_repo_image(namespace_name, repo_name, image_id)
if repo_image is None:
return None
return AttrDict({
'namespace_name': namespace_name,
'repo_name': repo_name,
'image_id': image_id,
'checksum': repo_image.v1_checksum,
'compat_json': repo_image.v1_json_metadata,
})
def update_docker_v1_metadata(namespace_name, repo_name, image_id, created_date_str, comment,
command, compat_json, parent_image_id=None):
""" Updates various pieces of V1 metadata associated with a particular image. """
parent_image = None
if parent_image_id is not None:
parent_image = model.image.get_repo_image(namespace_name, repo_name, parent_image_id)
model.image.set_image_metadata(image_id, namespace_name, repo_name, created_date_str, comment,
command, compat_json, parent=parent_image)
def storage_exists(namespace_name, repo_name, image_id):
""" Returns whether storage already exists for the image with the V1 Docker ID under the
given repository.
Returns a Repository object representing the repo data model instance given.
"""
repo_image = model.image.get_repo_image_and_storage(namespace_name, repo_name, image_id)
if repo_image is None or repo_image.storage is None:
return False
if repo_image.storage.uploading:
return False
layer_path = model.storage.get_layer_path(repo_image.storage)
return store.exists(repo_image.storage.locations, layer_path)
return Repository(
id=repo.id,
name=repo.name,
namespace_name=repo.namespace_user.username,
description=repo.description,
is_public=model.repository.is_repository_public(repo)
)
def store_docker_v1_checksums(namespace_name, repo_name, image_id, checksum, content_checksum):
""" Stores the various V1 checksums for the image with the V1 Docker ID. """
repo_image = model.image.get_repo_image_and_storage(namespace_name, repo_name, image_id)
if repo_image is None or repo_image.storage is None:
return
class DockerRegistryV1DataInterface(object):
"""
Interface that represents all data store interactions required by a Docker Registry v1.
"""
with db_transaction():
repo_image.storage.content_checksum = content_checksum
repo_image.v1_checksum = checksum
@classmethod
def placement_locations_docker_v1(cls, namespace_name, repo_name, image_id):
"""
Returns all the placements for the image with the given V1 Docker ID, found under the given
repository or None if no image was found.
"""
raise NotImplementedError()
@classmethod
def placement_locations_and_path_docker_v1(cls, namespace_name, repo_name, image_id):
"""
Returns all the placements for the image with the given V1 Docker ID, found under the given
repository or None if no image was found.
"""
raise NotImplementedError()
@classmethod
def docker_v1_metadata(cls, namespace_name, repo_name, image_id):
"""
Returns various pieces of metadata associated with an image with the given V1 Docker ID,
including the checksum and its V1 JSON metadata.
"""
raise NotImplementedError()
@classmethod
def update_docker_v1_metadata(cls, namespace_name, repo_name, image_id, created_date_str, comment,
command, compat_json, parent_image_id=None):
"""
Updates various pieces of V1 metadata associated with a particular image.
"""
raise NotImplementedError()
@classmethod
def storage_exists(cls, namespace_name, repo_name, image_id):
"""
Returns whether storage already exists for the image with the V1 Docker ID under the given
repository.
"""
raise NotImplementedError()
@classmethod
def store_docker_v1_checksums(cls, namespace_name, repo_name, image_id, checksum, content_checksum):
"""
Stores the various V1 checksums for the image with the V1 Docker ID.
"""
raise NotImplementedError()
@classmethod
def is_image_uploading(cls, namespace_name, repo_name, image_id):
"""
Returns whether the image with the V1 Docker ID is currently marked as uploading.
"""
raise NotImplementedError()
@classmethod
def update_image_uploading(cls, namespace_name, repo_name, image_id, is_uploading):
""" Marks the image with the V1 Docker ID with the given uploading status. """
raise NotImplementedError()
@classmethod
def update_image_sizes(cls, namespace_name, repo_name, image_id, size, uncompressed_size):
"""
Updates the sizing information for the image with the given V1 Docker ID.
"""
raise NotImplementedError()
@classmethod
def get_image_size(cls, namespace_name, repo_name, image_id):
"""
Returns the wire size of the image with the given Docker V1 ID.
"""
raise NotImplementedError()
@classmethod
def create_bittorrent_pieces(cls, namespace_name, repo_name, image_id, pieces_bytes):
"""
Saves the BitTorrent piece hashes for the image with the given Docker V1 ID.
"""
raise NotImplementedError()
@classmethod
def image_ancestry(cls, namespace_name, repo_name, image_id):
"""
Returns a list containing the full ancestry of Docker V1 IDs, in order, for the image with the
given Docker V1 ID.
"""
raise NotImplementedError()
@classmethod
def repository_exists(cls, namespace_name, repo_name):
"""
Returns whether the repository with the given name and namespace exists.
"""
raise NotImplementedError()
@classmethod
def create_or_link_image(cls, username, namespace_name, repo_name, image_id, storage_location):
"""
Adds the given image to the given repository, by either linking to an existing image visible to
the user with the given username, or creating a new one if no existing image matches.
"""
raise NotImplementedError()
@classmethod
def create_temp_hidden_tag(cls, namespace_name, repo_name, image_id, expiration):
"""
Creates a hidden tag under the matching namespace pointing to the image with the given V1 Docker
ID.
"""
raise NotImplementedError()
@classmethod
def list_tags(cls, namespace_name, repo_name):
"""
Returns all the tags defined in the repository with the given namespace and name.
"""
raise NotImplementedError()
@classmethod
def create_or_update_tag(cls, namespace_name, repo_name, image_id, tag_name):
"""
Creates or updates a tag under the matching repository to point to the image with the given
Docker V1 ID.
"""
raise NotImplementedError()
@classmethod
def find_image_id_by_tag(cls, namespace_name, repo_name, tag_name):
"""
Returns the Docker V1 image ID for the HEAD image for the tag with the given name under the
matching repository, or None if none.
"""
raise NotImplementedError()
@classmethod
def delete_tag(cls, namespace_name, repo_name, tag_name):
""" Deletes the given tag from the given repository. """
raise NotImplementedError()
@classmethod
def load_token(cls, token):
"""
Loads the data associated with the given (deprecated) access token, and, if
found returns True.
"""
raise NotImplementedError()
@classmethod
def verify_robot(cls, username, token):
"""
Returns True if the given robot username and token match an existing robot
account.
"""
raise NotImplementedError()
@classmethod
def change_user_password(cls, user, new_password):
"""
Changes the password associated with the given user.
"""
raise NotImplementedError()
@classmethod
def get_repository(cls, namespace_name, repo_name):
"""
Returns the repository with the given name under the given namespace or None
if none.
"""
raise NotImplementedError()
@classmethod
def create_repository(cls, namespace_name, repo_name, user=None):
"""
Creates a new repository under the given namespace with the given name, for
the given user.
"""
raise NotImplementedError()
@classmethod
def repository_is_public(cls, namespace_name, repo_name):
"""
Returns whether the repository with the given name under the given namespace
is public. If no matching repository was found, returns False.
"""
raise NotImplementedError()
@classmethod
def validate_oauth_token(cls, token):
""" Returns whether the given OAuth token validates. """
raise NotImplementedError()
@classmethod
def get_sorted_matching_repositories(cls, search_term, only_public, can_read, limit):
"""
Returns a sorted list of repositories matching the given search term.
can_read is a callback that will be invoked for each repository found, to
filter results to only those visible to the current user (if any).
"""
raise NotImplementedError()
class PreOCIModel(DockerRegistryV1DataInterface):
"""
PreOCIModel implements the data model for the v1 Docker Registry protocol using a database schema
before it was changed to support the OCI specification.
"""
@classmethod
def placement_locations_docker_v1(cls, namespace_name, repo_name, image_id):
repo_image = model.image.get_repo_image_and_storage(namespace_name, repo_name, image_id)
if repo_image is None or repo_image.storage is None:
return None
return repo_image.storage.locations
@classmethod
def placement_locations_and_path_docker_v1(cls, namespace_name, repo_name, image_id):
repo_image = model.image.get_repo_image_extended(namespace_name, repo_name, image_id)
if not repo_image or repo_image.storage is None:
return None, None
return repo_image.storage.locations, model.storage.get_layer_path(repo_image.storage)
@classmethod
def docker_v1_metadata(cls, namespace_name, repo_name, image_id):
repo_image = model.image.get_repo_image(namespace_name, repo_name, image_id)
if repo_image is None:
return None
return AttrDict({
'namespace_name': namespace_name,
'repo_name': repo_name,
'image_id': image_id,
'checksum': repo_image.v1_checksum,
'compat_json': repo_image.v1_json_metadata,
})
@classmethod
def update_docker_v1_metadata(cls, namespace_name, repo_name, image_id, created_date_str, comment,
command, compat_json, parent_image_id=None):
parent_image = None
if parent_image_id is not None:
parent_image = model.image.get_repo_image(namespace_name, repo_name, parent_image_id)
model.image.set_image_metadata(image_id, namespace_name, repo_name, created_date_str, comment,
command, compat_json, parent=parent_image)
@classmethod
def storage_exists(cls, namespace_name, repo_name, image_id):
repo_image = model.image.get_repo_image_and_storage(namespace_name, repo_name, image_id)
if repo_image is None or repo_image.storage is None:
return False
if repo_image.storage.uploading:
return False
layer_path = model.storage.get_layer_path(repo_image.storage)
return store.exists(repo_image.storage.locations, layer_path)
@classmethod
def store_docker_v1_checksums(cls, namespace_name, repo_name, image_id, checksum, content_checksum):
repo_image = model.image.get_repo_image_and_storage(namespace_name, repo_name, image_id)
if repo_image is None or repo_image.storage is None:
return
with db_transaction():
repo_image.storage.content_checksum = content_checksum
repo_image.v1_checksum = checksum
repo_image.storage.save()
repo_image.save()
@classmethod
def is_image_uploading(cls, namespace_name, repo_name, image_id):
repo_image = model.image.get_repo_image_and_storage(namespace_name, repo_name, image_id)
if repo_image is None or repo_image.storage is None:
return False
return repo_image.storage.uploading
@classmethod
def update_image_uploading(cls, namespace_name, repo_name, image_id, is_uploading):
repo_image = model.image.get_repo_image_and_storage(namespace_name, repo_name, image_id)
if repo_image is None or repo_image.storage is None:
return
repo_image.storage.uploading = is_uploading
repo_image.storage.save()
repo_image.save()
return repo_image.storage
@classmethod
def update_image_sizes(cls, namespace_name, repo_name, image_id, size, uncompressed_size):
model.storage.set_image_storage_metadata(image_id, namespace_name, repo_name, size,
uncompressed_size)
def is_image_uploading(namespace_name, repo_name, image_id):
""" Returns whether the image with the V1 Docker ID is currently marked as uploading. """
repo_image = model.image.get_repo_image_and_storage(namespace_name, repo_name, image_id)
if repo_image is None or repo_image.storage is None:
return False
@classmethod
def get_image_size(cls, namespace_name, repo_name, image_id):
repo_image = model.image.get_repo_image_and_storage(namespace_name, repo_name, image_id)
if repo_image is None or repo_image.storage is None:
return None
return repo_image.storage.image_size
return repo_image.storage.uploading
@classmethod
def create_bittorrent_pieces(cls, namespace_name, repo_name, image_id, pieces_bytes):
repo_image = model.image.get_repo_image_and_storage(namespace_name, repo_name, image_id)
if repo_image is None or repo_image.storage is None:
return
model.storage.save_torrent_info(repo_image.storage, app.config['BITTORRENT_PIECE_SIZE'],
pieces_bytes)
def update_image_uploading(namespace_name, repo_name, image_id, is_uploading):
""" Marks the image with the V1 Docker ID with the given uploading status. """
repo_image = model.image.get_repo_image_and_storage(namespace_name, repo_name, image_id)
if repo_image is None or repo_image.storage is None:
return
@classmethod
def image_ancestry(cls, namespace_name, repo_name, image_id):
try:
image = model.image.get_image_by_id(namespace_name, repo_name, image_id)
except model.InvalidImageException:
return None
repo_image.storage.uploading = is_uploading
repo_image.storage.save()
return repo_image.storage
parents = model.image.get_parent_images(namespace_name, repo_name, image)
ancestry_docker_ids = [image.docker_image_id]
ancestry_docker_ids.extend([parent.docker_image_id for parent in parents])
return ancestry_docker_ids
@classmethod
def repository_exists(cls, namespace_name, repo_name):
repo = model.repository.get_repository(namespace_name, repo_name)
return repo is not None
def update_image_sizes(namespace_name, repo_name, image_id, size, uncompressed_size):
""" Updates the sizing information for the image with the given V1 Docker ID. """
model.storage.set_image_storage_metadata(image_id, namespace_name, repo_name, size,
uncompressed_size)
@classmethod
def create_or_link_image(cls, username, namespace_name, repo_name, image_id, storage_location):
repo = model.repository.get_repository(namespace_name, repo_name)
model.image.find_create_or_link_image(image_id, repo, username, {}, storage_location)
@classmethod
def create_temp_hidden_tag(cls, namespace_name, repo_name, image_id, expiration):
repo_image = model.image.get_repo_image(namespace_name, repo_name, image_id)
if repo_image is None:
return
def get_image_size(namespace_name, repo_name, image_id):
""" Returns the wire size of the image with the given Docker V1 ID. """
repo_image = model.image.get_repo_image_and_storage(namespace_name, repo_name, image_id)
if repo_image is None or repo_image.storage is None:
return None
repo = repo_image.repository
model.tag.create_temporary_hidden_tag(repo, repo_image, expiration)
return repo_image.storage.image_size
@classmethod
def list_tags(cls, namespace_name, repo_name):
return model.tag.list_repository_tags(namespace_name, repo_name)
@classmethod
def create_or_update_tag(cls, namespace_name, repo_name, image_id, tag_name):
model.tag.create_or_update_tag(namespace_name, repo_name, tag_name, image_id)
def create_bittorrent_pieces(namespace_name, repo_name, image_id, pieces_bytes):
""" Saves the bittorrent piece hashes for the image with the given Docker V1 ID. """
repo_image = model.image.get_repo_image_and_storage(namespace_name, repo_name, image_id)
if repo_image is None or repo_image.storage is None:
return
@classmethod
def find_image_id_by_tag(cls, 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
model.storage.save_torrent_info(repo_image.storage, app.config['BITTORRENT_PIECE_SIZE'],
pieces_bytes)
return tag_image.docker_image_id
@classmethod
def delete_tag(cls, namespace_name, repo_name, tag_name):
model.tag.delete_tag(namespace_name, repo_name, tag_name)
def image_ancestry(namespace_name, repo_name, image_id):
""" Returns a list containing the full ancestry of Docker V1 IDs, in order, for the image with
the givne Docker V1 ID.
"""
try:
image = model.image.get_image_by_id(namespace_name, repo_name, image_id)
except model.InvalidImageException:
return None
@classmethod
def load_token(cls, token):
try:
model.token.load_token_data(token)
return True
except model.InvalidTokenException:
return False
parents = model.image.get_parent_images(namespace_name, repo_name, image)
ancestry_docker_ids = [image.docker_image_id]
ancestry_docker_ids.extend([parent.docker_image_id for parent in parents])
return ancestry_docker_ids
@classmethod
def verify_robot(cls, username, token):
try:
return bool(model.user.verify_robot(username, token))
except model.InvalidRobotException:
return False
@classmethod
def change_user_password(cls, user, new_password):
model.user.change_password(user, new_password)
def repository_exists(namespace_name, repo_name):
""" Returns whether the repository with the given name and namespace exists. """
repo = model.repository.get_repository(namespace_name, repo_name)
return repo is not None
@classmethod
def get_repository(cls, namespace_name, repo_name):
repo = model.repository.get_repository(namespace_name, repo_name)
if repo is None:
return None
return _repository_for_repo(repo)
@classmethod
def create_repository(cls, namespace_name, repo_name, user=None):
model.repository.create_repository(namespace_name, repo_name, user)
def create_or_link_image(username, namespace_name, repo_name, image_id, storage_location):
""" Adds the given image to the given repository, by either linking to an existing image
visible to the user with the given username, or creating a new one if no existing image
matches.
"""
repo = model.repository.get_repository(namespace_name, repo_name)
model.image.find_create_or_link_image(image_id, repo, username, {}, storage_location)
@classmethod
def repository_is_public(cls, namespace_name, repo_name):
return model.repository.repository_is_public(namespace_name, repo_name)
@classmethod
def validate_oauth_token(cls, token):
return bool(model.oauth.validate_access_token(token))
def create_temp_hidden_tag(namespace_name, repo_name, image_id, expiration):
""" Creates a hidden tag under the matching namespace pointing to the image with the given V1
Docker ID.
"""
repo_image = model.image.get_repo_image(namespace_name, repo_name, image_id)
if repo_image is None:
return
repo = repo_image.repository
model.tag.create_temporary_hidden_tag(repo, repo_image, expiration)
def list_tags(namespace_name, repo_name):
""" Returns all the tags defined in the repository with the given namespace and name. """
return model.tag.list_repository_tags(namespace_name, repo_name)
def create_or_update_tag(namespace_name, repo_name, image_id, tag_name):
""" Creates or updates a tag under the matching repository to point to the image with the given
Docker V1 ID.
"""
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):
""" Returns the Docker V1 image ID for the HEAD image for the tag with the given name under
the matching repository, or None if none.
"""
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):
""" Deletes the given tag from the given repository. """
model.tag.delete_tag(namespace_name, repo_name, tag_name)
def load_token(token):
""" Loads the data associated with the given (deprecated) access token, and, if found
returns True.
"""
try:
model.token.load_token_data(token)
return True
except model.InvalidTokenException:
return False
def verify_robot(username, token):
""" Returns True if the given robot username and token match an existing robot
account.
"""
try:
return bool(model.user.verify_robot(username, token))
except model.InvalidRobotException:
return False
def change_user_password(user, new_password):
""" Changes the password associated with the given user. """
model.user.change_password(user, new_password)
def get_repository(namespace_name, repo_name):
""" Returns the repository with the given name under the given namespace or None if none. """
repo = model.repository.get_repository(namespace_name, repo_name)
if repo is None:
return None
return repository_for_repo(repo)
def create_repository(namespace_name, repo_name, user=None):
""" Creates a new repository under the given namespace with the given name, for the given user.
"""
model.repository.create_repository(namespace_name, repo_name, user)
def repository_is_public(namespace_name, repo_name):
""" Returns whether the repository with the given name under the given namespace is public.
If no matching repository was found, returns False.
"""
return model.repository.repository_is_public(namespace_name, repo_name)
def validate_oauth_token(token):
""" Returns whether the given OAuth token validates. """
return bool(model.oauth.validate_access_token(token))
def get_sorted_matching_repositories(search_term, only_public, can_read, limit):
""" Returns a sorted list of repositories matching the given search term. can_read is a callback
that will be invoked for each repository found, to filter results to only those visible to
the current user (if any).
"""
repos = model.repository.get_sorted_matching_repositories(search_term, only_public, can_read,
limit=limit)
return [repository_for_repo(repo) for repo in repos]
@classmethod
def get_sorted_matching_repositories(cls, search_term, only_public, can_read, limit):
repos = model.repository.get_sorted_matching_repositories(search_term, only_public, can_read,
limit=limit)
return [_repository_for_repo(repo) for repo in repos]

View file

@ -1,95 +1,74 @@
from collections import namedtuple
from namedlist import namedlist
from peewee import IntegrityError
from data import model, database
from data.model import DataModelException
from image import Blob, BlobUpload, ManifestJSON, RepositoryReference, Tag
from image.docker.v1 import DockerV1Metadata
from data.interfaces.common import repository_for_repo
_MEDIA_TYPE = "application/vnd.docker.distribution.manifest.v1+prettyjws"
def create_repository(namespace_name, repo_name, creating_user=None):
""" Creates a new repository under the specified namespace with the given name. The user supplied
is the user creating the repository, if any.
class ManifestJSON(namedtuple('ManifestJSON', ['digest', 'json', 'media_type'])):
"""
return model.repository.create_repository(namespace_name, repo_name, creating_user)
def repository_is_public(namespace_name, repo_name):
""" Returns true if the repository with the given name under the given namespace has public
visibility.
ManifestJSON represents a Manifest of any format.
"""
return model.repository.repository_is_public(namespace_name, repo_name)
def get_repository(namespace_name, repo_name):
""" Returns a repository tuple for the repository with the given name under the given namespace.
Returns None if no such repository was found.
class Tag(namedtuple('Tag', ['name', 'repository'])):
"""
repo = model.repository.get_repository(namespace_name, repo_name)
if repo is None:
return None
return repository_for_repo(repo)
def has_active_tag(namespace_name, repo_name, tag_name):
""" Returns whether there is an active tag for the tag with the given name under the matching
repository, if any, or None if none.
Tag represents a user-facing alias for referencing a set of Manifests.
"""
try:
model.tag.get_active_tag(namespace_name, repo_name, tag_name)
return True
except database.RepositoryTag.DoesNotExist:
return False
def get_manifest_by_tag(namespace_name, repo_name, tag_name):
""" Returns the current manifest for the tag with the given name under the matching repository,
if any, or None if none.
class BlobUpload(namedlist('BlobUpload', ['uuid', 'byte_count', 'uncompressed_byte_count',
'chunk_count', 'sha_state', 'location_name',
'storage_metadata', 'piece_sha_state', 'piece_hashes',
'repo_namespace_name', 'repo_name'])):
"""
try:
manifest = model.tag.load_tag_manifest(namespace_name, repo_name, tag_name)
return ManifestJSON(digest=manifest.digest, json=manifest.json_data, media_type=_MEDIA_TYPE)
except model.InvalidManifestException:
return None
def get_manifest_by_digest(namespace_name, repo_name, digest):
""" Returns the manifest matching the given digest under the matching repository, if any,
or None if none.
BlobUpload represents the current state of an Blob being uploaded.
"""
try:
manifest = model.tag.load_manifest_by_digest(namespace_name, repo_name, digest)
return ManifestJSON(digest=digest, json=manifest.json_data, media_type=_MEDIA_TYPE)
except model.InvalidManifestException:
return None
def delete_manifest_by_digest(namespace_name, repo_name, digest):
""" Deletes the manifest with the associated digest (if any) and returns all removed tags
that pointed to that manifest. If the manifest was not found, returns an empty list.
class Blob(namedtuple('Blob', ['uuid', 'digest', 'size', 'locations'])):
"""
Blob represents an opaque binary blob saved to the storage system.
"""
tags = model.tag.delete_manifest_by_digest(namespace_name, repo_name, digest)
def _tag_view(tag):
return Tag(
name=tag.name,
repository=RepositoryReference(
id=tag.repository_id,
name=repo_name,
namespace_name=namespace_name,
)
)
return [_tag_view(tag) for tag in tags]
class RepositoryReference(namedtuple('RepositoryReference', ['id', 'name', 'namespace_name'])):
"""
RepositoryReference represents a reference to a Repository, without its full metadata.
"""
class Repository(namedtuple('Repository', ['id', 'name', 'namespace_name', 'description',
'is_public'])):
"""
Repository represents a namespaced collection of tags.
"""
def _repository_for_repo(repo):
"""
Returns a Repository object representing the repo data model instance given.
"""
return Repository(
id=repo.id,
name=repo.name,
namespace_name=repo.namespace_user.username,
description=repo.description,
is_public=model.repository.is_repository_public(repo)
)
def _docker_v1_metadata(namespace_name, repo_name, repo_image):
""" Returns a DockerV1Metadata object for the given image under the repository with the given
namespace and name. Note that the namespace and name are passed here as an optimization,
and are *not checked* against the image.
"""
Returns a DockerV1Metadata object for the given image under the repository with the given
namespace and name. Note that the namespace and name are passed here as an optimization, and are
*not checked* against the image.
"""
return DockerV1Metadata(
namespace_name=namespace_name,
@ -98,264 +77,474 @@ def _docker_v1_metadata(namespace_name, repo_name, repo_image):
checksum=repo_image.v1_checksum,
content_checksum=repo_image.storage.content_checksum,
compat_json=repo_image.v1_json_metadata,
created=repo_image.created,
comment=repo_image.comment,
command=repo_image.command,
parent_image_id=None, # TODO: make sure this isn't needed anywhere, as it is expensive to lookup
# TODO: make sure this isn't needed anywhere, as it is expensive to lookup
parent_image_id=None,
)
def get_docker_v1_metadata_by_tag(namespace_name, repo_name, tag_name):
""" Returns the Docker V1 metadata associated with the tag with the given name under the
matching repository, if any. If none, returns None.
class DockerRegistryV2DataInterface(object):
"""
try:
repo_image = model.tag.get_tag_image(namespace_name, repo_name, tag_name, include_storage=True)
return _docker_v1_metadata(namespace_name, repo_name, repo_image)
except DataModelException:
return None
def get_docker_v1_metadata_by_image_id(namespace_name, repo_name, docker_image_ids):
""" Returns a map of Docker V1 metadata for each given image ID, matched under the repository
with the given namespace and name. Returns an empty map if the matching repository was not
found.
Interface that represents all data store interactions required by a Docker Registry v1.
"""
repo = model.repository.get_repository(namespace_name, repo_name)
if repo is None:
return {}
images_query = model.image.lookup_repository_images(repo, docker_image_ids)
return {image.docker_image_id: _docker_v1_metadata(namespace_name, repo_name, image)
for image in images_query}
@classmethod
def create_repository(cls, namespace_name, repo_name, creating_user=None):
"""
Creates a new repository under the specified namespace with the given name. The user supplied is
the user creating the repository, if any.
"""
raise NotImplementedError()
@classmethod
def repository_is_public(cls, namespace_name, repo_name):
"""
Returns true if the repository with the given name under the given namespace has public
visibility.
"""
raise NotImplementedError()
@classmethod
def get_repository(cls, namespace_name, repo_name):
"""
Returns a repository tuple for the repository with the given name under the given namespace.
Returns None if no such repository was found.
"""
raise NotImplementedError()
@classmethod
def has_active_tag(cls, namespace_name, repo_name, tag_name):
"""
Returns whether there is an active tag for the tag with the given name under the matching
repository, if any, or none if none.
"""
raise NotImplementedError()
@classmethod
def get_manifest_by_tag(cls, namespace_name, repo_name, tag_name):
"""
Returns the current manifest for the tag with the given name under the matching repository, if
any, or None if none.
"""
raise NotImplementedError()
@classmethod
def get_manifest_by_digest(cls, namespace_name, repo_name, digest):
"""
Returns the manifest matching the given digest under the matching repository, if any, or None if
none.
"""
raise NotImplementedError()
@classmethod
def delete_manifest_by_digest(cls, namespace_name, repo_name, digest):
"""
Deletes the manifest with the associated digest (if any) and returns all removed tags that
pointed to that manifest. If the manifest was not found, returns an empty list.
"""
raise NotImplementedError()
@classmethod
def get_docker_v1_metadata_by_tag(cls, namespace_name, repo_name, tag_name):
"""
Returns the Docker V1 metadata associated with the tag with the given name under the matching
repository, if any. If none, returns None.
"""
raise NotImplementedError()
@classmethod
def get_docker_v1_metadata_by_image_id(cls, namespace_name, repo_name, docker_image_ids):
"""
Returns a map of Docker V1 metadata for each given image ID, matched under the repository with
the given namespace and name. Returns an empty map if the matching repository was not found.
"""
raise NotImplementedError()
@classmethod
def get_parents_docker_v1_metadata(cls, namespace_name, repo_name, docker_image_id):
"""
Returns an ordered list containing the Docker V1 metadata for each parent of the image with the
given docker ID under the matching repository. Returns an empty list if the image was not found.
"""
raise NotImplementedError()
@classmethod
def create_manifest_and_update_tag(cls, namespace_name, repo_name, tag_name, manifest_digest,
manifest_bytes):
"""
Creates a new manifest with the given digest and byte data, and assigns the tag with the given
name under the matching repository to it.
"""
raise NotImplementedError()
@classmethod
def synthesize_v1_image(cls, repository, storage, image_id, created, comment, command,
compat_json, parent_image_id):
"""
Synthesizes a V1 image under the specified repository, pointing to the given storage and returns
the V1 metadata for the synthesized image.
"""
raise NotImplementedError()
@classmethod
def save_manifest(cls, namespace_name, repo_name, tag_name, leaf_layer_docker_id, manifest_digest,
manifest_bytes):
"""
Saves a manifest pointing to the given leaf image, with the given manifest, under the matching
repository as a tag with the given name.
"""
raise NotImplementedError()
@classmethod
def repository_tags(cls, namespace_name, repo_name, limit, offset):
"""
Returns the active tags under the repository with the given name and namespace.
"""
raise NotImplementedError()
@classmethod
def get_visible_repositories(cls, username, limit, offset):
"""
Returns the repositories visible to the user with the given username, if any.
"""
raise NotImplementedError()
@classmethod
def create_blob_upload(cls, namespace_name, repo_name, upload_uuid, location_name,
storage_metadata):
"""
Creates a blob upload under the matching repository with the given UUID and metadata.
Returns whether the matching repository exists.
"""
raise NotImplementedError()
@classmethod
def blob_upload_by_uuid(cls, namespace_name, repo_name, upload_uuid):
"""
Searches for a blob upload with the given UUID under the given repository and returns it or None
if none.
"""
raise NotImplementedError()
@classmethod
def update_blob_upload(cls, blob_upload):
"""
Saves any changes to the blob upload object given to the backing data store.
Fields that can change:
- uncompressed_byte_count
- piece_hashes
- piece_sha_state
- storage_metadata
- byte_count
- chunk_count
- sha_state
"""
raise NotImplementedError()
@classmethod
def delete_blob_upload(cls, namespace_name, repo_name, uuid):
"""
Deletes the blob upload with the given uuid under the matching repository. If none, does
nothing.
"""
raise NotImplementedError()
@classmethod
def create_blob_and_temp_tag(cls, namespace_name, repo_name, blob_digest, blob_upload,
expiration_sec):
"""
Creates a blob and links a temporary tag with the specified expiration to it under the matching
repository.
"""
raise NotImplementedError()
@classmethod
def lookup_blobs_by_digest(cls, namespace_name, repo_name, digests):
"""
Returns all the blobs with matching digests found under the matching repository. If the
repository doesn't exist, returns {}.
"""
raise NotImplementedError()
@classmethod
def get_blob_by_digest(cls, namespace_name, repo_name, digest):
"""
Returns the blob with the given digest under the matching repository or None if none.
"""
raise NotImplementedError()
@classmethod
def save_bittorrent_pieces(cls, blob, piece_size, piece_bytes):
"""
Saves the BitTorrent piece hashes for the given blob.
"""
raise NotImplementedError()
@classmethod
def get_blob_path(cls, blob):
"""
Once everything is moved over, this could be in util.registry and not even touch the database.
"""
raise NotImplementedError()
def get_parents_docker_v1_metadata(namespace_name, repo_name, docker_image_id):
""" Returns an ordered list containing the Docker V1 metadata for each parent of the image
with the given docker ID under the matching repository. Returns an empty list if the image
was not found.
class PreOCIModel(DockerRegistryV2DataInterface):
"""
repo_image = model.image.get_repo_image(namespace_name, repo_name, docker_image_id)
if repo_image is None:
return []
parents = model.image.get_parent_images(namespace_name, repo_name, repo_image)
return [_docker_v1_metadata(namespace_name, repo_name, image) for image in parents]
def create_manifest_and_update_tag(namespace_name, repo_name, tag_name, manifest_digest,
manifest_bytes):
""" Creates a new manifest with the given digest and byte data, and assigns the tag with the
given name under the matching repository to it.
PreOCIModel implements the data model for the v2 Docker Registry protocol using a database schema
before it was changed to support the OCI specification.
"""
try:
model.tag.associate_generated_tag_manifest(namespace_name, repo_name, tag_name,
manifest_digest, manifest_bytes)
except IntegrityError:
# It's already there!
pass
@classmethod
def create_repository(cls, namespace_name, repo_name, creating_user=None):
return model.repository.create_repository(namespace_name, repo_name, creating_user)
@classmethod
def repository_is_public(cls, namespace_name, repo_name):
return model.repository.repository_is_public(namespace_name, repo_name)
def synthesize_v1_image(repository, storage, image_id, created, comment, command, compat_json,
parent_image_id):
""" Synthesizes a V1 image under the specified repository, pointing to the given storage
and returns the V1 metadata for the synthesized image.
"""
repo = model.repository.get_repository(repository.namespace_name, repository.name)
if repo is None:
raise DataModelException('Unknown repository: %s/%s' % (repository.namespace_name,
repository.name))
@classmethod
def get_repository(cls, namespace_name, repo_name):
repo = model.repository.get_repository(namespace_name, repo_name)
if repo is None:
return None
return _repository_for_repo(repo)
parent_image = None
if parent_image_id is not None:
parent_image = model.image.get_image(repo, parent_image_id)
if parent_image is None:
raise DataModelException('Unknown parent image: %s' % parent_image_id)
@classmethod
def has_active_tag(cls, namespace_name, repo_name, tag_name):
try:
model.tag.get_active_tag(namespace_name, repo_name, tag_name)
return True
except database.RepositoryTag.DoesNotExist:
return False
storage_obj = model.storage.get_storage_by_uuid(storage.uuid)
if storage_obj is None:
raise DataModelException('Unknown storage: %s' % storage.uuid)
@classmethod
def get_manifest_by_tag(cls, namespace_name, repo_name, tag_name):
try:
manifest = model.tag.load_tag_manifest(namespace_name, repo_name, tag_name)
return ManifestJSON(digest=manifest.digest, json=manifest.json_data, media_type=_MEDIA_TYPE)
except model.InvalidManifestException:
return None
repo_image = model.image.synthesize_v1_image(repo, storage_obj, image_id, created, comment,
command, compat_json, parent_image)
return _docker_v1_metadata(repo.namespace_user.username, repo.name, repo_image)
@classmethod
def get_manifest_by_digest(cls, namespace_name, repo_name, digest):
try:
manifest = model.tag.load_manifest_by_digest(namespace_name, repo_name, digest)
return ManifestJSON(digest=digest, json=manifest.json_data, media_type=_MEDIA_TYPE)
except model.InvalidManifestException:
return None
def save_manifest(namespace_name, repo_name, tag_name, leaf_layer_docker_id, manifest_digest,
manifest_bytes):
""" Saves a manifest pointing to the given leaf image, with the given manifest, under the matching
repository as a tag with the given name.
"""
model.tag.store_tag_manifest(namespace_name, repo_name, tag_name, leaf_layer_docker_id,
manifest_digest, manifest_bytes)
def repository_tags(namespace_name, repo_name, limit, offset):
""" Returns the active tags under the repository with the given name and namespace. """
tags_query = model.tag.list_repository_tags(namespace_name, repo_name)
tags_query = tags_query.limit(limit).offset(offset)
def _tag_view(tag):
return Tag(
name=tag.name,
repository=RepositoryReference(
id=tag.repository_id,
name=repo_name,
namespace_name=namespace_name,
@classmethod
def delete_manifest_by_digest(cls, namespace_name, repo_name, digest):
def _tag_view(tag):
return Tag(
name=tag.name,
repository=RepositoryReference(
id=tag.repository_id,
name=repo_name,
namespace_name=namespace_name,
)
)
tags = model.tag.delete_manifest_by_digest(namespace_name, repo_name, digest)
return [_tag_view(tag) for tag in tags]
@classmethod
def get_docker_v1_metadata_by_tag(cls, namespace_name, repo_name, tag_name):
try:
repo_img = model.tag.get_tag_image(namespace_name, repo_name, tag_name, include_storage=True)
return _docker_v1_metadata(namespace_name, repo_name, repo_img)
except DataModelException:
return None
@classmethod
def get_docker_v1_metadata_by_image_id(cls, namespace_name, repo_name, docker_image_ids):
repo = model.repository.get_repository(namespace_name, repo_name)
if repo is None:
return {}
images_query = model.image.lookup_repository_images(repo, docker_image_ids)
return {image.docker_image_id: _docker_v1_metadata(namespace_name, repo_name, image)
for image in images_query}
@classmethod
def get_parents_docker_v1_metadata(cls, namespace_name, repo_name, docker_image_id):
repo_image = model.image.get_repo_image(namespace_name, repo_name, docker_image_id)
if repo_image is None:
return []
parents = model.image.get_parent_images(namespace_name, repo_name, repo_image)
return [_docker_v1_metadata(namespace_name, repo_name, image) for image in parents]
@classmethod
def create_manifest_and_update_tag(cls, namespace_name, repo_name, tag_name, manifest_digest,
manifest_bytes):
try:
model.tag.associate_generated_tag_manifest(namespace_name, repo_name, tag_name,
manifest_digest, manifest_bytes)
except IntegrityError:
# It's already there!
pass
@classmethod
def synthesize_v1_image(cls, repository, storage, image_id, created, comment, command,
compat_json, parent_image_id):
repo = model.repository.get_repository(repository.namespace_name, repository.name)
if repo is None:
raise DataModelException('Unknown repository: %s/%s' % (repository.namespace_name,
repository.name))
parent_image = None
if parent_image_id is not None:
parent_image = model.image.get_image(repo, parent_image_id)
if parent_image is None:
raise DataModelException('Unknown parent image: %s' % parent_image_id)
storage_obj = model.storage.get_storage_by_uuid(storage.uuid)
if storage_obj is None:
raise DataModelException('Unknown storage: %s' % storage.uuid)
repo_image = model.image.synthesize_v1_image(repo, storage_obj, image_id, created, comment,
command, compat_json, parent_image)
return _docker_v1_metadata(repo.namespace_user.username, repo.name, repo_image)
@classmethod
def save_manifest(cls, namespace_name, repo_name, tag_name, leaf_layer_docker_id, manifest_digest,
manifest_bytes):
model.tag.store_tag_manifest(namespace_name, repo_name, tag_name, leaf_layer_docker_id,
manifest_digest, manifest_bytes)
@classmethod
def repository_tags(cls, namespace_name, repo_name, limit, offset):
def _tag_view(tag):
return Tag(
name=tag.name,
repository=RepositoryReference(
id=tag.repository_id,
name=repo_name,
namespace_name=namespace_name,
)
)
tags_query = model.tag.list_repository_tags(namespace_name, repo_name)
tags_query = tags_query.limit(limit).offset(offset)
return [_tag_view(tag) for tag in tags_query]
@classmethod
def get_visible_repositories(cls, username, limit, offset):
query = model.repository.get_visible_repositories(username, include_public=(username is None))
query = query.limit(limit).offset(offset)
return [_repository_for_repo(repo) for repo in query]
@classmethod
def create_blob_upload(cls, namespace_name, repo_name, upload_uuid, location_name,
storage_metadata):
try:
model.blob.initiate_upload(namespace_name, repo_name, upload_uuid, location_name,
storage_metadata)
return True
except database.Repository.DoesNotExist:
return False
@classmethod
def blob_upload_by_uuid(cls, namespace_name, repo_name, upload_uuid):
try:
found = model.blob.get_blob_upload(namespace_name, repo_name, upload_uuid)
except model.InvalidBlobUpload:
return None
return BlobUpload(
repo_namespace_name=namespace_name,
repo_name=repo_name,
uuid=upload_uuid,
byte_count=found.byte_count,
uncompressed_byte_count=found.uncompressed_byte_count,
chunk_count=found.chunk_count,
sha_state=found.sha_state,
piece_sha_state=found.piece_sha_state,
piece_hashes=found.piece_hashes,
location_name=found.location.name,
storage_metadata=found.storage_metadata,
)
return [_tag_view(tag) for tag in tags_query]
@classmethod
def update_blob_upload(cls, blob_upload):
# Lookup the blob upload object.
try:
blob_upload_record = model.blob.get_blob_upload(blob_upload.repo_namespace_name,
blob_upload.repo_name, blob_upload.uuid)
except model.InvalidBlobUpload:
return
blob_upload_record.uncompressed_byte_count = blob_upload.uncompressed_byte_count
blob_upload_record.piece_hashes = blob_upload.piece_hashes
blob_upload_record.piece_sha_state = blob_upload.piece_sha_state
blob_upload_record.storage_metadata = blob_upload.storage_metadata
blob_upload_record.byte_count = blob_upload.byte_count
blob_upload_record.chunk_count = blob_upload.chunk_count
blob_upload_record.sha_state = blob_upload.sha_state
blob_upload_record.save()
def get_visible_repositories(username, limit, offset):
""" Returns the repositories visible to the user with the given username, if any. """
query = model.repository.get_visible_repositories(username, include_public=(username is None))
query = query.limit(limit).offset(offset)
return [repository_for_repo(repo) for repo in query]
@classmethod
def delete_blob_upload(cls, namespace_name, repo_name, uuid):
try:
found = model.blob.get_blob_upload(namespace_name, repo_name, uuid)
found.delete_instance()
except model.InvalidBlobUpload:
return
def create_blob_upload(namespace_name, repo_name, upload_uuid, location_name, storage_metadata):
""" Creates a blob upload under the matching repository with the given UUID and metadata.
Returns whether the matching repository exists.
"""
try:
model.blob.initiate_upload(namespace_name, repo_name, upload_uuid, location_name,
storage_metadata)
return True
except database.Repository.DoesNotExist:
return False
def blob_upload_by_uuid(namespace_name, repo_name, upload_uuid):
""" Searches for a blob upload with the given UUID under the given repository and returns it
or None if none.
"""
try:
found = model.blob.get_blob_upload(namespace_name, repo_name, upload_uuid)
except model.InvalidBlobUpload:
return None
return BlobUpload(
repo_namespace_name=namespace_name,
repo_name=repo_name,
uuid=upload_uuid,
byte_count=found.byte_count,
uncompressed_byte_count=found.uncompressed_byte_count,
chunk_count=found.chunk_count,
sha_state=found.sha_state,
piece_sha_state=found.piece_sha_state,
piece_hashes=found.piece_hashes,
location_name=found.location.name,
storage_metadata=found.storage_metadata,
)
def update_blob_upload(blob_upload):
""" Saves any changes to the blob upload object given to the backing data store.
Fields that can change:
- uncompressed_byte_count
- piece_hashes
- piece_sha_state
- storage_metadata
- byte_count
- chunk_count
- sha_state
"""
# Lookup the blob upload object.
try:
blob_upload_record = model.blob.get_blob_upload(blob_upload.repo_namespace_name,
blob_upload.repo_name, blob_upload.uuid)
except model.InvalidBlobUpload:
return
blob_upload_record.uncompressed_byte_count = blob_upload.uncompressed_byte_count
blob_upload_record.piece_hashes = blob_upload.piece_hashes
blob_upload_record.piece_sha_state = blob_upload.piece_sha_state
blob_upload_record.storage_metadata = blob_upload.storage_metadata
blob_upload_record.byte_count = blob_upload.byte_count
blob_upload_record.chunk_count = blob_upload.chunk_count
blob_upload_record.sha_state = blob_upload.sha_state
blob_upload_record.save()
def delete_blob_upload(namespace_name, repo_name, uuid):
""" Deletes the blob upload with the given uuid under the matching repository. If none, does
nothing.
"""
try:
found = model.blob.get_blob_upload(namespace_name, repo_name, uuid)
except model.InvalidBlobUpload:
return
found.delete_instance()
def create_blob_and_temp_tag(namespace_name, repo_name, blob_digest, blob_upload, expiration_sec):
""" Crates a blob and links a temporary tag with the specified expiration to it under the
matching repository.
"""
location_obj = model.storage.get_image_location_for_name(blob_upload.location_name)
blob_record = model.blob.store_blob_record_and_temp_link(namespace_name, repo_name,
blob_digest,
location_obj.id,
blob_upload.byte_count,
expiration_sec,
blob_upload.uncompressed_byte_count)
return Blob(
uuid=blob_record.uuid,
digest=blob_digest,
size=blob_upload.byte_count,
locations=[blob_upload.location_name],
)
def lookup_blobs_by_digest(namespace_name, repo_name, digests):
""" Returns all the blobs with matching digests found under the matching repository. If the
repository doesn't exist, returns {}.
"""
repo = model.repository.get_repository(namespace_name, repo_name)
if repo is None:
return {}
def _blob_view(blob_record):
@classmethod
def create_blob_and_temp_tag(cls, namespace_name, repo_name, blob_digest, blob_upload,
expiration_sec):
location_obj = model.storage.get_image_location_for_name(blob_upload.location_name)
blob_record = model.blob.store_blob_record_and_temp_link(namespace_name, repo_name,
blob_digest,
location_obj.id,
blob_upload.byte_count,
expiration_sec,
blob_upload.uncompressed_byte_count)
return Blob(
uuid=blob_record.uuid,
digest=blob_record.content_checksum,
size=blob_record.image_size,
locations=None, # Note: Locations is None in this case.
digest=blob_digest,
size=blob_upload.byte_count,
locations=[blob_upload.location_name],
)
query = model.storage.lookup_repo_storages_by_content_checksum(repo, digests)
return {storage.content_checksum: _blob_view(storage) for storage in query}
@classmethod
def lookup_blobs_by_digest(cls, namespace_name, repo_name, digests):
def _blob_view(blob_record):
return Blob(
uuid=blob_record.uuid,
digest=blob_record.content_checksum,
size=blob_record.image_size,
locations=None, # Note: Locations is None in this case.
)
repo = model.repository.get_repository(namespace_name, repo_name)
if repo is None:
return {}
query = model.storage.lookup_repo_storages_by_content_checksum(repo, digests)
return {storage.content_checksum: _blob_view(storage) for storage in query}
def get_blob_by_digest(namespace_name, repo_name, digest):
""" Returns the blob with the given digest under the matching repository or None if none. """
try:
blob_record = model.blob.get_repo_blob_by_digest(namespace_name, repo_name, digest)
return Blob(
uuid=blob_record.uuid,
digest=digest,
size=blob_record.image_size,
locations=blob_record.locations,
)
except model.BlobDoesNotExist:
return None
@classmethod
def get_blob_by_digest(cls, namespace_name, repo_name, digest):
try:
blob_record = model.blob.get_repo_blob_by_digest(namespace_name, repo_name, digest)
return Blob(
uuid=blob_record.uuid,
digest=digest,
size=blob_record.image_size,
locations=blob_record.locations,
)
except model.BlobDoesNotExist:
return None
@classmethod
def save_bittorrent_pieces(cls, blob, piece_size, piece_bytes):
blob_record = model.storage.get_storage_by_uuid(blob.uuid)
model.storage.save_torrent_info(blob_record, piece_size, piece_bytes)
def save_bittorrent_pieces(blob, piece_size, piece_bytes):
""" Saves the BitTorrent piece hashes for the given blob. """
blob_record = model.storage.get_storage_by_uuid(blob.uuid)
model.storage.save_torrent_info(blob_record, piece_size, piece_bytes)
def get_blob_path(blob):
# Once everything is moved over, this could be in util.registry and not even
# touch the database.
blob_record = model.storage.get_storage_by_uuid(blob.uuid)
return model.storage.get_layer_path(blob_record)
@classmethod
def get_blob_path(cls, blob):
blob_record = model.storage.get_storage_by_uuid(blob.uuid)
return model.storage.get_layer_path(blob_record)