def get_or_create_shared_blob(digest, byte_data, storage): """ Returns the ImageStorage blob with the given digest or, if not present, adds a row and writes the given byte data to the storage engine. This method is *only* to be used for shared blobs that are globally accessible, such as the special empty gzipped tar layer that Docker no longer pushes to us. """ assert digest assert byte_data is not None and isinstance(byte_data, bytes) assert storage try: return ImageStorage.get(content_checksum=digest) except ImageStorage.DoesNotExist: preferred = storage.preferred_locations[0] location_obj = ImageStorageLocation.get(name=preferred) record = ImageStorage.create(image_size=len(byte_data), content_checksum=digest) try: storage.put_content([preferred], storage_model.get_layer_path(record), byte_data) ImageStoragePlacement.create(storage=record, location=location_obj) except: logger.exception("Exception when trying to write special layer %s", digest) record.delete_instance() raise return record
def store_blob_record_and_temp_link_in_repo( repository_id, blob_digest, location_obj, byte_count, link_expiration_s, uncompressed_byte_count=None, ): """ Store a record of the blob and temporarily link it to the specified repository. """ assert blob_digest assert byte_count is not None with db_transaction(): try: storage = ImageStorage.get(content_checksum=blob_digest) save_changes = False if storage.image_size is None: storage.image_size = byte_count save_changes = True if storage.uncompressed_size is None and uncompressed_byte_count is not None: storage.uncompressed_size = uncompressed_byte_count save_changes = True if save_changes: storage.save() ImageStoragePlacement.get(storage=storage, location=location_obj) except ImageStorage.DoesNotExist: storage = ImageStorage.create( content_checksum=blob_digest, uploading=False, image_size=byte_count, uncompressed_size=uncompressed_byte_count, ) ImageStoragePlacement.create(storage=storage, location=location_obj) except ImageStoragePlacement.DoesNotExist: ImageStoragePlacement.create(storage=storage, location=location_obj) _temp_link_blob(repository_id, storage, link_expiration_s) return storage
def _create_blob(self, digest: str, size: int, manifest_id: int, repo_id: int): try: blob = ImageStorage.get(content_checksum=digest) except ImageStorage.DoesNotExist: # TODO: which size should we really be setting here? blob = ImageStorage.create(content_checksum=digest, image_size=size, compressed_size=size) try: ManifestBlob.get(manifest_id=manifest_id, blob=blob, repository_id=repo_id) except ManifestBlob.DoesNotExist: ManifestBlob.create(manifest_id=manifest_id, blob=blob, repository_id=repo_id) return blob
def test_connect_existing_blobs_to_new_manifest(self, create_repo): repo_ref = create_repo(self.orgname, self.upstream_repository, self.user) input_manifest = parse_manifest_from_bytes( Bytes.for_string_or_unicode(UBI8_8_4_MANIFEST_SCHEMA2), DOCKER_SCHEMA2_MANIFEST_CONTENT_TYPE, ) layer = input_manifest.manifest_dict["layers"][0] blob = ImageStorage.create( image_size=layer["size"], uncompressed_size=layer["size"], content_checksum=layer["digest"], ) proxy_model = ProxyModel( self.orgname, self.upstream_repository, self.user, ) proxy_model._create_manifest_and_retarget_tag(repo_ref, input_manifest, self.tag) blob_count = (ImageStorage.select().where( ImageStorage.content_checksum == blob.content_checksum).count()) assert blob_count == 1
def create_v1_storage(location_name): storage = ImageStorage.create(cas_path=False, uploading=True) location = get_image_location_for_name(location_name) ImageStoragePlacement.create(location=location.id, storage=storage) storage.locations = {location_name} return storage