Search in sources :

Example 31 with EventHandlers

use of com.google.cloud.tools.jib.event.EventHandlers 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 32 with EventHandlers

use of com.google.cloud.tools.jib.event.EventHandlers 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 33 with EventHandlers

use of com.google.cloud.tools.jib.event.EventHandlers 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 34 with EventHandlers

use of com.google.cloud.tools.jib.event.EventHandlers in project jib by google.

the class MavenRawConfigurationTest method testGetters.

@Test
public void testGetters() {
    JibPluginConfiguration jibPluginConfiguration = Mockito.mock(JibPluginConfiguration.class);
    EventHandlers eventHandlers = Mockito.mock(EventHandlers.class);
    Server server = Mockito.mock(Server.class);
    Mockito.when(server.getUsername()).thenReturn("maven settings user");
    Mockito.when(server.getPassword()).thenReturn("maven settings password");
    Settings mavenSettings = Mockito.mock(Settings.class);
    Mockito.when(mavenSettings.getServer("base registry")).thenReturn(server);
    MavenSession mavenSession = Mockito.mock(MavenSession.class);
    Mockito.when(mavenSession.getSettings()).thenReturn(mavenSettings);
    FromAuthConfiguration auth = Mockito.mock(FromAuthConfiguration.class);
    Mockito.when(auth.getUsername()).thenReturn("user");
    Mockito.when(auth.getPassword()).thenReturn("password");
    Mockito.when(auth.getAuthDescriptor()).thenReturn("<from><auth>");
    Mockito.when(auth.getUsernameDescriptor()).thenReturn("<from><auth><username>");
    Mockito.when(auth.getPasswordDescriptor()).thenReturn("<from><auth><password>");
    Mockito.when(jibPluginConfiguration.getSession()).thenReturn(mavenSession);
    Mockito.when(jibPluginConfiguration.getBaseImageAuth()).thenReturn(auth);
    Mockito.when(jibPluginConfiguration.getAllowInsecureRegistries()).thenReturn(true);
    Mockito.when(jibPluginConfiguration.getAppRoot()).thenReturn("/app/root");
    Mockito.when(jibPluginConfiguration.getArgs()).thenReturn(Arrays.asList("--log", "info"));
    Mockito.when(jibPluginConfiguration.getBaseImage()).thenReturn("openjdk:15");
    CredHelperConfiguration baseImageCredHelperConfig = Mockito.mock(CredHelperConfiguration.class);
    Mockito.when(baseImageCredHelperConfig.getHelperName()).thenReturn(Optional.of("gcr"));
    Mockito.when(baseImageCredHelperConfig.getEnvironment()).thenReturn(Collections.singletonMap("ENV_VARIABLE", "Value1"));
    Mockito.when(jibPluginConfiguration.getBaseImageCredHelperConfig()).thenReturn(baseImageCredHelperConfig);
    CredHelperConfiguration targetImageCredHelperConfig = Mockito.mock(CredHelperConfiguration.class);
    Mockito.when(targetImageCredHelperConfig.getHelperName()).thenReturn(Optional.of("gcr"));
    Mockito.when(targetImageCredHelperConfig.getEnvironment()).thenReturn(Collections.singletonMap("ENV_VARIABLE", "Value2"));
    Mockito.when(jibPluginConfiguration.getTargetImageCredentialHelperConfig()).thenReturn(targetImageCredHelperConfig);
    Mockito.when(jibPluginConfiguration.getEntrypoint()).thenReturn(Arrays.asList("java", "Main"));
    Mockito.when(jibPluginConfiguration.getEnvironment()).thenReturn(new HashMap<>(ImmutableMap.of("currency", "dollar")));
    Mockito.when(jibPluginConfiguration.getExposedPorts()).thenReturn(Arrays.asList("80/tcp", "0"));
    Mockito.when(jibPluginConfiguration.getJvmFlags()).thenReturn(Arrays.asList("-cp", "."));
    Mockito.when(jibPluginConfiguration.getLabels()).thenReturn(new HashMap<>(ImmutableMap.of("unit", "cm")));
    Mockito.when(jibPluginConfiguration.getMainClass()).thenReturn("com.example.Main");
    Mockito.when(jibPluginConfiguration.getTargetImageAdditionalTags()).thenReturn(new HashSet<>(Arrays.asList("additional", "tags")));
    Mockito.when(jibPluginConfiguration.getUser()).thenReturn("admin:wheel");
    Mockito.when(jibPluginConfiguration.getFilesModificationTime()).thenReturn("2011-12-03T22:42:05Z");
    Mockito.when(jibPluginConfiguration.getDockerClientExecutable()).thenReturn(Paths.get("test"));
    Mockito.when(jibPluginConfiguration.getDockerClientEnvironment()).thenReturn(new HashMap<>(ImmutableMap.of("docker", "client")));
    Mockito.when(jibPluginConfiguration.getDigestOutputPath()).thenReturn(Paths.get("digest/path"));
    Mockito.when(jibPluginConfiguration.getImageIdOutputPath()).thenReturn(Paths.get("id/path"));
    Mockito.when(jibPluginConfiguration.getImageJsonOutputPath()).thenReturn(Paths.get("json/path"));
    Mockito.when(jibPluginConfiguration.getTarOutputPath()).thenReturn(Paths.get("tar/path"));
    MavenRawConfiguration rawConfiguration = new MavenRawConfiguration(jibPluginConfiguration);
    AuthProperty fromAuth = rawConfiguration.getFromAuth();
    Assert.assertEquals("user", fromAuth.getUsername());
    Assert.assertEquals("password", fromAuth.getPassword());
    Assert.assertEquals("<from><auth>", fromAuth.getAuthDescriptor());
    Assert.assertEquals("<from><auth><username>", fromAuth.getUsernameDescriptor());
    Assert.assertEquals("<from><auth><password>", fromAuth.getPasswordDescriptor());
    Assert.assertTrue(rawConfiguration.getAllowInsecureRegistries());
    Assert.assertEquals(Arrays.asList("java", "Main"), rawConfiguration.getEntrypoint().get());
    Assert.assertEquals(new HashMap<>(ImmutableMap.of("currency", "dollar")), rawConfiguration.getEnvironment());
    Assert.assertEquals("/app/root", rawConfiguration.getAppRoot());
    Assert.assertEquals("gcr", rawConfiguration.getFromCredHelper().getHelperName().get());
    Assert.assertEquals(Collections.singletonMap("ENV_VARIABLE", "Value1"), rawConfiguration.getFromCredHelper().getEnvironment());
    Assert.assertEquals("gcr", rawConfiguration.getToCredHelper().getHelperName().get());
    Assert.assertEquals(Collections.singletonMap("ENV_VARIABLE", "Value2"), rawConfiguration.getToCredHelper().getEnvironment());
    Assert.assertEquals("openjdk:15", rawConfiguration.getFromImage().get());
    Assert.assertEquals(Arrays.asList("-cp", "."), rawConfiguration.getJvmFlags());
    Assert.assertEquals(new HashMap<>(ImmutableMap.of("unit", "cm")), rawConfiguration.getLabels());
    Assert.assertEquals("com.example.Main", rawConfiguration.getMainClass().get());
    Assert.assertEquals(Arrays.asList("80/tcp", "0"), rawConfiguration.getPorts());
    Assert.assertEquals(Arrays.asList("--log", "info"), rawConfiguration.getProgramArguments().get());
    Assert.assertEquals(new HashSet<>(Arrays.asList("additional", "tags")), Sets.newHashSet(rawConfiguration.getToTags()));
    Assert.assertEquals("admin:wheel", rawConfiguration.getUser().get());
    Assert.assertEquals("2011-12-03T22:42:05Z", rawConfiguration.getFilesModificationTime());
    Assert.assertEquals(Paths.get("test"), rawConfiguration.getDockerExecutable().get());
    Assert.assertEquals(new HashMap<>(ImmutableMap.of("docker", "client")), rawConfiguration.getDockerEnvironment());
    Assert.assertEquals(Paths.get("digest/path"), jibPluginConfiguration.getDigestOutputPath());
    Assert.assertEquals(Paths.get("id/path"), jibPluginConfiguration.getImageIdOutputPath());
    Assert.assertEquals(Paths.get("json/path"), jibPluginConfiguration.getImageJsonOutputPath());
    Assert.assertEquals(Paths.get("tar/path"), jibPluginConfiguration.getTarOutputPath());
    Mockito.verifyNoMoreInteractions(eventHandlers);
}
Also used : AuthProperty(com.google.cloud.tools.jib.plugins.common.AuthProperty) MavenSession(org.apache.maven.execution.MavenSession) FromAuthConfiguration(com.google.cloud.tools.jib.maven.JibPluginConfiguration.FromAuthConfiguration) Server(org.apache.maven.settings.Server) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Settings(org.apache.maven.settings.Settings) CredHelperConfiguration(com.google.cloud.tools.jib.plugins.common.RawConfiguration.CredHelperConfiguration) Test(org.junit.Test)

