Search in sources :

Example 1 with Cache

use of com.google.cloud.tools.jib.cache.Cache in project jib by google.

the class BuildAndCacheApplicationLayersStepTest method testRun.

@Test
public void testRun() throws LayerPropertyNotFoundException, IOException, CacheMetadataCorruptedException, URISyntaxException, ExecutionException, InterruptedException {
    Mockito.when(mockBuildConfiguration.getBuildLogger()).thenReturn(new TestBuildLogger());
    TestSourceFilesConfiguration testSourceFilesConfiguration = new TestSourceFilesConfiguration();
    Path temporaryCacheDirectory = temporaryFolder.newFolder().toPath();
    ImageLayers<CachedLayer> applicationLayers = new ImageLayers<>();
    try (Cache cache = Cache.init(temporaryCacheDirectory)) {
        BuildAndCacheApplicationLayersStep buildAndCacheApplicationLayersStep = new BuildAndCacheApplicationLayersStep(mockBuildConfiguration, testSourceFilesConfiguration, cache, MoreExecutors.newDirectExecutorService());
        for (ListenableFuture<CachedLayer> applicationLayerFuture : buildAndCacheApplicationLayersStep.call()) {
            applicationLayers.add(applicationLayerFuture.get());
        }
        Assert.assertEquals(3, applicationLayers.size());
    }
    // Re-initialize cache with the updated metadata.
    Cache cache = Cache.init(temporaryCacheDirectory);
    // Verifies that the cached layers are up-to-date.
    CacheReader cacheReader = new CacheReader(cache);
    Assert.assertEquals(applicationLayers.get(0).getBlobDescriptor(), cacheReader.getUpToDateLayerBySourceFiles(testSourceFilesConfiguration.getDependenciesFiles()).getBlobDescriptor());
    Assert.assertEquals(applicationLayers.get(1).getBlobDescriptor(), cacheReader.getUpToDateLayerBySourceFiles(testSourceFilesConfiguration.getResourcesFiles()).getBlobDescriptor());
    Assert.assertEquals(applicationLayers.get(2).getBlobDescriptor(), cacheReader.getUpToDateLayerBySourceFiles(testSourceFilesConfiguration.getClassesFiles()).getBlobDescriptor());
    // Verifies that the cache reader gets the same layers as the newest application layers.
    Assert.assertEquals(applicationLayers.get(0).getContentFile(), cacheReader.getLayerFile(testSourceFilesConfiguration.getDependenciesFiles()));
    Assert.assertEquals(applicationLayers.get(1).getContentFile(), cacheReader.getLayerFile(testSourceFilesConfiguration.getResourcesFiles()));
    Assert.assertEquals(applicationLayers.get(2).getContentFile(), cacheReader.getLayerFile(testSourceFilesConfiguration.getClassesFiles()));
}
Also used : Path(java.nio.file.Path) ImageLayers(com.google.cloud.tools.jib.image.ImageLayers) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) CacheReader(com.google.cloud.tools.jib.cache.CacheReader) Cache(com.google.cloud.tools.jib.cache.Cache) Test(org.junit.Test)

Example 2 with Cache

use of com.google.cloud.tools.jib.cache.Cache in project jib by google.

the class BuildImageSteps method run.

