Search in sources :

Example 16 with FileEntry

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

the class JibOwnershipExtension method applyRulesToFileEntry.

private FileEntry applyRulesToFileEntry(FileEntry entry) {
    String newOwnership = null;
    for (Entry<PathMatcher, String> mapEntry : pathMatchers.entrySet()) {
        PathMatcher matcher = mapEntry.getKey();
        Path pathInContainer = Paths.get(entry.getExtractionPath().toString());
        if (matcher.matches(pathInContainer)) {
            newOwnership = mapEntry.getValue();
        }
    }
    return newOwnership == null ? entry : new FileEntry(entry.getSourceFile(), entry.getExtractionPath(), entry.getPermissions(), entry.getModificationTime(), newOwnership);
}
Also used : Path(java.nio.file.Path) PathMatcher(java.nio.file.PathMatcher) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry)

Example 17 with FileEntry

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

the class BuildAndCacheApplicationLayerStep method call.

@Override
public PreparedLayer call() throws IOException, CacheCorruptedException {
    String description = String.format(DESCRIPTION, layerName);
    EventHandlers eventHandlers = buildContext.getEventHandlers();
    eventHandlers.dispatch(LogEvent.progress(description + "..."));
    try (ProgressEventDispatcher ignored = progressEventDispatcherFactory.create("building " + layerName + " layer", 1);
        TimerEventDispatcher ignored2 = new TimerEventDispatcher(eventHandlers, description)) {
        Cache cache = buildContext.getApplicationLayersCache();
        ImmutableList<FileEntry> layerEntries = ImmutableList.copyOf(layerConfiguration.getEntries());
        // Don't build the layer if it exists already.
        Optional<CachedLayer> optionalCachedLayer = cache.retrieve(layerEntries);
        if (optionalCachedLayer.isPresent()) {
            return new PreparedLayer.Builder(optionalCachedLayer.get()).setName(layerName).build();
        }
        Blob layerBlob = new ReproducibleLayerBuilder(layerEntries).build();
        CachedLayer cachedLayer = cache.writeUncompressedLayer(layerBlob, layerEntries);
        eventHandlers.dispatch(LogEvent.debug(description + " built " + cachedLayer.getDigest()));
        return new PreparedLayer.Builder(cachedLayer).setName(layerName).build();
    }
}
Also used : Blob(com.google.cloud.tools.jib.blob.Blob) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) ReproducibleLayerBuilder(com.google.cloud.tools.jib.image.ReproducibleLayerBuilder) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) ReproducibleLayerBuilder(com.google.cloud.tools.jib.image.ReproducibleLayerBuilder) Cache(com.google.cloud.tools.jib.cache.Cache)

Example 18 with FileEntry

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

the class ReproducibleLayerBuilder method build.

/**
 * Builds and returns the layer {@link Blob}.
 *
 * @return the new layer
 * @throws IOException if an I/O error occurs
 */
public Blob build() throws IOException {
    UniqueTarArchiveEntries uniqueTarArchiveEntries = new UniqueTarArchiveEntries();
    // Adds all the layer entries as tar entries.
    for (FileEntry layerEntry : layerEntries) {
        // Adds the entries to uniqueTarArchiveEntries, which makes sure all entries are unique and
        // adds parent directories for each extraction path.
        TarArchiveEntry entry = new TarArchiveEntry(layerEntry.getSourceFile(), layerEntry.getExtractionPath().toString());
        // Sets the entry's permissions by masking out the permission bits from the entry's mode (the
        // lowest 9 bits) then using a bitwise OR to set them to the layerEntry's permissions.
        entry.setMode((entry.getMode() & ~0777) | layerEntry.getPermissions().getPermissionBits());
        entry.setModTime(layerEntry.getModificationTime().toEpochMilli());
        setUserAndGroup(entry, layerEntry);
        uniqueTarArchiveEntries.add(entry);
    }
    // Gets the entries sorted by extraction path.
    List<TarArchiveEntry> sortedFilesystemEntries = uniqueTarArchiveEntries.getSortedEntries();
    Set<String> names = new HashSet<>();
    // Adds all the files to a tar stream.
    TarStreamBuilder tarStreamBuilder = new TarStreamBuilder();
    for (TarArchiveEntry entry : sortedFilesystemEntries) {
        Verify.verify(!names.contains(entry.getName()));
        names.add(entry.getName());
        tarStreamBuilder.addTarArchiveEntry(entry);
    }
    return Blobs.from(tarStreamBuilder::writeAsTarArchiveTo, false);
}
Also used : FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) HashSet(java.util.HashSet) TarStreamBuilder(com.google.cloud.tools.jib.tar.TarStreamBuilder)

