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 synthesize_v1_image( repo, image_storage_id, storage_image_size, docker_image_id, created_date_str, comment, command, v1_json_metadata, parent_image=None, ): """ Find an existing image with this docker image id, and if none exists, write one with the specified metadata. """ ancestors = "/" if parent_image is not None: ancestors = "{0}{1}/".format(parent_image.ancestors, parent_image.id) created = None if created_date_str is not None: try: created = dateutil.parser.parse(created_date_str).replace(tzinfo=None) except: # parse raises different exceptions, so we cannot use a specific kind of handler here. pass # Get the aggregate size for the image. aggregate_size = _basequery.calculate_image_aggregate_size( ancestors, storage_image_size, parent_image ) try: return Image.create( docker_image_id=docker_image_id, ancestors=ancestors, comment=comment, command=command, v1_json_metadata=v1_json_metadata, created=created, storage=image_storage_id, repository=repo, parent=parent_image, aggregate_size=aggregate_size, ) except IntegrityError: return Image.get(docker_image_id=docker_image_id, repository=repo)