Search in sources :

Example 1 with TempDirectoryProvider

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();
    }
}
Also used : Path(java.nio.file.Path) TempDirectoryProvider(com.google.cloud.tools.jib.filesystem.TempDirectoryProvider)

Example 2 with TempDirectoryProvider

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();
    }
}
Also used : Path(java.nio.file.Path) TempDirectoryProvider(com.google.cloud.tools.jib.filesystem.TempDirectoryProvider)

Example 3 with TempDirectoryProvider

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("");
    }
}
Also used : InvalidContainerVolumeException(com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException) GlobalConfig(com.google.cloud.tools.jib.plugins.common.globalconfig.GlobalConfig) ExtraDirectoryNotFoundException(com.google.cloud.tools.jib.plugins.common.ExtraDirectoryNotFoundException) InvalidFilesModificationTimeException(com.google.cloud.tools.jib.plugins.common.InvalidFilesModificationTimeException) IncompatibleBaseImageJavaVersionException(com.google.cloud.tools.jib.plugins.common.IncompatibleBaseImageJavaVersionException) InvalidWorkingDirectoryException(com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException) TempDirectoryProvider(com.google.cloud.tools.jib.filesystem.TempDirectoryProvider) InvalidContainerizingModeException(com.google.cloud.tools.jib.plugins.common.InvalidContainerizingModeException) InvalidPlatformException(com.google.cloud.tools.jib.plugins.common.InvalidPlatformException) Path(java.nio.file.Path) InvalidAppRootException(com.google.cloud.tools.jib.plugins.common.InvalidAppRootException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Optional(java.util.Optional) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) BuildStepsExecutionException(com.google.cloud.tools.jib.plugins.common.BuildStepsExecutionException) IOException(java.io.IOException) InvalidGlobalConfigException(com.google.cloud.tools.jib.plugins.common.globalconfig.InvalidGlobalConfigException) CacheDirectoryCreationException(com.google.cloud.tools.jib.api.CacheDirectoryCreationException) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) InvalidCreationTimeException(com.google.cloud.tools.jib.plugins.common.InvalidCreationTimeException) MainClassInferenceException(com.google.cloud.tools.jib.plugins.common.MainClassInferenceException)

Example 4 with TempDirectoryProvider

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("");
    }
}
Also used : InvalidContainerVolumeException(com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException) GlobalConfig(com.google.cloud.tools.jib.plugins.common.globalconfig.GlobalConfig) ExtraDirectoryNotFoundException(com.google.cloud.tools.jib.plugins.common.ExtraDirectoryNotFoundException) InvalidFilesModificationTimeException(com.google.cloud.tools.jib.plugins.common.InvalidFilesModificationTimeException) IncompatibleBaseImageJavaVersionException(com.google.cloud.tools.jib.plugins.common.IncompatibleBaseImageJavaVersionException) InvalidWorkingDirectoryException(com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException) TempDirectoryProvider(com.google.cloud.tools.jib.filesystem.TempDirectoryProvider) InvalidContainerizingModeException(com.google.cloud.tools.jib.plugins.common.InvalidContainerizingModeException) InvalidPlatformException(com.google.cloud.tools.jib.plugins.common.InvalidPlatformException) InvalidAppRootException(com.google.cloud.tools.jib.plugins.common.InvalidAppRootException) Optional(java.util.Optional) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) BuildStepsExecutionException(com.google.cloud.tools.jib.plugins.common.BuildStepsExecutionException) IOException(java.io.IOException) InvalidGlobalConfigException(com.google.cloud.tools.jib.plugins.common.globalconfig.InvalidGlobalConfigException) CacheDirectoryCreationException(com.google.cloud.tools.jib.api.CacheDirectoryCreationException) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) InvalidCreationTimeException(com.google.cloud.tools.jib.plugins.common.InvalidCreationTimeException) MainClassInferenceException(com.google.cloud.tools.jib.plugins.common.MainClassInferenceException)

Example 5 with TempDirectoryProvider

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);
        }
    }
}
Also used : GradleProjectProperties(com.google.cloud.tools.jib.gradle.GradleProjectProperties) GradleException(org.gradle.api.GradleException) GradleRawConfiguration(com.google.cloud.tools.jib.gradle.GradleRawConfiguration) TempDirectoryProvider(com.google.cloud.tools.jib.filesystem.TempDirectoryProvider) InvalidContainerizingModeException(com.google.cloud.tools.jib.plugins.common.InvalidContainerizingModeException) InvalidContainerizingModeException(com.google.cloud.tools.jib.plugins.common.InvalidContainerizingModeException) GradleException(org.gradle.api.GradleException) TaskAction(org.gradle.api.tasks.TaskAction)

Aggregations

TempDirectoryProvider (com.google.cloud.tools.jib.filesystem.TempDirectoryProvider)22 InvalidContainerizingModeException (com.google.cloud.tools.jib.plugins.common.InvalidContainerizingModeException)16 Path (java.nio.file.Path)14 CacheDirectoryCreationException (com.google.cloud.tools.jib.api.CacheDirectoryCreationException)12 InvalidImageReferenceException (com.google.cloud.tools.jib.api.InvalidImageReferenceException)12 BuildStepsExecutionException (com.google.cloud.tools.jib.plugins.common.BuildStepsExecutionException)12 ExtraDirectoryNotFoundException (com.google.cloud.tools.jib.plugins.common.ExtraDirectoryNotFoundException)12 IncompatibleBaseImageJavaVersionException (com.google.cloud.tools.jib.plugins.common.IncompatibleBaseImageJavaVersionException)12 InvalidAppRootException (com.google.cloud.tools.jib.plugins.common.InvalidAppRootException)12 InvalidContainerVolumeException (com.google.cloud.tools.jib.plugins.common.InvalidContainerVolumeException)12 InvalidCreationTimeException (com.google.cloud.tools.jib.plugins.common.InvalidCreationTimeException)12 InvalidFilesModificationTimeException (com.google.cloud.tools.jib.plugins.common.InvalidFilesModificationTimeException)12 InvalidPlatformException (com.google.cloud.tools.jib.plugins.common.InvalidPlatformException)12 InvalidWorkingDirectoryException (com.google.cloud.tools.jib.plugins.common.InvalidWorkingDirectoryException)12 MainClassInferenceException (com.google.cloud.tools.jib.plugins.common.MainClassInferenceException)12 GlobalConfig (com.google.cloud.tools.jib.plugins.common.globalconfig.GlobalConfig)12 InvalidGlobalConfigException (com.google.cloud.tools.jib.plugins.common.globalconfig.InvalidGlobalConfigException)12 JibPluginExtensionException (com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException)12 IOException (java.io.IOException)12 Optional (java.util.Optional)12