Search in sources :

Example 1 with CountingDigestOutputStream

use of com.google.cloud.tools.jib.hash.CountingDigestOutputStream in project jib by google.

the class BlobTest method verifyBlobWriteTo.

/**
 * Checks that the {@link Blob} streams the expected string.
 */
private void verifyBlobWriteTo(String expected, Blob blob) throws IOException {
    OutputStream outputStream = new ByteArrayOutputStream();
    BlobDescriptor blobDescriptor = blob.writeTo(outputStream);
    String output = outputStream.toString();
    Assert.assertEquals(expected, output);
    byte[] expectedBytes = expected.getBytes(StandardCharsets.UTF_8);
    Assert.assertEquals(expectedBytes.length, blobDescriptor.getSize());
    CountingDigestOutputStream countingDigestOutputStream = new CountingDigestOutputStream(Mockito.mock(OutputStream.class));
    countingDigestOutputStream.write(expectedBytes);
    DescriptorDigest expectedDigest = countingDigestOutputStream.toBlobDescriptor().getDigest();
    Assert.assertEquals(expectedDigest, blobDescriptor.getDigest());
}
Also used : CountingDigestOutputStream(com.google.cloud.tools.jib.hash.CountingDigestOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CountingDigestOutputStream(com.google.cloud.tools.jib.hash.CountingDigestOutputStream) DescriptorDigest(com.google.cloud.tools.jib.image.DescriptorDigest) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 2 with CountingDigestOutputStream

use of com.google.cloud.tools.jib.hash.CountingDigestOutputStream in project jib by google.

the class WriterBlob method writeTo.

@Override
public BlobDescriptor writeTo(OutputStream outputStream) throws IOException {
    CountingDigestOutputStream countingDigestOutputStream = new CountingDigestOutputStream(outputStream);
    writer.writeTo(countingDigestOutputStream);
    countingDigestOutputStream.flush();
    return countingDigestOutputStream.toBlobDescriptor();
}
Also used : CountingDigestOutputStream(com.google.cloud.tools.jib.hash.CountingDigestOutputStream)

Example 3 with CountingDigestOutputStream

use of com.google.cloud.tools.jib.hash.CountingDigestOutputStream in project jib by google.

the class CacheWriterTest method getExpectedLayer.

/**
 * @return the expected layer to test against, represented by the {@code resourceBlob} resource
 *     file
 */
private ExpectedLayer getExpectedLayer() throws IOException {
    String expectedBlobAString = new String(Files.readAllBytes(resourceBlob), StandardCharsets.UTF_8);
    // Gets the expected content descriptor, diff ID, and compressed BLOB.
    ByteArrayOutputStream compressedBlobOutputStream = new ByteArrayOutputStream();
    CountingDigestOutputStream compressedDigestOutputStream = new CountingDigestOutputStream(compressedBlobOutputStream);
    CountingDigestOutputStream uncompressedDigestOutputStream;
    try (GZIPOutputStream compressorStream = new GZIPOutputStream(compressedDigestOutputStream)) {
        uncompressedDigestOutputStream = new CountingDigestOutputStream(compressorStream);
        uncompressedDigestOutputStream.write(expectedBlobAString.getBytes(StandardCharsets.UTF_8));
    }
    BlobDescriptor expectedBlobADescriptor = compressedDigestOutputStream.toBlobDescriptor();
    DescriptorDigest expectedBlobADiffId = uncompressedDigestOutputStream.toBlobDescriptor().getDigest();
    ByteArrayInputStream compressedBlobInputStream = new ByteArrayInputStream(compressedBlobOutputStream.toByteArray());
    Blob blob = Blobs.from(compressedBlobInputStream);
    return new ExpectedLayer(expectedBlobADescriptor, expectedBlobADiffId, blob);
}
Also used : CountingDigestOutputStream(com.google.cloud.tools.jib.hash.CountingDigestOutputStream) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) Blob(com.google.cloud.tools.jib.blob.Blob) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DescriptorDigest(com.google.cloud.tools.jib.image.DescriptorDigest) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with CountingDigestOutputStream

