Search in sources :

Example 6 with ModificationTimeProvider

use of com.google.cloud.tools.jib.api.buildplan.ModificationTimeProvider in project jib by google.

the class JavaContainerBuilderHelper method extraDirectoryLayerConfiguration.

/**
 * Returns a {@link FileEntriesLayer} for adding the extra directory to the container.
 *
 * @param sourceDirectory the source extra directory path
 * @param targetDirectory the root directory on the container to place the files in
 * @param includes the list of glob patterns to include from the source directory
 * @param excludes the list of glob patterns to exclude from the source directory
 * @param extraDirectoryPermissions map from path on container to file permissions
 * @param modificationTimeProvider file modification time provider
 * @return a {@link FileEntriesLayer} for adding the extra directory to the container
 * @throws IOException if walking the extra directory fails
 */
public static FileEntriesLayer extraDirectoryLayerConfiguration(Path sourceDirectory, AbsoluteUnixPath targetDirectory, List<String> includes, List<String> excludes, Map<String, FilePermissions> extraDirectoryPermissions, ModificationTimeProvider modificationTimeProvider) throws IOException {
    FileEntriesLayer.Builder builder = FileEntriesLayer.builder().setName(LayerType.EXTRA_FILES.getName());
    Map<PathMatcher, FilePermissions> permissionsPathMatchers = new LinkedHashMap<>();
    for (Map.Entry<String, FilePermissions> entry : extraDirectoryPermissions.entrySet()) {
        permissionsPathMatchers.put(FileSystems.getDefault().getPathMatcher(GLOB_PREFIX + entry.getKey()), entry.getValue());
    }
    DirectoryWalker walker = new DirectoryWalker(sourceDirectory).filterRoot();
    // add exclusion filters
    excludes.stream().map(pattern -> FileSystems.getDefault().getPathMatcher(GLOB_PREFIX + pattern)).forEach(pathMatcher -> walker.filter(path -> !pathMatcher.matches(sourceDirectory.relativize(path))));
    // add an inclusion filter
    includes.stream().map(pattern -> FileSystems.getDefault().getPathMatcher(GLOB_PREFIX + pattern)).map(pathMatcher -> (Predicate<Path>) path -> pathMatcher.matches(sourceDirectory.relativize(path))).reduce((matches1, matches2) -> matches1.or(matches2)).ifPresent(walker::filter);
    // walk the source tree and add layer entries
    walker.walk(localPath -> {
        AbsoluteUnixPath pathOnContainer = targetDirectory.resolve(sourceDirectory.relativize(localPath));
        Instant modificationTime = modificationTimeProvider.get(localPath, pathOnContainer);
        Optional<FilePermissions> permissions = determinePermissions(pathOnContainer, extraDirectoryPermissions, permissionsPathMatchers);
        if (permissions.isPresent()) {
            builder.addEntry(localPath, pathOnContainer, permissions.get(), modificationTime);
        } else {
            builder.addEntry(localPath, pathOnContainer, modificationTime);
        }
    });
    return builder.build();
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) Set(java.util.Set) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) IOException(java.io.IOException) JavaContainerBuilder(com.google.cloud.tools.jib.api.JavaContainerBuilder) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ModificationTimeProvider(com.google.cloud.tools.jib.api.buildplan.ModificationTimeProvider) Instant(java.time.Instant) LinkedHashMap(java.util.LinkedHashMap) LayerType(com.google.cloud.tools.jib.api.JavaContainerBuilder.LayerType) List(java.util.List) Paths(java.nio.file.Paths) Map(java.util.Map) RelativeUnixPath(com.google.cloud.tools.jib.api.buildplan.RelativeUnixPath) PathMatcher(java.nio.file.PathMatcher) Optional(java.util.Optional) Path(java.nio.file.Path) FileSystems(java.nio.file.FileSystems) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) RelativeUnixPath(com.google.cloud.tools.jib.api.buildplan.RelativeUnixPath) Path(java.nio.file.Path) Instant(java.time.Instant) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) LinkedHashMap(java.util.LinkedHashMap) PathMatcher(java.nio.file.PathMatcher) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 7 with ModificationTimeProvider

use of com.google.cloud.tools.jib.api.buildplan.ModificationTimeProvider in project jib by google.

the class PluginConfigurationProcessor method processCommonConfiguration.

