Search in sources :

Example 6 with ProgressEventDispatcher

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

the class PushImageStep method makeList.

static ImmutableList<PushImageStep> makeList(BuildContext buildContext, ProgressEventDispatcher.Factory progressEventDispatcherFactory, RegistryClient registryClient, BlobDescriptor containerConfigurationDigestAndSize, Image builtImage, boolean manifestAlreadyExists) throws IOException {
    // Gets the image manifest to push.
    BuildableManifestTemplate manifestTemplate = new ImageToJsonTranslator(builtImage).getManifestTemplate(buildContext.getTargetFormat(), containerConfigurationDigestAndSize);
    DescriptorDigest manifestDigest = Digests.computeJsonDigest(manifestTemplate);
    Set<String> imageQualifiers = getImageQualifiers(buildContext, builtImage, manifestDigest);
    EventHandlers eventHandlers = buildContext.getEventHandlers();
    try (TimerEventDispatcher ignored = new TimerEventDispatcher(eventHandlers, "Preparing manifest pushers");
        ProgressEventDispatcher progressDispatcher = progressEventDispatcherFactory.create("launching manifest pushers", imageQualifiers.size())) {
        if (JibSystemProperties.skipExistingImages() && manifestAlreadyExists) {
            eventHandlers.dispatch(LogEvent.info("Skipping pushing manifest; already exists."));
            return ImmutableList.of();
        }
        return imageQualifiers.stream().map(qualifier -> new PushImageStep(buildContext, progressDispatcher.newChildProducer(), registryClient, manifestTemplate, qualifier, manifestDigest, containerConfigurationDigestAndSize.getDigest())).collect(ImmutableList.toImmutableList());
    }
}
Also used : TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) BuildableManifestTemplate(com.google.cloud.tools.jib.image.json.BuildableManifestTemplate) ImageToJsonTranslator(com.google.cloud.tools.jib.image.json.ImageToJsonTranslator) Set(java.util.Set) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) RegistryException(com.google.cloud.tools.jib.api.RegistryException) BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) Collectors(java.util.stream.Collectors) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) Digests(com.google.cloud.tools.jib.hash.Digests) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient) LogEvent(com.google.cloud.tools.jib.api.LogEvent) ImmutableList(com.google.common.collect.ImmutableList) JibSystemProperties(com.google.cloud.tools.jib.global.JibSystemProperties) ManifestTemplate(com.google.cloud.tools.jib.image.json.ManifestTemplate) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Collections(java.util.Collections) Image(com.google.cloud.tools.jib.image.Image) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) ImageToJsonTranslator(com.google.cloud.tools.jib.image.json.ImageToJsonTranslator) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) BuildableManifestTemplate(com.google.cloud.tools.jib.image.json.BuildableManifestTemplate)

Example 7 with ProgressEventDispatcher

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

the class StepsRunner method obtainBaseImageLayers.

// This method updates the given "preparedLayersCache" and should not be called concurrently.
@VisibleForTesting
List<Future<PreparedLayer>> obtainBaseImageLayers(Image baseImage, boolean layersRequiredLocally, Map<DescriptorDigest, Future<PreparedLayer>> preparedLayersCache, ProgressEventDispatcher.Factory progressDispatcherFactory) throws InterruptedException, ExecutionException {
    List<Future<PreparedLayer>> preparedLayers = new ArrayList<>();
    try (ProgressEventDispatcher progressDispatcher = progressDispatcherFactory.create("launching base image layer pullers", baseImage.getLayers().size())) {
        for (Layer layer : baseImage.getLayers()) {
            DescriptorDigest digest = layer.getBlobDescriptor().getDigest();
            Future<PreparedLayer> preparedLayer = preparedLayersCache.get(digest);
            if (preparedLayer != null) {
                progressDispatcher.dispatchProgress(1);
            } else {
                // If we haven't obtained this layer yet, launcher a puller.
                preparedLayer = executorService.submit(layersRequiredLocally ? ObtainBaseImageLayerStep.forForcedDownload(buildContext, progressDispatcher.newChildProducer(), layer, results.baseImagesAndRegistryClient.get().registryClient) : ObtainBaseImageLayerStep.forSelectiveDownload(buildContext, progressDispatcher.newChildProducer(), layer, results.baseImagesAndRegistryClient.get().registryClient, results.targetRegistryClient.get()));
                preparedLayersCache.put(digest, preparedLayer);
            }
            preparedLayers.add(preparedLayer);
        }
        return preparedLayers;
    }
}
Also used : ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) Layer(com.google.cloud.tools.jib.image.Layer) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 8 with ProgressEventDispatcher

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

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 9 with ProgressEventDispatcher

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

the class BuildManifestListOrSingleManifestStep method call.

@Override
public ManifestTemplate call() throws IOException {
    Preconditions.checkState(!builtImages.isEmpty(), "no images given");
    EventHandlers eventHandlers = buildContext.getEventHandlers();
    try (TimerEventDispatcher ignored = new TimerEventDispatcher(eventHandlers, DESCRIPTION);
        ProgressEventDispatcher ignored2 = progressEventDispatcherFactory.create("building a manifest list or a single manifest", 1)) {
        if (builtImages.size() == 1) {
            eventHandlers.dispatch(LogEvent.info("Building a single manifest"));
            ImageToJsonTranslator imageTranslator = new ImageToJsonTranslator(builtImages.get(0));
            BlobDescriptor configDescriptor = Digests.computeDigest(imageTranslator.getContainerConfiguration());
            return imageTranslator.getManifestTemplate(buildContext.getTargetFormat(), configDescriptor);
        }
        eventHandlers.dispatch(LogEvent.info("Building a manifest list"));
        return new ManifestListGenerator(builtImages).getManifestListTemplate(buildContext.getTargetFormat());
    }
}
Also used : BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) ImageToJsonTranslator(com.google.cloud.tools.jib.image.json.ImageToJsonTranslator) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) ManifestListGenerator(com.google.cloud.tools.jib.image.json.ManifestListGenerator)

Example 10 with ProgressEventDispatcher

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

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