use of com.google.cloud.tools.jib.hash.CountingDigestOutputStream in project jib by google.

the class BuildAndPushContainerConfigurationStep method afterBaseImageLayerFuturesFuture.

/**
 * Depends on {@code pushAuthorizationFuture}, {@code pullBaseImageLayerFuturesFuture.get()}, and
 * {@code buildApplicationLayerFutures}.
 */
private BlobDescriptor afterBaseImageLayerFuturesFuture() throws ExecutionException, InterruptedException, LayerPropertyNotFoundException, IOException, RegistryException {
    try (Timer timer = new Timer(buildConfiguration.getBuildLogger(), DESCRIPTION)) {
        RegistryClient registryClient = new RegistryClient(NonBlockingFutures.get(pushAuthorizationFuture), buildConfiguration.getTargetRegistry(), buildConfiguration.getTargetRepository()).setTimer(timer);
        // Constructs the image.
        Image image = new Image();
        for (Future<CachedLayer> cachedLayerFuture : NonBlockingFutures.get(pullBaseImageLayerFuturesFuture)) {
            image.addLayer(NonBlockingFutures.get(cachedLayerFuture));
        }
        for (Future<CachedLayer> cachedLayerFuture : buildApplicationLayerFutures) {
            image.addLayer(NonBlockingFutures.get(cachedLayerFuture));
        }
        image.setEnvironment(buildConfiguration.getEnvironment());
        image.setEntrypoint(entrypoint);
        ImageToJsonTranslator imageToJsonTranslator = new ImageToJsonTranslator(image);
        // Gets the container configuration content descriptor.
        Blob containerConfigurationBlob = imageToJsonTranslator.getContainerConfigurationBlob();
        CountingDigestOutputStream digestOutputStream = new CountingDigestOutputStream(ByteStreams.nullOutputStream());
        containerConfigurationBlob.writeTo(digestOutputStream);
        BlobDescriptor containerConfigurationBlobDescriptor = digestOutputStream.toBlobDescriptor();
        timer.lap("Pushing container configuration " + containerConfigurationBlobDescriptor.getDigest());
        // TODO: Use PushBlobStep.
        // Pushes the container configuration.
        registryClient.pushBlob(containerConfigurationBlobDescriptor.getDigest(), containerConfigurationBlob);
        return containerConfigurationBlobDescriptor;
    }
}
Also used : CountingDigestOutputStream(com.google.cloud.tools.jib.hash.CountingDigestOutputStream) Blob(com.google.cloud.tools.jib.blob.Blob) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) Timer(com.google.cloud.tools.jib.Timer) ImageToJsonTranslator(com.google.cloud.tools.jib.image.json.ImageToJsonTranslator) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) Image(com.google.cloud.tools.jib.image.Image)

Example 5 with CountingDigestOutputStream

use of com.google.cloud.tools.jib.hash.CountingDigestOutputStream 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)

Aggregations

CountingDigestOutputStream (com.google.cloud.tools.jib.hash.CountingDigestOutputStream)7 DescriptorDigest (com.google.cloud.tools.jib.image.DescriptorDigest)4 BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)3 Blob (com.google.cloud.tools.jib.blob.Blob)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 GZIPOutputStream (java.util.zip.GZIPOutputStream)2 Timer (com.google.cloud.tools.jib.Timer)1 CachedLayer (com.google.cloud.tools.jib.cache.CachedLayer)1 Image (com.google.cloud.tools.jib.image.Image)1 UnwrittenLayer (com.google.cloud.tools.jib.image.UnwrittenLayer)1 ImageToJsonTranslator (com.google.cloud.tools.jib.image.json.ImageToJsonTranslator)1 V21ManifestTemplate (com.google.cloud.tools.jib.image.json.V21ManifestTemplate)1 RegistryClient (com.google.cloud.tools.jib.registry.RegistryClient)1 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 OutputStream (java.io.OutputStream)1 Path (java.nio.file.Path)1 Test (org.junit.Test)1