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());
}
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);
}
Aggregations