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