Search in sources :

Example 26 with AbsoluteUnixPath

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

the class ArtifactProcessors method fromWar.

/**
 * Creates a {@link ArtifactProcessor} instance.
 *
 * @param warPath path to the war
 * @param cacheDirectories the location of the relevant caches
 * @param warOptions war cli options
 * @param commonContainerConfigCliOptions common cli options shared between jar and war command
 * @return ArtifactProcessor
 * @throws InvalidImageReferenceException if base image reference is invalid
 */
public static ArtifactProcessor fromWar(Path warPath, CacheDirectories cacheDirectories, War warOptions, CommonContainerConfigCliOptions commonContainerConfigCliOptions) throws InvalidImageReferenceException {
    Optional<AbsoluteUnixPath> appRoot = warOptions.getAppRoot();
    if (!commonContainerConfigCliOptions.isJettyBaseimage() && !appRoot.isPresent()) {
        throw new IllegalArgumentException("Please set the app root of the container with `--app-root` when specifying a base image that is not jetty.");
    }
    AbsoluteUnixPath chosenAppRoot = appRoot.orElse(AbsoluteUnixPath.get(DEFAULT_JETTY_APP_ROOT));
    return new StandardWarExplodedProcessor(warPath, cacheDirectories.getExplodedArtifactDirectory(), chosenAppRoot);
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) StandardWarExplodedProcessor(com.google.cloud.tools.jib.cli.war.StandardWarExplodedProcessor)

Example 27 with AbsoluteUnixPath

use of com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath in project component-runtime by Talend.

the class RemoteEngineCustomizer method registerComponents.