public void run() throws InterruptedException, ExecutionException, CacheMetadataCorruptedException, IOException, CacheDirectoryNotOwnedException {
    List<String> entrypoint = EntrypointBuilder.makeEntrypoint(sourceFilesConfiguration, buildConfiguration.getJvmFlags(), buildConfiguration.getMainClass());
    try (Timer timer = new Timer(buildConfiguration.getBuildLogger(), DESCRIPTION)) {
        try (Timer timer2 = timer.subTimer("Initializing cache")) {
            ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
            try (Caches caches = cachesInitializer.init()) {
                Cache baseLayersCache = caches.getBaseCache();
                Cache applicationLayersCache = caches.getApplicationCache();
                timer2.lap("Setting up credential retrieval");
                ListenableFuture<Authorization> retrieveTargetRegistryCredentialsFuture = listeningExecutorService.submit(new RetrieveRegistryCredentialsStep(buildConfiguration, buildConfiguration.getTargetRegistry()));
                ListenableFuture<Authorization> retrieveBaseImageRegistryCredentialsFuture = listeningExecutorService.submit(new RetrieveRegistryCredentialsStep(buildConfiguration, buildConfiguration.getBaseImageRegistry()));
                timer2.lap("Setting up image push authentication");
                // Authenticates push.
                ListenableFuture<Authorization> authenticatePushFuture = Futures.whenAllSucceed(retrieveTargetRegistryCredentialsFuture).call(new AuthenticatePushStep(buildConfiguration, retrieveTargetRegistryCredentialsFuture), listeningExecutorService);
                timer2.lap("Setting up image pull authentication");
                // Authenticates base image pull.
                ListenableFuture<Authorization> authenticatePullFuture = Futures.whenAllSucceed(retrieveBaseImageRegistryCredentialsFuture).call(new AuthenticatePullStep(buildConfiguration, retrieveBaseImageRegistryCredentialsFuture), listeningExecutorService);
                timer2.lap("Setting up base image pull");
                // Pulls the base image.
                ListenableFuture<Image> pullBaseImageFuture = Futures.whenAllSucceed(authenticatePullFuture).call(new PullBaseImageStep(buildConfiguration, authenticatePullFuture), listeningExecutorService);
                timer2.lap("Setting up base image layer pull");
                // Pulls and caches the base image layers.
                ListenableFuture<List<ListenableFuture<CachedLayer>>> pullBaseImageLayerFuturesFuture = Futures.whenAllSucceed(pullBaseImageFuture).call(new PullAndCacheBaseImageLayersStep(buildConfiguration, baseLayersCache, listeningExecutorService, authenticatePullFuture, pullBaseImageFuture), listeningExecutorService);
                timer2.lap("Setting up base image layer push");
                // Pushes the base image layers.
                ListenableFuture<List<ListenableFuture<Void>>> pushBaseImageLayerFuturesFuture = Futures.whenAllSucceed(pullBaseImageLayerFuturesFuture).call(new PushLayersStep(buildConfiguration, listeningExecutorService, authenticatePushFuture, pullBaseImageLayerFuturesFuture), listeningExecutorService);
                timer2.lap("Setting up build application layers");
                // Builds the application layers.
                List<ListenableFuture<CachedLayer>> buildAndCacheApplicationLayerFutures = new BuildAndCacheApplicationLayersStep(buildConfiguration, sourceFilesConfiguration, applicationLayersCache, listeningExecutorService).call();
                timer2.lap("Setting up container configuration push");
                // Builds and pushes the container configuration.
                ListenableFuture<ListenableFuture<BlobDescriptor>> buildAndPushContainerConfigurationFutureFuture = Futures.whenAllSucceed(pullBaseImageLayerFuturesFuture).call(new BuildAndPushContainerConfigurationStep(buildConfiguration, listeningExecutorService, authenticatePushFuture, pullBaseImageLayerFuturesFuture, buildAndCacheApplicationLayerFutures, entrypoint), listeningExecutorService);
                timer2.lap("Setting up application layer push");
                // Pushes the application layers.
                List<ListenableFuture<Void>> pushApplicationLayersFuture = new PushLayersStep(buildConfiguration, listeningExecutorService, authenticatePushFuture, Futures.immediateFuture(buildAndCacheApplicationLayerFutures)).call();
                timer2.lap("Setting up image manifest push");
                // Pushes the new image manifest.
                ListenableFuture<Void> pushImageFuture = Futures.whenAllSucceed(pushBaseImageLayerFuturesFuture, buildAndPushContainerConfigurationFutureFuture).call(new PushImageStep(buildConfiguration, listeningExecutorService, authenticatePushFuture, pullBaseImageLayerFuturesFuture, buildAndCacheApplicationLayerFutures, pushBaseImageLayerFuturesFuture, pushApplicationLayersFuture, buildAndPushContainerConfigurationFutureFuture), listeningExecutorService);
                timer2.lap("Running push new image");
                pushImageFuture.get();
            }
        }
    }
    buildConfiguration.getBuildLogger().lifecycle("");
    buildConfiguration.getBuildLogger().lifecycle("Container entrypoint set to " + entrypoint);
}
Also used : Caches(com.google.cloud.tools.jib.cache.Caches) Image(com.google.cloud.tools.jib.image.Image) Authorization(com.google.cloud.tools.jib.http.Authorization) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) List(java.util.List) Timer(com.google.cloud.tools.jib.Timer) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Cache(com.google.cloud.tools.jib.cache.Cache)

Aggregations

Cache (com.google.cloud.tools.jib.cache.Cache)2 CachedLayer (com.google.cloud.tools.jib.cache.CachedLayer)2 Timer (com.google.cloud.tools.jib.Timer)1 CacheReader (com.google.cloud.tools.jib.cache.CacheReader)1 Caches (com.google.cloud.tools.jib.cache.Caches)1 Authorization (com.google.cloud.tools.jib.http.Authorization)1 Image (com.google.cloud.tools.jib.image.Image)1 ImageLayers (com.google.cloud.tools.jib.image.ImageLayers)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)1 Path (java.nio.file.Path)1 List (java.util.List)1 Test (org.junit.Test)1