Search in sources :

Example 16 with AbsoluteUnixPath

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

the class ArtifactLayers method getDirectoryContentsAsLayer.

/**
 * Creates a layer containing contents of a directory. Only paths that match the given predicate
 * will be added.
 *
 * @param layerName name of the layer
 * @param sourceRoot path to source directory
 * @param pathFilter predicate to determine whether to add the path or not
 * @param basePathInContainer path to destination on container
 * @return {@link FileEntriesLayer} representing the layer
 * @throws IOException if io exception occurs when reading from the source directory
 */
public static FileEntriesLayer getDirectoryContentsAsLayer(String layerName, Path sourceRoot, Predicate<Path> pathFilter, AbsoluteUnixPath basePathInContainer) throws IOException {
    FileEntriesLayer.Builder builder = FileEntriesLayer.builder().setName(layerName);
    new DirectoryWalker(sourceRoot).filterRoot().filter(path -> pathFilter.test(path)).walk(path -> {
        AbsoluteUnixPath pathOnContainer = basePathInContainer.resolve(sourceRoot.relativize(path));
        builder.addEntry(path, pathOnContainer);
    });
    return builder.build();
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Predicate(java.util.function.Predicate) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker) IOException(java.io.IOException) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) DirectoryWalker(com.google.cloud.tools.jib.filesystem.DirectoryWalker)

Example 17 with AbsoluteUnixPath

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

the class PluginConfigurationProcessor method computeEntrypoint.

/**
 * Computes the container entrypoint.
 *
 * <p>Computation occurs in this order:
 *
 * <ol>
 *   <li>null (inheriting from the base image), if the user specified value is {@code INHERIT}
 *   <li>the user specified one, if set
 *   <li>for a WAR project, null (inheriting) if a custom base image is specified, and {@code
 *       ["java", "-jar", "/usr/local/jetty/start.jar"]} otherwise (default Jetty base image)
 *   <li>for a non-WAR project, by resolving the main class
 * </ol>
 *
 * @param rawConfiguration raw configuration data
 * @param projectProperties used for providing additional information
 * @param jibContainerBuilder container builder
 * @return the entrypoint
 * @throws MainClassInferenceException if no valid main class is configured or discovered
 * @throws InvalidAppRootException if {@code appRoot} value is not an absolute Unix path
 * @throws InvalidContainerizingModeException if {@code containerizingMode} value is invalid
 */
