Search in sources :

Example 6 with TimerEventDispatcher

use of com.google.cloud.tools.jib.builder.TimerEventDispatcher 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 7 with TimerEventDispatcher

use of com.google.cloud.tools.jib.builder.TimerEventDispatcher 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 8 with TimerEventDispatcher

use of com.google.cloud.tools.jib.builder.TimerEventDispatcher 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 9 with TimerEventDispatcher

use of com.google.cloud.tools.jib.builder.TimerEventDispatcher 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 10 with TimerEventDispatcher

use of com.google.cloud.tools.jib.builder.TimerEventDispatcher 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)

Aggregations

TimerEventDispatcher (com.google.cloud.tools.jib.builder.TimerEventDispatcher)32 ProgressEventDispatcher (com.google.cloud.tools.jib.builder.ProgressEventDispatcher)28 EventHandlers (com.google.cloud.tools.jib.event.EventHandlers)22 DescriptorDigest (com.google.cloud.tools.jib.api.DescriptorDigest)12 Image (com.google.cloud.tools.jib.image.Image)10 RegistryClient (com.google.cloud.tools.jib.registry.RegistryClient)10 IOException (java.io.IOException)10 BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)8 RegistryException (com.google.cloud.tools.jib.api.RegistryException)6 BuildContext (com.google.cloud.tools.jib.configuration.BuildContext)6 ImageToJsonTranslator (com.google.cloud.tools.jib.image.json.ImageToJsonTranslator)6 ImmutableList (com.google.common.collect.ImmutableList)6 Credential (com.google.cloud.tools.jib.api.Credential)4 LogEvent (com.google.cloud.tools.jib.api.LogEvent)4 ImagesAndRegistryClient (com.google.cloud.tools.jib.builder.steps.PullBaseImageStep.ImagesAndRegistryClient)4 Cache (com.google.cloud.tools.jib.cache.Cache)4 CachedLayer (com.google.cloud.tools.jib.cache.CachedLayer)4 JibSystemProperties (com.google.cloud.tools.jib.global.JibSystemProperties)4 Digests (com.google.cloud.tools.jib.hash.Digests)4 BuildableManifestTemplate (com.google.cloud.tools.jib.image.json.BuildableManifestTemplate)4