Search in sources :

Example 6 with Containerizer

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

the class War method call.

@Override
public Integer call() {
    commonCliOptions.validate();
    SingleThreadedExecutor executor = new SingleThreadedExecutor();
    ConsoleLogger logger = CliLogger.newLogger(commonCliOptions.getVerbosity(), commonCliOptions.getHttpTrace(), commonCliOptions.getConsoleOutput(), spec.commandLine().getOut(), spec.commandLine().getErr(), executor);
    Future<Optional<String>> updateCheckFuture = Futures.immediateFuture(Optional.empty());
    try {
        JibCli.configureHttpLogging(commonCliOptions.getHttpTrace().toJulLevel());
        GlobalConfig globalConfig = GlobalConfig.readConfig();
        updateCheckFuture = JibCli.newUpdateChecker(globalConfig, commonCliOptions.getVerbosity(), logEvent -> logger.log(logEvent.getLevel(), logEvent.getMessage()));
        if (!Files.exists(warFile)) {
            logger.log(LogEvent.Level.ERROR, "The file path provided does not exist: " + warFile);
            return 1;
        }
        if (Files.isDirectory(warFile)) {
            logger.log(LogEvent.Level.ERROR, "The file path provided is for a directory. Please provide a path to a WAR: " + warFile);
            return 1;
        }
        Path warFileParentDir = Verify.verifyNotNull(warFile.toAbsolutePath().getParent());
        CacheDirectories cacheDirectories = CacheDirectories.from(commonCliOptions, warFileParentDir);
        ArtifactProcessor processor = ArtifactProcessors.fromWar(warFile, cacheDirectories, this, commonContainerConfigCliOptions);
        JibContainerBuilder containerBuilder = WarFiles.toJibContainerBuilder(processor, commonCliOptions, commonContainerConfigCliOptions, logger);
        Containerizer containerizer = Containerizers.from(commonCliOptions, logger, cacheDirectories);
        // Enable registry mirrors
        Multimaps.asMap(globalConfig.getRegistryMirrors()).forEach(containerizer::addRegistryMirrors);
        JibContainer jibContainer = containerBuilder.containerize(containerizer);
        JibCli.writeImageJson(commonCliOptions.getImageJsonPath(), jibContainer);
    } catch (InterruptedException ex) {
        JibCli.logTerminatingException(logger, ex, commonCliOptions.isStacktrace());
        Thread.currentThread().interrupt();
        return 1;
    } catch (Exception ex) {
        JibCli.logTerminatingException(logger, ex, commonCliOptions.isStacktrace());
        return 1;
    } finally {
        JibCli.finishUpdateChecker(logger, updateCheckFuture);
        executor.shutDownAndAwaitTermination(Duration.ofSeconds(3));
    }
    return 0;
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Verify(com.google.common.base.Verify) SingleThreadedExecutor(com.google.cloud.tools.jib.plugins.common.logging.SingleThreadedExecutor) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) WarFiles(com.google.cloud.tools.jib.cli.war.WarFiles) GlobalConfig(com.google.cloud.tools.jib.plugins.common.globalconfig.GlobalConfig) Files(java.nio.file.Files) JibContainer(com.google.cloud.tools.jib.api.JibContainer) Callable(java.util.concurrent.Callable) Multimaps(com.google.common.collect.Multimaps) LogEvent(com.google.cloud.tools.jib.api.LogEvent) Futures(com.google.common.util.concurrent.Futures) Future(java.util.concurrent.Future) CliLogger(com.google.cloud.tools.jib.cli.logging.CliLogger) Containerizer(com.google.cloud.tools.jib.api.Containerizer) Duration(java.time.Duration) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Path(java.nio.file.Path) ConsoleLogger(com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger) CommandLine(picocli.CommandLine) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Path(java.nio.file.Path) Optional(java.util.Optional) GlobalConfig(com.google.cloud.tools.jib.plugins.common.globalconfig.GlobalConfig) JibContainer(com.google.cloud.tools.jib.api.JibContainer) SingleThreadedExecutor(com.google.cloud.tools.jib.plugins.common.logging.SingleThreadedExecutor) ConsoleLogger(com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger) Containerizer(com.google.cloud.tools.jib.api.Containerizer) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder)

Example 7 with Containerizer

use of com.google.cloud.tools.jib.api.Containerizer 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 8 with Containerizer

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

the class Build method call.