// CHECKSTYLE:OFF
public void registerComponents(final String remoteEngineDirConf, final String workDirConf, final String cacheDirConf, final String baseImageConf, final String targetImageConf, final Collection<String> carPaths, final ImageType fromImageType, final ImageType targetImageType, final DockerConfiguration dockerConfiguration, final RegistryConfiguration registryConfiguration, final ConnectorLoader connectorLoader, final boolean updateOriginalFile) {
    // CHECKSTYLE:ON
    final Path remoteEngineDir = PathFactory.get(requireNonNull(remoteEngineDirConf, "Missing remote engine folder"));
    final Path workDir = PathFactory.get(workDirConf);
    final Path cacheDir = cacheDirConf.startsWith("${remote.engine.dir}/") ? remoteEngineDir.resolve(cacheDirConf.substring("${remote.engine.dir}/".length())) : PathFactory.get(cacheDirConf);
    final Collection<Path> cars = carPaths.stream().map(PathFactory::get).collect(toList());
    final List<Path> missingCars = cars.stream().filter(it -> !Files.exists(it)).collect(toList());
    if (!missingCars.isEmpty()) {
        throw new IllegalArgumentException("Missing component archives: " + missingCars);
    }
    try {
        final Properties filtering = IO.loadProperties(remoteEngineDir.resolve(".env"));
        final Path compose = remoteEngineDir.resolve("docker-compose.yml");
        final List<String> lines = IO.readFile(compose);
        final ImageAndLine connectorsImageRef = findImage(lines, "connectors");
        final String fromConnectorsImage = ofNullable(baseImageConf).filter(it -> !"auto".equals(it)).orElseGet(() -> filterPlaceholders(filtering, connectorsImageRef.image));
        final String toConnectorsImage = ofNullable(targetImageConf).filter(it -> !"auto".equals(it)).orElseGet(() -> timestampImage(fromConnectorsImage));
        final Containerizer targetContainer = targetImageType == ImageType.DOCKER ? Containerizer.to(dockerConfiguration.toImage(toConnectorsImage)) : Containerizer.to(registryConfiguration.toImage(toConnectorsImage));
        log.info("Building image '{}' from '{}' adding {}", toConnectorsImage, fromConnectorsImage, cars);
        final ExecutorService executor = Executors.newFixedThreadPool(Math.max(Runtime.getRuntime().availableProcessors(), 4));
        try (final AutoCloseable ignored = IO.autoDir(workDir)) {
            final Path registry = workDir.resolve("component-registry.properties");
            final Path registryDigest = workDir.resolve("component-registry-digest.properties");
            final AbsoluteUnixPath rootContainerPath = AbsoluteUnixPath.get("/opt/talend/connectors");
            final Instant now = Instant.now();
            final Collection<ConnectorLoader.ConnectorLayer> connectorsLayer = cars.stream().map(it -> connectorLoader.createConnectorLayer(rootContainerPath, workDir, it)).collect(toList());
            final Path baseCache = cacheDir.resolve("base");
            final Path appCache = cacheDir.resolve("application");
            log.info("Looking for component-registry.properties configuration, this can be a bit long...");
            final Image image;
            try {
                image = loadImage(fromConnectorsImage, toConnectorsImage, executor, baseCache, appCache, dockerConfiguration, fromImageType);
            } catch (final ExecutionException ee) {
                log.error("Please validate the connectors container image is an official one, " + "we don't support customizations on custom images or set the from image type");
                throw ee;
            }
            final Map<String, Properties> propertiesContents = image.getLayers().reverse().stream().map(it -> extractProperties(it, Stream.of(registry, registryDigest).map(f -> "opt/talend/connectors/" + f.getFileName()).collect(toSet()))).filter(it -> !it.isEmpty()).findFirst().orElseThrow(() -> new IllegalStateException("No layer containing the component registry in '" + fromConnectorsImage + "'"));
            final Properties componentProperties = requireNonNull(propertiesContents.get("/opt/talend/connectors/component-registry.properties"), "Missing component-registry.properties");
            connectorsLayer.forEach(c -> componentProperties.put(c.getGav().split(":")[1], c.getGav()));
            final Optional<Properties> digestProperties = ofNullable(propertiesContents.get("/opt/talend/connectors/component-registry-digest.properties"));
            digestProperties.ifPresent(digests -> connectorsLayer.forEach(cl -> cl.getDependencies().forEach((key, path) -> {
                try (final DigestOutputStream out = new DigestOutputStream(ByteStreams.nullOutputStream(), MessageDigest.getInstance("SHA-512"))) {
                    java.nio.file.Files.copy(path, out);
                    out.flush();
                    final byte[] digest = out.getMessageDigest().digest();
                    if (digests.put(key, Hex.hex(digest)) != null) {
                        log.info("'{}' digest will be overriding existing entry (entry='{}')", key, cl.getGav());
                    }
                } catch (final NoSuchAlgorithmException | IOException e) {
                    throw new IllegalStateException(e);
                }
            })));
            try (final Writer writer = Files.newBufferedWriter(registry)) {
                componentProperties.store(writer, "Generated by " + getClass().getName());
            }
            if (digestProperties.isPresent()) {
                try (final Writer writer = Files.newBufferedWriter(registryDigest)) {
                    digestProperties.orElseThrow(IllegalStateException::new).store(writer, "Generated by " + getClass().getName());
                }
            }
            log.info("Building image '{}'", toConnectorsImage);
            final JibContainerBuilder from = from(fromImageType, dockerConfiguration, fromConnectorsImage);
            connectorsLayer.stream().map(ConnectorLoader.ConnectorLayer::getLayer).forEach(from::addLayer);
            from.addLayer(LayerConfiguration.builder().addEntry(registry, rootContainerPath.resolve(registry.getFileName().toString()), FilePermissions.DEFAULT_FILE_PERMISSIONS, now).addEntry(registryDigest, rootContainerPath.resolve(registryDigest.getFileName().toString()), FilePermissions.DEFAULT_FILE_PERMISSIONS, now).build()).setCreationTime(now).containerize(targetContainer.setToolName("Talend Component Kit Remote Engine Customizer " + Versions.VERSION).setExecutorService(executor).setBaseImageLayersCache(baseCache).setApplicationLayersCache(appCache));
            if (updateOriginalFile) {
                rewriteCompose(remoteEngineDir, compose, lines, connectorsImageRef, toConnectorsImage);
                log.info("Restart your remote engine to take into account the new connector image");
            } else {
                log.info("You can update '{}' connectors container with image '{}'", compose, toConnectorsImage);
            }
        } finally {
            executor.shutdownNow();
            if (!executor.awaitTermination(5, SECONDS)) {
                log.warn("Executor is not terminated but exiting since it is not critical");
            }
        }
    } catch (final InterruptedException e) {
        Thread.currentThread().interrupt();
    } catch (final Exception e) {
        throw new IllegalStateException(e);
    }
}
Also used : DockerConfiguration(org.talend.sdk.component.remoteengine.customizer.model.DockerConfiguration) ImageReference(com.google.cloud.tools.jib.api.ImageReference) ZonedDateTime(java.time.ZonedDateTime) IO(org.talend.sdk.component.remoteengine.customizer.lang.IO) ImageType(org.talend.sdk.component.remoteengine.customizer.model.ImageType) RegistryConfiguration(org.talend.sdk.component.remoteengine.customizer.model.RegistryConfiguration) Future(java.util.concurrent.Future) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) Path(java.nio.file.Path) Collectors.toSet(java.util.stream.Collectors.toSet) Versions(org.talend.sdk.component.remoteengine.customizer.Versions) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Layer(com.google.cloud.tools.jib.image.Layer) Collection(java.util.Collection) StandardOpenOption(java.nio.file.StandardOpenOption) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) Instant(java.time.Instant) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) InvocationTargetException(java.lang.reflect.InvocationTargetException) DigestOutputStream(java.security.DigestOutputStream) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) ImageFormat(com.google.cloud.tools.jib.api.buildplan.ImageFormat) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Writer(java.io.Writer) ByteStreams(com.google.common.io.ByteStreams) Optional(java.util.Optional) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) Blobs(com.google.cloud.tools.jib.blob.Blobs) Image(com.google.cloud.tools.jib.image.Image) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) ImageConfiguration(com.google.cloud.tools.jib.configuration.ImageConfiguration) MessageDigest(java.security.MessageDigest) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) HashMap(java.util.HashMap) Hex(org.talend.sdk.component.remoteengine.customizer.lang.Hex) BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) Jib(com.google.cloud.tools.jib.api.Jib) LayerConfiguration(com.google.cloud.tools.jib.api.LayerConfiguration) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) Objects.requireNonNull(java.util.Objects.requireNonNull) PathFactory(org.talend.sdk.component.remoteengine.customizer.lang.PathFactory) ExecutorService(java.util.concurrent.ExecutorService) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) Properties(java.util.Properties) Files(java.nio.file.Files) Optional.ofNullable(java.util.Optional.ofNullable) IOException(java.io.IOException) Field(java.lang.reflect.Field) StepsRunner(com.google.cloud.tools.jib.builder.steps.StepsRunner) ExecutionException(java.util.concurrent.ExecutionException) LogEvent(com.google.cloud.tools.jib.api.LogEvent) Collectors.toList(java.util.stream.Collectors.toList) Reflects.asAccessible(org.talend.sdk.component.remoteengine.customizer.lang.Reflects.asAccessible) Containerizer(com.google.cloud.tools.jib.api.Containerizer) DateTimeFormatter(java.time.format.DateTimeFormatter) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) ConnectorLoader(org.talend.sdk.component.remoteengine.customizer.service.ConnectorLoader) AllArgsConstructor(lombok.AllArgsConstructor) SECONDS(java.util.concurrent.TimeUnit.SECONDS) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Properties(java.util.Properties) Image(com.google.cloud.tools.jib.image.Image) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) DigestOutputStream(java.security.DigestOutputStream) Containerizer(com.google.cloud.tools.jib.api.Containerizer) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) ExecutionException(java.util.concurrent.ExecutionException) ConnectorLoader(org.talend.sdk.component.remoteengine.customizer.service.ConnectorLoader) Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Instant(java.time.Instant) IOException(java.io.IOException) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ExecutorService(java.util.concurrent.ExecutorService) Writer(java.io.Writer)

