Search in sources :

Example 1 with RunningDevService

use of io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService in project quarkus by quarkusio.

the class RabbitMQDevServicesProcessor method startRabbitMQDevService.

@BuildStep(onlyIfNot = IsNormal.class, onlyIf = GlobalDevServicesConfig.Enabled.class)
public DevServicesResultBuildItem startRabbitMQDevService(DockerStatusBuildItem dockerStatusBuildItem, LaunchModeBuildItem launchMode, RabbitMQBuildTimeConfig rabbitmqClientBuildTimeConfig, Optional<ConsoleInstalledBuildItem> consoleInstalledBuildItem, LoggingSetupBuildItem loggingSetupBuildItem, GlobalDevServicesConfig devServicesConfig) {
    RabbitMQDevServiceCfg configuration = getConfiguration(rabbitmqClientBuildTimeConfig);
    if (devService != null) {
        boolean shouldShutdownTheBroker = !configuration.equals(cfg);
        if (!shouldShutdownTheBroker) {
            return devService.toBuildItem();
        }
        shutdownBroker();
        cfg = null;
    }
    StartupLogCompressor compressor = new StartupLogCompressor((launchMode.isTest() ? "(test) " : "") + "RabbitMQ Dev Services Starting:", consoleInstalledBuildItem, loggingSetupBuildItem);
    try {
        RunningDevService newDevService = startRabbitMQBroker(dockerStatusBuildItem, configuration, launchMode, devServicesConfig.timeout);
        if (newDevService != null) {
            devService = newDevService;
            Map<String, String> config = devService.getConfig();
            if (devService.isOwner()) {
                log.info("Dev Services for RabbitMQ started.");
                log.infof("Other Quarkus applications in dev mode will find the " + "broker automatically. For Quarkus applications in production mode, you can connect to" + " this by starting your application with -Drabbitmq-host=%s -Drabbitmq-port=%s -Drabbitmq-username=%s -Drabbitmq-password=%s", config.get(RABBITMQ_HOST_PROP), config.get(RABBITMQ_PORT_PROP), config.get(RABBITMQ_USERNAME_PROP), config.get(RABBITMQ_PASSWORD_PROP));
            }
        }
        if (devService == null) {
            compressor.closeAndDumpCaptured();
        } else {
            compressor.close();
        }
    } catch (Throwable t) {
        compressor.closeAndDumpCaptured();
        throw new RuntimeException(t);
    }
    if (devService == null) {
        return null;
    }
    // Configure the watch dog
    if (first) {
        first = false;
        Runnable closeTask = () -> {
            if (devService != null) {
                shutdownBroker();
                log.info("Dev Services for RabbitMQ shut down.");
            }
            first = true;
            devService = null;
            cfg = null;
        };
        QuarkusClassLoader cl = (QuarkusClassLoader) Thread.currentThread().getContextClassLoader();
        ((QuarkusClassLoader) cl.parent()).addCloseTask(closeTask);
    }
    cfg = configuration;
    return devService.toBuildItem();
}
Also used : StartupLogCompressor(io.quarkus.deployment.console.StartupLogCompressor) RunningDevService(io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService) QuarkusClassLoader(io.quarkus.bootstrap.classloading.QuarkusClassLoader) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 2 with RunningDevService

use of io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService in project quarkus by quarkusio.

the class InfinispanDevServiceProcessor method startInfinispanContainers.

