Search in sources :

Example 36 with DescriptorDigest

use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.

the class BlobPusherIntegrationTest method testPush.

@Test
public void testPush() throws DigestException, IOException, RegistryException {
    Blob testBlob = Blobs.from("crepecake");
    // Known digest for 'crepecake'
    DescriptorDigest testBlobDigest = DescriptorDigest.fromHash("52a9e4d4ba4333ce593707f98564fee1e6d898db0d3602408c0b2a6a424d357c");
    RegistryClient registryClient = RegistryClient.factory(EventHandlers.NONE, "localhost:5000", "testimage", httpClient).newRegistryClient();
    Assert.assertFalse(registryClient.pushBlob(testBlobDigest, testBlob, null, ignored -> {
    }));
}
Also used : Blob(com.google.cloud.tools.jib.blob.Blob) FailoverHttpClient(com.google.cloud.tools.jib.http.FailoverHttpClient) DigestException(java.security.DigestException) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Blobs(com.google.cloud.tools.jib.blob.Blobs) IOException(java.io.IOException) Test(org.junit.Test) RegistryException(com.google.cloud.tools.jib.api.RegistryException) Assert(org.junit.Assert) ClassRule(org.junit.ClassRule) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Blob(com.google.cloud.tools.jib.blob.Blob) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Test(org.junit.Test)

Example 37 with DescriptorDigest

use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.

the class CacheStorageWriter method writeUncompressedLayerBlobToDirectory.

/**
 * Writes an uncompressed {@code layerBlob} to the {@code layerDirectory}.
 *
 * @param uncompressedLayerBlob the uncompressed layer {@link Blob}
 * @param layerDirectory the directory for the layer
 * @return a {@link WrittenLayer} with the written layer information
 * @throws IOException if an I/O exception occurs
 */
private WrittenLayer writeUncompressedLayerBlobToDirectory(Blob uncompressedLayerBlob, Path layerDirectory) throws IOException {
    Path temporaryLayerFile = cacheStorageFiles.getTemporaryLayerFile(layerDirectory);
    try (CountingDigestOutputStream compressedDigestOutputStream = new CountingDigestOutputStream(new BufferedOutputStream(Files.newOutputStream(temporaryLayerFile)))) {
        // 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 layerDiffId = uncompressedLayerBlob.writeTo(compressorStream).getDigest();
        // The GZIPOutputStream must be closed in order to write out the remaining compressed data.
        compressorStream.close();
        BlobDescriptor blobDescriptor = compressedDigestOutputStream.computeDigest();
        DescriptorDigest layerDigest = blobDescriptor.getDigest();
        long layerSize = blobDescriptor.getSize();
        // Renames the temporary layer file to the correct filename.
        Path layerFile = layerDirectory.resolve(cacheStorageFiles.getLayerFilename(layerDiffId));
        moveIfDoesNotExist(temporaryLayerFile, layerFile);
        return new WrittenLayer(layerDigest, layerDiffId, layerSize);
    }
}
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.api.DescriptorDigest) BufferedOutputStream(java.io.BufferedOutputStream)

Example 38 with DescriptorDigest

use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.

the class CacheStorageWriter method writeCompressedLayerBlobToDirectory.

/**
 * Writes a compressed {@code layerBlob} to the {@code layerDirectory}.
 *
 * @param compressedLayerBlob the compressed layer {@link Blob}
 * @param layerDirectory the directory for the layer
 * @return a {@link WrittenLayer} with the written layer information
 * @throws IOException if an I/O exception occurs
 */
private WrittenLayer writeCompressedLayerBlobToDirectory(Blob compressedLayerBlob, Path layerDirectory) throws IOException {
    // Writes the layer file to the temporary directory.
    Path temporaryLayerFile = cacheStorageFiles.getTemporaryLayerFile(layerDirectory);
    BlobDescriptor layerBlobDescriptor;
    try (OutputStream fileOutputStream = new BufferedOutputStream(Files.newOutputStream(temporaryLayerFile))) {
        layerBlobDescriptor = compressedLayerBlob.writeTo(fileOutputStream);
    }
    // Gets the diff ID.
    DescriptorDigest layerDiffId = getDiffIdByDecompressingFile(temporaryLayerFile);
    // Renames the temporary layer file to the correct filename.
    Path layerFile = layerDirectory.resolve(cacheStorageFiles.getLayerFilename(layerDiffId));
    moveIfDoesNotExist(temporaryLayerFile, layerFile);
    return new WrittenLayer(layerBlobDescriptor.getDigest(), layerDiffId, layerBlobDescriptor.getSize());
}
Also used : Path(java.nio.file.Path) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) CountingDigestOutputStream(com.google.cloud.tools.jib.hash.CountingDigestOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) BufferedOutputStream(java.io.BufferedOutputStream)

Example 39 with DescriptorDigest

use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.

the class JsonToImageTranslator method toImage.

/**
 * Translates {@link BuildableManifestTemplate} to {@link Image}. Uses the corresponding {@link
 * ContainerConfigurationTemplate} to get the layer diff IDs.
 *
 * @param manifestTemplate the template containing the image layers.
 * @param containerConfigurationTemplate the template containing the diff IDs and container
 *     configuration properties.
 * @return the translated {@link Image}.
 * @throws LayerCountMismatchException if the manifest and configuration contain conflicting layer
 *     information.
 * @throws LayerPropertyNotFoundException if adding image layers fails.
 * @throws BadContainerConfigurationFormatException if the container configuration is in a bad
 *     format
 */
public static Image toImage(BuildableManifestTemplate manifestTemplate, ContainerConfigurationTemplate containerConfigurationTemplate) throws LayerCountMismatchException, LayerPropertyNotFoundException, BadContainerConfigurationFormatException {
    List<ReferenceNoDiffIdLayer> layers = new ArrayList<>();
    for (BuildableManifestTemplate.ContentDescriptorTemplate layerObjectTemplate : manifestTemplate.getLayers()) {
        if (layerObjectTemplate.getDigest() == null) {
            throw new IllegalArgumentException("All layers in the manifest template must have digest set");
        }
        layers.add(new ReferenceNoDiffIdLayer(new BlobDescriptor(layerObjectTemplate.getSize(), layerObjectTemplate.getDigest())));
    }
    List<DescriptorDigest> diffIds = containerConfigurationTemplate.getDiffIds();
    if (layers.size() != diffIds.size()) {
        throw new LayerCountMismatchException("Mismatch between image manifest and container configuration");
    }
    Image.Builder imageBuilder = Image.builder(manifestTemplate.getClass());
    for (int layerIndex = 0; layerIndex < layers.size(); layerIndex++) {
        ReferenceNoDiffIdLayer noDiffIdLayer = layers.get(layerIndex);
        DescriptorDigest diffId = diffIds.get(layerIndex);
        imageBuilder.addLayer(new ReferenceLayer(noDiffIdLayer.getBlobDescriptor(), diffId));
    }
    configureBuilderWithContainerConfiguration(imageBuilder, containerConfigurationTemplate);
    return imageBuilder.build();
}
Also used : LayerCountMismatchException(com.google.cloud.tools.jib.image.LayerCountMismatchException) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) ArrayList(java.util.ArrayList) Image(com.google.cloud.tools.jib.image.Image) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) ReferenceLayer(com.google.cloud.tools.jib.image.ReferenceLayer) ReferenceNoDiffIdLayer(com.google.cloud.tools.jib.image.ReferenceNoDiffIdLayer)

Example 40 with DescriptorDigest

use of com.google.cloud.tools.jib.api.DescriptorDigest in project jib by google.

the class JsonToImageTranslator method toImage.

/**
 * Translates {@link V21ManifestTemplate} to {@link Image}.
 *
 * @param manifestTemplate the template containing the image layers.
 * @return the translated {@link Image}.
 * @throws LayerPropertyNotFoundException if adding image layers fails.
 * @throws BadContainerConfigurationFormatException if the container configuration is in a bad
 *     format
 */
public static Image toImage(V21ManifestTemplate manifestTemplate) throws LayerPropertyNotFoundException, BadContainerConfigurationFormatException {
    Image.Builder imageBuilder = Image.builder(V21ManifestTemplate.class);
    // V21 layers are in reverse order of V22. (The first layer is the latest one.)
    for (DescriptorDigest digest : Lists.reverse(manifestTemplate.getLayerDigests())) {
        imageBuilder.addLayer(new DigestOnlyLayer(digest));
    }
    Optional<ContainerConfigurationTemplate> configuration = manifestTemplate.getContainerConfiguration();
    if (configuration.isPresent()) {
        configureBuilderWithContainerConfiguration(imageBuilder, configuration.get());
    }
    return imageBuilder.build();
}
Also used : DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) DigestOnlyLayer(com.google.cloud.tools.jib.image.DigestOnlyLayer) Image(com.google.cloud.tools.jib.image.Image)

Aggregations

DescriptorDigest (com.google.cloud.tools.jib.api.DescriptorDigest)112 Test (org.junit.Test)68 Path (java.nio.file.Path)28 BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)24 Blob (com.google.cloud.tools.jib.blob.Blob)18 ProgressEventDispatcher (com.google.cloud.tools.jib.builder.ProgressEventDispatcher)16 EventHandlers (com.google.cloud.tools.jib.event.EventHandlers)16 V22ManifestTemplate (com.google.cloud.tools.jib.image.json.V22ManifestTemplate)16 Image (com.google.cloud.tools.jib.image.Image)14 ByteArrayInputStream (java.io.ByteArrayInputStream)14 IOException (java.io.IOException)14 InputStream (java.io.InputStream)14 RegistryException (com.google.cloud.tools.jib.api.RegistryException)12 TimerEventDispatcher (com.google.cloud.tools.jib.builder.TimerEventDispatcher)12 ManifestTemplate (com.google.cloud.tools.jib.image.json.ManifestTemplate)10 RegistryClient (com.google.cloud.tools.jib.registry.RegistryClient)10 DigestException (java.security.DigestException)10 FailoverHttpClient (com.google.cloud.tools.jib.http.FailoverHttpClient)8 Layer (com.google.cloud.tools.jib.image.Layer)8 OutputStream (java.io.OutputStream)8