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