|
| 1 | +/* |
| 2 | + * Copyright 2018 Google LLC. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.tools.jib.builder; |
| 18 | + |
| 19 | +import com.google.cloud.tools.jib.Timer; |
| 20 | +import com.google.cloud.tools.jib.blob.Blob; |
| 21 | +import com.google.cloud.tools.jib.cache.Cache; |
| 22 | +import com.google.cloud.tools.jib.cache.CacheDirectoryNotOwnedException; |
| 23 | +import com.google.cloud.tools.jib.cache.CacheMetadataCorruptedException; |
| 24 | +import com.google.cloud.tools.jib.cache.CachedLayer; |
| 25 | +import com.google.cloud.tools.jib.cache.Caches; |
| 26 | +import com.google.cloud.tools.jib.http.Authorization; |
| 27 | +import com.google.cloud.tools.jib.image.Image; |
| 28 | +import com.google.common.util.concurrent.Futures; |
| 29 | +import com.google.common.util.concurrent.ListenableFuture; |
| 30 | +import com.google.common.util.concurrent.ListeningExecutorService; |
| 31 | +import com.google.common.util.concurrent.MoreExecutors; |
| 32 | +import java.io.IOException; |
| 33 | +import java.util.List; |
| 34 | +import java.util.concurrent.ExecutionException; |
| 35 | +import java.util.concurrent.Executors; |
| 36 | + |
| 37 | +/** |
| 38 | + * All the steps to build to Docker daemon. |
| 39 | + * |
| 40 | + * <p>TODO: Refactor for less duplicate code w/ BuildImageSteps |
| 41 | + */ |
| 42 | +public class BuildDockerSteps { |
| 43 | + |
| 44 | + private static final String DESCRIPTION = "Building and pushing image"; |
| 45 | + |
| 46 | + private final BuildConfiguration buildConfiguration; |
| 47 | + private final SourceFilesConfiguration sourceFilesConfiguration; |
| 48 | + private final Caches.Initializer cachesInitializer; |
| 49 | + |
| 50 | + BuildDockerSteps( |
| 51 | + BuildConfiguration buildConfiguration, |
| 52 | + SourceFilesConfiguration sourceFilesConfiguration, |
| 53 | + Caches.Initializer cachesInitializer) { |
| 54 | + this.buildConfiguration = buildConfiguration; |
| 55 | + this.sourceFilesConfiguration = sourceFilesConfiguration; |
| 56 | + this.cachesInitializer = cachesInitializer; |
| 57 | + } |
| 58 | + |
| 59 | + public BuildConfiguration getBuildConfiguration() { |
| 60 | + return buildConfiguration; |
| 61 | + } |
| 62 | + |
| 63 | + public void run() |
| 64 | + throws InterruptedException, ExecutionException, CacheMetadataCorruptedException, IOException, |
| 65 | + CacheDirectoryNotOwnedException { |
| 66 | + List<String> entrypoint = |
| 67 | + EntrypointBuilder.makeEntrypoint( |
| 68 | + sourceFilesConfiguration, |
| 69 | + buildConfiguration.getJvmFlags(), |
| 70 | + buildConfiguration.getMainClass()); |
| 71 | + |
| 72 | + try (Timer timer = new Timer(buildConfiguration.getBuildLogger(), DESCRIPTION)) { |
| 73 | + try (Timer timer2 = timer.subTimer("Initializing cache")) { |
| 74 | + ListeningExecutorService listeningExecutorService = |
| 75 | + MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); |
| 76 | + |
| 77 | + try (Caches caches = cachesInitializer.init()) { |
| 78 | + Cache baseLayersCache = caches.getBaseCache(); |
| 79 | + Cache applicationLayersCache = caches.getApplicationCache(); |
| 80 | + |
| 81 | + timer2.lap("Setting up credential retrieval"); |
| 82 | + ListenableFuture<Authorization> retrieveBaseImageRegistryCredentialsFuture = |
| 83 | + listeningExecutorService.submit( |
| 84 | + RetrieveRegistryCredentialsStep.forBaseImage(buildConfiguration)); |
| 85 | + |
| 86 | + timer2.lap("Setting up image pull authentication"); |
| 87 | + // Authenticates base image pull. |
| 88 | + ListenableFuture<Authorization> authenticatePullFuture = |
| 89 | + Futures.whenAllSucceed(retrieveBaseImageRegistryCredentialsFuture) |
| 90 | + .call( |
| 91 | + new AuthenticatePullStep( |
| 92 | + buildConfiguration, retrieveBaseImageRegistryCredentialsFuture), |
| 93 | + listeningExecutorService); |
| 94 | + |
| 95 | + timer2.lap("Setting up base image pull"); |
| 96 | + // Pulls the base image. |
| 97 | + ListenableFuture<Image> pullBaseImageFuture = |
| 98 | + Futures.whenAllSucceed(authenticatePullFuture) |
| 99 | + .call( |
| 100 | + new PullBaseImageStep(buildConfiguration, authenticatePullFuture), |
| 101 | + listeningExecutorService); |
| 102 | + timer2.lap("Setting up base image layer pull"); |
| 103 | + // Pulls and caches the base image layers. |
| 104 | + ListenableFuture<List<ListenableFuture<CachedLayer>>> pullBaseImageLayerFuturesFuture = |
| 105 | + Futures.whenAllSucceed(pullBaseImageFuture) |
| 106 | + .call( |
| 107 | + new PullAndCacheBaseImageLayersStep( |
| 108 | + buildConfiguration, |
| 109 | + baseLayersCache, |
| 110 | + listeningExecutorService, |
| 111 | + authenticatePullFuture, |
| 112 | + pullBaseImageFuture), |
| 113 | + listeningExecutorService); |
| 114 | + |
| 115 | + timer2.lap("Setting up build application layers"); |
| 116 | + // Builds the application layers. |
| 117 | + List<ListenableFuture<CachedLayer>> buildAndCacheApplicationLayerFutures = |
| 118 | + new BuildAndCacheApplicationLayersStep( |
| 119 | + buildConfiguration, |
| 120 | + sourceFilesConfiguration, |
| 121 | + applicationLayersCache, |
| 122 | + listeningExecutorService) |
| 123 | + .call(); |
| 124 | + |
| 125 | + timer2.lap("Setting up build container configuration"); |
| 126 | + // Builds the container configuration. |
| 127 | + ListenableFuture<ListenableFuture<Blob>> buildContainerConfigurationFutureFuture = |
| 128 | + Futures.whenAllSucceed(pullBaseImageLayerFuturesFuture) |
| 129 | + .call( |
| 130 | + new BuildContainerConfigurationStep( |
| 131 | + buildConfiguration, |
| 132 | + listeningExecutorService, |
| 133 | + pullBaseImageLayerFuturesFuture, |
| 134 | + buildAndCacheApplicationLayerFutures, |
| 135 | + entrypoint), |
| 136 | + listeningExecutorService); |
| 137 | + |
| 138 | + timer2.lap("Setting up build to docker daemon"); |
| 139 | + // Builds the image tarball and loads into the Docker daemon. |
| 140 | + ListenableFuture<Void> buildToDockerFutureFuture = |
| 141 | + Futures.whenAllSucceed( |
| 142 | + pullBaseImageLayerFuturesFuture, buildContainerConfigurationFutureFuture) |
| 143 | + .call( |
| 144 | + new BuildTarballAndLoadDockerStep( |
| 145 | + buildConfiguration, |
| 146 | + listeningExecutorService, |
| 147 | + pullBaseImageLayerFuturesFuture, |
| 148 | + buildAndCacheApplicationLayerFutures, |
| 149 | + buildContainerConfigurationFutureFuture), |
| 150 | + listeningExecutorService); |
| 151 | + |
| 152 | + timer2.lap("Running build to docker daemon"); |
| 153 | + buildToDockerFutureFuture.get(); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + buildConfiguration.getBuildLogger().lifecycle(""); |
| 159 | + buildConfiguration.getBuildLogger().lifecycle("Container entrypoint set to " + entrypoint); |
| 160 | + } |
| 161 | +} |
0 commit comments