Search in sources :

Example 6 with DirectoryWalker

use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by GoogleContainerTools.

the class JavaContainerBuilder method addDirectoryContentsToLayer.

private void addDirectoryContentsToLayer(Map<LayerType, FileEntriesLayer.Builder> layerBuilders, LayerType layerType, Path sourceRoot, Predicate<Path> pathFilter, AbsoluteUnixPath basePathInContainer) throws IOException {
    if (!layerBuilders.containsKey(layerType)) {
        layerBuilders.put(layerType, FileEntriesLayer.builder());
    }
    FileEntriesLayer.Builder builder = layerBuilders.get(layerType);
    new DirectoryWalker(sourceRoot).filterRoot().filter(path -> Files.isDirectory(path) || pathFilter.test(path)).walk(path -> {
        AbsoluteUnixPath pathOnContainer = basePathInContainer.resolve(sourceRoot.relativize(path));
        Instant modificationTime = modificationTimeProvider.get(path, pathOnContainer);
        builder.addEntry(path, pathOnContainer, modificationTime);
    });
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) Arrays(java.util.Arrays) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) ImmutableMap(com.google.common.collect.ImmutableMap) Files(java.nio.file.Files) EnumMap(java.util.EnumMap) Predicate(java.util.function.Predicate) NotDirectoryException(java.nio.file.NotDirectoryException) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) IOException(java.io.IOException) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ModificationTimeProvider(com.google.cloud.tools.jib.api.buildplan.ModificationTimeProvider) Streams(com.google.common.collect.Streams) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) ProjectInfo(com.google.cloud.tools.jib.ProjectInfo) RelativeUnixPath(com.google.cloud.tools.jib.api.buildplan.RelativeUnixPath) Preconditions(com.google.common.base.Preconditions) Path(java.nio.file.Path) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Instant(java.time.Instant) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker)

Example 7 with DirectoryWalker

use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project quarkus by quarkusio.

the class ContainerBuilderHelper 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 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, Map<String, FilePermissions> extraDirectoryPermissions, BiFunction<Path, AbsoluteUnixPath, Instant> modificationTimeProvider) throws IOException {
    FileEntriesLayer.Builder builder = FileEntriesLayer.builder().setName(JavaContainerBuilder.LayerType.EXTRA_FILES.getName());
    Map<PathMatcher, FilePermissions> pathMatchers = new LinkedHashMap<>();
    for (Map.Entry<String, FilePermissions> entry : extraDirectoryPermissions.entrySet()) {
        pathMatchers.put(FileSystems.getDefault().getPathMatcher("glob:" + entry.getKey()), entry.getValue());
    }
    new DirectoryWalker(sourceDirectory).filterRoot().walk(localPath -> {
        AbsoluteUnixPath pathOnContainer = targetDirectory.resolve(sourceDirectory.relativize(localPath));
        Instant modificationTime = modificationTimeProvider.apply(localPath, pathOnContainer);
        FilePermissions permissions = extraDirectoryPermissions.get(pathOnContainer.toString());
        if (permissions == null) {
            // Check for matching globs
            Path containerPath = Paths.get(pathOnContainer.toString());
            for (Map.Entry<PathMatcher, FilePermissions> entry : pathMatchers.entrySet()) {
                if (entry.getKey().matches(containerPath)) {
                    builder.addEntry(localPath, pathOnContainer, entry.getValue(), modificationTime);
                    return;
                }
            }
            // Add with default permissions
            if (localPath.toFile().canExecute()) {
                // make sure the file or directory can be executed
                builder.addEntry(localPath, pathOnContainer, FilePermissions.fromOctalString("755"), modificationTime);
            } else {
                builder.addEntry(localPath, pathOnContainer, modificationTime);
            }
        } else {
            // Add with explicit permissions
            builder.addEntry(localPath, pathOnContainer, permissions, modificationTime);
        }
    });
    return builder.build();
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) 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 8 with DirectoryWalker

use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by GoogleContainerTools.

the class TestProject method copyProject.

/**
 * Copies test project {@code projectName} to {@code destination} folder.
 */
private static void copyProject(String projectName, Path destination) throws IOException, URISyntaxException {
    Path projectPathInResources = Paths.get(Resources.getResource(PROJECTS_PATH_IN_RESOURCES + projectName).toURI());
    new DirectoryWalker(projectPathInResources).filterRoot().walk(path -> {
        // Creates the same path in the destDir.
        Path destPath = destination.resolve(projectPathInResources.relativize(path));
        if (Files.isDirectory(path)) {
            Files.createDirectory(destPath);
        } else {
            Files.copy(path, destPath);
        }
    });
}
Also used : Path(java.nio.file.Path) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker)

Example 9 with DirectoryWalker

use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by google.

the class CacheReaderTest method testGetUpToDateLayerBySourceFiles.

