Search in sources :

Example 6 with TarStreamBuilder

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

the class LayerBuilder method build.

/**
 * Builds and returns the layer.
 */
public UnwrittenLayer build() throws IOException {
    List<TarArchiveEntry> filesystemEntries = new ArrayList<>();
    for (Path sourceFile : sourceFiles) {
        if (Files.isDirectory(sourceFile)) {
            new DirectoryWalker(sourceFile).filter(path -> !path.equals(sourceFile)).walk(path -> {
                /*
                   * Builds the same file path as in the source file for extraction. The iteration
                   * is necessary because the path needs to be in Unix-style.
                   */
                StringBuilder subExtractionPath = new StringBuilder(extractionPath);
                Path sourceFileRelativePath = sourceFile.getParent().relativize(path);
                for (Path sourceFileRelativePathComponent : sourceFileRelativePath) {
                    subExtractionPath.append('/').append(sourceFileRelativePathComponent);
                }
                filesystemEntries.add(new TarArchiveEntry(path.toFile(), subExtractionPath.toString()));
            });
        } else {
            TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(sourceFile.toFile(), extractionPath + "/" + sourceFile.getFileName());
            filesystemEntries.add(tarArchiveEntry);
        }
    }
    if (enableReproducibleBuilds) {
        makeListReproducible(filesystemEntries);
    }
    // Adds all the files to a tar stream.
    TarStreamBuilder tarStreamBuilder = new TarStreamBuilder();
    for (TarArchiveEntry entry : filesystemEntries) {
        tarStreamBuilder.addEntry(entry);
    }
    return new UnwrittenLayer(tarStreamBuilder.toBlob());
}
Also used : Path(java.nio.file.Path) List(java.util.List) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) Files(java.nio.file.Files) TarStreamBuilder(com.google.cloud.tools.jib.tar.TarStreamBuilder) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) IOException(java.io.IOException) Comparator(java.util.Comparator) Path(java.nio.file.Path) Collections(java.util.Collections) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) TarStreamBuilder(com.google.cloud.tools.jib.tar.TarStreamBuilder)

Example 7 with TarStreamBuilder

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

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)

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