Search in sources :

Example 21 with TimerEventDispatcher

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

the class CheckManifestStep method call.

@Override
public Optional<ManifestAndDigest<ManifestTemplate>> call() throws IOException, RegistryException {
    DescriptorDigest manifestDigest = Digests.computeJsonDigest(manifestTemplate);
    EventHandlers eventHandlers = buildContext.getEventHandlers();
    try (TimerEventDispatcher ignored = new TimerEventDispatcher(eventHandlers, DESCRIPTION);
        ProgressEventDispatcher ignored2 = progressEventDispatcherFactory.create("checking existence of manifest for " + manifestDigest, 1)) {
        eventHandlers.dispatch(LogEvent.info("Checking existence of manifest for " + manifestDigest + "..."));
        if (!JibSystemProperties.skipExistingImages()) {
            eventHandlers.dispatch(LogEvent.info("Skipping manifest existence check; system property set to false"));
            return Optional.empty();
        }
        return registryClient.checkManifest(manifestDigest.toString());
    }
}
Also used : ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers)

Example 22 with TimerEventDispatcher

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

the class LoadDockerStep method call.

@Override
public BuildResult call() throws InterruptedException, IOException {
    EventHandlers eventHandlers = buildContext.getEventHandlers();
    try (TimerEventDispatcher ignored = new TimerEventDispatcher(eventHandlers, "Loading to Docker daemon")) {
        eventHandlers.dispatch(LogEvent.progress("Loading to Docker daemon..."));
        ImageTarball imageTarball = new ImageTarball(builtImage, buildContext.getTargetImageConfiguration().getImage(), buildContext.getAllTargetImageTags());
        // See https://github.com/GoogleContainerTools/jib/pull/1960#discussion_r321898390
        try (ProgressEventDispatcher progressEventDispatcher = progressEventDispatcherFactory.create("loading to Docker daemon", imageTarball.getTotalLayerSize());
            ThrottledAccumulatingConsumer throttledProgressReporter = new ThrottledAccumulatingConsumer(progressEventDispatcher::dispatchProgress)) {
            // Load the image to docker daemon.
            eventHandlers.dispatch(LogEvent.debug(dockerClient.load(imageTarball, throttledProgressReporter)));
            return BuildResult.fromImage(builtImage, buildContext.getTargetFormat());
        }
    }
}
Also used : ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) ImageTarball(com.google.cloud.tools.jib.image.ImageTarball) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) ThrottledAccumulatingConsumer(com.google.cloud.tools.jib.event.progress.ThrottledAccumulatingConsumer) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers)

Example 23 with TimerEventDispatcher

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

the class ObtainBaseImageLayerStep method call.

@Override
public PreparedLayer call() throws IOException, CacheCorruptedException, RegistryException {
    EventHandlers eventHandlers = buildContext.getEventHandlers();
    DescriptorDigest layerDigest = layer.getBlobDescriptor().getDigest();
    try (ProgressEventDispatcher progressEventDispatcher = progressEventDispatcherFactory.create("checking base image layer " + layerDigest, 1);
        TimerEventDispatcher ignored = new TimerEventDispatcher(eventHandlers, String.format(DESCRIPTION, layerDigest))) {
        StateInTarget stateInTarget = blobExistenceChecker.check(layerDigest);
        if (stateInTarget == StateInTarget.EXISTING) {
            eventHandlers.dispatch(LogEvent.info("Skipping pull; BLOB already exists on target registry : " + layer.getBlobDescriptor()));
            return new PreparedLayer.Builder(layer).setStateInTarget(stateInTarget).build();
        }
        Cache cache = buildContext.getBaseImageLayersCache();
        // Checks if the layer already exists in the cache.
        Optional<CachedLayer> optionalCachedLayer = cache.retrieve(layerDigest);
        if (optionalCachedLayer.isPresent()) {
            CachedLayer cachedLayer = optionalCachedLayer.get();
            return new PreparedLayer.Builder(cachedLayer).setStateInTarget(stateInTarget).build();
        } else if (buildContext.isOffline()) {
            throw new IOException("Cannot run Jib in offline mode; local Jib cache for base image is missing image layer " + layerDigest + ". Rerun Jib in online mode with \"-Djib.alwaysCacheBaseImage=true\" to " + "re-download the base image layers.");
        }
        try (ThrottledProgressEventDispatcherWrapper progressEventDispatcherWrapper = new ThrottledProgressEventDispatcherWrapper(progressEventDispatcher.newChildProducer(), "pulling base image layer " + layerDigest)) {
            CachedLayer cachedLayer = cache.writeCompressedLayer(Verify.verifyNotNull(registryClient).pullBlob(layerDigest, progressEventDispatcherWrapper::setProgressTarget, progressEventDispatcherWrapper::dispatchProgress));
            return new PreparedLayer.Builder(cachedLayer).setStateInTarget(stateInTarget).build();
        }
    }
}
Also used : ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) DescriptorDigest(com.google.cloud.tools.jib.api.DescriptorDigest) StateInTarget(com.google.cloud.tools.jib.builder.steps.PreparedLayer.StateInTarget) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) IOException(java.io.IOException) Cache(com.google.cloud.tools.jib.cache.Cache)

