Search in sources :

Example 11 with JibContainer

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

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 12 with JibContainer

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

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)

Example 13 with JibContainer

use of com.google.cloud.tools.jib.api.JibContainer in project quarkus by quarkusio.

the class JibProcessor method containerize.

private JibContainer containerize(ContainerImageConfig containerImageConfig, JibConfig jibConfig, ContainerImageInfoBuildItem containerImage, JibContainerBuilder jibContainerBuilder, boolean pushRequested) {
    Containerizer containerizer = createContainerizer(containerImageConfig, jibConfig, containerImage, pushRequested);
    for (String additionalTag : containerImage.getAdditionalTags()) {
        containerizer.withAdditionalTag(additionalTag);
    }
    String previousContextStorageSysProp = null;
    try {
        // Jib uses the Google HTTP Client under the hood which attempts to record traces via OpenCensus which is wired
        // to delegate to OpenTelemetry.
        // This can lead to problems with the Quarkus OpenTelemetry extension which expects Vert.x to be running,
        // something that is not the case at build time, see https://github.com/quarkusio/quarkus/issues/22864.
        previousContextStorageSysProp = System.setProperty(OPENTELEMETRY_CONTEXT_CONTEXT_STORAGE_PROVIDER_SYS_PROP, "default");
        JibContainer container = jibContainerBuilder.containerize(containerizer);
        log.infof("%s container image %s (%s)\n", containerImageConfig.isPushExplicitlyEnabled() ? "Pushed" : "Created", container.getTargetImage(), container.getDigest());
        return container;
    } catch (Exception e) {
        throw new RuntimeException("Unable to create container image", e);
    } finally {
        if (previousContextStorageSysProp == null) {
            System.clearProperty(OPENTELEMETRY_CONTEXT_CONTEXT_STORAGE_PROVIDER_SYS_PROP);
        } else {
            System.setProperty(OPENTELEMETRY_CONTEXT_CONTEXT_STORAGE_PROVIDER_SYS_PROP, previousContextStorageSysProp);
        }
    }
}
Also used : JibContainer(com.google.cloud.tools.jib.api.JibContainer) Containerizer(com.google.cloud.tools.jib.api.Containerizer) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException)

Example 14 with JibContainer

use of com.google.cloud.tools.jib.api.JibContainer in project geronimo-arthur by apache.

the class JibMojo method execute.

@Override
public void execute() {
    final JibContainerBuilder prepared = prepare();
    withExecutor(es -> {
        try {
            final Containerizer containerizer = createContainer();
            final JibContainer container = prepared.containerize(configure(containerizer, es));
            if (propertiesPrefix != null) {
                project.getProperties().setProperty(propertiesPrefix + "image.imageId", container.getImageId().getHash());
                project.getProperties().setProperty(propertiesPrefix + "image.digest", container.getDigest().getHash());
            }
            getLog().info("Built '" + to + "'");
        } catch (final InterruptedException e) {
            Thread.currentThread().interrupt();
        } catch (final RegistryException | IOException | CacheDirectoryCreationException | ExecutionException | InvalidImageReferenceException e) {
            throw new IllegalStateException(e);
        }
    });
}
Also used : CacheDirectoryCreationException(com.google.cloud.tools.jib.api.CacheDirectoryCreationException) JibContainer(com.google.cloud.tools.jib.api.JibContainer) InvalidImageReferenceException(com.google.cloud.tools.jib.api.InvalidImageReferenceException) Containerizer(com.google.cloud.tools.jib.api.Containerizer) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RegistryException(com.google.cloud.tools.jib.api.RegistryException)

Aggregations

JibContainer (com.google.cloud.tools.jib.api.JibContainer)14 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)9 Containerizer (com.google.cloud.tools.jib.api.Containerizer)8 LogEvent (com.google.cloud.tools.jib.api.LogEvent)6 CliLogger (com.google.cloud.tools.jib.cli.logging.CliLogger)6 GlobalConfig (com.google.cloud.tools.jib.plugins.common.globalconfig.GlobalConfig)6 ConsoleLogger (com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger)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 Files (java.nio.file.Files)6 Path (java.nio.file.Path)6 Duration (java.time.Duration)6 Optional (java.util.Optional)6 Callable (java.util.concurrent.Callable)6 Future (java.util.concurrent.Future)6 CommandLine (picocli.CommandLine)6 Verify (com.google.common.base.Verify)4 Collections (java.util.Collections)4