def set_image_storage_metadata(docker_image_id, namespace_name, repository_name, image_size, uncompressed_size): """ Sets metadata that is specific to the binary storage of the data, irrespective of how it is used in the layer tree. """ if image_size is None: raise DataModelException('Empty image size field') try: image = (Image.select(Image, ImageStorage).join(Repository).join( Namespace, on=(Repository.namespace_user == Namespace.id )).switch(Image).join(ImageStorage).where( Repository.name == repository_name, Namespace.username == namespace_name, Image.docker_image_id == docker_image_id).get()) except ImageStorage.DoesNotExist: raise InvalidImageException( 'No image with specified id and repository') # We MUST do this here, it can't be done in the corresponding image call because the storage # has not yet been pushed image.aggregate_size = _basequery.calculate_image_aggregate_size( image.ancestors, image_size, image.parent) image.save() image.storage.image_size = image_size image.storage.uncompressed_size = uncompressed_size image.storage.save() return image.storage
def get_storage_by_uuid(storage_uuid): def filter_to_uuid(query): return query.where(ImageStorage.uuid == storage_uuid) try: return _get_storage(filter_to_uuid) except InvalidImageException: raise InvalidImageException("No storage found with uuid: %s", storage_uuid)
def get_image_by_id(namespace_name, repository_name, docker_image_id): """ Returns the repository image with the given Docker image ID or raises if not found. Includes the storage object. """ image = get_repo_image_and_storage(namespace_name, repository_name, docker_image_id) if not image: raise InvalidImageException( 'Unable to find image \'%s\' for repo \'%s/%s\'' % (docker_image_id, namespace_name, repository_name)) return image
def _get_storage(query_modifier): query = (ImageStoragePlacement.select( ImageStoragePlacement, ImageStorage).switch(ImageStoragePlacement).join(ImageStorage)) placements = list(query_modifier(query)) if not placements: raise InvalidImageException() found = placements[0].storage found.locations = { get_image_location_for_id(placement.location_id).name for placement in placements } return found