Example 28 with AbsoluteUnixPath

use of com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath in project component-runtime by Talend.

the class SingerMojo method configure.

/*
     * TALEND_SINGER_COMPONENT_ARCHIVE=/opt/talend/components/mailio-1.0.0.car \
     * java -jar component-kitap-$KIT_VERSION-fatjar.jar \
     * --config config.json
     */
private void configure(final JibHelper helper) {
    final JibContainerBuilder builder = helper.getBuilder();
    final AbsoluteUnixPath workingDirectory = helper.getWorkingDirectory();
    // 1. add the kitapp "main" + workdir (1 layer)
    final Path main = findMain();
    final Path fakeWorkDir = PathFactory.get(project.getBuild().getDirectory()).resolve("component_mojo_singer_work");
    if (!Files.exists(fakeWorkDir)) {
        try {
            Files.createDirectories(fakeWorkDir);
        } catch (final IOException e) {
            throw new IllegalStateException(e);
        }
    }
    final AbsoluteUnixPath targetWorkDir = workingDirectory.resolve("work/");
    final AbsoluteUnixPath targetMain = workingDirectory.resolve("lib/" + main.getFileName().toString());
    builder.addLayer(LayerConfiguration.builder().addEntry(main, targetMain, DEFAULT_FILE_PERMISSIONS, helper.getCreationTime()).addEntry(fakeWorkDir, targetWorkDir, DEFAULT_FOLDER_PERMISSIONS, helper.getCreationTime()).build());
    builder.addEnvironmentVariable("TALEND_SINGER_WORK_DIR", targetWorkDir.toString());
    // 3. add the .car and setup it in the environment
    final AbsoluteUnixPath targetCarPath = workingDirectory.resolve("lib/" + car.getName());
    builder.addLayer(LayerConfiguration.builder().addEntry(car.toPath(), targetCarPath, DEFAULT_FILE_PERMISSIONS, helper.getCreationTime()).build());
    builder.addEnvironmentVariable("TALEND_SINGER_COMPONENT_ARCHIVE", targetCarPath.toString());
    // 4. set the entry point
    builder.setEntrypoint("java", "-jar", targetMain.toString());
}
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) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) IOException(java.io.IOException)