@BuildStep(onlyIfNot = IsNormal.class, onlyIf = { GlobalDevServicesConfig.Enabled.class })
public List<DevServicesResultBuildItem> startInfinispanContainers(LaunchModeBuildItem launchMode, DockerStatusBuildItem dockerStatusBuildItem, List<DevServicesSharedNetworkBuildItem> devServicesSharedNetworkBuildItem, InfinispanClientDevServiceBuildTimeConfig config, Optional<ConsoleInstalledBuildItem> consoleInstalledBuildItem, CuratedApplicationShutdownBuildItem closeBuildItem, LoggingSetupBuildItem loggingSetupBuildItem, GlobalDevServicesConfig devServicesConfig) {
    // if not and the Infinispan containers have already started we just return
    if (devServices != null) {
        boolean restartRequired = !config.devService.equals(capturedDevServicesConfiguration);
        if (!restartRequired) {
            return devServices.stream().map(RunningDevService::toBuildItem).collect(Collectors.toList());
        }
        for (Closeable closeable : devServices) {
            try {
                closeable.close();
            } catch (Throwable e) {
                log.error("Failed to stop infinispan container", e);
            }
        }
        devServices = null;
        capturedDevServicesConfiguration = null;
    }
    capturedDevServicesConfiguration = config.devService;
    List<RunningDevService> newDevServices = new ArrayList<>();
    StartupLogCompressor compressor = new StartupLogCompressor((launchMode.isTest() ? "(test) " : "") + "Infinispan Dev Services Starting:", consoleInstalledBuildItem, loggingSetupBuildItem);
    try {
        RunningDevService devService = startContainer(dockerStatusBuildItem, config.devService.devservices, launchMode.getLaunchMode(), !devServicesSharedNetworkBuildItem.isEmpty(), devServicesConfig.timeout);
        if (devService == null) {
            compressor.closeAndDumpCaptured();
            return null;
        }
        newDevServices.add(devService);
        log.infof("The infinispan server is ready to accept connections on %s", devService.getConfig().get(getConfigPrefix() + "server-list"));
        compressor.close();
    } catch (Throwable t) {
        compressor.closeAndDumpCaptured();
        throw new RuntimeException(t);
    }
    devServices = newDevServices;
    if (first) {
        first = false;
        Runnable closeTask = () -> {
            if (devServices != null) {
                for (Closeable closeable : devServices) {
                    try {
                        closeable.close();
                    } catch (Throwable t) {
                        log.error("Failed to stop infinispan", t);
                    }
                }
            }
            first = true;
            devServices = null;
            capturedDevServicesConfiguration = null;
        };
        closeBuildItem.addCloseTask(closeTask, true);
    }
    return devServices.stream().map(RunningDevService::toBuildItem).collect(Collectors.toList());
}
Also used : StartupLogCompressor(io.quarkus.deployment.console.StartupLogCompressor) Closeable(java.io.Closeable) RunningDevService(io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService) ArrayList(java.util.ArrayList) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 3 with RunningDevService

use of io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService in project quarkus by quarkusio.

the class DevServicesKafkaProcessor method startKafka.

private RunningDevService startKafka(DockerStatusBuildItem dockerStatusBuildItem, KafkaDevServiceCfg config, LaunchModeBuildItem launchMode, boolean useSharedNetwork, Optional<Duration> timeout) {
    if (!config.devServicesEnabled) {
        // explicitly disabled
        log.debug("Not starting dev services for Kafka, as it has been disabled in the config.");
        return null;
    }
    // Check if kafka.bootstrap.servers is set
    if (ConfigUtils.isPropertyPresent(KAFKA_BOOTSTRAP_SERVERS)) {
        log.debug("Not starting dev services for Kafka, the kafka.bootstrap.servers is configured.");
        return null;
    }
    // Verify that we have kafka channels without bootstrap.servers
    if (!hasKafkaChannelWithoutBootstrapServers()) {
        log.debug("Not starting dev services for Kafka, all the channels are configured.");
        return null;
    }
    if (!dockerStatusBuildItem.isDockerAvailable()) {
        log.warn("Docker isn't working, please configure the Kafka bootstrap servers property (kafka.bootstrap.servers).");
        return null;
    }
    final Optional<ContainerAddress> maybeContainerAddress = kafkaContainerLocator.locateContainer(config.serviceName, config.shared, launchMode.getLaunchMode());
    // Starting the broker
    final Supplier<RunningDevService> defaultKafkaBrokerSupplier = () -> {
        if (config.imageName.contains("strimzi")) {
            StrimziKafkaContainer container = new StrimziKafkaContainer(config.imageName).withBrokerId(1).withKraft().waitForRunning();
            ConfigureUtil.configureSharedNetwork(container, "kafka");
            if (config.serviceName != null) {
                container.withLabel(DevServicesKafkaProcessor.DEV_SERVICE_LABEL, config.serviceName);
            }
            if (config.fixedExposedPort != 0) {
                container.withPort(config.fixedExposedPort);
            }
            timeout.ifPresent(container::withStartupTimeout);
            container.start();
            return new RunningDevService(Feature.KAFKA_CLIENT.getName(), container.getContainerId(), container::close, KAFKA_BOOTSTRAP_SERVERS, container.getBootstrapServers());
        } else {
            RedPandaKafkaContainer container = new RedPandaKafkaContainer(DockerImageName.parse(config.imageName), config.fixedExposedPort, launchMode.getLaunchMode() == LaunchMode.DEVELOPMENT ? config.serviceName : null, useSharedNetwork, config.redpanda);
            timeout.ifPresent(container::withStartupTimeout);
            container.start();
            return new RunningDevService(Feature.KAFKA_CLIENT.getName(), container.getContainerId(), container::close, KAFKA_BOOTSTRAP_SERVERS, container.getBootstrapServers());
        }
    };
    return maybeContainerAddress.map(containerAddress -> new RunningDevService(Feature.KAFKA_CLIENT.getName(), containerAddress.getId(), null, KAFKA_BOOTSTRAP_SERVERS, containerAddress.getUrl())).orElseGet(defaultKafkaBrokerSupplier);
}
Also used : IsNormal(io.quarkus.deployment.IsNormal) ConfigUtils(io.quarkus.runtime.configuration.ConfigUtils) DockerImageName(org.testcontainers.utility.DockerImageName) DevServicesSharedNetworkBuildItem(io.quarkus.deployment.builditem.DevServicesSharedNetworkBuildItem) Logger(org.jboss.logging.Logger) TimeoutException(java.util.concurrent.TimeoutException) LaunchMode(io.quarkus.runtime.LaunchMode) HashMap(java.util.HashMap) RunningDevService(io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService) ConsoleInstalledBuildItem(io.quarkus.deployment.console.ConsoleInstalledBuildItem) Supplier(java.util.function.Supplier) ConfigureUtil(io.quarkus.devservices.common.ConfigureUtil) AdminClient(org.apache.kafka.clients.admin.AdminClient) BuildStep(io.quarkus.deployment.annotations.BuildStep) CuratedApplicationShutdownBuildItem(io.quarkus.deployment.builditem.CuratedApplicationShutdownBuildItem) Duration(java.time.Duration) Map(java.util.Map) CreateTopicsResult(org.apache.kafka.clients.admin.CreateTopicsResult) LoggingSetupBuildItem(io.quarkus.deployment.logging.LoggingSetupBuildItem) DevServicesResultBuildItem(io.quarkus.deployment.builditem.DevServicesResultBuildItem) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) KafkaAdminClient(org.apache.kafka.clients.admin.KafkaAdminClient) StartupLogCompressor(io.quarkus.deployment.console.StartupLogCompressor) ContainerAddress(io.quarkus.devservices.common.ContainerAddress) StrimziKafkaContainer(io.strimzi.test.container.StrimziKafkaContainer) AdminClientConfig(org.apache.kafka.clients.admin.AdminClientConfig) GlobalDevServicesConfig(io.quarkus.deployment.dev.devservices.GlobalDevServicesConfig) NewTopic(org.apache.kafka.clients.admin.NewTopic) Set(java.util.Set) ContainerLocator(io.quarkus.devservices.common.ContainerLocator) Config(org.eclipse.microprofile.config.Config) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Feature(io.quarkus.deployment.Feature) List(java.util.List) ConfigProvider(org.eclipse.microprofile.config.ConfigProvider) LaunchModeBuildItem(io.quarkus.deployment.builditem.LaunchModeBuildItem) Optional(java.util.Optional) DockerStatusBuildItem(io.quarkus.deployment.builditem.DockerStatusBuildItem) StrimziKafkaContainer(io.strimzi.test.container.StrimziKafkaContainer) RunningDevService(io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService) ContainerAddress(io.quarkus.devservices.common.ContainerAddress)

Example 4 with RunningDevService

use of io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService in project quarkus by quarkusio.

the class DevServicesMongoProcessor method startMongo.

private RunningDevService startMongo(DockerStatusBuildItem dockerStatusBuildItem, String connectionName, CapturedProperties capturedProperties, boolean useSharedNetwork, Optional<Duration> timeout) {
    if (!capturedProperties.devServicesEnabled) {
        // explicitly disabled
        log.debug("Not starting devservices for " + (isDefault(connectionName) ? "default datasource" : connectionName) + " as it has been disabled in the config");
        return null;
    }
    String configPrefix = getConfigPrefix(connectionName);
    // TODO: do we need to check the hosts as well?
    boolean needToStart = !ConfigUtils.isPropertyPresent(configPrefix + "connection-string");
    if (!needToStart) {
        // a connection string has been provided
        log.debug("Not starting devservices for " + (isDefault(connectionName) ? "default datasource" : connectionName) + " as a connection string has been provided");
        return null;
    }
    if (!dockerStatusBuildItem.isDockerAvailable()) {
        log.warn("Please configure datasource URL for " + (isDefault(connectionName) ? "default datasource" : connectionName) + " or get a working docker instance");
        return null;
    }
    MongoDBContainer mongoDBContainer;
    if (capturedProperties.imageName != null) {
        mongoDBContainer = new QuarkusMongoDBContainer(DockerImageName.parse(capturedProperties.imageName).asCompatibleSubstituteFor("mongo"), capturedProperties.fixedExposedPort, useSharedNetwork);
    } else {
        mongoDBContainer = new QuarkusMongoDBContainer(capturedProperties.fixedExposedPort, useSharedNetwork);
    }
    timeout.ifPresent(mongoDBContainer::withStartupTimeout);
    mongoDBContainer.start();
    Optional<String> databaseName = ConfigProvider.getConfig().getOptionalValue(configPrefix + "database", String.class);
    String effectiveURL = databaseName.map(mongoDBContainer::getReplicaSetUrl).orElse(mongoDBContainer.getReplicaSetUrl());
    if ((capturedProperties.connectionProperties != null) && !capturedProperties.connectionProperties.isEmpty()) {
        effectiveURL = effectiveURL + "?" + URLEncodedUtils.format(capturedProperties.connectionProperties.entrySet().stream().map(e -> new BasicNameValuePair(e.getKey(), e.getValue())).collect(Collectors.toList()), StandardCharsets.UTF_8);
    }
    return new RunningDevService(Feature.MONGODB_CLIENT.getName(), mongoDBContainer.getContainerId(), mongoDBContainer::close, getConfigPrefix(connectionName) + "connection-string", effectiveURL);
}
Also used : MongoDBContainer(org.testcontainers.containers.MongoDBContainer) BasicNameValuePair(com.github.dockerjava.zerodep.shaded.org.apache.hc.core5.http.message.BasicNameValuePair) RunningDevService(io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService)

Example 5 with RunningDevService

use of io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService in project quarkus by quarkusio.

the class DevServicesMongoProcessor method startMongo.