@Nullable
@VisibleForTesting
static List<String> computeEntrypoint(RawConfiguration rawConfiguration, ProjectProperties projectProperties, JibContainerBuilder jibContainerBuilder) throws MainClassInferenceException, InvalidAppRootException, IOException, InvalidContainerizingModeException {
    Optional<List<String>> rawEntrypoint = rawConfiguration.getEntrypoint();
    List<String> rawExtraClasspath = rawConfiguration.getExtraClasspath();
    boolean entrypointDefined = rawEntrypoint.isPresent() && !rawEntrypoint.get().isEmpty();
    if (entrypointDefined && (rawConfiguration.getMainClass().isPresent() || !rawConfiguration.getJvmFlags().isEmpty() || !rawExtraClasspath.isEmpty() || rawConfiguration.getExpandClasspathDependencies())) {
        projectProperties.log(LogEvent.warn("mainClass, extraClasspath, jvmFlags, and expandClasspathDependencies are ignored " + "when entrypoint is specified"));
    }
    if (projectProperties.isWarProject()) {
        if (entrypointDefined) {
            return rawEntrypoint.get().size() == 1 && "INHERIT".equals(rawEntrypoint.get().get(0)) ? null : rawEntrypoint.get();
        }
        if (rawConfiguration.getMainClass().isPresent() || !rawConfiguration.getJvmFlags().isEmpty() || !rawExtraClasspath.isEmpty() || rawConfiguration.getExpandClasspathDependencies()) {
            projectProperties.log(LogEvent.warn("mainClass, extraClasspath, jvmFlags, and expandClasspathDependencies are ignored " + "for WAR projects"));
        }
        return rawConfiguration.getFromImage().isPresent() ? // Inherit if a custom base image.
        null : Arrays.asList("java", "-jar", "/usr/local/jetty/start.jar");
    }
    List<String> classpath = new ArrayList<>(rawExtraClasspath);
    AbsoluteUnixPath appRoot = getAppRootChecked(rawConfiguration, projectProperties);
    ContainerizingMode mode = getContainerizingModeChecked(rawConfiguration, projectProperties);
    switch(mode) {
        case EXPLODED:
            classpath.add(appRoot.resolve("resources").toString());
            classpath.add(appRoot.resolve("classes").toString());
            break;
        case PACKAGED:
            classpath.add(appRoot.resolve("classpath/*").toString());
            break;
        default:
            throw new IllegalStateException("unknown containerizing mode: " + mode);
    }
    if (projectProperties.getMajorJavaVersion() >= 9 || rawConfiguration.getExpandClasspathDependencies()) {
        List<Path> jars = projectProperties.getDependencies();
        Map<String, Long> occurrences = jars.stream().map(path -> path.getFileName().toString()).collect(Collectors.groupingBy(filename -> filename, Collectors.counting()));
        List<String> duplicates = occurrences.entrySet().stream().filter(entry -> entry.getValue() > 1).map(Map.Entry::getKey).collect(Collectors.toList());
        for (Path jar : jars) {
            // Handle duplicates by appending filesize to the end of the file. This renaming logic
            // must be in sync with the code that does the same in the other place. See
            // https://github.com/GoogleContainerTools/jib/issues/3331
            String jarName = jar.getFileName().toString();
            if (duplicates.contains(jarName)) {
                jarName = jarName.replaceFirst("\\.jar$", "-" + Files.size(jar)) + ".jar";
            }
            classpath.add(appRoot.resolve("libs").resolve(jarName).toString());
        }
    } else {
        classpath.add(appRoot.resolve("libs/*").toString());
    }
    String classpathString = String.join(":", classpath);
    String mainClass;
    try {
        mainClass = MainClassResolver.resolveMainClass(rawConfiguration.getMainClass().orElse(null), projectProperties);
    } catch (MainClassInferenceException ex) {
        if (entrypointDefined) {
            // We will use the user-given entrypoint, so don't fail.
            mainClass = "could-not-infer-a-main-class";
        } else {
            throw ex;
        }
    }
    addJvmArgFilesLayer(rawConfiguration, projectProperties, jibContainerBuilder, classpathString, mainClass);
    if (projectProperties.getMajorJavaVersion() >= 9) {
        classpathString = "@" + appRoot.resolve(JIB_CLASSPATH_FILE);
    }
    if (entrypointDefined) {
        return rawEntrypoint.get().size() == 1 && "INHERIT".equals(rawEntrypoint.get().get(0)) ? null : rawEntrypoint.get();
    }
    List<String> entrypoint = new ArrayList<>(4 + rawConfiguration.getJvmFlags().size());
    entrypoint.add("java");
    entrypoint.addAll(rawConfiguration.getJvmFlags());
    entrypoint.add("-cp");
    entrypoint.add(classpathString);
    entrypoint.add(mainClass);
    return entrypoint;
}
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) ArrayList(java.util.ArrayList) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Nullable(javax.annotation.Nullable)

Example 18 with AbsoluteUnixPath

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

the class PluginConfigurationProcessor method getVolumesSet.

/**
 * Parses the list of raw volumes directories to a set of {@link AbsoluteUnixPath}.
 *
 * @param rawConfiguration raw configuration data
 * @return the set of parsed volumes.
 * @throws InvalidContainerVolumeException if {@code volumes} are not valid absolute Unix paths
 */