Example 35 with EventHandlers

use of com.google.cloud.tools.jib.event.EventHandlers in project jib by google.

the class ProgressEventTest method testAccumulateProgress.

@Test
public void testAccumulateProgress() {
    Consumer<ProgressEvent> progressEventConsumer = progressEvent -> {
        double fractionOfRoot = progressEvent.getAllocation().getFractionOfRoot();
        long units = progressEvent.getUnits();
        progress += units * fractionOfRoot;
    };
    EventHandlers eventHandlers = makeEventHandlers(progressEventConsumer);
    eventHandlers.dispatch(new ProgressEvent(AllocationTree.child1Child, 50));
    Assert.assertEquals(1.0 / 2 / 100 * 50, progress, DOUBLE_ERROR_MARGIN);
    eventHandlers.dispatch(new ProgressEvent(AllocationTree.child1Child, 50));
    Assert.assertEquals(1.0 / 2, progress, DOUBLE_ERROR_MARGIN);
    eventHandlers.dispatch(new ProgressEvent(AllocationTree.child2, 10));
    Assert.assertEquals(1.0 / 2 + 1.0 / 2 / 200 * 10, progress, DOUBLE_ERROR_MARGIN);
    eventHandlers.dispatch(new ProgressEvent(AllocationTree.child2, 190));
    Assert.assertEquals(1.0, progress, DOUBLE_ERROR_MARGIN);
}
Also used : Consumer(java.util.function.Consumer) Allocation(com.google.cloud.tools.jib.event.progress.Allocation) Map(java.util.Map) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) HashMap(java.util.HashMap) Test(org.junit.Test) Assert(org.junit.Assert) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Test(org.junit.Test)

