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