Example 24 with TimerEventDispatcher

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

the class AuthenticatePushStep method call.

@Override
public RegistryClient call() throws CredentialRetrievalException, IOException, RegistryException {
    String registry = buildContext.getTargetImageConfiguration().getImageRegistry();
    try (ProgressEventDispatcher progressDispatcher = progressEventDispatcherFactory.create("authenticating push to " + registry, 2);
        TimerEventDispatcher ignored2 = new TimerEventDispatcher(buildContext.getEventHandlers(), String.format(DESCRIPTION, registry))) {
        Credential credential = RegistryCredentialRetriever.getTargetImageCredential(buildContext).orElse(null);
        progressDispatcher.dispatchProgress(1);
        RegistryClient registryClient = buildContext.newTargetImageRegistryClientFactory().setCredential(credential).newRegistryClient();
        if (!registryClient.doPushBearerAuth()) {
            // server returned "WWW-Authenticate: Basic ..." (e.g., local Docker registry)
            if (credential != null && !credential.isOAuth2RefreshToken()) {
                registryClient.configureBasicAuth();
            }
        }
        return registryClient;
    }
}
Also used : Credential(com.google.cloud.tools.jib.api.Credential) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) RegistryClient(com.google.cloud.tools.jib.registry.RegistryClient)

Example 25 with TimerEventDispatcher

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

the class JibContainerBuilder method containerize.

/**
 * Builds the container.
 *
 * @param containerizer the {@link Containerizer} that configures how to containerize
 * @return the built container
 * @throws IOException if an I/O exception occurs
 * @throws CacheDirectoryCreationException if a directory to be used for the cache could not be
 *     created
 * @throws HttpHostConnectException if jib failed to connect to a registry
 * @throws RegistryUnauthorizedException if a registry request is unauthorized and needs
 *     authentication
 * @throws RegistryAuthenticationFailedException if registry authentication failed
 * @throws UnknownHostException if the registry does not exist
 * @throws InsecureRegistryException if a server could not be verified due to an insecure
 *     connection
 * @throws RegistryException if some other error occurred while interacting with a registry
 * @throws ExecutionException if some other exception occurred during execution
 * @throws InterruptedException if the execution was interrupted
 */
public JibContainer containerize(Containerizer containerizer) throws InterruptedException, RegistryException, IOException, CacheDirectoryCreationException, ExecutionException {
    try (BuildContext buildContext = toBuildContext(containerizer);
        TimerEventDispatcher ignored = new TimerEventDispatcher(buildContext.getEventHandlers(), containerizer.getDescription())) {
        logSources(buildContext.getEventHandlers());
        BuildResult buildResult = containerizer.run(buildContext);
        return JibContainer.from(buildContext, buildResult);
    } catch (ExecutionException ex) {
        // If an ExecutionException occurs, re-throw the cause to be more easily handled by the user
        if (ex.getCause() instanceof RegistryException) {
            throw (RegistryException) ex.getCause();
        }
        throw ex;
    }
}
Also used : BuildResult(com.google.cloud.tools.jib.builder.steps.BuildResult) BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) ExecutionException(java.util.concurrent.ExecutionException)

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