Aggregations

EventHandlers (com.google.cloud.tools.jib.event.EventHandlers)42 ProgressEventDispatcher (com.google.cloud.tools.jib.builder.ProgressEventDispatcher)24 TimerEventDispatcher (com.google.cloud.tools.jib.builder.TimerEventDispatcher)22 Test (org.junit.Test)14 DescriptorDigest (com.google.cloud.tools.jib.api.DescriptorDigest)10 Image (com.google.cloud.tools.jib.image.Image)10 IOException (java.io.IOException)10 RegistryClient (com.google.cloud.tools.jib.registry.RegistryClient)8 ImmutableList (com.google.common.collect.ImmutableList)8 Map (java.util.Map)8 RegistryException (com.google.cloud.tools.jib.api.RegistryException)6 Platform (com.google.cloud.tools.jib.api.buildplan.Platform)6 BlobDescriptor (com.google.cloud.tools.jib.blob.BlobDescriptor)6 Cache (com.google.cloud.tools.jib.cache.Cache)6 Allocation (com.google.cloud.tools.jib.event.progress.Allocation)6 BuildableManifestTemplate (com.google.cloud.tools.jib.image.json.BuildableManifestTemplate)6 ImageToJsonTranslator (com.google.cloud.tools.jib.image.json.ImageToJsonTranslator)6 ManifestTemplate (com.google.cloud.tools.jib.image.json.ManifestTemplate)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6