@Override
public Integer call() {
    commonCliOptions.validate();
    Path buildFile = getBuildFile();
    SingleThreadedExecutor executor = new SingleThreadedExecutor();
    ConsoleLogger logger = CliLogger.newLogger(commonCliOptions.getVerbosity(), commonCliOptions.getHttpTrace(), commonCliOptions.getConsoleOutput(), spec.commandLine().getOut(), spec.commandLine().getErr(), executor);
    Future<Optional<String>> updateCheckFuture = Futures.immediateFuture(Optional.empty());
    try {
        JibCli.configureHttpLogging(commonCliOptions.getHttpTrace().toJulLevel());
        GlobalConfig globalConfig = GlobalConfig.readConfig();
        updateCheckFuture = JibCli.newUpdateChecker(globalConfig, commonCliOptions.getVerbosity(), logEvent -> logger.log(logEvent.getLevel(), logEvent.getMessage()));
        if (!Files.isReadable(buildFile)) {
            logger.log(LogEvent.Level.ERROR, "The Build File YAML either does not exist or cannot be opened for reading: " + buildFile);
            return 1;
        }
        if (!Files.isRegularFile(buildFile)) {
            logger.log(LogEvent.Level.ERROR, "Build File YAML path is a not a file: " + buildFile);
            return 1;
        }
        CacheDirectories cacheDirectories = CacheDirectories.from(commonCliOptions, contextRoot);
        Containerizer containerizer = Containerizers.from(commonCliOptions, logger, cacheDirectories);
        JibContainerBuilder containerBuilder = BuildFiles.toJibContainerBuilder(contextRoot, buildFile, this, commonCliOptions, logger);
        // Enable registry mirrors
        Multimaps.asMap(globalConfig.getRegistryMirrors()).forEach(containerizer::addRegistryMirrors);
        JibContainer jibContainer = containerBuilder.containerize(containerizer);
        JibCli.writeImageJson(commonCliOptions.getImageJsonPath(), jibContainer);
    } catch (InterruptedException ex) {
        JibCli.logTerminatingException(logger, ex, commonCliOptions.isStacktrace());
        Thread.currentThread().interrupt();
        return 1;
    } catch (Exception ex) {
        JibCli.logTerminatingException(logger, ex, commonCliOptions.isStacktrace());
        return 1;
    } finally {
        JibCli.finishUpdateChecker(logger, updateCheckFuture);
        executor.shutDownAndAwaitTermination(Duration.ofSeconds(3));
    }
    return 0;
}
Also used : Path(java.nio.file.Path) SingleThreadedExecutor(com.google.cloud.tools.jib.plugins.common.logging.SingleThreadedExecutor) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) GlobalConfig(com.google.cloud.tools.jib.plugins.common.globalconfig.GlobalConfig) Files(java.nio.file.Files) JibContainer(com.google.cloud.tools.jib.api.JibContainer) Callable(java.util.concurrent.Callable) Multimaps(com.google.common.collect.Multimaps) LogEvent(com.google.cloud.tools.jib.api.LogEvent) Futures(com.google.common.util.concurrent.Futures) Future(java.util.concurrent.Future) CliLogger(com.google.cloud.tools.jib.cli.logging.CliLogger) Containerizer(com.google.cloud.tools.jib.api.Containerizer) BuildFiles(com.google.cloud.tools.jib.cli.buildfile.BuildFiles) Duration(java.time.Duration) Map(java.util.Map) Optional(java.util.Optional) CommandSpec(picocli.CommandLine.Model.CommandSpec) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Path(java.nio.file.Path) Collections(java.util.Collections) ConsoleLogger(com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger) CommandLine(picocli.CommandLine) Optional(java.util.Optional) GlobalConfig(com.google.cloud.tools.jib.plugins.common.globalconfig.GlobalConfig) JibContainer(com.google.cloud.tools.jib.api.JibContainer) SingleThreadedExecutor(com.google.cloud.tools.jib.plugins.common.logging.SingleThreadedExecutor) ConsoleLogger(com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger) Containerizer(com.google.cloud.tools.jib.api.Containerizer) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder)

Example 9 with Containerizer

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

the class Containerizers method create.

