Search in sources :

Example 11 with LogEvent

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

use of com.google.cloud.tools.jib.api.LogEvent 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)

Example 13 with LogEvent

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

the class FailoverHttpClientTest method testRetries.

@Test
public void testRetries() throws IOException {
    HttpServer server = HttpServer.create(new InetSocketAddress(0), 1);
    AtomicBoolean failed = new AtomicBoolean();
    server.createContext("/").setHandler(exchange -> exchange.sendResponseHeaders(failed.compareAndSet(false, true) ? 123 : 200, -1));
    try {
        server.start();
        int port = server.getAddress().getPort();
        List<LogEvent> events = new ArrayList<>();
        int returnCode = new FailoverHttpClient(true, true, events::add).get(new URL("http://localhost:" + port), Request.builder().build()).getStatusCode();
        assertThat(returnCode).isEqualTo(200);
        assertThat(failed.get()).isTrue();
        assertThat(events).containsExactly(LogEvent.warn("GET http://localhost:" + port + " failed and will be retried"));
    } finally {
        server.stop(0);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LogEvent(com.google.cloud.tools.jib.api.LogEvent) InetSocketAddress(java.net.InetSocketAddress) HttpServer(com.sun.net.httpserver.HttpServer) ArrayList(java.util.ArrayList) URL(java.net.URL) Test(org.junit.Test)

Example 14 with LogEvent

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

the class LogEventTest method verifyNextLogEvent.

private void verifyNextLogEvent(Level level, String message) {
    Assert.assertFalse(receivedLogEvents.isEmpty());
    LogEvent logEvent = receivedLogEvents.poll();
    Assert.assertEquals(level, logEvent.getLevel());
    Assert.assertEquals(message, logEvent.getMessage());
}
Also used : LogEvent(com.google.cloud.tools.jib.api.LogEvent)

Example 15 with LogEvent

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

the class LogEventTest method verifyNextLogEvent.

private void verifyNextLogEvent(Level level, String message) {
    Assert.assertFalse(receivedLogEvents.isEmpty());
    LogEvent logEvent = receivedLogEvents.poll();
    Assert.assertEquals(level, logEvent.getLevel());
    Assert.assertEquals(message, logEvent.getMessage());
}
Also used : LogEvent(com.google.cloud.tools.jib.api.LogEvent)

Aggregations

LogEvent (com.google.cloud.tools.jib.api.LogEvent)20 Containerizer (com.google.cloud.tools.jib.api.Containerizer)14 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)12 Files (java.nio.file.Files)12 Path (java.nio.file.Path)12 Optional (java.util.Optional)12 List (java.util.List)10 InvalidImageReferenceException (com.google.cloud.tools.jib.api.InvalidImageReferenceException)8 RegistryImage (com.google.cloud.tools.jib.api.RegistryImage)8 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)8 ConsoleLogger (com.google.cloud.tools.jib.plugins.common.logging.ConsoleLogger)8 Paths (java.nio.file.Paths)8 Collections (java.util.Collections)7 CacheDirectoryCreationException (com.google.cloud.tools.jib.api.CacheDirectoryCreationException)6 JavaContainerBuilder (com.google.cloud.tools.jib.api.JavaContainerBuilder)6 Jib (com.google.cloud.tools.jib.api.Jib)6 JibContainerBuilderTestHelper (com.google.cloud.tools.jib.api.JibContainerBuilderTestHelper)6 ContainerBuildPlan (com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)6 FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)6 FileEntry (com.google.cloud.tools.jib.api.buildplan.FileEntry)6