Search in sources :

Example 11 with FileEntriesLayer

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

the class LayersTest method parseLayers.

public static List<FileEntriesLayer> parseLayers(Path testDir, int expectedLayerCount) throws IOException {
    Path layersSpecYaml = testDir.resolve("layers.yaml");
    List<FileEntriesLayer> layers = Layers.toLayers(layersSpecYaml.getParent(), new ObjectMapper(new YAMLFactory()).readValue(Files.newBufferedReader(layersSpecYaml, Charsets.UTF_8), LayersSpec.class));
    Assert.assertEquals(expectedLayerCount, layers.size());
    return layers;
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Path(java.nio.file.Path) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 12 with FileEntriesLayer

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

the class LayersTest method testToLayers_writeToRoot.

@Test
public void testToLayers_writeToRoot() throws IOException, URISyntaxException {
    // this test defines the current behavior of writing to root, perhaps we should ignore
    // root at this level or we should ignore it at the builder level
    Path testRoot = getLayersTestRoot("writeToRoot");
    List<FileEntriesLayer> layers = parseLayers(testRoot, 2);
    checkLayer(layers.get(0), "root writer", ImmutableSet.of(newEntry(testRoot, "dir", "/", "755", 1000, ""), newEntry(testRoot, "dir/file.txt", "/file.txt", "644", 1000, "")));
    checkLayer(layers.get(1), "root parent fill", ImmutableSet.of(newEntry(testRoot, ".", "/", "755", 1000, ""), newEntry(testRoot, "./dir", "/dir", "755", 1000, ""), newEntry(testRoot, "./dir/file.txt", "/dir/file.txt", "644", 1000, "")));
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Path(java.nio.file.Path) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) Test(org.junit.Test)

Example 13 with FileEntriesLayer

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

the class LayersTest method testToLayers_includeExcludes.

@Test
public void testToLayers_includeExcludes() throws IOException, URISyntaxException {
    Path testRoot = getLayersTestRoot("includesExcludesTest");
    List<FileEntriesLayer> layers = parseLayers(testRoot, 6);
    checkLayer(layers.get(0), "includes and excludes", ImmutableSet.of(newEntry(testRoot, "project", "/target/ie", "755", 0, "0:0"), newEntry(testRoot, "project/includedDir/", "/target/ie/includedDir/", "755", 0, "0:0"), newEntry(testRoot, "project/includedDir/include.me", "/target/ie/includedDir/include.me", "644", 0, "0:0")));
    checkLayer(layers.get(1), "includes only", ImmutableSet.of(newEntry(testRoot, "project", "/target/io", "755", 0, "0:0"), newEntry(testRoot, "project/includedDir/", "/target/io/includedDir/", "755", 0, "0:0"), newEntry(testRoot, "project/includedDir/include.me", "/target/io/includedDir/include.me", "644", 0, "0:0")));
    checkLayer(layers.get(2), "excludes only", ImmutableSet.of(newEntry(testRoot, "project", "/target/eo", "755", 0, "0:0"), newEntry(testRoot, "project/excludedDir", "/target/eo/excludedDir", "755", 0, "0:0"), newEntry(testRoot, "project/includedDir", "/target/eo/includedDir", "755", 0, "0:0"), newEntry(testRoot, "project/includedDir/include.me", "/target/eo/includedDir/include.me", "644", 0, "0:0"), newEntry(testRoot, "project/wild.card", "/target/eo/wild.card", "644", 0, "0:0")));
    checkLayer(layers.get(3), "excludes only shortcut", ImmutableSet.of(newEntry(testRoot, "project", "/target/eo", "755", 0, "0:0"), newEntry(testRoot, "project/excludedDir", "/target/eo/excludedDir", "755", 0, "0:0"), newEntry(testRoot, "project/includedDir", "/target/eo/includedDir", "755", 0, "0:0"), newEntry(testRoot, "project/includedDir/include.me", "/target/eo/includedDir/include.me", "644", 0, "0:0"), newEntry(testRoot, "project/wild.card", "/target/eo/wild.card", "644", 0, "0:0")));
    checkLayer(layers.get(4), "exclude dir and contents", ImmutableSet.of(newEntry(testRoot, "project", "/target/edac", "755", 0, "0:0"), newEntry(testRoot, "project/includedDir/", "/target/edac/includedDir/", "755", 0, "0:0"), newEntry(testRoot, "project/includedDir/include.me", "/target/edac/includedDir/include.me", "644", 0, "0:0"), newEntry(testRoot, "project/wild.card", "/target/edac/wild.card", "644", 0, "0:0")));
    checkLayer(layers.get(5), "excludes only wrong", ImmutableSet.of(newEntry(testRoot, "project", "/target/eo", "755", 0, "0:0"), newEntry(testRoot, "project/excludedDir", "/target/eo/excludedDir", "755", 0, "0:0"), newEntry(testRoot, "project/excludedDir/exclude.me", "/target/eo/excludedDir/exclude.me", "644", 0, "0:0"), newEntry(testRoot, "project/includedDir", "/target/eo/includedDir", "755", 0, "0:0"), newEntry(testRoot, "project/includedDir/include.me", "/target/eo/includedDir/include.me", "644", 0, "0:0"), newEntry(testRoot, "project/wild.card", "/target/eo/wild.card", "644", 0, "0:0")));
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Path(java.nio.file.Path) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) Test(org.junit.Test)

Example 14 with FileEntriesLayer

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

the class JarFiles method toJibContainerBuilder.

/**
 * Generates a {@link JibContainerBuilder} from contents of a jar file.
 *
 * @param processor jar processor
 * @param jarOptions jar cli options
 * @param commonCliOptions common cli options
 * @param commonContainerConfigCliOptions common command line options shared between jar and war
 *     command
 * @param logger console logger
 * @return JibContainerBuilder
 * @throws IOException if I/O error occurs when opening the jar file or if temporary directory
 *     provided doesn't exist
 * @throws InvalidImageReferenceException if the base image reference is invalid
 */
public static JibContainerBuilder toJibContainerBuilder(ArtifactProcessor processor, Jar jarOptions, CommonCliOptions commonCliOptions, CommonContainerConfigCliOptions commonContainerConfigCliOptions, ConsoleLogger logger) throws IOException, InvalidImageReferenceException {
    String imageReference = commonContainerConfigCliOptions.getFrom().orElseGet(() -> getDefaultBaseImage(processor));
    JibContainerBuilder containerBuilder = ContainerBuilders.create(imageReference, Collections.emptySet(), commonCliOptions, logger);
    List<FileEntriesLayer> layers = processor.createLayers();
    List<String> customEntrypoint = commonContainerConfigCliOptions.getEntrypoint();
    List<String> entrypoint = customEntrypoint.isEmpty() ? processor.computeEntrypoint(jarOptions.getJvmFlags()) : customEntrypoint;
    containerBuilder.setEntrypoint(entrypoint).setFileEntriesLayers(layers).setExposedPorts(commonContainerConfigCliOptions.getExposedPorts()).setVolumes(commonContainerConfigCliOptions.getVolumes()).setEnvironment(commonContainerConfigCliOptions.getEnvironment()).setLabels(commonContainerConfigCliOptions.getLabels()).setProgramArguments(commonContainerConfigCliOptions.getProgramArguments());
    commonContainerConfigCliOptions.getUser().ifPresent(containerBuilder::setUser);
    commonContainerConfigCliOptions.getFormat().ifPresent(containerBuilder::setFormat);
    commonContainerConfigCliOptions.getCreationTime().ifPresent(containerBuilder::setCreationTime);
    return containerBuilder;
}
Also used : FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder)