@VisibleForTesting
static JibContainerBuilder processCommonConfiguration(RawConfiguration rawConfiguration, InferredAuthProvider inferredAuthProvider, ProjectProperties projectProperties) throws InvalidFilesModificationTimeException, InvalidAppRootException, IncompatibleBaseImageJavaVersionException, IOException, InvalidImageReferenceException, InvalidContainerizingModeException, MainClassInferenceException, InvalidPlatformException, InvalidContainerVolumeException, InvalidWorkingDirectoryException, InvalidCreationTimeException, ExtraDirectoryNotFoundException {
    // Create and configure JibContainerBuilder
    ModificationTimeProvider modificationTimeProvider = createModificationTimeProvider(rawConfiguration.getFilesModificationTime());
    JavaContainerBuilder javaContainerBuilder = getJavaContainerBuilderWithBaseImage(rawConfiguration, projectProperties, inferredAuthProvider).setAppRoot(getAppRootChecked(rawConfiguration, projectProperties)).setModificationTimeProvider(modificationTimeProvider);
    JibContainerBuilder jibContainerBuilder = projectProperties.createJibContainerBuilder(javaContainerBuilder, getContainerizingModeChecked(rawConfiguration, projectProperties));
    jibContainerBuilder.setFormat(rawConfiguration.getImageFormat()).setPlatforms(getPlatformsSet(rawConfiguration)).setEntrypoint(computeEntrypoint(rawConfiguration, projectProperties, jibContainerBuilder)).setProgramArguments(rawConfiguration.getProgramArguments().orElse(null)).setEnvironment(rawConfiguration.getEnvironment()).setExposedPorts(Ports.parse(rawConfiguration.getPorts())).setVolumes(getVolumesSet(rawConfiguration)).setLabels(rawConfiguration.getLabels()).setUser(rawConfiguration.getUser().orElse(null)).setCreationTime(getCreationTime(rawConfiguration.getCreationTime(), projectProperties));
    getWorkingDirectoryChecked(rawConfiguration).ifPresent(jibContainerBuilder::setWorkingDirectory);
    // Adds all the extra files.
    for (ExtraDirectoriesConfiguration extraDirectory : rawConfiguration.getExtraDirectories()) {
        Path from = extraDirectory.getFrom();
        if (Files.exists(from)) {
            jibContainerBuilder.addFileEntriesLayer(JavaContainerBuilderHelper.extraDirectoryLayerConfiguration(from, AbsoluteUnixPath.get(extraDirectory.getInto()), extraDirectory.getIncludesList(), extraDirectory.getExcludesList(), rawConfiguration.getExtraDirectoryPermissions(), modificationTimeProvider));
        } else if (!from.endsWith(DEFAULT_JIB_DIR)) {
            throw new ExtraDirectoryNotFoundException(from.toString(), from.toString());
        }
    }
    return jibContainerBuilder;
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) JavaContainerBuilder(com.google.cloud.tools.jib.api.JavaContainerBuilder) ModificationTimeProvider(com.google.cloud.tools.jib.api.buildplan.ModificationTimeProvider) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) ExtraDirectoriesConfiguration(com.google.cloud.tools.jib.plugins.common.RawConfiguration.ExtraDirectoriesConfiguration) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 8 with ModificationTimeProvider

use of com.google.cloud.tools.jib.api.buildplan.ModificationTimeProvider in project jib by GoogleContainerTools.

the class JavaContainerBuilderHelper method extraDirectoryLayerConfiguration.

/**
 * Returns a {@link FileEntriesLayer} for adding the extra directory to the container.
 *
 * @param sourceDirectory the source extra directory path
 * @param targetDirectory the root directory on the container to place the files in
 * @param includes the list of glob patterns to include from the source directory
 * @param excludes the list of glob patterns to exclude from the source directory
 * @param extraDirectoryPermissions map from path on container to file permissions
 * @param modificationTimeProvider file modification time provider
 * @return a {@link FileEntriesLayer} for adding the extra directory to the container
 * @throws IOException if walking the extra directory fails
 */
