Search in sources :

Example 96 with FileEntriesLayer

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

the class StandardPackagedProcessor method createLayers.

@Override
public List<FileEntriesLayer> createLayers() throws IOException {
    // Add dependencies layers.
    List<FileEntriesLayer> layers = JarLayers.getDependenciesLayers(jarPath, ProcessingMode.packaged);
    // Add layer for jar.
    FileEntriesLayer jarLayer = FileEntriesLayer.builder().setName(JarLayers.JAR).addEntry(jarPath, JarLayers.APP_ROOT.resolve(jarPath.getFileName())).build();
    layers.add(jarLayer);
    return layers;
}
Also used : FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)

Example 97 with FileEntriesLayer

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

the class StandardWarExplodedProcessor method createLayers.

@Override
public List<FileEntriesLayer> createLayers() throws IOException {
    // Clear the exploded-artifact root first
    if (Files.exists(targetExplodedWarRoot)) {
        MoreFiles.deleteRecursively(targetExplodedWarRoot, RecursiveDeleteOption.ALLOW_INSECURE);
    }
    ZipUtil.unzip(warPath, targetExplodedWarRoot, true);
    Predicate<Path> isFile = Files::isRegularFile;
    Predicate<Path> isInWebInfLib = path -> path.startsWith(targetExplodedWarRoot.resolve("WEB-INF").resolve("lib"));
    Predicate<Path> isSnapshot = path -> path.getFileName().toString().contains("SNAPSHOT");
    // Non-snapshot layer
    Predicate<Path> isInWebInfLibAndIsNotSnapshot = isInWebInfLib.and(isSnapshot.negate());
    FileEntriesLayer nonSnapshotLayer = ArtifactLayers.getDirectoryContentsAsLayer(ArtifactLayers.DEPENDENCIES, targetExplodedWarRoot, isFile.and(isInWebInfLibAndIsNotSnapshot), appRoot);
    // Snapshot layer
    Predicate<Path> isInWebInfLibAndIsSnapshot = isInWebInfLib.and(isSnapshot);
    FileEntriesLayer snapshotLayer = ArtifactLayers.getDirectoryContentsAsLayer(ArtifactLayers.SNAPSHOT_DEPENDENCIES, targetExplodedWarRoot, isFile.and(isInWebInfLibAndIsSnapshot), appRoot);
    // Classes layer.
    Predicate<Path> isClass = path -> path.getFileName().toString().endsWith(".class");
    Predicate<Path> isInWebInfClasses = path -> path.startsWith(targetExplodedWarRoot.resolve("WEB-INF").resolve("classes"));
    Predicate<Path> classesPredicate = isInWebInfClasses.and(isClass);
    FileEntriesLayer classesLayer = ArtifactLayers.getDirectoryContentsAsLayer(ArtifactLayers.CLASSES, targetExplodedWarRoot, classesPredicate, appRoot);
    // Resources layer.
    Predicate<Path> resourcesPredicate = isInWebInfLib.or(isClass).negate();
    FileEntriesLayer resourcesLayer = ArtifactLayers.getDirectoryContentsAsLayer(ArtifactLayers.RESOURCES, targetExplodedWarRoot, isFile.and(resourcesPredicate), appRoot);
    ArrayList<FileEntriesLayer> layers = new ArrayList<>();
    if (!nonSnapshotLayer.getEntries().isEmpty()) {
        layers.add(nonSnapshotLayer);
    }
    if (!snapshotLayer.getEntries().isEmpty()) {
        layers.add(snapshotLayer);
    }
    if (!resourcesLayer.getEntries().isEmpty()) {
        layers.add(resourcesLayer);
    }
    if (!classesLayer.getEntries().isEmpty()) {
        layers.add(classesLayer);
    }
    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) RecursiveDeleteOption(com.google.common.io.RecursiveDeleteOption) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ArtifactLayers(com.google.cloud.tools.jib.cli.ArtifactLayers) ZipUtil(com.google.cloud.tools.jib.plugins.common.ZipUtil) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ArtifactProcessor(com.google.cloud.tools.jib.cli.ArtifactProcessor) MoreFiles(com.google.common.io.MoreFiles) Path(java.nio.file.Path) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ArrayList(java.util.ArrayList)

Example 98 with FileEntriesLayer

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

