Search in sources :

Example 11 with ProgressEventDispatcher

use of com.google.cloud.tools.jib.builder.ProgressEventDispatcher in project jib by GoogleContainerTools.

the class BuildAndCacheApplicationLayerStep method call.

@Override
public PreparedLayer call() throws IOException, CacheCorruptedException {
    String description = String.format(DESCRIPTION, layerName);
    EventHandlers eventHandlers = buildContext.getEventHandlers();
    eventHandlers.dispatch(LogEvent.progress(description + "..."));
    try (ProgressEventDispatcher ignored = progressEventDispatcherFactory.create("building " + layerName + " layer", 1);
        TimerEventDispatcher ignored2 = new TimerEventDispatcher(eventHandlers, description)) {
        Cache cache = buildContext.getApplicationLayersCache();
        ImmutableList<FileEntry> layerEntries = ImmutableList.copyOf(layerConfiguration.getEntries());
        // Don't build the layer if it exists already.
        Optional<CachedLayer> optionalCachedLayer = cache.retrieve(layerEntries);
        if (optionalCachedLayer.isPresent()) {
            return new PreparedLayer.Builder(optionalCachedLayer.get()).setName(layerName).build();
        }
        Blob layerBlob = new ReproducibleLayerBuilder(layerEntries).build();
        CachedLayer cachedLayer = cache.writeUncompressedLayer(layerBlob, layerEntries);
        eventHandlers.dispatch(LogEvent.debug(description + " built " + cachedLayer.getDigest()));
        return new PreparedLayer.Builder(cachedLayer).setName(layerName).build();
    }
}
Also used : Blob(com.google.cloud.tools.jib.blob.Blob) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) ReproducibleLayerBuilder(com.google.cloud.tools.jib.image.ReproducibleLayerBuilder) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) ReproducibleLayerBuilder(com.google.cloud.tools.jib.image.ReproducibleLayerBuilder) Cache(com.google.cloud.tools.jib.cache.Cache)

Example 12 with ProgressEventDispatcher

use of com.google.cloud.tools.jib.builder.ProgressEventDispatcher in project jib by google.

the class BuildAndCacheApplicationLayerStep method call.

@Override
public PreparedLayer call() throws IOException, CacheCorruptedException {
    String description = String.format(DESCRIPTION, layerName);
    EventHandlers eventHandlers = buildContext.getEventHandlers();
    eventHandlers.dispatch(LogEvent.progress(description + "..."));
    try (ProgressEventDispatcher ignored = progressEventDispatcherFactory.create("building " + layerName + " layer", 1);
        TimerEventDispatcher ignored2 = new TimerEventDispatcher(eventHandlers, description)) {
        Cache cache = buildContext.getApplicationLayersCache();
        ImmutableList<FileEntry> layerEntries = ImmutableList.copyOf(layerConfiguration.getEntries());
        // Don't build the layer if it exists already.
        Optional<CachedLayer> optionalCachedLayer = cache.retrieve(layerEntries);
        if (optionalCachedLayer.isPresent()) {
            return new PreparedLayer.Builder(optionalCachedLayer.get()).setName(layerName).build();
        }
        Blob layerBlob = new ReproducibleLayerBuilder(layerEntries).build();
        CachedLayer cachedLayer = cache.writeUncompressedLayer(layerBlob, layerEntries);
        eventHandlers.dispatch(LogEvent.debug(description + " built " + cachedLayer.getDigest()));
        return new PreparedLayer.Builder(cachedLayer).setName(layerName).build();
    }
}
Also used : Blob(com.google.cloud.tools.jib.blob.Blob) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) ReproducibleLayerBuilder(com.google.cloud.tools.jib.image.ReproducibleLayerBuilder) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) ReproducibleLayerBuilder(com.google.cloud.tools.jib.image.ReproducibleLayerBuilder) Cache(com.google.cloud.tools.jib.cache.Cache)

Example 13 with ProgressEventDispatcher

use of com.google.cloud.tools.jib.builder.ProgressEventDispatcher in project jib by google.

the class BuildImageStep method call.