@Test
public void testGetUpToDateLayerBySourceFiles() throws URISyntaxException, IOException, CacheMetadataCorruptedException {
    // The two last modified times to use. Must be in thousands as most file time granularity is in
    // seconds.
    FileTime olderLastModifiedTime = FileTime.fromMillis(1000);
    FileTime newerLastModifiedTime = FileTime.fromMillis(2000);
    // Copies test files to a modifiable temporary folder.
    Path resourceSourceFiles = Paths.get(Resources.getResource("layer").toURI());
    Path testSourceFiles = temporaryFolder.newFolder().toPath();
    copyDirectory(resourceSourceFiles, testSourceFiles);
    // Walk the files in reverse order so that the subfiles are changed before the parent
    // directories are.
    List<Path> paths = new DirectoryWalker(testSourceFiles).walk();
    paths.sort(Comparator.reverseOrder());
    for (Path path : paths) {
        Files.setLastModifiedTime(path, olderLastModifiedTime);
    }
    // Sets the metadata source file to the new temporary folder.
    CachedLayerWithMetadata classesCachedLayer;
    try (Cache cache = Cache.init(testCacheFolder)) {
        ImageLayers<CachedLayerWithMetadata> cachedLayers = cache.getMetadata().filterLayers().filter();
        Assert.assertEquals(3, cachedLayers.size());
        classesCachedLayer = cachedLayers.get(2);
        classesCachedLayer.getMetadata().setSourceFiles(Collections.singletonList(testSourceFiles.toString()));
    }
    try (Cache cache = Cache.init(testCacheFolder)) {
        CacheReader cacheReader = new CacheReader(cache);
        Assert.assertEquals(classesCachedLayer.getBlobDescriptor(), cacheReader.getUpToDateLayerBySourceFiles(Collections.singletonList(testSourceFiles)).getBlobDescriptor());
        // Changes a file and checks that the change is detected.
        Files.setLastModifiedTime(testSourceFiles.resolve("a").resolve("b").resolve("bar"), newerLastModifiedTime);
        Assert.assertNull(cacheReader.getUpToDateLayerBySourceFiles(Collections.singletonList(testSourceFiles)));
        // Any non-cached directory should be deemed modified.
        Assert.assertNull(cacheReader.getUpToDateLayerBySourceFiles(Collections.singletonList(resourceSourceFiles)));
    }
}
Also used : Path(java.nio.file.Path) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) FileTime(java.nio.file.attribute.FileTime) Test(org.junit.Test)

Example 10 with DirectoryWalker

use of com.google.cloud.tools.jib.filesystem.DirectoryWalker in project jib by google.

the class CacheReaderTest method copyDirectory.

// TODO: Replace with filesystem.DirectoryWalker.
private static void copyDirectory(Path source, Path destination) throws IOException {
    new DirectoryWalker(source).filter(path -> !path.equals(source)).walk(path -> {
        Path newPath = destination.resolve(source.relativize(path));
        Files.copy(path, newPath);
    });
}
Also used : ImageLayers(com.google.cloud.tools.jib.image.ImageLayers) Resources(com.google.common.io.Resources) Files(java.nio.file.Files) URISyntaxException(java.net.URISyntaxException) FileTime(java.nio.file.attribute.FileTime) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) IOException(java.io.IOException) Test(org.junit.Test) LayerPropertyNotFoundException(com.google.cloud.tools.jib.image.LayerPropertyNotFoundException) ReferenceLayer(com.google.cloud.tools.jib.image.ReferenceLayer) BlobDescriptor(com.google.cloud.tools.jib.blob.BlobDescriptor) List(java.util.List) Rule(org.junit.Rule) DescriptorDigest(com.google.cloud.tools.jib.image.DescriptorDigest) Paths(java.nio.file.Paths) DigestException(java.security.DigestException) Comparator(java.util.Comparator) Assert(org.junit.Assert) Path(java.nio.file.Path) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) Before(org.junit.Before) Path(java.nio.file.Path) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker)

Aggregations

DirectoryWalker (com.google.cloud.tools.jib.filesystem.DirectoryWalker)52 Path (java.nio.file.Path)48 Test (org.junit.Test)23 Result (com.google.cloud.tools.jib.api.MainClassFinder.Result)20 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)15 IOException (java.io.IOException)11 FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)9 Files (java.nio.file.Files)9 FileTime (java.nio.file.attribute.FileTime)8 List (java.util.List)8 Predicate (java.util.function.Predicate)8 Instant (java.time.Instant)7 Map (java.util.Map)7 ModificationTimeProvider (com.google.cloud.tools.jib.api.buildplan.ModificationTimeProvider)6 RelativeUnixPath (com.google.cloud.tools.jib.api.buildplan.RelativeUnixPath)6 InputStream (java.io.InputStream)6 Paths (java.nio.file.Paths)6 ArrayList (java.util.ArrayList)6 StringJoiner (java.util.StringJoiner)6 FilePermissions (com.google.cloud.tools.jib.api.buildplan.FilePermissions)5