Example 15 with FileEntriesLayer

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

the class JarLayers method getDependenciesLayers.

static List<FileEntriesLayer> getDependenciesLayers(Path jarPath, ProcessingMode mode) throws IOException {
    // adding the dependencies layers.
    try (JarFile jarFile = new JarFile(jarPath.toFile())) {
        String classPath = jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
        if (classPath == null) {
            return new ArrayList<>();
        }
        List<FileEntriesLayer> layers = new ArrayList<>();
        Path jarParent = jarPath.getParent() == null ? Paths.get("") : jarPath.getParent();
        Predicate<String> isSnapshot = name -> name.contains("SNAPSHOT");
        List<String> allDependencies = Splitter.onPattern("\\s+").splitToList(classPath.trim());
        List<Path> nonSnapshots = allDependencies.stream().filter(isSnapshot.negate()).map(Paths::get).collect(Collectors.toList());
        List<Path> snapshots = allDependencies.stream().filter(isSnapshot).map(Paths::get).collect(Collectors.toList());
        if (!nonSnapshots.isEmpty()) {
            FileEntriesLayer.Builder nonSnapshotLayer = FileEntriesLayer.builder().setName(ArtifactLayers.DEPENDENCIES);
            nonSnapshots.forEach(path -> addDependency(nonSnapshotLayer, jarParent.resolve(path), mode.equals(ProcessingMode.packaged) ? APP_ROOT.resolve(path) : APP_ROOT.resolve(ArtifactLayers.DEPENDENCIES).resolve(path.getFileName())));
            layers.add(nonSnapshotLayer.build());
        }
        if (!snapshots.isEmpty()) {
            FileEntriesLayer.Builder snapshotLayer = FileEntriesLayer.builder().setName(ArtifactLayers.SNAPSHOT_DEPENDENCIES);
            snapshots.forEach(path -> addDependency(snapshotLayer, jarParent.resolve(path), mode.equals(ProcessingMode.packaged) ? APP_ROOT.resolve(path) : APP_ROOT.resolve(ArtifactLayers.DEPENDENCIES).resolve(path.getFileName())));
            layers.add(snapshotLayer.build());
        }
        return layers;
    }
}
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) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) Attributes(java.util.jar.Attributes) Collectors(java.util.stream.Collectors) ArtifactLayers(com.google.cloud.tools.jib.cli.ArtifactLayers) ArrayList(java.util.ArrayList) List(java.util.List) Paths(java.nio.file.Paths) Splitter(com.google.common.base.Splitter) Path(java.nio.file.Path) ArrayList(java.util.ArrayList) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) JarFile(java.util.jar.JarFile)

Aggregations

FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)154 Test (org.junit.Test)111 Path (java.nio.file.Path)92 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)85 ContainerBuildPlan (com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)55 List (java.util.List)29 IOException (java.io.IOException)26 ArrayList (java.util.ArrayList)26 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)24 FileEntry (com.google.cloud.tools.jib.api.buildplan.FileEntry)23 Map (java.util.Map)23 Files (java.nio.file.Files)21 Predicate (java.util.function.Predicate)20 Platform (com.google.cloud.tools.jib.api.buildplan.Platform)16 Collectors (java.util.stream.Collectors)16 FilePermissions (com.google.cloud.tools.jib.api.buildplan.FilePermissions)13 Paths (java.nio.file.Paths)13 Optional (java.util.Optional)13 JavaContainerBuilder (com.google.cloud.tools.jib.api.JavaContainerBuilder)12 JibPluginExtensionException (com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException)12