the class JavaContainerBuilderHelper 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 includes the list of glob patterns to include from the source directory
 * @param excludes the list of glob patterns to exclude from the source directory
 * @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, List<String> includes, List<String> excludes, Map<String, FilePermissions> extraDirectoryPermissions, ModificationTimeProvider modificationTimeProvider) throws IOException {
    FileEntriesLayer.Builder builder = FileEntriesLayer.builder().setName(LayerType.EXTRA_FILES.getName());
    Map<PathMatcher, FilePermissions> permissionsPathMatchers = new LinkedHashMap<>();
    for (Map.Entry<String, FilePermissions> entry : extraDirectoryPermissions.entrySet()) {
        permissionsPathMatchers.put(FileSystems.getDefault().getPathMatcher(GLOB_PREFIX + entry.getKey()), entry.getValue());
    }
    DirectoryWalker walker = new DirectoryWalker(sourceDirectory).filterRoot();
    // add exclusion filters
    excludes.stream().map(pattern -> FileSystems.getDefault().getPathMatcher(GLOB_PREFIX + pattern)).forEach(pathMatcher -> walker.filter(path -> !pathMatcher.matches(sourceDirectory.relativize(path))));
    // add an inclusion filter
    includes.stream().map(pattern -> FileSystems.getDefault().getPathMatcher(GLOB_PREFIX + pattern)).map(pathMatcher -> (Predicate<Path>) path -> pathMatcher.matches(sourceDirectory.relativize(path))).reduce((matches1, matches2) -> matches1.or(matches2)).ifPresent(walker::filter);
    // walk the source tree and add layer entries
    walker.walk(localPath -> {
        AbsoluteUnixPath pathOnContainer = targetDirectory.resolve(sourceDirectory.relativize(localPath));
        Instant modificationTime = modificationTimeProvider.get(localPath, pathOnContainer);
        Optional<FilePermissions> permissions = determinePermissions(pathOnContainer, extraDirectoryPermissions, permissionsPathMatchers);
        if (permissions.isPresent()) {
            builder.addEntry(localPath, pathOnContainer, permissions.get(), modificationTime);
        } else {
            builder.addEntry(localPath, pathOnContainer, modificationTime);
        }
    });
    return builder.build();
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) Set(java.util.Set) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) IOException(java.io.IOException) JavaContainerBuilder(com.google.cloud.tools.jib.api.JavaContainerBuilder) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) ModificationTimeProvider(com.google.cloud.tools.jib.api.buildplan.ModificationTimeProvider) Instant(java.time.Instant) LinkedHashMap(java.util.LinkedHashMap) LayerType(com.google.cloud.tools.jib.api.JavaContainerBuilder.LayerType) List(java.util.List) Paths(java.nio.file.Paths) Map(java.util.Map) RelativeUnixPath(com.google.cloud.tools.jib.api.buildplan.RelativeUnixPath) PathMatcher(java.nio.file.PathMatcher) Optional(java.util.Optional) Path(java.nio.file.Path) FileSystems(java.nio.file.FileSystems) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) RelativeUnixPath(com.google.cloud.tools.jib.api.buildplan.RelativeUnixPath) 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 99 with FileEntriesLayer

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

the class PluginConfigurationProcessor method getSkaffoldSyncMap.

/**
 * Generate a skaffold syncmap JSON string for an image build configuration.
 *
 * @param rawConfiguration the raw configuration from the plugin
 * @param projectProperties an plugin specific implementation of {@link ProjectProperties}
 * @param excludes a set of paths to exclude, directories include in this list will be expanded
 * @return new json string representation of the Sync Map
 * @throws InvalidImageReferenceException if the image reference is invalid
 * @throws MainClassInferenceException if a main class could not be found
 * @throws InvalidAppRootException if the specific path for application root is invalid
 * @throws IOException if an error occurs creating the container builder
 * @throws InvalidWorkingDirectoryException if the working directory specified for the build is
 *     invalid
 * @throws InvalidPlatformException if there exists a {@link PlatformConfiguration} in the
 *     specified platforms list that is missing required fields or has invalid values
 * @throws InvalidContainerVolumeException if a specific container volume is invalid
 * @throws IncompatibleBaseImageJavaVersionException if the base image java version cannot support
 *     this build
 * @throws NumberFormatException if a string to number conversion operation fails
 * @throws InvalidContainerizingModeException if an invalid {@link ContainerizingMode} was
 *     specified
 * @throws InvalidFilesModificationTimeException if configured modification time could not be
 *     parsed
 * @throws InvalidCreationTimeException if configured creation time could not be parsed
 * @throws ExtraDirectoryNotFoundException if the extra directory specified for the build is not
 *     found
 */
