|
| 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.frontend; |
| 18 | + |
| 19 | +import com.google.api.client.http.HttpResponseException; |
| 20 | +import com.google.api.client.http.HttpStatusCodes; |
| 21 | +import com.google.cloud.tools.jib.builder.BuildConfiguration; |
| 22 | +import com.google.cloud.tools.jib.builder.BuildImageSteps; |
| 23 | +import com.google.cloud.tools.jib.builder.SourceFilesConfiguration; |
| 24 | +import com.google.cloud.tools.jib.cache.CacheDirectoryNotOwnedException; |
| 25 | +import com.google.cloud.tools.jib.cache.CacheMetadataCorruptedException; |
| 26 | +import com.google.cloud.tools.jib.cache.Caches; |
| 27 | +import com.google.cloud.tools.jib.registry.RegistryAuthenticationFailedException; |
| 28 | +import com.google.cloud.tools.jib.registry.RegistryUnauthorizedException; |
| 29 | +import com.google.common.annotations.VisibleForTesting; |
| 30 | +import java.io.IOException; |
| 31 | +import java.net.UnknownHostException; |
| 32 | +import java.nio.file.Files; |
| 33 | +import java.nio.file.Path; |
| 34 | +import java.util.concurrent.ExecutionException; |
| 35 | +import java.util.function.Supplier; |
| 36 | +import org.apache.http.conn.HttpHostConnectException; |
| 37 | + |
| 38 | +/** Runs {@link BuildImageSteps} and builds helpful error messages. */ |
| 39 | +public class BuildImageStepsRunner { |
| 40 | + |
| 41 | + private static final HelpfulMessageBuilder helpfulMessageBuilder = |
| 42 | + new HelpfulMessageBuilder("Build image failed"); |
| 43 | + |
| 44 | + /** |
| 45 | + * Sets up a new {@link BuildImageStepsRunner}. Creates the directory for the cache, if needed. |
| 46 | + * |
| 47 | + * @param useOnlyProjectCache if {@code true}, sets the base layers cache directory to be the same |
| 48 | + * as the application layers cache directory |
| 49 | + * @throws CacheDirectoryCreationException if the {@code cacheDirectory} could not be created |
| 50 | + */ |
| 51 | + public static BuildImageStepsRunner newRunner( |
| 52 | + BuildConfiguration buildConfiguration, |
| 53 | + SourceFilesConfiguration sourceFilesConfiguration, |
| 54 | + Path cacheDirectory, |
| 55 | + boolean useOnlyProjectCache) |
| 56 | + throws CacheDirectoryCreationException { |
| 57 | + if (!Files.exists(cacheDirectory)) { |
| 58 | + try { |
| 59 | + Files.createDirectory(cacheDirectory); |
| 60 | + |
| 61 | + } catch (IOException ex) { |
| 62 | + throw new CacheDirectoryCreationException(cacheDirectory, ex); |
| 63 | + } |
| 64 | + } |
| 65 | + Caches.Initializer cachesInitializer = Caches.newInitializer(cacheDirectory); |
| 66 | + if (useOnlyProjectCache) { |
| 67 | + cachesInitializer.setBaseCacheDirectory(cacheDirectory); |
| 68 | + } |
| 69 | + |
| 70 | + return new BuildImageStepsRunner( |
| 71 | + buildConfiguration, sourceFilesConfiguration, cachesInitializer); |
| 72 | + } |
| 73 | + |
| 74 | + private final Supplier<BuildImageSteps> buildImageStepsSupplier; |
| 75 | + |
| 76 | + @VisibleForTesting |
| 77 | + BuildImageStepsRunner(Supplier<BuildImageSteps> buildImageStepsSupplier) { |
| 78 | + this.buildImageStepsSupplier = buildImageStepsSupplier; |
| 79 | + } |
| 80 | + |
| 81 | + private BuildImageStepsRunner( |
| 82 | + BuildConfiguration buildConfiguration, |
| 83 | + SourceFilesConfiguration sourceFilesConfiguration, |
| 84 | + Caches.Initializer cachesInitializer) { |
| 85 | + buildImageStepsSupplier = |
| 86 | + () -> new BuildImageSteps(buildConfiguration, sourceFilesConfiguration, cachesInitializer); |
| 87 | + } |
| 88 | + |
| 89 | + public void buildImage() throws BuildImageStepsExecutionException { |
| 90 | + BuildImageSteps buildImageSteps = buildImageStepsSupplier.get(); |
| 91 | + |
| 92 | + try { |
| 93 | + buildImageSteps.run(); |
| 94 | + |
| 95 | + } catch (CacheMetadataCorruptedException cacheMetadataCorruptedException) { |
| 96 | + // TODO: Have this be different for Maven and Gradle. |
| 97 | + throw new BuildImageStepsExecutionException( |
| 98 | + helpfulMessageBuilder.withSuggestion("run 'mvn clean' to clear the cache"), |
| 99 | + cacheMetadataCorruptedException); |
| 100 | + |
| 101 | + } catch (ExecutionException executionException) { |
| 102 | + BuildConfiguration buildConfiguration = buildImageSteps.getBuildConfiguration(); |
| 103 | + |
| 104 | + if (executionException.getCause() instanceof HttpHostConnectException) { |
| 105 | + // Failed to connect to registry. |
| 106 | + throw new BuildImageStepsExecutionException( |
| 107 | + helpfulMessageBuilder.withSuggestion( |
| 108 | + "make sure your Internet is up and that the registry you are pushing to exists"), |
| 109 | + executionException.getCause()); |
| 110 | + |
| 111 | + } else if (executionException.getCause() instanceof RegistryUnauthorizedException) { |
| 112 | + handleRegistryUnauthorizedException( |
| 113 | + (RegistryUnauthorizedException) executionException.getCause(), buildConfiguration); |
| 114 | + |
| 115 | + } else if (executionException.getCause() instanceof RegistryAuthenticationFailedException |
| 116 | + && executionException.getCause().getCause() instanceof HttpResponseException) { |
| 117 | + handleRegistryUnauthorizedException( |
| 118 | + new RegistryUnauthorizedException( |
| 119 | + buildConfiguration.getTargetImageRegistry(), |
| 120 | + buildConfiguration.getTargetImageRepository(), |
| 121 | + (HttpResponseException) executionException.getCause().getCause()), |
| 122 | + buildConfiguration); |
| 123 | + |
| 124 | + } else if (executionException.getCause() instanceof UnknownHostException) { |
| 125 | + throw new BuildImageStepsExecutionException( |
| 126 | + helpfulMessageBuilder.withSuggestion( |
| 127 | + "make sure that the registry you configured exists/is spelled properly"), |
| 128 | + executionException.getCause()); |
| 129 | + |
| 130 | + } else { |
| 131 | + throw new BuildImageStepsExecutionException( |
| 132 | + helpfulMessageBuilder.withNoHelp(), executionException.getCause()); |
| 133 | + } |
| 134 | + |
| 135 | + } catch (InterruptedException | IOException ex) { |
| 136 | + // TODO: Add more suggestions for various build failures. |
| 137 | + throw new BuildImageStepsExecutionException(helpfulMessageBuilder.withNoHelp(), ex); |
| 138 | + |
| 139 | + } catch (CacheDirectoryNotOwnedException ex) { |
| 140 | + throw new BuildImageStepsExecutionException( |
| 141 | + helpfulMessageBuilder.withSuggestion( |
| 142 | + "check that '" |
| 143 | + + ex.getCacheDirectory() |
| 144 | + + "' is not used by another application or set the `useOnlyProjectCache` " |
| 145 | + + "configuration"), |
| 146 | + ex); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private void handleRegistryUnauthorizedException( |
| 151 | + RegistryUnauthorizedException registryUnauthorizedException, |
| 152 | + BuildConfiguration buildConfiguration) |
| 153 | + throws BuildImageStepsExecutionException { |
| 154 | + if (registryUnauthorizedException.getHttpResponseException().getStatusCode() |
| 155 | + == HttpStatusCodes.STATUS_CODE_FORBIDDEN) { |
| 156 | + // No permissions for registry/repository. |
| 157 | + throw new BuildImageStepsExecutionException( |
| 158 | + helpfulMessageBuilder.withSuggestion( |
| 159 | + "make sure you have permissions for " |
| 160 | + + registryUnauthorizedException.getImageReference()), |
| 161 | + registryUnauthorizedException); |
| 162 | + |
| 163 | + } else { |
| 164 | + boolean isRegistryForBase = |
| 165 | + registryUnauthorizedException |
| 166 | + .getRegistry() |
| 167 | + .equals(buildConfiguration.getBaseImageRegistry()); |
| 168 | + boolean isRegistryForTarget = |
| 169 | + registryUnauthorizedException |
| 170 | + .getRegistry() |
| 171 | + .equals(buildConfiguration.getTargetImageRegistry()); |
| 172 | + boolean areBaseImageCredentialsConfigured = |
| 173 | + buildConfiguration.getBaseImageCredentialHelperName() != null |
| 174 | + || buildConfiguration.getKnownBaseRegistryCredentials() != null; |
| 175 | + boolean areTargetImageCredentialsConfigured = |
| 176 | + buildConfiguration.getTargetImageCredentialHelperName() != null |
| 177 | + || buildConfiguration.getKnownTargetRegistryCredentials() != null; |
| 178 | + |
| 179 | + if ((isRegistryForBase && !areBaseImageCredentialsConfigured) |
| 180 | + || (isRegistryForTarget && !areTargetImageCredentialsConfigured)) { |
| 181 | + // No credential helpers defined. |
| 182 | + throw new BuildImageStepsExecutionException( |
| 183 | + helpfulMessageBuilder.withSuggestion( |
| 184 | + "set a credential helper name with the configuration 'credHelpers' or " |
| 185 | + + "set credentials for '" |
| 186 | + + registryUnauthorizedException.getRegistry() |
| 187 | + + "' in your Maven settings"), |
| 188 | + registryUnauthorizedException); |
| 189 | + } |
| 190 | + |
| 191 | + // Credential helper probably was not configured correctly or did not have the necessary |
| 192 | + // credentials. |
| 193 | + throw new BuildImageStepsExecutionException( |
| 194 | + helpfulMessageBuilder.withSuggestion( |
| 195 | + "make sure your credentials for '" |
| 196 | + + registryUnauthorizedException.getRegistry() |
| 197 | + + "' are set up correctly"), |
| 198 | + registryUnauthorizedException); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments