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