Search in sources :

Example 1 with TarStreamBuilder

use of com.google.cloud.tools.jib.tar.TarStreamBuilder in project jib by GoogleContainerTools.

the class ImageTarball method ociWriteTo.

private void ociWriteTo(OutputStream out) throws IOException {
    TarStreamBuilder tarStreamBuilder = new TarStreamBuilder();
    OciManifestTemplate manifest = new OciManifestTemplate();
    // Adds all the layers to the tarball and manifest
    for (Layer layer : image.getLayers()) {
        DescriptorDigest digest = layer.getBlobDescriptor().getDigest();
        long size = layer.getBlobDescriptor().getSize();
        tarStreamBuilder.addBlobEntry(layer.getBlob(), size, BLOB_PREFIX + digest.getHash(), TAR_ENTRY_MODIFICATION_TIME);
        manifest.addLayer(size, digest);
    }
    // Adds the container configuration to the tarball and manifest
    JsonTemplate containerConfiguration = new ImageToJsonTranslator(image).getContainerConfiguration();
    BlobDescriptor configDescriptor = Digests.computeDigest(containerConfiguration);
    manifest.setContainerConfiguration(configDescriptor.getSize(), configDescriptor.getDigest());
    tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(containerConfiguration), BLOB_PREFIX + configDescriptor.getDigest().getHash(), TAR_ENTRY_MODIFICATION_TIME);
    // Adds the manifest to the tarball
    BlobDescriptor manifestDescriptor = Digests.computeDigest(manifest);
    tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(manifest), BLOB_PREFIX + manifestDescriptor.getDigest().getHash(), TAR_ENTRY_MODIFICATION_TIME);
    // Adds the oci-layout and index.json
    tarStreamBuilder.addByteEntry("{\"imageLayoutVersion\": \"1.0.0\"}".getBytes(StandardCharsets.UTF_8), "oci-layout", TAR_ENTRY_MODIFICATION_TIME);
    OciIndexTemplate index = new OciIndexTemplate();
    // TODO: figure out how to tag with allTargetImageTags
    index.addManifest(manifestDescriptor, imageReference.toStringWithQualifier());
    tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(index), "index.json", TAR_ENTRY_MODIFICATION_TIME);
    tarStreamBuilder.writeAsTarArchiveTo(out);
}
Also used : OciManifestTemplate(com.google.cloud.tools.jib.image.json.OciManifestTemplate) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) ImageToJsonTranslator(com.google.cloud.tools.jib.image.json.ImageToJsonTranslator) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) OciIndexTemplate(com.google.cloud.tools.jib.image.json.OciIndexTemplate) JsonTemplate(com.google.cloud.tools.jib.json.JsonTemplate) TarStreamBuilder(com.google.cloud.tools.jib.tar.TarStreamBuilder)

Example 2 with TarStreamBuilder

use of com.google.cloud.tools.jib.tar.TarStreamBuilder in project jib by GoogleContainerTools.

the class ImageTarball method dockerWriteTo.

private void dockerWriteTo(OutputStream out) throws IOException {
    TarStreamBuilder tarStreamBuilder = new TarStreamBuilder();
    DockerManifestEntryTemplate manifestTemplate = new DockerManifestEntryTemplate();
    // Adds all the layers to the tarball and manifest.
    for (Layer layer : image.getLayers()) {
        String layerName = layer.getBlobDescriptor().getDigest().getHash() + LAYER_FILE_EXTENSION;
        tarStreamBuilder.addBlobEntry(layer.getBlob(), layer.getBlobDescriptor().getSize(), layerName, TAR_ENTRY_MODIFICATION_TIME);
        manifestTemplate.addLayerFile(layerName);
    }
    // Adds the container configuration to the tarball.
    JsonTemplate containerConfiguration = new ImageToJsonTranslator(image).getContainerConfiguration();
    tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(containerConfiguration), CONTAINER_CONFIGURATION_JSON_FILE_NAME, TAR_ENTRY_MODIFICATION_TIME);
    // Adds the manifest to tarball.
    for (String tag : allTargetImageTags) {
        manifestTemplate.addRepoTag(imageReference.withQualifier(tag).toStringWithQualifier());
    }
    tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(Collections.singletonList(manifestTemplate)), MANIFEST_JSON_FILE_NAME, TAR_ENTRY_MODIFICATION_TIME);
    tarStreamBuilder.writeAsTarArchiveTo(out);
}
Also used : ImageToJsonTranslator(com.google.cloud.tools.jib.image.json.ImageToJsonTranslator) DockerManifestEntryTemplate(com.google.cloud.tools.jib.docker.json.DockerManifestEntryTemplate) JsonTemplate(com.google.cloud.tools.jib.json.JsonTemplate) TarStreamBuilder(com.google.cloud.tools.jib.tar.TarStreamBuilder)

Example 3 with TarStreamBuilder

use of com.google.cloud.tools.jib.tar.TarStreamBuilder in project jib by GoogleContainerTools.

the class ReproducibleLayerBuilder method build.

/**
 * Builds and returns the layer {@link Blob}.
 *
 * @return the new layer
 * @throws IOException if an I/O error occurs
 */
public Blob build() throws IOException {
    UniqueTarArchiveEntries uniqueTarArchiveEntries = new UniqueTarArchiveEntries();
    // Adds all the layer entries as tar entries.
    for (FileEntry layerEntry : layerEntries) {
        // Adds the entries to uniqueTarArchiveEntries, which makes sure all entries are unique and
        // adds parent directories for each extraction path.
        TarArchiveEntry entry = new TarArchiveEntry(layerEntry.getSourceFile(), layerEntry.getExtractionPath().toString());
        // Sets the entry's permissions by masking out the permission bits from the entry's mode (the
        // lowest 9 bits) then using a bitwise OR to set them to the layerEntry's permissions.
        entry.setMode((entry.getMode() & ~0777) | layerEntry.getPermissions().getPermissionBits());
        entry.setModTime(layerEntry.getModificationTime().toEpochMilli());
        setUserAndGroup(entry, layerEntry);
        uniqueTarArchiveEntries.add(entry);
    }
    // Gets the entries sorted by extraction path.
    List<TarArchiveEntry> sortedFilesystemEntries = uniqueTarArchiveEntries.getSortedEntries();
    Set<String> names = new HashSet<>();
    // Adds all the files to a tar stream.
    TarStreamBuilder tarStreamBuilder = new TarStreamBuilder();
    for (TarArchiveEntry entry : sortedFilesystemEntries) {
        Verify.verify(!names.contains(entry.getName()));
        names.add(entry.getName());
        tarStreamBuilder.addTarArchiveEntry(entry);
    }
    return Blobs.from(tarStreamBuilder::writeAsTarArchiveTo, false);
}
Also used : FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) HashSet(java.util.HashSet) TarStreamBuilder(com.google.cloud.tools.jib.tar.TarStreamBuilder)

Example 4 with TarStreamBuilder

use of com.google.cloud.tools.jib.tar.TarStreamBuilder in project jib by google.

the class ImageTarball method ociWriteTo.

private void ociWriteTo(OutputStream out) throws IOException {
    TarStreamBuilder tarStreamBuilder = new TarStreamBuilder();
    OciManifestTemplate manifest = new OciManifestTemplate();
    // Adds all the layers to the tarball and manifest
    for (Layer layer : image.getLayers()) {
        DescriptorDigest digest = layer.getBlobDescriptor().getDigest();
        long size = layer.getBlobDescriptor().getSize();
        tarStreamBuilder.addBlobEntry(layer.getBlob(), size, BLOB_PREFIX + digest.getHash(), TAR_ENTRY_MODIFICATION_TIME);
        manifest.addLayer(size, digest);
    }
    // Adds the container configuration to the tarball and manifest
    JsonTemplate containerConfiguration = new ImageToJsonTranslator(image).getContainerConfiguration();
    BlobDescriptor configDescriptor = Digests.computeDigest(containerConfiguration);
    manifest.setContainerConfiguration(configDescriptor.getSize(), configDescriptor.getDigest());
    tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(containerConfiguration), BLOB_PREFIX + configDescriptor.getDigest().getHash(), TAR_ENTRY_MODIFICATION_TIME);
    // Adds the manifest to the tarball
    BlobDescriptor manifestDescriptor = Digests.computeDigest(manifest);
    tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(manifest), BLOB_PREFIX + manifestDescriptor.getDigest().getHash(), TAR_ENTRY_MODIFICATION_TIME);
    // Adds the oci-layout and index.json
    tarStreamBuilder.addByteEntry("{\"imageLayoutVersion\": \"1.0.0\"}".getBytes(StandardCharsets.UTF_8), "oci-layout", TAR_ENTRY_MODIFICATION_TIME);
    OciIndexTemplate index = new OciIndexTemplate();
    // TODO: figure out how to tag with allTargetImageTags
    index.addManifest(manifestDescriptor, imageReference.toStringWithQualifier());
    tarStreamBuilder.addByteEntry(JsonTemplateMapper.toByteArray(index), "index.json", TAR_ENTRY_MODIFICATION_TIME);
    tarStreamBuilder.writeAsTarArchiveTo(out);
}
Also used : OciManifestTemplate(com.google.cloud.tools.jib.image.json.OciManifestTemplate) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) ImageToJsonTranslator(com.google.cloud.tools.jib.image.json.ImageToJsonTranslator) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) OciIndexTemplate(com.google.cloud.tools.jib.image.json.OciIndexTemplate) JsonTemplate(com.google.cloud.tools.jib.json.JsonTemplate) TarStreamBuilder(com.google.cloud.tools.jib.tar.TarStreamBuilder)