public static FileEntriesLayer extraDirectoryLayerConfiguration(Path sourceDirectory, AbsoluteUnixPath targetDirectory, List<String> includes, List<String> excludes, Map<String, FilePermissions> extraDirectoryPermissions, ModificationTimeProvider modificationTimeProvider) throws IOException {
    FileEntriesLayer.Builder builder = FileEntriesLayer.builder().setName(LayerType.EXTRA_FILES.getName());
    Map<PathMatcher, FilePermissions> permissionsPathMatchers = new LinkedHashMap<>();
    for (Map.Entry<String, FilePermissions> entry : extraDirectoryPermissions.entrySet()) {
        permissionsPathMatchers.put(FileSystems.getDefault().getPathMatcher(GLOB_PREFIX + entry.getKey()), entry.getValue());
    }
    DirectoryWalker walker = new DirectoryWalker(sourceDirectory).filterRoot();
    // add exclusion filters
    excludes.stream().map(pattern -> FileSystems.getDefault().getPathMatcher(GLOB_PREFIX + pattern)).forEach(pathMatcher -> walker.filter(path -> !pathMatcher.matches(sourceDirectory.relativize(path))));
    // add an inclusion filter
    includes.stream().map(pattern -> FileSystems.getDefault().getPathMatcher(GLOB_PREFIX + pattern)).map(pathMatcher -> (Predicate<Path>) path -> pathMatcher.matches(sourceDirectory.relativize(path))).reduce((matches1, matches2) -> matches1.or(matches2)).ifPresent(walker::filter);
    // walk the source tree and add layer entries
    walker.walk(localPath -> {
        AbsoluteUnixPath pathOnContainer = targetDirectory.resolve(sourceDirectory.relativize(localPath));
        Instant modificationTime = modificationTimeProvider.get(localPath, pathOnContainer);
        Optional<FilePermissions> permissions = determinePermissions(pathOnContainer, extraDirectoryPermissions, permissionsPathMatchers);
        if (permissions.isPresent()) {
            builder.addEntry(localPath, pathOnContainer, permissions.get(), modificationTime);
        } else {
            builder.addEntry(localPath, pathOnContainer, modificationTime);
        }
    });
    return builder.build();
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) Set(java.util.Set) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) IOException(java.io.IOException) JavaContainerBuilder(com.google.cloud.tools.jib.api.JavaContainerBuilder) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ModificationTimeProvider(com.google.cloud.tools.jib.api.buildplan.ModificationTimeProvider) Instant(java.time.Instant) LinkedHashMap(java.util.LinkedHashMap) LayerType(com.google.cloud.tools.jib.api.JavaContainerBuilder.LayerType) List(java.util.List) Paths(java.nio.file.Paths) Map(java.util.Map) RelativeUnixPath(com.google.cloud.tools.jib.api.buildplan.RelativeUnixPath) PathMatcher(java.nio.file.PathMatcher) Optional(java.util.Optional) Path(java.nio.file.Path) FileSystems(java.nio.file.FileSystems) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) RelativeUnixPath(com.google.cloud.tools.jib.api.buildplan.RelativeUnixPath) Path(java.nio.file.Path) Instant(java.time.Instant) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) LinkedHashMap(java.util.LinkedHashMap) PathMatcher(java.nio.file.PathMatcher) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

ModificationTimeProvider (com.google.cloud.tools.jib.api.buildplan.ModificationTimeProvider)8 JavaContainerBuilder (com.google.cloud.tools.jib.api.JavaContainerBuilder)4 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)4 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)4 Path (java.nio.file.Path)4 Instant (java.time.Instant)4 Test (org.junit.Test)4 LayerType (com.google.cloud.tools.jib.api.JavaContainerBuilder.LayerType)2 FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)2 FilePermissions (com.google.cloud.tools.jib.api.buildplan.FilePermissions)2 RelativeUnixPath (com.google.cloud.tools.jib.api.buildplan.RelativeUnixPath)2 DirectoryWalker (com.google.cloud.tools.jib.filesystem.DirectoryWalker)2 ExtraDirectoriesConfiguration (com.google.cloud.tools.jib.plugins.common.RawConfiguration.ExtraDirectoriesConfiguration)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 IOException (java.io.IOException)2 FileSystems (java.nio.file.FileSystems)2 Files (java.nio.file.Files)2 PathMatcher (java.nio.file.PathMatcher)2 Paths (java.nio.file.Paths)2 LinkedHashMap (java.util.LinkedHashMap)2