Search in sources :

Example 41 with FileEntriesLayer

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

the class JibLayerFilterExtensionTest method testExtendContainerBuildPlan_complex.

@Test
public void testExtendContainerBuildPlan_complex() throws JibPluginExtensionException {
    FileEntriesLayer layer1 = buildLayer("foo", Arrays.asList("/alpha/Alice", "/alpha/Bob", "/beta/Alice", "/beta/Bob"));
    FileEntriesLayer layer2 = buildLayer("app", Arrays.asList("/alpha/Charlie", "/alpha/David", "/beta/Charlie", "/beta/David"));
    FileEntriesLayer layer3 = buildLayer("app", Arrays.asList("/unmatched/foo", "/unmatched/bar"));
    FileEntriesLayer layer4 = buildLayer("app", Arrays.asList("/gamma/Alice", "/gamma/Bob", "/gamma/Charlie", "/gamme/David"));
    ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().setLayers(Arrays.asList(layer1, layer2, layer3, layer4)).build();
    Configuration.Filter filter1 = mockFilter("/alpha/**", "alpha Alice");
    Configuration.Filter filter2 = mockFilter("/?????/*", "alpha gamma");
    Configuration.Filter filter3 = mockFilter("**/Bob", "Bob");
    Configuration.Filter filter4 = mockFilter("/gamma/C*", "gamma Charlie");
    Configuration.Filter filter5 = mockFilter("**/Alice", "alpha Alice");
    Configuration.Filter filter6 = mockFilter("**/David", "");
    when(config.getFilters()).thenReturn(Arrays.asList(filter1, filter2, filter3, filter4, filter5, filter6));
    ContainerBuildPlan newPlan = new JibLayerFilterExtension().extendContainerBuildPlan(buildPlan, null, Optional.of(config), null, logger);
    assertEquals(6, newPlan.getLayers().size());
    List<String> layerNames = newPlan.getLayers().stream().map(LayerObject::getName).collect(Collectors.toList());
    assertEquals(Arrays.asList("app", "app", "alpha Alice", "alpha gamma", "Bob", "gamma Charlie"), layerNames);
    FileEntriesLayer newLayer1 = (FileEntriesLayer) newPlan.getLayers().get(0);
    FileEntriesLayer newLayer2 = (FileEntriesLayer) newPlan.getLayers().get(1);
    FileEntriesLayer newLayer3 = (FileEntriesLayer) newPlan.getLayers().get(2);
    FileEntriesLayer newLayer4 = (FileEntriesLayer) newPlan.getLayers().get(3);
    FileEntriesLayer newLayer5 = (FileEntriesLayer) newPlan.getLayers().get(4);
    FileEntriesLayer newLayer6 = (FileEntriesLayer) newPlan.getLayers().get(5);
    assertEquals(Arrays.asList("/beta/Charlie"), layerToExtractionPaths(newLayer1));
    assertEquals(Arrays.asList("/unmatched/foo", "/unmatched/bar"), layerToExtractionPaths(newLayer2));
    assertEquals(Arrays.asList("/alpha/Alice", "/beta/Alice", "/gamma/Alice"), layerToExtractionPaths(newLayer3));
    assertEquals(Arrays.asList("/alpha/Charlie"), layerToExtractionPaths(newLayer4));
    assertEquals(Arrays.asList("/alpha/Bob", "/beta/Bob", "/gamma/Bob"), layerToExtractionPaths(newLayer5));
    assertEquals(Arrays.asList("/gamma/Charlie"), layerToExtractionPaths(newLayer6));
}
Also used : FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Example 42 with FileEntriesLayer

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

the class JibLayerFilterExtensionTest method testExtendContainerBuildPlan_deletion.

@Test
public void testExtendContainerBuildPlan_deletion() throws JibPluginExtensionException {
    FileEntriesLayer layer1 = buildLayer("", Arrays.asList("/foo"));
    FileEntriesLayer layer2 = buildLayer("", Arrays.asList("/foo", "/bar", "/foo/baz"));
    ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().addLayer(layer1).addLayer(layer2).build();
    Configuration.Filter filter = mockFilter("**", "");
    when(config.getFilters()).thenReturn(Arrays.asList(filter));
    ContainerBuildPlan newPlan = new JibLayerFilterExtension().extendContainerBuildPlan(buildPlan, null, Optional.of(config), null, logger);
    assertEquals(0, newPlan.getLayers().size());
}
Also used : FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Example 43 with FileEntriesLayer

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

the class JibNativeImageExtension method extendContainerBuildPlan.