private static Containerizer create(CommonCliOptions commonCliOptions, ConsoleLogger logger) throws InvalidImageReferenceException, FileNotFoundException {
    String imageSpec = commonCliOptions.getTargetImage();
    if (imageSpec.startsWith(DOCKER_DAEMON_IMAGE_PREFIX)) {
        // TODO: allow setting docker env and docker executable (along with path/env)
        return Containerizer.to(DockerDaemonImage.named(imageSpec.replaceFirst(DOCKER_DAEMON_IMAGE_PREFIX, "")));
    }
    if (imageSpec.startsWith(TAR_IMAGE_PREFIX)) {
        return Containerizer.to(TarImage.at(Paths.get(imageSpec.replaceFirst(TAR_IMAGE_PREFIX, ""))).named(commonCliOptions.getName()));
    }
    ImageReference imageReference = ImageReference.parse(imageSpec.replaceFirst(REGISTRY_IMAGE_PREFIX, ""));
    RegistryImage registryImage = RegistryImage.named(imageReference);
    DefaultCredentialRetrievers defaultCredentialRetrievers = DefaultCredentialRetrievers.init(CredentialRetrieverFactory.forImage(imageReference, logEvent -> logger.log(logEvent.getLevel(), logEvent.getMessage())));
    Credentials.getToCredentialRetrievers(commonCliOptions, defaultCredentialRetrievers).forEach(registryImage::addCredentialRetriever);
    return Containerizer.to(registryImage);
}
Also used : DockerDaemonImage(com.google.cloud.tools.jib.api.DockerDaemonImage) ImageReference(com.google.cloud.tools.jib.api.ImageReference) TAR_IMAGE_PREFIX(com.google.cloud.tools.jib.api.Jib.TAR_IMAGE_PREFIX) RegistryImage(com.google.cloud.tools.jib.api.RegistryImage) ProgressEvent(com.google.cloud.tools.jib.event.events.ProgressEvent) TarImage(com.google.cloud.tools.jib.api.TarImage) DefaultCredentialRetrievers(com.google.cloud.tools.jib.plugins.common.DefaultCredentialRetrievers) DOCKER_DAEMON_IMAGE_PREFIX(com.google.cloud.tools.jib.api.Jib.DOCKER_DAEMON_IMAGE_PREFIX) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) ProgressEventHandler(com.google.cloud.tools.jib.event.progress.ProgressEventHandler) FileNotFoundException(java.io.FileNotFoundException) REGISTRY_IMAGE_PREFIX(com.google.cloud.tools.jib.api.Jib.REGISTRY_IMAGE_PREFIX) LogEvent(com.google.cloud.tools.jib.api.LogEvent) List(java.util.List) Containerizer(com.google.cloud.tools.jib.api.Containerizer) Paths(java.nio.file.Paths) JibSystemProperties(com.google.cloud.tools.jib.global.JibSystemProperties) ProgressDisplayGenerator(com.google.cloud.tools.jib.plugins.common.logging.ProgressDisplayGenerator) CredentialRetrieverFactory(com.google.cloud.tools.jib.frontend.CredentialRetrieverFactory) ConsoleLogger(com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger) ImageReference(com.google.cloud.tools.jib.api.ImageReference) DefaultCredentialRetrievers(com.google.cloud.tools.jib.plugins.common.DefaultCredentialRetrievers) RegistryImage(com.google.cloud.tools.jib.api.RegistryImage)

Example 10 with Containerizer

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

the class Jar method call.

