Search in sources :

Example 1 with FilePermissions

use of com.google.cloud.tools.jib.api.buildplan.FilePermissions 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 2 with FilePermissions

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

the class MojoCommon method convertPermissionsList.

/**
 * Converts a list of {@link PermissionConfiguration} to an equivalent {@code
 * String->FilePermission} map.
 *
 * @param permissionList the list to convert
 * @return the resulting map
 */
static Map<String, FilePermissions> convertPermissionsList(List<PermissionConfiguration> permissionList) {
    // Order is important, so use a LinkedHashMap
    Map<String, FilePermissions> permissionsMap = new LinkedHashMap<>();
    for (PermissionConfiguration permission : permissionList) {
        Optional<String> file = permission.getFile();
        Optional<String> mode = permission.getMode();
        if (!file.isPresent() || !mode.isPresent()) {
            throw new IllegalArgumentException("Incomplete <permission> configuration; requires <file> and <mode> fields to be set");
        }
        permissionsMap.put(file.get(), FilePermissions.fromOctalString(mode.get()));
    }
    return permissionsMap;
}
Also used : PermissionConfiguration(com.google.cloud.tools.jib.maven.JibPluginConfiguration.PermissionConfiguration) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with FilePermissions

use of com.google.cloud.tools.jib.api.buildplan.FilePermissions in project jkube by eclipse.

the class JibServiceUtil method layers.

@Nonnull
public static List<FileEntriesLayer> layers(BuildDirs buildDirs, Map<Assembly, List<AssemblyFileEntry>> layers) {
    final List<FileEntriesLayer> fileEntriesLayers = new ArrayList<>();
    for (Map.Entry<Assembly, List<AssemblyFileEntry>> layer : layers.entrySet()) {
        final FileEntriesLayer.Builder fel = FileEntriesLayer.builder();
        final String layerId = layer.getKey().getId();
        final Path outputPath;
        if (StringUtils.isBlank(layerId)) {
            outputPath = buildDirs.getOutputDirectory().toPath();
        } else {
            fel.setName(layerId);
            outputPath = new File(buildDirs.getOutputDirectory(), layerId).toPath();
        }
        for (AssemblyFileEntry afe : layer.getValue()) {
            final Path source = afe.getSource().toPath();
            final AbsoluteUnixPath target = AbsoluteUnixPath.get(StringUtils.prependIfMissing(FilenameUtils.separatorsToUnix(outputPath.relativize(afe.getDest().toPath()).normalize().toString()), "/"));
            final FilePermissions permissions = StringUtils.isNotBlank(afe.getFileMode()) ? FilePermissions.fromOctalString(StringUtils.right(afe.getFileMode(), 3)) : DEFAULT_FILE_PERMISSIONS_PROVIDER.get(source, target);
            fel.addEntry(source, target, permissions);
        }
        fileEntriesLayers.add(fel.build());
    }
    return fileEntriesLayers;
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ArrayList(java.util.ArrayList) AssemblyFileEntry(org.eclipse.jkube.kit.common.AssemblyFileEntry) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) File(java.io.File) Assembly(org.eclipse.jkube.kit.common.Assembly) Nonnull(javax.annotation.Nonnull)

Example 4 with FilePermissions

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

the class JavaContainerBuilderHelperTest method testExtraDirectoryLayerConfiguration_overlappingPermissions.

@Test
public void testExtraDirectoryLayerConfiguration_overlappingPermissions() throws URISyntaxException, IOException {
    Path extraFilesDirectory = Paths.get(Resources.getResource("core/layer").toURI());
    Map<String, FilePermissions> permissionsMap = ImmutableMap.of("/a**", FilePermissions.fromOctalString("123"), // Should be ignored, since first match takes priority
    "/a/b**", FilePermissions.fromOctalString("000"), // Should override first match since explicit path is used instead of glob
    "/a/b/bar", FilePermissions.fromOctalString("765"));
    FileEntriesLayer fileEntriesLayer = JavaContainerBuilderHelper.extraDirectoryLayerConfiguration(extraFilesDirectory, AbsoluteUnixPath.get("/"), Collections.emptyList(), Collections.emptyList(), permissionsMap, (ignored1, ignored2) -> Instant.EPOCH);
    assertThat(fileEntriesLayer.getEntries()).comparingElementsUsing(EXTRACTION_PATH_OF).containsExactly("/a", "/a/b", "/a/b/bar", "/c", "/c/cat", "/foo");
    Map<AbsoluteUnixPath, FilePermissions> expectedPermissions = ImmutableMap.<AbsoluteUnixPath, FilePermissions>builder().put(AbsoluteUnixPath.get("/a"), FilePermissions.fromOctalString("123")).put(AbsoluteUnixPath.get("/a/b"), FilePermissions.fromOctalString("123")).put(AbsoluteUnixPath.get("/a/b/bar"), FilePermissions.fromOctalString("765")).put(AbsoluteUnixPath.get("/c"), FilePermissions.DEFAULT_FOLDER_PERMISSIONS).put(AbsoluteUnixPath.get("/c/cat"), FilePermissions.DEFAULT_FILE_PERMISSIONS).put(AbsoluteUnixPath.get("/foo"), FilePermissions.DEFAULT_FILE_PERMISSIONS).build();
    for (FileEntry entry : fileEntriesLayer.getEntries()) {
        assertThat(entry.getPermissions()).isEqualTo(expectedPermissions.get(entry.getExtractionPath()));
    }
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) Test(org.junit.Test)

Example 5 with FilePermissions

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

the class LayerEntriesSelectorTest method testGenerateSelector_targetModificationTimeChanged.

@Test
public void testGenerateSelector_targetModificationTimeChanged() throws IOException {
    Path layerFile = temporaryFolder.newFile().toPath();
    AbsoluteUnixPath pathInContainer = AbsoluteUnixPath.get("/bar");
    FilePermissions permissions = FilePermissions.fromOctalString("111");
    FileEntry layerEntry1 = new FileEntry(layerFile, pathInContainer, permissions, Instant.now());
    FileEntry layerEntry2 = new FileEntry(layerFile, pathInContainer, permissions, Instant.EPOCH);
    // Verify that different target modification times generate different selectors
    Assert.assertNotEquals(LayerEntriesSelector.generateSelector(ImmutableList.of(layerEntry1)), LayerEntriesSelector.generateSelector(ImmutableList.of(layerEntry2)));
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) Test(org.junit.Test)

Aggregations

FilePermissions (com.google.cloud.tools.jib.api.buildplan.FilePermissions)14 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)12 Path (java.nio.file.Path)12 FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)10 FileEntry (com.google.cloud.tools.jib.api.buildplan.FileEntry)8 Test (org.junit.Test)6 PathMatcher (java.nio.file.PathMatcher)5 LinkedHashMap (java.util.LinkedHashMap)5 List (java.util.List)5 IOException (java.io.IOException)4 FileSystems (java.nio.file.FileSystems)4 Files (java.nio.file.Files)4 Map (java.util.Map)4 Set (java.util.Set)4 DirectoryWalker (com.google.cloud.tools.jib.filesystem.DirectoryWalker)3 Instant (java.time.Instant)3 ArrayList (java.util.ArrayList)3 JavaContainerBuilder (com.google.cloud.tools.jib.api.JavaContainerBuilder)2 LayerType (com.google.cloud.tools.jib.api.JavaContainerBuilder.LayerType)2 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)2