@Override
public ContainerBuildPlan extendContainerBuildPlan(ContainerBuildPlan buildPlan, Map<String, String> properties, Optional<Void> config, MavenData mavenData, ExtensionLogger logger) throws JibPluginExtensionException {
    logger.log(LogLevel.LIFECYCLE, "Running Jib Native Image extension");
    MavenProject project = mavenData.getMavenProject();
    Optional<String> executableName = getExecutableName(project, properties);
    if (!executableName.isPresent()) {
        throw new JibPluginExtensionException(getClass(), "cannot auto-detect native-image executable name; consider setting 'imageName' property");
    }
    String outputDirectory = project.getBuild().getDirectory();
    Path localExecutable = Paths.get(outputDirectory, executableName.get());
    if (!Files.isRegularFile(localExecutable)) {
        throw new JibPluginExtensionException(getClass(), "Native-image executable does not exist or not a file: " + localExecutable + "\nDid you run the 'native-image:native-image' goal?");
    }
    // TODO: also check system and Maven properties (e.g., -Djib.container.appRoot).
    String appRoot = getPluginConfigValue(project, JIB_APP_ROOT).orElse("/app");
    AbsoluteUnixPath targetExecutable = AbsoluteUnixPath.get(appRoot).resolve(executableName.get());
    ContainerBuildPlan.Builder planBuilder = buildPlan.toBuilder();
    FileEntriesLayer nativeImageLayer = FileEntriesLayer.builder().setName("native image").addEntry(localExecutable, targetExecutable, FilePermissions.fromOctalString("755")).build();
    planBuilder.setLayers(Collections.singletonList(nativeImageLayer));
    // Preserve extra directories layers.
    String extraFilesLayerName = JavaContainerBuilder.LayerType.EXTRA_FILES.getName();
    buildPlan.getLayers().stream().filter(layer -> layer.getName().startsWith(extraFilesLayerName)).forEach(planBuilder::addLayer);
    // TODO: also check system and Maven properties (e.g., -Djib.container.entrypoint).
    if (!getPluginConfigValue(project, JIB_ENTRYPOINT).isPresent()) {
        planBuilder.setEntrypoint(Collections.singletonList(targetExecutable.toString()));
    }
    return planBuilder.build();
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) JibMavenPluginExtension(com.google.cloud.tools.jib.maven.extension.JibMavenPluginExtension) JavaContainerBuilder(com.google.cloud.tools.jib.api.JavaContainerBuilder) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ValueContainer(com.google.cloud.tools.jib.maven.extension.nativeimage.ConfigValueLocation.ValueContainer) ExtensionLogger(com.google.cloud.tools.jib.plugins.extension.ExtensionLogger) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) MavenProject(org.apache.maven.project.MavenProject) Locale(java.util.Locale) Map(java.util.Map) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Files(java.nio.file.Files) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) LogLevel(com.google.cloud.tools.jib.plugins.extension.ExtensionLogger.LogLevel) Plugin(org.apache.maven.model.Plugin) Paths(java.nio.file.Paths) MavenData(com.google.cloud.tools.jib.maven.extension.MavenData) Optional(java.util.Optional) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) MavenProject(org.apache.maven.project.MavenProject) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)

Example 44 with FileEntriesLayer

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

the class JibOwnershipExtensionTest method testExtendContainerBuildPlan.

@Test
public void testExtendContainerBuildPlan() throws JibPluginExtensionException {
    FileEntriesLayer layer1 = FileEntriesLayer.builder().addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/file")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/another")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/sub/dir/file")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/sub/dir/another")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/untouched/file")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/untouched/another")).build();
    FileEntriesLayer layer2 = FileEntriesLayer.builder().addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/foo")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/bar")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/sub/dir/foo")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/target/sub/dir/bar")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/untouched/foo")).addEntry(Paths.get("whatever"), AbsoluteUnixPath.get("/untouched/bar")).build();
    ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().addLayer(layer1).addLayer(layer2).build();
    Configuration.Rule rule1 = mockRule("/target/**", "10:20");
    Configuration.Rule rule2 = mockRule("**/bar", "999:777");
    when(config.getRules()).thenReturn(Arrays.asList(rule1, rule2));
    ContainerBuildPlan newPlan = new JibOwnershipExtension().extendContainerBuildPlan(buildPlan, null, Optional.of(config), null, logger);
    FileEntriesLayer newLayer1 = (FileEntriesLayer) newPlan.getLayers().get(0);
    FileEntriesLayer newLayer2 = (FileEntriesLayer) newPlan.getLayers().get(1);
    assertEquals(Arrays.asList(AbsoluteUnixPath.get("/target/file"), AbsoluteUnixPath.get("/target/another"), AbsoluteUnixPath.get("/target/sub/dir/file"), AbsoluteUnixPath.get("/target/sub/dir/another"), AbsoluteUnixPath.get("/untouched/file"), AbsoluteUnixPath.get("/untouched/another")), mapLayerEntries(newLayer1, FileEntry::getExtractionPath));
    assertEquals(Arrays.asList("10:20", "10:20", "10:20", "10:20", "", ""), mapLayerEntries(newLayer1, FileEntry::getOwnership));
    assertEquals(Arrays.asList(AbsoluteUnixPath.get("/target/foo"), AbsoluteUnixPath.get("/target/bar"), AbsoluteUnixPath.get("/target/sub/dir/foo"), AbsoluteUnixPath.get("/target/sub/dir/bar"), AbsoluteUnixPath.get("/untouched/foo"), AbsoluteUnixPath.get("/untouched/bar")), mapLayerEntries(newLayer2, FileEntry::getExtractionPath));
    assertEquals(Arrays.asList("10:20", "999:777", "10:20", "999:777", "", "999:777"), mapLayerEntries(newLayer2, FileEntry::getOwnership));
}
Also used : FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Example 45 with FileEntriesLayer

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

the class JibQuarkusExtension method addDependencyLayer.

private void addDependencyLayer(ContainerBuildPlan.Builder planBuilder, Path libDirectory, String layerName, Predicate<Path> predicate) throws IOException {
    FileEntriesLayer.Builder layerBuilder = FileEntriesLayer.builder().setName(layerName);
    try (Stream<Path> files = Files.list(libDirectory)) {
        files.filter(predicate).forEach(path -> {
            layerBuilder.addEntry(path, appRoot.resolve("lib").resolve(path.getFileName().toString()));
        });
    }
    FileEntriesLayer layer = layerBuilder.build();
    if (!layer.getEntries().isEmpty()) {
        planBuilder.addLayer(layer);
    }
}
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)

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