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