public static String getSkaffoldSyncMap(RawConfiguration rawConfiguration, ProjectProperties projectProperties, Set<Path> excludes) throws IOException, InvalidCreationTimeException, InvalidImageReferenceException, IncompatibleBaseImageJavaVersionException, InvalidPlatformException, InvalidContainerVolumeException, MainClassInferenceException, InvalidAppRootException, InvalidWorkingDirectoryException, InvalidFilesModificationTimeException, InvalidContainerizingModeException, ExtraDirectoryNotFoundException {
    JibContainerBuilder jibContainerBuilder = processCommonConfiguration(rawConfiguration, ignored -> Optional.empty(), projectProperties);
    SkaffoldSyncMapTemplate syncMap = new SkaffoldSyncMapTemplate();
    // since jib has already expanded out directories after processing everything, we just
    // ignore directories and provide only files to watch
    Set<Path> excludesExpanded = getAllFiles(excludes);
    for (LayerObject layerObject : jibContainerBuilder.toContainerBuildPlan().getLayers()) {
        Verify.verify(layerObject instanceof FileEntriesLayer, "layer types other than FileEntriesLayer not yet supported in build plan layers");
        FileEntriesLayer layer = (FileEntriesLayer) layerObject;
        if (CONST_LAYERS.contains(layer.getName())) {
            continue;
        }
        if (GENERATED_LAYERS.contains(layer.getName())) {
            layer.getEntries().stream().filter(layerEntry -> Files.isRegularFile(layerEntry.getSourceFile())).filter(layerEntry -> !excludesExpanded.contains(layerEntry.getSourceFile().toAbsolutePath())).forEach(syncMap::addGenerated);
        } else {
            // this is a direct layer
            layer.getEntries().stream().filter(layerEntry -> Files.isRegularFile(layerEntry.getSourceFile())).filter(layerEntry -> !excludesExpanded.contains(layerEntry.getSourceFile().toAbsolutePath())).forEach(syncMap::addDirect);
        }
    }
    return syncMap.getJsonString();
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) CredHelperConfiguration(com.google.cloud.tools.jib.plugins.common.RawConfiguration.CredHelperConfiguration) Arrays(java.util.Arrays) ImageReference(com.google.cloud.tools.jib.api.ImageReference) PlatformConfiguration(com.google.cloud.tools.jib.plugins.common.RawConfiguration.PlatformConfiguration) JavaContainerBuilder(com.google.cloud.tools.jib.api.JavaContainerBuilder) ExtraDirectoriesConfiguration(com.google.cloud.tools.jib.plugins.common.RawConfiguration.ExtraDirectoriesConfiguration) Credential(com.google.cloud.tools.jib.api.Credential) LayerType(com.google.cloud.tools.jib.api.JavaContainerBuilder.LayerType) Map(java.util.Map) Splitter(com.google.common.base.Splitter) Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Verify(com.google.common.base.Verify) GlobalConfig(com.google.cloud.tools.jib.plugins.common.globalconfig.GlobalConfig) Set(java.util.Set) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) StandardCharsets(java.nio.charset.StandardCharsets) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) LayerObject(com.google.cloud.tools.jib.api.buildplan.LayerObject) Stream(java.util.stream.Stream) ImageFormat(com.google.cloud.tools.jib.api.buildplan.ImageFormat) JibSystemProperties(com.google.cloud.tools.jib.global.JibSystemProperties) Optional(java.util.Optional) Platform(com.google.cloud.tools.jib.api.buildplan.Platform) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) Ports(com.google.cloud.tools.jib.api.Ports) DockerDaemonImage(com.google.cloud.tools.jib.api.DockerDaemonImage) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) RegistryImage(com.google.cloud.tools.jib.api.RegistryImage) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) JibPluginExtensionException(com.google.cloud.tools.jib.plugins.extension.JibPluginExtensionException) Multimaps(com.google.common.collect.Multimaps) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Jib(com.google.cloud.tools.jib.api.Jib) ImmutableList(com.google.common.collect.ImmutableList) CredentialRetrieverFactory(com.google.cloud.tools.jib.frontend.CredentialRetrieverFactory) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) Files(java.nio.file.Files) TarImage(com.google.cloud.tools.jib.api.TarImage) IOException(java.io.IOException) ModificationTimeProvider(com.google.cloud.tools.jib.api.buildplan.ModificationTimeProvider) LogEvent(com.google.cloud.tools.jib.api.LogEvent) Containerizer(com.google.cloud.tools.jib.api.Containerizer) Paths(java.nio.file.Paths) DateTimeFormatter(java.time.format.DateTimeFormatter) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) LayerObject(com.google.cloud.tools.jib.api.buildplan.LayerObject) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder)

