use of com.google.cloud.tools.jib.filesystem.TempDirectoryProvider in project jib by google.
the class BuildTarTask method buildTar.
/**
* Task Action, builds an image to tar file.
*
* @throws IOException if an error occurs creating the jib runner
* @throws BuildStepsExecutionException if an error occurs while executing build steps
* @throws CacheDirectoryCreationException if a new cache directory could not be created
* @throws MainClassInferenceException if a main class could not be found
* @throws InvalidGlobalConfigException if the global config file is invalid
*/
@TaskAction
public void buildTar() throws BuildStepsExecutionException, IOException, CacheDirectoryCreationException, MainClassInferenceException, InvalidGlobalConfigException {
// Asserts required @Input parameters are not null.
Preconditions.checkNotNull(jibExtension);
TaskCommon.disableHttpLogging();
TempDirectoryProvider tempDirectoryProvider = new TempDirectoryProvider();
GradleProjectProperties projectProperties = GradleProjectProperties.getForProject(getProject(), getLogger(), tempDirectoryProvider, jibExtension.getConfigurationName().get());
GlobalConfig globalConfig = GlobalConfig.readConfig();
Future<Optional<String>> updateCheckFuture = TaskCommon.newUpdateChecker(projectProperties, globalConfig, getLogger());
try {
PluginConfigurationProcessor.createJibBuildRunnerForTarImage(new GradleRawConfiguration(jibExtension), ignored -> Optional.empty(), projectProperties, globalConfig, new GradleHelpfulSuggestions(HELPFUL_SUGGESTIONS_PREFIX)).runBuild();
} catch (InvalidAppRootException ex) {
throw new GradleException("container.appRoot is not an absolute Unix-style path: " + ex.getInvalidPathValue(), ex);
} catch (InvalidContainerizingModeException ex) {
throw new GradleException("invalid value for containerizingMode: " + ex.getInvalidContainerizingMode(), ex);
} catch (InvalidWorkingDirectoryException ex) {
throw new GradleException("container.workingDirectory is not an absolute Unix-style path: " + ex.getInvalidPathValue(), ex);
} catch (InvalidPlatformException ex) {
throw new GradleException("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 GradleException("container.volumes is not an absolute Unix-style path: " + ex.getInvalidVolume(), ex);
} catch (InvalidFilesModificationTimeException ex) {
throw new GradleException("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 GradleException("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 GradleException("error running extension '" + extensionName + "': " + ex.getMessage(), ex);
} catch (IncompatibleBaseImageJavaVersionException ex) {
throw new GradleException(HelpfulSuggestions.forIncompatibleBaseImageJavaVersionForGradle(ex.getBaseImageMajorJavaVersion(), ex.getProjectMajorJavaVersion()), ex);
} catch (InvalidImageReferenceException ex) {
throw new GradleException(HelpfulSuggestions.forInvalidImageReference(ex.getInvalidReference()), ex);
} catch (ExtraDirectoryNotFoundException ex) {
throw new GradleException("extraDirectories.paths contain \"from\" directory that doesn't exist locally: " + ex.getPath(), ex);
} finally {
tempDirectoryProvider.close();
TaskCommon.finishUpdateChecker(projectProperties, updateCheckFuture);
projectProperties.waitForLoggingThread();
}
}
use of com.google.cloud.tools.jib.filesystem.TempDirectoryProvider in project jib by google.
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 google.
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 google.
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);
}
}
}
use of com.google.cloud.tools.jib.filesystem.TempDirectoryProvider in project jib by google.
the class BuildDockerTask method buildDocker.
/**
* Task Action, builds an image to docker daemon.
*
* @throws IOException if an error occurs creating the jib runner
* @throws BuildStepsExecutionException if an error occurs while executing build steps
* @throws CacheDirectoryCreationException if a new cache directory could not be created
* @throws MainClassInferenceException if a main class could not be found
* @throws InvalidGlobalConfigException if the global config file is invalid
*/
@TaskAction
public void buildDocker() throws IOException, BuildStepsExecutionException, CacheDirectoryCreationException, MainClassInferenceException, InvalidGlobalConfigException {
Preconditions.checkNotNull(jibExtension);
// Check deprecated parameters
Path dockerExecutable = jibExtension.getDockerClient().getExecutablePath();
boolean isDockerInstalled = dockerExecutable == null ? DockerClient.isDefaultDockerInstalled() : DockerClient.isDockerInstalled(dockerExecutable);
if (!isDockerInstalled) {
throw new GradleException(HelpfulSuggestions.forDockerNotInstalled(HELPFUL_SUGGESTIONS_PREFIX));
}
TaskCommon.disableHttpLogging();
TempDirectoryProvider tempDirectoryProvider = new TempDirectoryProvider();
GradleProjectProperties projectProperties = GradleProjectProperties.getForProject(getProject(), getLogger(), tempDirectoryProvider, jibExtension.getConfigurationName().get());
GlobalConfig globalConfig = GlobalConfig.readConfig();
Future<Optional<String>> updateCheckFuture = TaskCommon.newUpdateChecker(projectProperties, globalConfig, getLogger());
try {
PluginConfigurationProcessor.createJibBuildRunnerForDockerDaemonImage(new GradleRawConfiguration(jibExtension), ignored -> java.util.Optional.empty(), projectProperties, globalConfig, new GradleHelpfulSuggestions(HELPFUL_SUGGESTIONS_PREFIX)).runBuild();
} catch (InvalidAppRootException ex) {
throw new GradleException("container.appRoot is not an absolute Unix-style path: " + ex.getInvalidPathValue(), ex);
} catch (InvalidContainerizingModeException ex) {
throw new GradleException("invalid value for containerizingMode: " + ex.getInvalidContainerizingMode(), ex);
} catch (InvalidWorkingDirectoryException ex) {
throw new GradleException("container.workingDirectory is not an absolute Unix-style path: " + ex.getInvalidPathValue(), ex);
} catch (InvalidPlatformException ex) {
throw new GradleException("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 GradleException("container.volumes is not an absolute Unix-style path: " + ex.getInvalidVolume(), ex);
} catch (InvalidFilesModificationTimeException ex) {
throw new GradleException("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 GradleException("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 GradleException("error running extension '" + extensionName + "': " + ex.getMessage(), ex);
} catch (IncompatibleBaseImageJavaVersionException ex) {
throw new GradleException(HelpfulSuggestions.forIncompatibleBaseImageJavaVersionForGradle(ex.getBaseImageMajorJavaVersion(), ex.getProjectMajorJavaVersion()), ex);
} catch (InvalidImageReferenceException ex) {
throw new GradleException(HelpfulSuggestions.forInvalidImageReference(ex.getInvalidReference()), ex);
} catch (ExtraDirectoryNotFoundException ex) {
throw new GradleException("extraDirectories.paths contain \"from\" directory that doesn't exist locally: " + ex.getPath(), ex);
} finally {
tempDirectoryProvider.close();
TaskCommon.finishUpdateChecker(projectProperties, updateCheckFuture);
projectProperties.waitForLoggingThread();
}
}
Aggregations