@Override
public Image call() throws LayerPropertyNotFoundException {
    try (ProgressEventDispatcher ignored = progressEventDispatcherFactory.create("building image format", 1);
        TimerEventDispatcher ignored2 = new TimerEventDispatcher(buildContext.getEventHandlers(), DESCRIPTION)) {
        // Constructs the image.
        Image.Builder imageBuilder = Image.builder(buildContext.getTargetFormat());
        // Base image layers
        baseImageLayers.forEach(imageBuilder::addLayer);
        // Passthrough config and count non-empty history entries
        int nonEmptyLayerCount = 0;
        for (HistoryEntry historyObject : baseImage.getHistory()) {
            imageBuilder.addHistory(historyObject);
            if (!historyObject.hasCorrespondingLayer()) {
                nonEmptyLayerCount++;
            }
        }
        imageBuilder.setArchitecture(baseImage.getArchitecture()).setOs(baseImage.getOs()).addEnvironment(baseImage.getEnvironment()).addLabels(baseImage.getLabels()).setHealthCheck(baseImage.getHealthCheck()).addExposedPorts(baseImage.getExposedPorts()).addVolumes(baseImage.getVolumes()).setUser(baseImage.getUser()).setWorkingDirectory(baseImage.getWorkingDirectory());
        ContainerConfiguration containerConfiguration = buildContext.getContainerConfiguration();
        // Add history elements for non-empty layers that don't have one yet
        Instant layerCreationTime = containerConfiguration.getCreationTime();
        for (int count = 0; count < baseImageLayers.size() - nonEmptyLayerCount; count++) {
            imageBuilder.addHistory(HistoryEntry.builder().setCreationTimestamp(layerCreationTime).setComment("auto-generated by Jib").build());
        }
        // Add built layers/configuration
        for (PreparedLayer applicationLayer : applicationLayers) {
            imageBuilder.addLayer(applicationLayer).addHistory(HistoryEntry.builder().setCreationTimestamp(layerCreationTime).setAuthor("Jib").setCreatedBy(buildContext.getToolName() + ":" + buildContext.getToolVersion()).setComment(applicationLayer.getName()).build());
        }
        imageBuilder.addEnvironment(containerConfiguration.getEnvironmentMap()).setCreated(containerConfiguration.getCreationTime()).setEntrypoint(computeEntrypoint(baseImage, containerConfiguration)).setProgramArguments(computeProgramArguments(baseImage, containerConfiguration)).addExposedPorts(containerConfiguration.getExposedPorts()).addVolumes(containerConfiguration.getVolumes()).addLabels(containerConfiguration.getLabels());
        if (containerConfiguration.getUser() != null) {
            imageBuilder.setUser(containerConfiguration.getUser());
        }
        if (containerConfiguration.getWorkingDirectory() != null) {
            imageBuilder.setWorkingDirectory(containerConfiguration.getWorkingDirectory().toString());
        }
        // Gets the container configuration content descriptor.
        return imageBuilder.build();
    }
}
Also used : ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) Instant(java.time.Instant) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) HistoryEntry(com.google.cloud.tools.jib.image.json.HistoryEntry) Image(com.google.cloud.tools.jib.image.Image) ContainerConfiguration(com.google.cloud.tools.jib.configuration.ContainerConfiguration)

Example 14 with ProgressEventDispatcher

use of com.google.cloud.tools.jib.builder.ProgressEventDispatcher in project jib by google.

the class LocalBaseImageSteps method cacheDockerImageTar.

@VisibleForTesting
static LocalImage cacheDockerImageTar(BuildContext buildContext, Path tarPath, ProgressEventDispatcher.Factory progressEventDispatcherFactory, TempDirectoryProvider tempDirectoryProvider) throws IOException, LayerCountMismatchException {
    ExecutorService executorService = buildContext.getExecutorService();
    Path destination = tempDirectoryProvider.newDirectory();
    try (TimerEventDispatcher ignored = new TimerEventDispatcher(buildContext.getEventHandlers(), "Extracting tar " + tarPath + " into " + destination)) {
        TarExtractor.extract(tarPath, destination);
        InputStream manifestStream = Files.newInputStream(destination.resolve("manifest.json"));
        DockerManifestEntryTemplate loadManifest = new ObjectMapper().configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true).readValue(manifestStream, DockerManifestEntryTemplate[].class)[0];
        manifestStream.close();
        Path configPath = destination.resolve(loadManifest.getConfig());
        ContainerConfigurationTemplate configurationTemplate = JsonTemplateMapper.readJsonFromFile(configPath, ContainerConfigurationTemplate.class);
        // Don't compute the digest of the loaded Java JSON instance.
        BlobDescriptor originalConfigDescriptor = Blobs.from(configPath).writeTo(ByteStreams.nullOutputStream());
        List<String> layerFiles = loadManifest.getLayerFiles();
        if (configurationTemplate.getLayerCount() != layerFiles.size()) {
            throw new LayerCountMismatchException("Invalid base image format: manifest contains " + layerFiles.size() + " layers, but container configuration contains " + configurationTemplate.getLayerCount() + " layers");
        }
        buildContext.getBaseImageLayersCache().writeLocalConfig(originalConfigDescriptor.getDigest(), configurationTemplate);
        // Check the first layer to see if the layers are compressed already. 'docker save' output
        // is uncompressed, but a jib-built tar has compressed layers.
        boolean layersAreCompressed = !layerFiles.isEmpty() && isGzipped(destination.resolve(layerFiles.get(0)));
        // Process layer blobs
        try (ProgressEventDispatcher progressEventDispatcher = progressEventDispatcherFactory.create("processing base image layers", layerFiles.size())) {
            // Start compressing layers in parallel
            List<Future<PreparedLayer>> preparedLayers = new ArrayList<>();
            for (int index = 0; index < layerFiles.size(); index++) {
                Path layerFile = destination.resolve(layerFiles.get(index));
                DescriptorDigest diffId = configurationTemplate.getLayerDiffId(index);
                ProgressEventDispatcher.Factory layerProgressDispatcherFactory = progressEventDispatcher.newChildProducer();
                preparedLayers.add(executorService.submit(() -> compressAndCacheTarLayer(buildContext.getBaseImageLayersCache(), diffId, layerFile, layersAreCompressed, layerProgressDispatcherFactory)));
            }
            return new LocalImage(preparedLayers, configurationTemplate);
        }
    }
}
Also used : Path(java.nio.file.Path) LayerCountMismatchException(com.google.cloud.tools.jib.image.LayerCountMismatchException) ContainerConfigurationTemplate(com.google.cloud.tools.jib.image.json.ContainerConfigurationTemplate) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) ArrayList(java.util.ArrayList) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) DockerManifestEntryTemplate(com.google.cloud.tools.jib.docker.json.DockerManifestEntryTemplate) ExecutorService(java.util.concurrent.ExecutorService) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) Future(java.util.concurrent.Future) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 15 with ProgressEventDispatcher

use of com.google.cloud.tools.jib.builder.ProgressEventDispatcher in project jib by google.

the class LocalBaseImageSteps method compressAndCacheTarLayer.

private static PreparedLayer compressAndCacheTarLayer(Cache cache, DescriptorDigest diffId, Path layerFile, boolean layersAreCompressed, ProgressEventDispatcher.Factory progressEventDispatcherFactory) throws IOException, CacheCorruptedException {
    try (ProgressEventDispatcher childDispatcher = progressEventDispatcherFactory.create("compressing layer " + diffId, Files.size(layerFile));
        ThrottledAccumulatingConsumer throttledProgressReporter = new ThrottledAccumulatingConsumer(childDispatcher::dispatchProgress)) {
        // Retrieve pre-compressed layer from cache
        Optional<CachedLayer> optionalLayer = cache.retrieveTarLayer(diffId);
        if (optionalLayer.isPresent()) {
            return new PreparedLayer.Builder(optionalLayer.get()).build();
        }
        // Just write layers that are already compressed
        if (layersAreCompressed) {
            return new PreparedLayer.Builder(cache.writeTarLayer(diffId, Blobs.from(layerFile))).build();
        }
        // Compress uncompressed layers while writing
        Blob compressedBlob = Blobs.from(outputStream -> {
            try (GZIPOutputStream compressorStream = new GZIPOutputStream(outputStream);
                NotifyingOutputStream notifyingOutputStream = new NotifyingOutputStream(compressorStream, throttledProgressReporter)) {
                Blobs.from(layerFile).writeTo(notifyingOutputStream);
            }
        }, true);
        return new PreparedLayer.Builder(cache.writeTarLayer(diffId, compressedBlob)).build();
    }
}
Also used : NotifyingOutputStream(com.google.cloud.tools.jib.http.NotifyingOutputStream) Blob(com.google.cloud.tools.jib.blob.Blob) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) GZIPOutputStream(java.util.zip.GZIPOutputStream) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) ThrottledAccumulatingConsumer(com.google.cloud.tools.jib.event.progress.ThrottledAccumulatingConsumer)

Aggregations

ProgressEventDispatcher (com.google.cloud.tools.jib.builder.ProgressEventDispatcher)35 TimerEventDispatcher (com.google.cloud.tools.jib.builder.TimerEventDispatcher)28 EventHandlers (com.google.cloud.tools.jib.event.EventHandlers)24 DescriptorDigest (com.google.cloud.tools.jib.api.DescriptorDigest)14 Image (com.google.cloud.tools.jib.image.Image)12 RegistryClient (com.google.cloud.tools.jib.registry.RegistryClient)10 IOException (java.io.IOException)10 BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)8 ImmutableList (com.google.common.collect.ImmutableList)8 ArrayList (java.util.ArrayList)8 RegistryException (com.google.cloud.tools.jib.api.RegistryException)6 Cache (com.google.cloud.tools.jib.cache.Cache)6 CachedLayer (com.google.cloud.tools.jib.cache.CachedLayer)6 ThrottledAccumulatingConsumer (com.google.cloud.tools.jib.event.progress.ThrottledAccumulatingConsumer)6 BuildableManifestTemplate (com.google.cloud.tools.jib.image.json.BuildableManifestTemplate)6 ImageToJsonTranslator (com.google.cloud.tools.jib.image.json.ImageToJsonTranslator)6 ManifestTemplate (com.google.cloud.tools.jib.image.json.ManifestTemplate)6 VisibleForTesting (com.google.common.annotations.VisibleForTesting)6 Credential (com.google.cloud.tools.jib.api.Credential)4 LogEvent (com.google.cloud.tools.jib.api.LogEvent)4