Example 5 with TarStreamBuilder

use of com.google.cloud.tools.jib.tar.TarStreamBuilder in project jib by google.

the class ReproducibleLayerBuilder method build.

/**
 * Builds and returns the layer {@link Blob}.
 *
 * @return the new layer
 * @throws IOException if an I/O error occurs
 */
public Blob build() throws IOException {
    UniqueTarArchiveEntries uniqueTarArchiveEntries = new UniqueTarArchiveEntries();
    // Adds all the layer entries as tar entries.
    for (FileEntry layerEntry : layerEntries) {
        // Adds the entries to uniqueTarArchiveEntries, which makes sure all entries are unique and
        // adds parent directories for each extraction path.
        TarArchiveEntry entry = new TarArchiveEntry(layerEntry.getSourceFile(), layerEntry.getExtractionPath().toString());
        // Sets the entry's permissions by masking out the permission bits from the entry's mode (the
        // lowest 9 bits) then using a bitwise OR to set them to the layerEntry's permissions.
        entry.setMode((entry.getMode() & ~0777) | layerEntry.getPermissions().getPermissionBits());
        entry.setModTime(layerEntry.getModificationTime().toEpochMilli());
        setUserAndGroup(entry, layerEntry);
        uniqueTarArchiveEntries.add(entry);
    }
    // Gets the entries sorted by extraction path.
    List<TarArchiveEntry> sortedFilesystemEntries = uniqueTarArchiveEntries.getSortedEntries();
    Set<String> names = new HashSet<>();
    // Adds all the files to a tar stream.
    TarStreamBuilder tarStreamBuilder = new TarStreamBuilder();
    for (TarArchiveEntry entry : sortedFilesystemEntries) {
        Verify.verify(!names.contains(entry.getName()));
        names.add(entry.getName());
        tarStreamBuilder.addTarArchiveEntry(entry);
    }
    return Blobs.from(tarStreamBuilder::writeAsTarArchiveTo, false);
}
Also used : FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) HashSet(java.util.HashSet) TarStreamBuilder(com.google.cloud.tools.jib.tar.TarStreamBuilder)

Aggregations

TarStreamBuilder (com.google.cloud.tools.jib.tar.TarStreamBuilder)7 ImageToJsonTranslator (com.google.cloud.tools.jib.image.json.ImageToJsonTranslator)4 JsonTemplate (com.google.cloud.tools.jib.json.JsonTemplate)4 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)3 DescriptorDigest (com.google.cloud.tools.jib.api.DescriptorDigest)2 FileEntry (com.google.cloud.tools.jib.api.buildplan.FileEntry)2 BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)2 DockerManifestEntryTemplate (com.google.cloud.tools.jib.docker.json.DockerManifestEntryTemplate)2 OciIndexTemplate (com.google.cloud.tools.jib.image.json.OciIndexTemplate)2 OciManifestTemplate (com.google.cloud.tools.jib.image.json.OciManifestTemplate)2 HashSet (java.util.HashSet)2 DirectoryWalker (com.google.cloud.tools.jib.filesystem.DirectoryWalker)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 List (java.util.List)1