use of com.google.cloud.tools.jib.filesystem.TempDirectoryProvider in project jib by GoogleContainerTools.
the class CacheStorageWriter method writeUncompressed.
/**
* Writes an uncompressed {@link Blob} out to the cache directory.
*
* <p>Cache is written out in the form:
*
* <ul>
* <li>The {@code uncompressedLayerBlob} is written to the layer directory under the layers
* directory corresponding to the layer blob.
* <li>The {@code selector} is written to the selector file under the selectors directory.
* </ul>
*
* @param uncompressedLayerBlob the {@link Blob} containing the uncompressed layer contents to
* write out
* @param selector the optional selector digest to also reference this layer data. A selector
* digest may be a secondary identifier for a layer that is distinct from the default layer
* digest.
* @return the {@link CachedLayer} representing the written entry
* @throws IOException if an I/O exception occurs
*/
CachedLayer writeUncompressed(Blob uncompressedLayerBlob, @Nullable DescriptorDigest selector) throws IOException {
// Creates the layers directory if it doesn't exist.
Files.createDirectories(cacheStorageFiles.getLayersDirectory());
// Creates the temporary directory. The temporary directory must be in the same FileStore as the
// final location for Files.move to work.
Files.createDirectories(cacheStorageFiles.getTemporaryDirectory());
try (TempDirectoryProvider tempDirectoryProvider = new TempDirectoryProvider()) {
Path temporaryLayerDirectory = tempDirectoryProvider.newDirectory(cacheStorageFiles.getTemporaryDirectory());
// Writes the layer file to the temporary directory.
WrittenLayer writtenLayer = writeUncompressedLayerBlobToDirectory(uncompressedLayerBlob, temporaryLayerDirectory);
// Moves the temporary directory to the final location.
moveIfDoesNotExist(temporaryLayerDirectory, cacheStorageFiles.getLayerDirectory(writtenLayer.layerDigest));
// Updates cachedLayer with the blob information.
Path layerFile = cacheStorageFiles.getLayerFile(writtenLayer.layerDigest, writtenLayer.layerDiffId);
CachedLayer.Builder cachedLayerBuilder = CachedLayer.builder().setLayerDigest(writtenLayer.layerDigest).setLayerDiffId(writtenLayer.layerDiffId).setLayerSize(writtenLayer.layerSize).setLayerBlob(Blobs.from(layerFile));
// Write the selector file.
if (selector != null) {
writeSelector(selector, writtenLayer.layerDigest);
}
return cachedLayerBuilder.build();
}
}
use of com.google.cloud.tools.jib.filesystem.TempDirectoryProvider in project jib by GoogleContainerTools.
the class CacheStorageWriter method writeCompressed.
/**
* Writes a compressed layer {@link Blob}.
*
* <p>The {@code compressedLayerBlob} is written to the layer directory under the layers directory
* corresponding to the layer blob.
*
* @param compressedLayerBlob the compressed layer {@link Blob} to write out
* @return the {@link CachedLayer} representing the written entry
* @throws IOException if an I/O exception occurs
*/
CachedLayer writeCompressed(Blob compressedLayerBlob) throws IOException {
// Creates the layers directory if it doesn't exist.
Files.createDirectories(cacheStorageFiles.getLayersDirectory());
// Creates the temporary directory.
Files.createDirectories(cacheStorageFiles.getTemporaryDirectory());
try (TempDirectoryProvider tempDirectoryProvider = new TempDirectoryProvider()) {
Path temporaryLayerDirectory = tempDirectoryProvider.newDirectory(cacheStorageFiles.getTemporaryDirectory());
// Writes the layer file to the temporary directory.
WrittenLayer writtenLayer = writeCompressedLayerBlobToDirectory(compressedLayerBlob, temporaryLayerDirectory);
// Moves the temporary directory to the final location.
moveIfDoesNotExist(temporaryLayerDirectory, cacheStorageFiles.getLayerDirectory(writtenLayer.layerDigest));
// Updates cachedLayer with the blob information.
Path layerFile = cacheStorageFiles.getLayerFile(writtenLayer.layerDigest, writtenLayer.layerDiffId);
return CachedLayer.builder().setLayerDigest(writtenLayer.layerDigest).setLayerDiffId(writtenLayer.layerDiffId).setLayerSize(writtenLayer.layerSize).setLayerBlob(Blobs.from(layerFile)).build();
}
}
use of com.google.cloud.tools.jib.filesystem.TempDirectoryProvider in project jib by GoogleContainerTools.
the class BuildDockerMojo method execute.
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
checkJibVersion();
if (MojoCommon.shouldSkipJibExecution(this)) {
return;
}
Path dockerExecutable = getDockerClientExecutable();
boolean isDockerInstalled = dockerExecutable == null ? DockerClient.isDefaultDockerInstalled() : DockerClient.isDockerInstalled(dockerExecutable);
if (!isDockerInstalled) {
throw new MojoExecutionException(HelpfulSuggestions.forDockerNotInstalled(HELPFUL_SUGGESTIONS_PREFIX));
}
MavenSettingsProxyProvider.activateHttpAndHttpsProxies(getSession().getSettings(), getSettingsDecrypter());
TempDirectoryProvider tempDirectoryProvider = new TempDirectoryProvider();
MavenProjectProperties projectProperties = MavenProjectProperties.getForProject(Preconditions.checkNotNull(descriptor), getProject(), getSession(), getLog(), tempDirectoryProvider, getInjectedPluginExtensions());
Future<Optional<String>> updateCheckFuture = Futures.immediateFuture(Optional.empty());
try {
GlobalConfig globalConfig = GlobalConfig.readConfig();
updateCheckFuture = MojoCommon.newUpdateChecker(projectProperties, globalConfig, getLog());
PluginConfigurationProcessor.createJibBuildRunnerForDockerDaemonImage(new MavenRawConfiguration(this), new MavenSettingsServerCredentials(getSession().getSettings(), getSettingsDecrypter()), projectProperties, globalConfig, new MavenHelpfulSuggestions(HELPFUL_SUGGESTIONS_PREFIX)).runBuild();
} catch (InvalidAppRootException ex) {
throw new MojoExecutionException("<container><appRoot> is not an absolute Unix-style path: " + ex.getInvalidPathValue(), ex);
} catch (InvalidContainerizingModeException ex) {
throw new MojoExecutionException("invalid value for <containerizingMode>: " + ex.getInvalidContainerizingMode(), ex);
} catch (InvalidWorkingDirectoryException ex) {
throw new MojoExecutionException("<container><workingDirectory> is not an absolute Unix-style path: " + ex.getInvalidPathValue(), ex);
} catch (InvalidPlatformException ex) {
throw new MojoExecutionException("<from><platforms> contains a platform configuration that is missing required values or has invalid values: " + ex.getMessage() + ": " + ex.getInvalidPlatform(), ex);
} catch (InvalidContainerVolumeException ex) {
throw new MojoExecutionException("<container><volumes> is not an absolute Unix-style path: " + ex.getInvalidVolume(), ex);
} catch (InvalidFilesModificationTimeException ex) {
throw new MojoExecutionException("<container><filesModificationTime> should be an ISO 8601 date-time (see " + "DateTimeFormatter.ISO_DATE_TIME) or special keyword \"EPOCH_PLUS_SECOND\": " + ex.getInvalidFilesModificationTime(), ex);
} catch (InvalidCreationTimeException ex) {
throw new MojoExecutionException("<container><creationTime> should be an ISO 8601 date-time (see " + "DateTimeFormatter.ISO_DATE_TIME) or a special keyword (\"EPOCH\", " + "\"USE_CURRENT_TIMESTAMP\"): " + ex.getInvalidCreationTime(), ex);
} catch (JibPluginExtensionException ex) {
String extensionName = ex.getExtensionClass().getName();
throw new MojoExecutionException("error running extension '" + extensionName + "': " + ex.getMessage(), ex);
} catch (IncompatibleBaseImageJavaVersionException ex) {
throw new MojoExecutionException(HelpfulSuggestions.forIncompatibleBaseImageJavaVersionForMaven(ex.getBaseImageMajorJavaVersion(), ex.getProjectMajorJavaVersion()), ex);
} catch (InvalidImageReferenceException ex) {
throw new MojoExecutionException(HelpfulSuggestions.forInvalidImageReference(ex.getInvalidReference()), ex);
} catch (IOException | CacheDirectoryCreationException | MainClassInferenceException | InvalidGlobalConfigException ex) {
throw new MojoExecutionException(ex.getMessage(), ex);
} catch (BuildStepsExecutionException ex) {
throw new MojoExecutionException(ex.getMessage(), ex.getCause());
} catch (ExtraDirectoryNotFoundException ex) {
throw new MojoExecutionException("<extraDirectories><paths> contain \"from\" directory that doesn't exist locally: " + ex.getPath(), ex);
} finally {
tempDirectoryProvider.close();
MojoCommon.finishUpdateChecker(projectProperties, updateCheckFuture);
projectProperties.waitForLoggingThread();
getLog().info("");
}
}
use of com.google.cloud.tools.jib.filesystem.TempDirectoryProvider in project jib by GoogleContainerTools.
the class BuildTarMojo method execute.
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
checkJibVersion();
if (MojoCommon.shouldSkipJibExecution(this)) {
return;
}
MavenSettingsProxyProvider.activateHttpAndHttpsProxies(getSession().getSettings(), getSettingsDecrypter());
TempDirectoryProvider tempDirectoryProvider = new TempDirectoryProvider();
MavenProjectProperties projectProperties = MavenProjectProperties.getForProject(Preconditions.checkNotNull(descriptor), getProject(), getSession(), getLog(), tempDirectoryProvider, getInjectedPluginExtensions());
Future<Optional<String>> updateCheckFuture = Futures.immediateFuture(Optional.empty());
try {
GlobalConfig globalConfig = GlobalConfig.readConfig();
updateCheckFuture = MojoCommon.newUpdateChecker(projectProperties, globalConfig, getLog());
PluginConfigurationProcessor.createJibBuildRunnerForTarImage(new MavenRawConfiguration(this), new MavenSettingsServerCredentials(getSession().getSettings(), getSettingsDecrypter()), projectProperties, globalConfig, new MavenHelpfulSuggestions(HELPFUL_SUGGESTIONS_PREFIX)).runBuild();
} catch (InvalidAppRootException ex) {
throw new MojoExecutionException("<container><appRoot> is not an absolute Unix-style path: " + ex.getInvalidPathValue(), ex);
} catch (InvalidContainerizingModeException ex) {
throw new MojoExecutionException("invalid value for <containerizingMode>: " + ex.getInvalidContainerizingMode(), ex);
} catch (InvalidWorkingDirectoryException ex) {
throw new MojoExecutionException("<container><workingDirectory> is not an absolute Unix-style path: " + ex.getInvalidPathValue(), ex);
} catch (InvalidPlatformException ex) {
throw new MojoExecutionException("<from><platforms> contains a platform configuration that is missing required values or has invalid values: " + ex.getMessage() + ": " + ex.getInvalidPlatform(), ex);
} catch (InvalidContainerVolumeException ex) {
throw new MojoExecutionException("<container><volumes> is not an absolute Unix-style path: " + ex.getInvalidVolume(), ex);
} catch (InvalidFilesModificationTimeException ex) {
throw new MojoExecutionException("<container><filesModificationTime> should be an ISO 8601 date-time (see " + "DateTimeFormatter.ISO_DATE_TIME) or special keyword \"EPOCH_PLUS_SECOND\": " + ex.getInvalidFilesModificationTime(), ex);
} catch (InvalidCreationTimeException ex) {
throw new MojoExecutionException("<container><creationTime> should be an ISO 8601 date-time (see " + "DateTimeFormatter.ISO_DATE_TIME) or a special keyword (\"EPOCH\", " + "\"USE_CURRENT_TIMESTAMP\"): " + ex.getInvalidCreationTime(), ex);
} catch (JibPluginExtensionException ex) {
String extensionName = ex.getExtensionClass().getName();
throw new MojoExecutionException("error running extension '" + extensionName + "': " + ex.getMessage(), ex);
} catch (IncompatibleBaseImageJavaVersionException ex) {
throw new MojoExecutionException(HelpfulSuggestions.forIncompatibleBaseImageJavaVersionForMaven(ex.getBaseImageMajorJavaVersion(), ex.getProjectMajorJavaVersion()), ex);
} catch (InvalidImageReferenceException ex) {
throw new MojoExecutionException(HelpfulSuggestions.forInvalidImageReference(ex.getInvalidReference()), ex);
} catch (IOException | CacheDirectoryCreationException | MainClassInferenceException | InvalidGlobalConfigException ex) {
throw new MojoExecutionException(ex.getMessage(), ex);
} catch (BuildStepsExecutionException ex) {
throw new MojoExecutionException(ex.getMessage(), ex.getCause());
} catch (ExtraDirectoryNotFoundException ex) {
throw new MojoExecutionException("<extraDirectories><paths> contain \"from\" directory that doesn't exist locally: " + ex.getPath(), ex);
} finally {
tempDirectoryProvider.close();
MojoCommon.finishUpdateChecker(projectProperties, updateCheckFuture);
projectProperties.waitForLoggingThread();
getLog().info("");
}
}
use of com.google.cloud.tools.jib.filesystem.TempDirectoryProvider in project jib by GoogleContainerTools.
the class SyncMapTask method listFilesAndTargets.
/**
* Task Action, lists files and container targets.
*/
@TaskAction
public void listFilesAndTargets() {
Preconditions.checkNotNull(jibExtension);
try (TempDirectoryProvider tempDirectoryProvider = new TempDirectoryProvider()) {
GradleProjectProperties projectProperties = GradleProjectProperties.getForProject(getProject(), getLogger(), tempDirectoryProvider, jibExtension.getConfigurationName().get());
GradleRawConfiguration configuration = new GradleRawConfiguration(jibExtension);
// TODO: move these shared checks with SyncMapMojo into plugins-common
if (projectProperties.isWarProject()) {
throw new GradleException("Skaffold sync is currently only available for 'jar' style Jib projects, but the project " + getProject().getName() + " is configured to generate a 'war'");
}
try {
if (!ContainerizingMode.EXPLODED.equals(ContainerizingMode.from(jibExtension.getContainerizingMode()))) {
throw new GradleException("Skaffold sync is currently only available for Jib projects in 'exploded' containerizing mode, but the containerizing mode of " + getProject().getName() + " is '" + jibExtension.getContainerizingMode() + "'");
}
} catch (InvalidContainerizingModeException ex) {
throw new GradleException("Invalid containerizing mode", ex);
}
try {
String syncMapJson = PluginConfigurationProcessor.getSkaffoldSyncMap(configuration, projectProperties, jibExtension.getSkaffold().getSync().getExcludes());
System.out.println();
System.out.println("BEGIN JIB JSON: SYNCMAP/1");
System.out.println(syncMapJson);
} catch (Exception ex) {
throw new GradleException("Failed to generate a Jib file map for sync with Skaffold", ex);
}
}
}
Aggregations