Search in sources :

Example 16 with BlobDescriptor

use of com.google.cloud.tools.jib.blob.BlobDescriptor in project jib by google.

the class CacheWriter method writeLayer.

/**
 * Builds an {@link UnwrittenLayer} from a {@link LayerBuilder} and compresses and writes the
 * {@link UnwrittenLayer}'s uncompressed layer content BLOB to cache.
 *
 * @param layerBuilder the layer builder
 * @return the cached layer
 */
public CachedLayer writeLayer(LayerBuilder layerBuilder) throws IOException, LayerPropertyNotFoundException {
    UnwrittenLayer unwrittenLayer = layerBuilder.build();
    // Writes to a temporary file first because the UnwrittenLayer needs to be written first to
    // obtain its digest.
    Path tempLayerFile = Files.createTempFile(cache.getCacheDirectory(), null, null);
    // TODO: Find a way to do this with java.nio.file
    tempLayerFile.toFile().deleteOnExit();
    // Writes the UnwrittenLayer layer BLOB to a file to convert into a CachedLayer.
    try (CountingDigestOutputStream compressedDigestOutputStream = new CountingDigestOutputStream(new BufferedOutputStream(Files.newOutputStream(tempLayerFile)))) {
        // Writes the layer with GZIP compression. The original bytes are captured as the layer's
        // diff ID and the bytes outputted from the GZIP compression are captured as the layer's
        // content descriptor.
        GZIPOutputStream compressorStream = new GZIPOutputStream(compressedDigestOutputStream);
        DescriptorDigest diffId = unwrittenLayer.getBlob().writeTo(compressorStream).getDigest();
        // The GZIPOutputStream must be closed in order to write out the remaining compressed data.
        compressorStream.close();
        BlobDescriptor compressedBlobDescriptor = compressedDigestOutputStream.toBlobDescriptor();
        // Renames the temporary layer file to the correct filename. If the file already exists, we
        // skip renaming and use the existing file. This happens if a new layer happens to have the
        // same content as a previously-cached layer.
        Path layerFile = getLayerFile(compressedBlobDescriptor.getDigest());
        Files.move(tempLayerFile, layerFile, StandardCopyOption.REPLACE_EXISTING);
        CachedLayer cachedLayer = new CachedLayer(layerFile, compressedBlobDescriptor, diffId);
        LayerMetadata layerMetadata = LayerMetadata.from(layerBuilder.getSourceFiles(), FileTime.from(Instant.now()));
        cache.addLayerToMetadata(cachedLayer, layerMetadata);
        return cachedLayer;
    }
}
Also used : Path(java.nio.file.Path) CountingDigestOutputStream(com.google.cloud.tools.jib.hash.CountingDigestOutputStream) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) GZIPOutputStream(java.util.zip.GZIPOutputStream) DescriptorDigest(com.google.cloud.tools.jib.image.DescriptorDigest) BufferedOutputStream(java.io.BufferedOutputStream) UnwrittenLayer(com.google.cloud.tools.jib.image.UnwrittenLayer)

Example 17 with BlobDescriptor

use of com.google.cloud.tools.jib.blob.BlobDescriptor in project jib by google.

the class CountingDigestOutputStream method toBlobDescriptor.

/**
 * Builds a {@link BlobDescriptor} with the hash and size of the bytes written.
 */
public BlobDescriptor toBlobDescriptor() {
    try {
        byte[] hashedBytes = digest.digest();
        // Encodes each hashed byte into 2-character hexadecimal representation.
        StringBuilder stringBuilder = new StringBuilder(2 * hashedBytes.length);
        for (byte b : hashedBytes) {
            stringBuilder.append(String.format("%02x", b));
        }
        String hash = stringBuilder.toString();
        DescriptorDigest digest = DescriptorDigest.fromHash(hash);
        return new BlobDescriptor(totalBytes, digest);
    } catch (DigestException ex) {
        throw new RuntimeException("SHA-256 algorithm produced invalid hash: " + ex.getMessage(), ex);
    }
}
Also used : BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) DescriptorDigest(com.google.cloud.tools.jib.image.DescriptorDigest) DigestException(java.security.DigestException)

Aggregations

BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)17 DescriptorDigest (com.google.cloud.tools.jib.image.DescriptorDigest)6 Path (java.nio.file.Path)5 Image (com.google.cloud.tools.jib.image.Image)4 Test (org.junit.Test)4 Blob (com.google.cloud.tools.jib.blob.Blob)3 CountingDigestOutputStream (com.google.cloud.tools.jib.hash.CountingDigestOutputStream)3 Layer (com.google.cloud.tools.jib.image.Layer)3 ReferenceLayer (com.google.cloud.tools.jib.image.ReferenceLayer)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Before (org.junit.Before)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 GZIPOutputStream (java.util.zip.GZIPOutputStream)2 HttpResponseException (com.google.api.client.http.HttpResponseException)1 Timer (com.google.cloud.tools.jib.Timer)1 CachedLayer (com.google.cloud.tools.jib.cache.CachedLayer)1 CacheMetadataLayerObjectTemplate (com.google.cloud.tools.jib.cache.json.CacheMetadataLayerObjectTemplate)1 CacheMetadataLayerPropertiesObjectTemplate (com.google.cloud.tools.jib.cache.json.CacheMetadataLayerPropertiesObjectTemplate)1 DigestOnlyLayer (com.google.cloud.tools.jib.image.DigestOnlyLayer)1 ImageLayers (com.google.cloud.tools.jib.image.ImageLayers)1