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