Example 19 with FileEntry

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

the class PluginConfigurationProcessorTest method testClasspathArgumentFile.

@Test
public void testClasspathArgumentFile() throws NumberFormatException, InvalidImageReferenceException, MainClassInferenceException, InvalidAppRootException, IOException, InvalidWorkingDirectoryException, InvalidPlatformException, InvalidContainerVolumeException, IncompatibleBaseImageJavaVersionException, InvalidContainerizingModeException, InvalidFilesModificationTimeException, InvalidCreationTimeException, ExtraDirectoryNotFoundException {
    when(rawConfiguration.getExtraClasspath()).thenReturn(Collections.singletonList("/foo"));
    when(projectProperties.getMajorJavaVersion()).thenReturn(9);
    ContainerBuildPlan buildPlan = processCommonConfiguration();
    assertThat(buildPlan.getEntrypoint()).containsExactly("java", "-cp", "@/app/jib-classpath-file", "java.lang.Object").inOrder();
    List<FileEntry> jvmArgFiles = getLayerEntries(buildPlan, "jvm arg files");
    assertThat(jvmArgFiles).comparingElementsUsing(SOURCE_FILE_OF).containsExactly(appCacheDirectory.resolve("jib-classpath-file"), appCacheDirectory.resolve("jib-main-class-file"));
    assertThat(jvmArgFiles).comparingElementsUsing(EXTRACTION_PATH_OF).containsExactly("/app/jib-classpath-file", "/app/jib-main-class-file");
    String classpath = new String(Files.readAllBytes(appCacheDirectory.resolve("jib-classpath-file")), StandardCharsets.UTF_8);
    assertThat(classpath).isEqualTo("/foo:/app/resources:/app/classes:/app/libs/foo-1.jar:/app/libs/bar-2.jar");
    String mainClass = new String(Files.readAllBytes(appCacheDirectory.resolve("jib-main-class-file")), StandardCharsets.UTF_8);
    assertThat(mainClass).isEqualTo("java.lang.Object");
}
Also used : FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Example 20 with FileEntry

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

the class PluginConfigurationProcessorTest method testAddJvmArgFilesLayer.

@Test
public void testAddJvmArgFilesLayer() throws IOException, InvalidAppRootException {
    String classpath = "/extra:/app/classes:/app/libs/dep.jar";
    String mainClass = "com.example.Main";
    PluginConfigurationProcessor.addJvmArgFilesLayer(rawConfiguration, projectProperties, jibContainerBuilder, classpath, mainClass);
    Path classpathFile = appCacheDirectory.resolve("jib-classpath-file");
    Path mainClassFile = appCacheDirectory.resolve("jib-main-class-file");
    String classpathRead = new String(Files.readAllBytes(classpathFile), StandardCharsets.UTF_8);
    String mainClassRead = new String(Files.readAllBytes(mainClassFile), StandardCharsets.UTF_8);
    assertThat(classpathRead).isEqualTo(classpath);
    assertThat(mainClassRead).isEqualTo(mainClass);
    List<FileEntry> layerEntries = getLayerEntries(jibContainerBuilder.toContainerBuildPlan(), "jvm arg files");
    assertThat(layerEntries).comparingElementsUsing(SOURCE_FILE_OF).containsExactly(appCacheDirectory.resolve("jib-classpath-file"), appCacheDirectory.resolve("jib-main-class-file"));
    assertThat(layerEntries).comparingElementsUsing(EXTRACTION_PATH_OF).containsExactly("/app/jib-classpath-file", "/app/jib-main-class-file");
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) Test(org.junit.Test)

Aggregations

FileEntry (com.google.cloud.tools.jib.api.buildplan.FileEntry)64 Test (org.junit.Test)45 Path (java.nio.file.Path)39 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)34 FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)24 ContainerBuildPlan (com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)16 ArrayList (java.util.ArrayList)13 FilePermissions (com.google.cloud.tools.jib.api.buildplan.FilePermissions)10 Blob (com.google.cloud.tools.jib.blob.Blob)10 VisibleForTesting (com.google.common.annotations.VisibleForTesting)9 List (java.util.List)9 BufferedOutputStream (java.io.BufferedOutputStream)8 OutputStream (java.io.OutputStream)8 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)8 PathMatcher (java.nio.file.PathMatcher)7 Instant (java.time.Instant)7 Optional (java.util.Optional)7 Collectors (java.util.stream.Collectors)7 Platform (com.google.cloud.tools.jib.api.buildplan.Platform)6 CachedLayer (com.google.cloud.tools.jib.cache.CachedLayer)6