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();
}
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;
}
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;
}
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()));
}
}
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)));
}
Aggregations