Search in sources :

Example 6 with AbsoluteUnixPath

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

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

the class PluginConfigurationProcessor method getVolumesSet.

/**
 * Parses the list of raw volumes directories to a set of {@link AbsoluteUnixPath}.
 *
 * @param rawConfiguration raw configuration data
 * @return the set of parsed volumes.
 * @throws InvalidContainerVolumeException if {@code volumes} are not valid absolute Unix paths
 */
@VisibleForTesting
static Set<AbsoluteUnixPath> getVolumesSet(RawConfiguration rawConfiguration) throws InvalidContainerVolumeException {
    Set<AbsoluteUnixPath> volumes = new HashSet<>();
    for (String path : rawConfiguration.getVolumes()) {
        try {
            AbsoluteUnixPath absoluteUnixPath = AbsoluteUnixPath.get(path);
            volumes.add(absoluteUnixPath);
        } catch (IllegalArgumentException exception) {
            throw new InvalidContainerVolumeException(path, path, exception);
        }
    }
    return volumes;
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 8 with AbsoluteUnixPath

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

the class PluginConfigurationProcessor method addJvmArgFilesLayer.

@VisibleForTesting
static void addJvmArgFilesLayer(RawConfiguration rawConfiguration, ProjectProperties projectProperties, JibContainerBuilder jibContainerBuilder, String classpath, String mainClass) throws IOException, InvalidAppRootException {
    Path projectCache = projectProperties.getDefaultCacheDirectory();
    Path classpathFile = projectCache.resolve(JIB_CLASSPATH_FILE);
    Path mainClassFile = projectCache.resolve(JIB_MAIN_CLASS_FILE);
    // It's perfectly fine to always generate a new temp file or rewrite an existing file. However,
    // fixing the source file path and preserving the file timestamp prevents polluting the Jib
    // layer cache space by not creating new cache selectors every time. (Note, however, creating
    // new selectors does not affect correctness at all.)
    writeFileConservatively(classpathFile, classpath);
    writeFileConservatively(mainClassFile, mainClass);
    AbsoluteUnixPath appRoot = getAppRootChecked(rawConfiguration, projectProperties);
    jibContainerBuilder.addFileEntriesLayer(FileEntriesLayer.builder().setName(LayerType.JVM_ARG_FILES.getName()).addEntry(classpathFile, appRoot.resolve(JIB_CLASSPATH_FILE)).addEntry(mainClassFile, appRoot.resolve(JIB_MAIN_CLASS_FILE)).build());
}
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) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 9 with AbsoluteUnixPath

use of com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath in project geronimo-arthur by apache.

the class JibMojo method createOthersLayer.

private FileEntriesLayer createOthersLayer() {
    final FileEntriesLayer.Builder builder = FileEntriesLayer.builder().setName("Others");
    otherFiles.stream().map(File::toPath).forEach(f -> {
        final AbsoluteUnixPath containerPath = AbsoluteUnixPath.get(project.getBasedir().toPath().relativize(f).toString());
        if (containerPath.toString().contains("..")) {
            throw new IllegalArgumentException("You can only include files included in basedir");
        }
        try {
            if (Files.isDirectory(f)) {
                builder.addEntryRecursive(f, containerPath, (l, c) -> FilePermissions.DEFAULT_FILE_PERMISSIONS, (l, c) -> {
                    try {
                        return getTimestamp(l);
                    } catch (final IOException e) {
                        throw new IllegalStateException(e);
                    }
                });
            } else {
                builder.addEntry(f, containerPath, FilePermissions.DEFAULT_FILE_PERMISSIONS, getTimestamp(f));
            }
        } catch (final IOException e) {
            throw new IllegalStateException(e);
        }
    });
    return builder.build();
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) IOException(java.io.IOException)

Example 10 with AbsoluteUnixPath

use of com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath 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)

Aggregations

AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)46 Path (java.nio.file.Path)34 FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)27 IOException (java.io.IOException)18 Test (org.junit.Test)18 FilePermissions (com.google.cloud.tools.jib.api.buildplan.FilePermissions)15 Files (java.nio.file.Files)14 Map (java.util.Map)14 List (java.util.List)13 Instant (java.time.Instant)11 ArrayList (java.util.ArrayList)10 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)9 FileEntry (com.google.cloud.tools.jib.api.buildplan.FileEntry)9 VisibleForTesting (com.google.common.annotations.VisibleForTesting)9 Optional (java.util.Optional)8 Predicate (java.util.function.Predicate)8 Collectors (java.util.stream.Collectors)8 Stream (java.util.stream.Stream)8 JavaContainerBuilder (com.google.cloud.tools.jib.api.JavaContainerBuilder)7 BuildContext (com.google.cloud.tools.jib.configuration.BuildContext)7