2015-07-15 21:25:41 +00:00
|
|
|
from uuid import uuid4
|
2015-07-06 19:00:07 +00:00
|
|
|
|
2015-12-03 21:19:22 +00:00
|
|
|
from data.model import (tag, _basequery, BlobDoesNotExist, InvalidBlobUpload, db_transaction,
|
|
|
|
storage as storage_model, InvalidImageException)
|
2015-07-15 21:25:41 +00:00
|
|
|
from data.database import (Repository, Namespace, ImageStorage, Image, ImageStorageLocation,
|
2015-08-12 20:39:32 +00:00
|
|
|
ImageStoragePlacement, BlobUpload)
|
2015-07-06 19:00:07 +00:00
|
|
|
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
def get_repo_blob_by_digest(namespace, repo_name, blob_digest):
|
|
|
|
""" Find the content-addressable blob linked to the specified repository.
|
|
|
|
"""
|
2015-12-03 21:19:22 +00:00
|
|
|
try:
|
|
|
|
storage_id_query = (ImageStorage
|
2015-12-30 22:20:40 +00:00
|
|
|
.select(ImageStorage.id)
|
|
|
|
.join(Image)
|
|
|
|
.join(Repository)
|
|
|
|
.join(Namespace, on=(Namespace.id == Repository.namespace_user))
|
|
|
|
.where(Repository.name == repo_name, Namespace.username == namespace,
|
|
|
|
ImageStorage.content_checksum == blob_digest,
|
|
|
|
ImageStorage.uploading == False)
|
|
|
|
.limit(1))
|
2015-07-06 19:00:07 +00:00
|
|
|
|
2015-12-03 21:19:22 +00:00
|
|
|
return storage_model.get_storage_by_subquery(storage_id_query)
|
|
|
|
except InvalidImageException:
|
|
|
|
raise BlobDoesNotExist('Blob does not exist with digest: {0}'.format(blob_digest))
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
|
2015-08-14 16:51:04 +00:00
|
|
|
def store_blob_record_and_temp_link(namespace, repo_name, blob_digest, location_obj, byte_count,
|
2015-11-30 19:25:01 +00:00
|
|
|
link_expiration_s, uncompressed_byte_count=None):
|
2015-07-15 21:25:41 +00:00
|
|
|
""" Store a record of the blob and temporarily link it to the specified repository.
|
|
|
|
"""
|
|
|
|
random_image_name = str(uuid4())
|
2015-08-12 20:39:32 +00:00
|
|
|
with db_transaction():
|
2015-07-15 21:25:41 +00:00
|
|
|
repo = _basequery.get_existing_repository(namespace, repo_name)
|
|
|
|
try:
|
2015-11-04 21:18:53 +00:00
|
|
|
storage = ImageStorage.get(content_checksum=blob_digest)
|
2015-08-14 16:51:04 +00:00
|
|
|
storage.image_size = byte_count
|
2015-11-30 19:25:01 +00:00
|
|
|
storage.uncompressed_size = uncompressed_byte_count
|
2015-08-14 16:51:04 +00:00
|
|
|
storage.save()
|
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
ImageStoragePlacement.get(storage=storage, location=location_obj)
|
2015-07-15 21:25:41 +00:00
|
|
|
except ImageStorage.DoesNotExist:
|
2015-11-06 23:18:29 +00:00
|
|
|
storage = ImageStorage.create(content_checksum=blob_digest, uploading=False,
|
|
|
|
image_size=byte_count)
|
2015-08-12 20:39:32 +00:00
|
|
|
ImageStoragePlacement.create(storage=storage, location=location_obj)
|
2015-07-15 21:25:41 +00:00
|
|
|
except ImageStoragePlacement.DoesNotExist:
|
2015-08-12 20:39:32 +00:00
|
|
|
ImageStoragePlacement.create(storage=storage, location=location_obj)
|
2015-07-06 19:00:07 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
# Create a temporary link into the repository, to be replaced by the v1 metadata later
|
|
|
|
# and create a temporary tag to reference it
|
|
|
|
image = Image.create(storage=storage, docker_image_id=random_image_name, repository=repo)
|
|
|
|
tag.create_temporary_hidden_tag(repo, image, link_expiration_s)
|
2015-08-12 20:39:32 +00:00
|
|
|
|
2015-12-30 22:19:19 +00:00
|
|
|
return storage
|
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
|
|
|
|
def get_blob_upload(namespace, repo_name, upload_uuid):
|
|
|
|
""" Load the upload which is already in progress.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return (BlobUpload
|
|
|
|
.select()
|
|
|
|
.join(Repository)
|
|
|
|
.join(Namespace, on=(Namespace.id == Repository.namespace_user))
|
|
|
|
.where(Repository.name == repo_name, Namespace.username == namespace,
|
|
|
|
BlobUpload.uuid == upload_uuid)
|
|
|
|
.get())
|
|
|
|
except BlobUpload.DoesNotExist:
|
|
|
|
raise InvalidBlobUpload()
|
|
|
|
|
|
|
|
|
2015-08-26 21:08:42 +00:00
|
|
|
def initiate_upload(namespace, repo_name, uuid, location_name, storage_metadata):
|
2015-08-12 20:39:32 +00:00
|
|
|
repo = _basequery.get_existing_repository(namespace, repo_name)
|
|
|
|
location = ImageStorageLocation.get(name=location_name)
|
2015-08-26 21:08:42 +00:00
|
|
|
return BlobUpload.create(repository=repo, location=location, uuid=uuid,
|
|
|
|
storage_metadata=storage_metadata)
|