@BuildStep(onlyIfNot = IsNormal.class, onlyIf = GlobalDevServicesConfig.Enabled.class)
public List<DevServicesResultBuildItem> startMongo(List<MongoConnectionNameBuildItem> mongoConnections, DockerStatusBuildItem dockerStatusBuildItem, MongoClientBuildTimeConfig mongoClientBuildTimeConfig, List<DevServicesSharedNetworkBuildItem> devServicesSharedNetworkBuildItem, Optional<ConsoleInstalledBuildItem> consoleInstalledBuildItem, CuratedApplicationShutdownBuildItem closeBuildItem, LaunchModeBuildItem launchMode, LoggingSetupBuildItem loggingSetupBuildItem, GlobalDevServicesConfig globalDevServicesConfig) {
    List<String> connectionNames = new ArrayList<>(mongoConnections.size());
    for (MongoConnectionNameBuildItem mongoConnection : mongoConnections) {
        connectionNames.add(mongoConnection.getName());
    }
    // TODO: handle named connections as well
    if (connectionNames.size() != 1) {
        return null;
    }
    if (!isDefault(connectionNames.get(0))) {
        return null;
    }
    Map<String, CapturedProperties> currentCapturedProperties = captureProperties(connectionNames, mongoClientBuildTimeConfig);
    // if not and the DB's have already started we just return
    if (devServices != null) {
        boolean restartRequired = !currentCapturedProperties.equals(capturedProperties);
        if (!restartRequired) {
            return devServices.stream().map(RunningDevService::toBuildItem).collect(Collectors.toList());
        }
        for (Closeable i : devServices) {
            try {
                i.close();
            } catch (Throwable e) {
                log.error("Failed to stop database", e);
            }
        }
        devServices = null;
        capturedProperties = null;
    }
    List<RunningDevService> newDevServices = new ArrayList<>(mongoConnections.size());
    // TODO: we need to go through each connection
    String connectionName = connectionNames.get(0);
    RunningDevService devService;
    StartupLogCompressor compressor = new StartupLogCompressor((launchMode.isTest() ? "(test) " : "") + "Mongo Dev Services Starting:", consoleInstalledBuildItem, loggingSetupBuildItem);
    try {
        devService = startMongo(dockerStatusBuildItem, connectionName, currentCapturedProperties.get(connectionName), !devServicesSharedNetworkBuildItem.isEmpty(), globalDevServicesConfig.timeout);
        if (devService == null) {
            compressor.closeAndDumpCaptured();
        } else {
            compressor.close();
        }
    } catch (Throwable t) {
        compressor.closeAndDumpCaptured();
        throw new RuntimeException(t);
    }
    if (devService != null) {
        newDevServices.add(devService);
    }
    if (first) {
        first = false;
        Runnable closeTask = new Runnable() {

            @Override
            public void run() {
                if (devServices != null) {
                    for (Closeable i : devServices) {
                        try {
                            i.close();
                        } catch (Throwable t) {
                            log.error("Failed to stop database", t);
                        }
                    }
                }
                first = true;
                devServices = null;
                capturedProperties = null;
            }
        };
        closeBuildItem.addCloseTask(closeTask, true);
    }
    devServices = newDevServices;
    capturedProperties = currentCapturedProperties;
    return devServices.stream().map(RunningDevService::toBuildItem).collect(Collectors.toList());
}
Also used : Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) StartupLogCompressor(io.quarkus.deployment.console.StartupLogCompressor) RunningDevService(io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Aggregations

RunningDevService (io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService)12 StartupLogCompressor (io.quarkus.deployment.console.StartupLogCompressor)11 BuildStep (io.quarkus.deployment.annotations.BuildStep)10 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 DevServicesResultBuildItem (io.quarkus.deployment.builditem.DevServicesResultBuildItem)5 GlobalDevServicesConfig (io.quarkus.deployment.dev.devservices.GlobalDevServicesConfig)5 Closeable (java.io.Closeable)5 IsNormal (io.quarkus.deployment.IsNormal)4 CuratedApplicationShutdownBuildItem (io.quarkus.deployment.builditem.CuratedApplicationShutdownBuildItem)4 DockerStatusBuildItem (io.quarkus.deployment.builditem.DockerStatusBuildItem)4 LaunchModeBuildItem (io.quarkus.deployment.builditem.LaunchModeBuildItem)4 ConsoleInstalledBuildItem (io.quarkus.deployment.console.ConsoleInstalledBuildItem)4 LoggingSetupBuildItem (io.quarkus.deployment.logging.LoggingSetupBuildItem)4 LaunchMode (io.quarkus.runtime.LaunchMode)4 List (java.util.List)4 Map (java.util.Map)4 Optional (java.util.Optional)4 Collectors (java.util.stream.Collectors)4 Logger (org.jboss.logging.Logger)4