Example 29 with AbsoluteUnixPath

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

the class StandardExplodedProcessorTest method testCreateLayers_withoutClassPathInManifest_containsOnlyClasses.

@Test
public void testCreateLayers_withoutClassPathInManifest_containsOnlyClasses() throws IOException, URISyntaxException {
    Path standardJar = Paths.get(Resources.getResource(STANDARD_JAR_WITH_ONLY_CLASSES).toURI());
    Path destDir = temporaryFolder.newFolder().toPath();
    StandardExplodedProcessor standardExplodedModeProcessor = new StandardExplodedProcessor(standardJar, destDir, JAR_JAVA_VERSION);
    List<FileEntriesLayer> layers = standardExplodedModeProcessor.createLayers();
    assertThat(layers.size()).isEqualTo(2);
    FileEntriesLayer resourcesLayer = layers.get(0);
    FileEntriesLayer classesLayer = layers.get(1);
    // Validate resources layer.
    assertThat(resourcesLayer.getEntries().size()).isEqualTo(1);
    assertThat(resourcesLayer.getEntries().get(0).getExtractionPath()).isEqualTo(AbsoluteUnixPath.get("/app/explodedJar/META-INF/MANIFEST.MF"));
    // Validate classes layer.
    List<AbsoluteUnixPath> actualClassesPath = classesLayer.getEntries().stream().map(FileEntry::getExtractionPath).collect(Collectors.toList());
    assertThat(actualClassesPath).containsExactly(AbsoluteUnixPath.get("/app/explodedJar/class1.class"), AbsoluteUnixPath.get("/app/explodedJar/class2.class"));
}
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) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) Test(org.junit.Test)

Example 30 with AbsoluteUnixPath

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

the class JavaContainerBuilderHelperTest method testExtraDirectoryLayerConfiguration_globPermissions.

@Test
public void testExtraDirectoryLayerConfiguration_globPermissions() throws URISyntaxException, IOException {
    Path extraFilesDirectory = Paths.get(Resources.getResource("core/layer").toURI());
    Map<String, FilePermissions> permissionsMap = ImmutableMap.of("/a", FilePermissions.fromOctalString("123"), "/a/*", FilePermissions.fromOctalString("456"), "**/bar", FilePermissions.fromOctalString("765"));
    FileEntriesLayer fileEntriesLayer = JavaContainerBuilderHelper.extraDirectoryLayerConfiguration(extraFilesDirectory, AbsoluteUnixPath.get("/"), Collections.emptyList(), Collections.emptyList(), permissionsMap, (ignored1, ignored2) -> Instant.EPOCH);
    assertThat(fileEntriesLayer.getEntries()).comparingElementsUsing(EXTRACTION_PATH_OF).containsExactly("/a", "/a/b", "/a/b/bar", "/c", "/c/cat", "/foo");
    Map<AbsoluteUnixPath, FilePermissions> expectedPermissions = ImmutableMap.<AbsoluteUnixPath, FilePermissions>builder().put(AbsoluteUnixPath.get("/a"), FilePermissions.fromOctalString("123")).put(AbsoluteUnixPath.get("/a/b"), FilePermissions.fromOctalString("456")).put(AbsoluteUnixPath.get("/a/b/bar"), FilePermissions.fromOctalString("765")).put(AbsoluteUnixPath.get("/c"), FilePermissions.DEFAULT_FOLDER_PERMISSIONS).put(AbsoluteUnixPath.get("/c/cat"), FilePermissions.DEFAULT_FILE_PERMISSIONS).put(AbsoluteUnixPath.get("/foo"), FilePermissions.DEFAULT_FILE_PERMISSIONS).build();
    for (FileEntry entry : fileEntriesLayer.getEntries()) {
        assertThat(entry.getPermissions()).isEqualTo(expectedPermissions.get(entry.getExtractionPath()));
    }
}
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) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) 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