Example 100 with FileEntriesLayer

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

the class BuildAndCacheApplicationLayerStepTest method testRun.

@Test
public void testRun() throws LayerPropertyNotFoundException, IOException, CacheCorruptedException {
    ImmutableList<FileEntriesLayer> fakeLayerConfigurations = ImmutableList.of(fakeDependenciesLayerConfiguration, fakeSnapshotDependenciesLayerConfiguration, fakeResourcesLayerConfiguration, fakeClassesLayerConfiguration, fakeExtraFilesLayerConfiguration);
    Mockito.when(mockBuildContext.getLayerConfigurations()).thenReturn(fakeLayerConfigurations);
    // Populates the cache.
    List<Layer> applicationLayers = buildFakeLayersToCache();
    Assert.assertEquals(5, applicationLayers.size());
    ImmutableList<FileEntry> dependenciesLayerEntries = ImmutableList.copyOf(fakeLayerConfigurations.get(0).getEntries());
    ImmutableList<FileEntry> snapshotDependenciesLayerEntries = ImmutableList.copyOf(fakeLayerConfigurations.get(1).getEntries());
    ImmutableList<FileEntry> resourcesLayerEntries = ImmutableList.copyOf(fakeLayerConfigurations.get(2).getEntries());
    ImmutableList<FileEntry> classesLayerEntries = ImmutableList.copyOf(fakeLayerConfigurations.get(3).getEntries());
    ImmutableList<FileEntry> extraFilesLayerEntries = ImmutableList.copyOf(fakeLayerConfigurations.get(4).getEntries());
    CachedLayer dependenciesCachedLayer = cache.retrieve(dependenciesLayerEntries).orElseThrow(AssertionError::new);
    CachedLayer snapshotDependenciesCachedLayer = cache.retrieve(snapshotDependenciesLayerEntries).orElseThrow(AssertionError::new);
    CachedLayer resourcesCachedLayer = cache.retrieve(resourcesLayerEntries).orElseThrow(AssertionError::new);
    CachedLayer classesCachedLayer = cache.retrieve(classesLayerEntries).orElseThrow(AssertionError::new);
    CachedLayer extraFilesCachedLayer = cache.retrieve(extraFilesLayerEntries).orElseThrow(AssertionError::new);
    // Verifies that the cached layers are up-to-date.
    Assert.assertEquals(applicationLayers.get(0).getBlobDescriptor().getDigest(), dependenciesCachedLayer.getDigest());
    Assert.assertEquals(applicationLayers.get(1).getBlobDescriptor().getDigest(), snapshotDependenciesCachedLayer.getDigest());
    Assert.assertEquals(applicationLayers.get(2).getBlobDescriptor().getDigest(), resourcesCachedLayer.getDigest());
    Assert.assertEquals(applicationLayers.get(3).getBlobDescriptor().getDigest(), classesCachedLayer.getDigest());
    Assert.assertEquals(applicationLayers.get(4).getBlobDescriptor().getDigest(), extraFilesCachedLayer.getDigest());
    // Verifies that the cache reader gets the same layers as the newest application layers.
    assertBlobsEqual(applicationLayers.get(0).getBlob(), dependenciesCachedLayer.getBlob());
    assertBlobsEqual(applicationLayers.get(1).getBlob(), snapshotDependenciesCachedLayer.getBlob());
    assertBlobsEqual(applicationLayers.get(2).getBlob(), resourcesCachedLayer.getBlob());
    assertBlobsEqual(applicationLayers.get(3).getBlob(), classesCachedLayer.getBlob());
    assertBlobsEqual(applicationLayers.get(4).getBlob(), extraFilesCachedLayer.getBlob());
}
Also used : FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) Layer(com.google.cloud.tools.jib.image.Layer) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) Test(org.junit.Test)

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