@VisibleForTesting
static Set<AbsoluteUnixPath> getVolumesSet(RawConfiguration rawConfiguration) throws InvalidContainerVolumeException {
    Set<AbsoluteUnixPath> volumes = new HashSet<>();
    for (String path : rawConfiguration.getVolumes()) {
        try {
            AbsoluteUnixPath absoluteUnixPath = AbsoluteUnixPath.get(path);
            volumes.add(absoluteUnixPath);
        } catch (IllegalArgumentException exception) {
            throw new InvalidContainerVolumeException(path, path, exception);
        }
    }
    return volumes;
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 19 with AbsoluteUnixPath

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

the class PluginConfigurationProcessor method addJvmArgFilesLayer.

@VisibleForTesting
static void addJvmArgFilesLayer(RawConfiguration rawConfiguration, ProjectProperties projectProperties, JibContainerBuilder jibContainerBuilder, String classpath, String mainClass) throws IOException, InvalidAppRootException {
    Path projectCache = projectProperties.getDefaultCacheDirectory();
    Path classpathFile = projectCache.resolve(JIB_CLASSPATH_FILE);
    Path mainClassFile = projectCache.resolve(JIB_MAIN_CLASS_FILE);
    // It's perfectly fine to always generate a new temp file or rewrite an existing file. However,
    // fixing the source file path and preserving the file timestamp prevents polluting the Jib
    // layer cache space by not creating new cache selectors every time. (Note, however, creating
    // new selectors does not affect correctness at all.)
    writeFileConservatively(classpathFile, classpath);
    writeFileConservatively(mainClassFile, mainClass);
    AbsoluteUnixPath appRoot = getAppRootChecked(rawConfiguration, projectProperties);
    jibContainerBuilder.addFileEntriesLayer(FileEntriesLayer.builder().setName(LayerType.JVM_ARG_FILES.getName()).addEntry(classpathFile, appRoot.resolve(JIB_CLASSPATH_FILE)).addEntry(mainClassFile, appRoot.resolve(JIB_MAIN_CLASS_FILE)).build());
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 20 with AbsoluteUnixPath

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

the class LayerEntriesSelectorTest method testGenerateSelector_targetModificationTimeChanged.

@Test
public void testGenerateSelector_targetModificationTimeChanged() throws IOException {
    Path layerFile = temporaryFolder.newFile().toPath();
    AbsoluteUnixPath pathInContainer = AbsoluteUnixPath.get("/bar");
    FilePermissions permissions = FilePermissions.fromOctalString("111");
    FileEntry layerEntry1 = new FileEntry(layerFile, pathInContainer, permissions, Instant.now());
    FileEntry layerEntry2 = new FileEntry(layerFile, pathInContainer, permissions, Instant.EPOCH);
    // Verify that different target modification times generate different selectors
    Assert.assertNotEquals(LayerEntriesSelector.generateSelector(ImmutableList.of(layerEntry1)), LayerEntriesSelector.generateSelector(ImmutableList.of(layerEntry2)));
}
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) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) Test(org.junit.Test)

Aggregations

AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)46 Path (java.nio.file.Path)34 FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)27 IOException (java.io.IOException)18 Test (org.junit.Test)18 FilePermissions (com.google.cloud.tools.jib.api.buildplan.FilePermissions)15 Files (java.nio.file.Files)14 Map (java.util.Map)14 List (java.util.List)13 Instant (java.time.Instant)11 ArrayList (java.util.ArrayList)10 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)9 FileEntry (com.google.cloud.tools.jib.api.buildplan.FileEntry)9 VisibleForTesting (com.google.common.annotations.VisibleForTesting)9 Optional (java.util.Optional)8 Predicate (java.util.function.Predicate)8 Collectors (java.util.stream.Collectors)8 Stream (java.util.stream.Stream)8 JavaContainerBuilder (com.google.cloud.tools.jib.api.JavaContainerBuilder)7 BuildContext (com.google.cloud.tools.jib.configuration.BuildContext)7