@Override
public Integer call() {
    commonCliOptions.validate();
    SingleThreadedExecutor executor = new SingleThreadedExecutor();
    ConsoleLogger logger = CliLogger.newLogger(commonCliOptions.getVerbosity(), commonCliOptions.getHttpTrace(), commonCliOptions.getConsoleOutput(), spec.commandLine().getOut(), spec.commandLine().getErr(), executor);
    Future<Optional<String>> updateCheckFuture = Futures.immediateFuture(Optional.empty());
    try {
        JibCli.configureHttpLogging(commonCliOptions.getHttpTrace().toJulLevel());
        GlobalConfig globalConfig = GlobalConfig.readConfig();
        updateCheckFuture = JibCli.newUpdateChecker(globalConfig, commonCliOptions.getVerbosity(), logEvent -> logger.log(logEvent.getLevel(), logEvent.getMessage()));
        if (!Files.exists(jarFile)) {
            logger.log(LogEvent.Level.ERROR, "The file path provided does not exist: " + jarFile);
            return 1;
        }
        if (Files.isDirectory(jarFile)) {
            logger.log(LogEvent.Level.ERROR, "The file path provided is for a directory. Please provide a path to a JAR: " + jarFile);
            return 1;
        }
        if (!commonContainerConfigCliOptions.getEntrypoint().isEmpty() && !jvmFlags.isEmpty()) {
            logger.log(LogEvent.Level.WARN, "--jvm-flags is ignored when --entrypoint is specified");
        }
        Path jarFileParentDir = Verify.verifyNotNull(jarFile.toAbsolutePath().getParent());
        CacheDirectories cacheDirectories = CacheDirectories.from(commonCliOptions, jarFileParentDir);
        ArtifactProcessor processor = ArtifactProcessors.fromJar(jarFile, cacheDirectories, this, commonContainerConfigCliOptions);
        JibContainerBuilder containerBuilder = JarFiles.toJibContainerBuilder(processor, this, commonCliOptions, commonContainerConfigCliOptions, logger);
        Containerizer containerizer = Containerizers.from(commonCliOptions, logger, cacheDirectories);
        // Enable registry mirrors
        Multimaps.asMap(globalConfig.getRegistryMirrors()).forEach(containerizer::addRegistryMirrors);
        JibContainer jibContainer = containerBuilder.containerize(containerizer);
        JibCli.writeImageJson(commonCliOptions.getImageJsonPath(), jibContainer);
    } catch (InterruptedException ex) {
        JibCli.logTerminatingException(logger, ex, commonCliOptions.isStacktrace());
        Thread.currentThread().interrupt();
        return 1;
    } catch (Exception ex) {
        JibCli.logTerminatingException(logger, ex, commonCliOptions.isStacktrace());
        return 1;
    } finally {
        JibCli.finishUpdateChecker(logger, updateCheckFuture);
        executor.shutDownAndAwaitTermination(Duration.ofSeconds(3));
    }
    return 0;
}
Also used : Verify(com.google.common.base.Verify) SingleThreadedExecutor(com.google.cloud.tools.jib.plugins.common.logging.SingleThreadedExecutor) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) GlobalConfig(com.google.cloud.tools.jib.plugins.common.globalconfig.GlobalConfig) Files(java.nio.file.Files) JibContainer(com.google.cloud.tools.jib.api.JibContainer) Callable(java.util.concurrent.Callable) JarFiles(com.google.cloud.tools.jib.cli.jar.JarFiles) Multimaps(com.google.common.collect.Multimaps) LogEvent(com.google.cloud.tools.jib.api.LogEvent) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) Future(java.util.concurrent.Future) ProcessingMode(com.google.cloud.tools.jib.cli.jar.ProcessingMode) CliLogger(com.google.cloud.tools.jib.cli.logging.CliLogger) Containerizer(com.google.cloud.tools.jib.api.Containerizer) Duration(java.time.Duration) Optional(java.util.Optional) CommandSpec(picocli.CommandLine.Model.CommandSpec) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Path(java.nio.file.Path) Collections(java.util.Collections) ConsoleLogger(com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger) CommandLine(picocli.CommandLine) Path(java.nio.file.Path) Optional(java.util.Optional) GlobalConfig(com.google.cloud.tools.jib.plugins.common.globalconfig.GlobalConfig) JibContainer(com.google.cloud.tools.jib.api.JibContainer) SingleThreadedExecutor(com.google.cloud.tools.jib.plugins.common.logging.SingleThreadedExecutor) ConsoleLogger(com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger) Containerizer(com.google.cloud.tools.jib.api.Containerizer) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder)

Aggregations

Containerizer (com.google.cloud.tools.jib.api.Containerizer)22 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)16 LogEvent (com.google.cloud.tools.jib.api.LogEvent)11 Path (java.nio.file.Path)11 ImageReference (com.google.cloud.tools.jib.api.ImageReference)10 Files (java.nio.file.Files)9 Optional (java.util.Optional)9 JibContainer (com.google.cloud.tools.jib.api.JibContainer)8 ConsoleLogger (com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger)8 InvalidImageReferenceException (com.google.cloud.tools.jib.api.InvalidImageReferenceException)7 RegistryImage (com.google.cloud.tools.jib.api.RegistryImage)7 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)7 List (java.util.List)7 Future (java.util.concurrent.Future)7 CliLogger (com.google.cloud.tools.jib.cli.logging.CliLogger)6 GlobalConfig (com.google.cloud.tools.jib.plugins.common.globalconfig.GlobalConfig)6 SingleThreadedExecutor (com.google.cloud.tools.jib.plugins.common.logging.SingleThreadedExecutor)6 VisibleForTesting (com.google.common.annotations.VisibleForTesting)6 Multimaps (com.google.common.collect.Multimaps)6 Futures (com.google.common.util.concurrent.Futures)6