Search in sources :

Example 1 with DockerStatusBuildItem

use of io.quarkus.deployment.builditem.DockerStatusBuildItem 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 2 with DockerStatusBuildItem

use of io.quarkus.deployment.builditem.DockerStatusBuildItem in project quarkus by quarkusio.

the class KeycloakDevServicesProcessor method startContainer.

private RunningDevService startContainer(DockerStatusBuildItem dockerStatusBuildItem, BuildProducer<KeycloakDevServicesConfigBuildItem> keycloakBuildItemBuildProducer, boolean useSharedNetwork, Optional<Duration> timeout) {
    if (!capturedDevServicesConfiguration.enabled) {
        // explicitly disabled
        LOG.debug("Not starting Dev Services for Keycloak as it has been disabled in the config");
        return null;
    }
    if (!isOidcTenantEnabled()) {
        LOG.debug("Not starting Dev Services for Keycloak as 'quarkus.oidc.tenant.enabled' is false");
        return null;
    }
    if (ConfigUtils.isPropertyPresent(AUTH_SERVER_URL_CONFIG_KEY)) {
        LOG.debug("Not starting Dev Services for Keycloak as 'quarkus.oidc.auth-server-url' has been provided");
        return null;
    }
    if (ConfigUtils.isPropertyPresent(PROVIDER_CONFIG_KEY)) {
        LOG.debug("Not starting Dev Services for Keycloak as 'quarkus.oidc.provider' has been provided");
        return null;
    }
    if (!dockerStatusBuildItem.isDockerAvailable()) {
        LOG.warn("Please configure 'quarkus.oidc.auth-server-url' or get a working docker instance");
        return null;
    }
    final Optional<ContainerAddress> maybeContainerAddress = keycloakDevModeContainerLocator.locateContainer(capturedDevServicesConfiguration.serviceName, capturedDevServicesConfiguration.shared, LaunchMode.current());
    String imageName = capturedDevServicesConfiguration.imageName;
    DockerImageName dockerImageName = DockerImageName.parse(imageName).asCompatibleSubstituteFor(imageName);
    final Supplier<RunningDevService> defaultKeycloakContainerSupplier = () -> {
        QuarkusOidcContainer oidcContainer = new QuarkusOidcContainer(dockerImageName, capturedDevServicesConfiguration.port, useSharedNetwork, capturedDevServicesConfiguration.realmPath, capturedDevServicesConfiguration.serviceName, capturedDevServicesConfiguration.shared, capturedDevServicesConfiguration.javaOpts);
        timeout.ifPresent(oidcContainer::withStartupTimeout);
        oidcContainer.start();
        String internalUrl = startURL(oidcContainer.getHost(), oidcContainer.getPort(), oidcContainer.keycloakX);
        String hostUrl = oidcContainer.useSharedNetwork ? startURL("localhost", oidcContainer.fixedExposedPort.getAsInt(), oidcContainer.keycloakX) : null;
        Map<String, String> configs = prepareConfiguration(keycloakBuildItemBuildProducer, internalUrl, hostUrl, oidcContainer.realmRep, oidcContainer.keycloakX);
        return new RunningDevService(KEYCLOAK_CONTAINER_NAME, oidcContainer.getContainerId(), oidcContainer::close, configs);
    };
    return maybeContainerAddress.map(containerAddress -> {
        // TODO: this probably needs to be addressed
        Map<String, String> configs = prepareConfiguration(keycloakBuildItemBuildProducer, getSharedContainerUrl(containerAddress), getSharedContainerUrl(containerAddress), null, false);
        return new RunningDevService(KEYCLOAK_CONTAINER_NAME, containerAddress.getId(), null, configs);
    }).orElseGet(defaultKeycloakContainerSupplier);
}
Also used : IsNormal(io.quarkus.deployment.IsNormal) Arrays(java.util.Arrays) BindMode(org.testcontainers.containers.BindMode) DockerImageName(org.testcontainers.utility.DockerImageName) DevServicesSharedNetworkBuildItem(io.quarkus.deployment.builditem.DevServicesSharedNetworkBuildItem) URL(java.net.URL) FileTime(java.nio.file.attribute.FileTime) RunningDevService(io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService) ConsoleInstalledBuildItem(io.quarkus.deployment.console.ConsoleInstalledBuildItem) ConfigureUtil(io.quarkus.devservices.common.ConfigureUtil) BuildProducer(io.quarkus.deployment.annotations.BuildProducer) ServerSocket(java.net.ServerSocket) OidcDevServicesBuildItem(io.quarkus.oidc.deployment.devservices.OidcDevServicesBuildItem) CuratedApplicationShutdownBuildItem(io.quarkus.deployment.builditem.CuratedApplicationShutdownBuildItem) Duration(java.time.Duration) Map(java.util.Map) LoggingSetupBuildItem(io.quarkus.deployment.logging.LoggingSetupBuildItem) CredentialRepresentation(org.keycloak.representations.idm.CredentialRepresentation) URI(java.net.URI) Path(java.nio.file.Path) StartupLogCompressor(io.quarkus.deployment.console.StartupLogCompressor) Predicate(java.util.function.Predicate) GlobalDevServicesConfig(io.quarkus.deployment.dev.devservices.GlobalDevServicesConfig) Set(java.util.Set) HttpHeaders(io.vertx.core.http.HttpHeaders) HttpResponse(io.vertx.mutiny.ext.web.client.HttpResponse) ContainerLocator(io.quarkus.devservices.common.ContainerLocator) Collectors(java.util.stream.Collectors) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) UncheckedIOException(java.io.UncheckedIOException) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) List(java.util.List) Buffer(io.vertx.mutiny.core.buffer.Buffer) ConfigProvider(org.eclipse.microprofile.config.ConfigProvider) OidcDevServicesUtils(io.quarkus.oidc.deployment.devservices.OidcDevServicesUtils) Optional(java.util.Optional) DockerStatusBuildItem(io.quarkus.deployment.builditem.DockerStatusBuildItem) ConfigUtils(io.quarkus.runtime.configuration.ConfigUtils) Logger(org.jboss.logging.Logger) LaunchMode(io.quarkus.runtime.LaunchMode) HashMap(java.util.HashMap) OptionalInt(java.util.OptionalInt) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Uni(io.smallrye.mutiny.Uni) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) BuildStep(io.quarkus.deployment.annotations.BuildStep) ConnectException(java.net.ConnectException) GenericContainer(org.testcontainers.containers.GenericContainer) DevServicesResultBuildItem(io.quarkus.deployment.builditem.DevServicesResultBuildItem) RoleRepresentation(org.keycloak.representations.idm.RoleRepresentation) IsEnabled(io.quarkus.oidc.deployment.OidcBuildStep.IsEnabled) Wait(org.testcontainers.containers.wait.strategy.Wait) RolesRepresentation(org.keycloak.representations.idm.RolesRepresentation) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) ContainerAddress(io.quarkus.devservices.common.ContainerAddress) Vertx(io.vertx.core.Vertx) IOException(java.io.IOException) JsonSerialization(org.keycloak.util.JsonSerialization) Paths(java.nio.file.Paths) LaunchModeBuildItem(io.quarkus.deployment.builditem.LaunchModeBuildItem) WebClient(io.vertx.mutiny.ext.web.client.WebClient) InputStream(java.io.InputStream) DockerImageName(org.testcontainers.utility.DockerImageName) RunningDevService(io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ContainerAddress(io.quarkus.devservices.common.ContainerAddress)

Example 3 with DockerStatusBuildItem

use of io.quarkus.deployment.builditem.DockerStatusBuildItem in project quarkus by quarkusio.

the class DevServicesElasticsearchProcessor method startElasticsearch.

private DevServicesResultBuildItem.RunningDevService startElasticsearch(DockerStatusBuildItem dockerStatusBuildItem, ElasticsearchDevServicesBuildTimeConfig config, DevservicesElasticsearchBuildItemsConfiguration buildItemConfig, LaunchModeBuildItem launchMode, boolean useSharedNetwork, Optional<Duration> timeout) throws BuildException {
    if (!config.enabled.orElse(true)) {
        // explicitly disabled
        log.debug("Not starting dev services for Elasticsearch, as it has been disabled in the config.");
        return null;
    }
    for (String hostsConfigProperty : buildItemConfig.hostsConfigProperties) {
        // Check if elasticsearch hosts property is set
        if (ConfigUtils.isPropertyPresent(hostsConfigProperty)) {
            log.debugf("Not starting dev services for Elasticsearch, the %s property is configured.", hostsConfigProperty);
            return null;
        }
    }
    if (!dockerStatusBuildItem.isDockerAvailable()) {
        log.warnf("Docker isn't working, please configure the Elasticsearch hosts property (%s).", displayProperties(buildItemConfig.hostsConfigProperties));
        return null;
    }
    // We only support ELASTIC container for now
    if (buildItemConfig.distribution == DevservicesElasticsearchBuildItem.Distribution.OPENSEARCH) {
        throw new BuildException("Dev services for Elasticsearch didn't support Opensearch", Collections.emptyList());
    }
    // with the image we are about to launch
    if (buildItemConfig.version != null) {
        String containerTag = config.imageName.substring(config.imageName.indexOf(':') + 1);
        if (!containerTag.startsWith(buildItemConfig.version)) {
            throw new BuildException("Dev services for Elasticsearch detected a version mismatch, container image is " + config.imageName + " but the configured version is " + buildItemConfig.version + ". Either configure a different image or disable dev services for Elasticsearch.", Collections.emptyList());
        }
    }
    final Optional<ContainerAddress> maybeContainerAddress = elasticsearchContainerLocator.locateContainer(config.serviceName, config.shared, launchMode.getLaunchMode());
    // Starting the server
    final Supplier<DevServicesResultBuildItem.RunningDevService> defaultElasticsearchSupplier = () -> {
        ElasticsearchContainer container = new ElasticsearchContainer(DockerImageName.parse(config.imageName));
        ConfigureUtil.configureSharedNetwork(container, "elasticsearch");
        if (config.serviceName != null) {
            container.withLabel(DEV_SERVICE_LABEL, config.serviceName);
        }
        if (config.port.isPresent()) {
            container.setPortBindings(List.of(config.port.get() + ":" + config.port.get()));
        }
        timeout.ifPresent(container::withStartupTimeout);
        container.addEnv("ES_JAVA_OPTS", config.javaOpts);
        // Disable security as else we would need to configure it correctly to avoid tons of WARNING in the log
        container.addEnv("xpack.security.enabled", "false");
        container.start();
        return new DevServicesResultBuildItem.RunningDevService(Feature.ELASTICSEARCH_REST_CLIENT_COMMON.getName(), container.getContainerId(), container::close, buildPropertiesMap(buildItemConfig, container.getHttpHostAddress()));
    };
    return maybeContainerAddress.map(containerAddress -> new DevServicesResultBuildItem.RunningDevService(Feature.ELASTICSEARCH_REST_CLIENT_COMMON.getName(), containerAddress.getId(), null, buildPropertiesMap(buildItemConfig, containerAddress.getUrl()))).orElseGet(defaultElasticsearchSupplier);
}
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) HashMap(java.util.HashMap) ConsoleInstalledBuildItem(io.quarkus.deployment.console.ConsoleInstalledBuildItem) Supplier(java.util.function.Supplier) ConfigureUtil(io.quarkus.devservices.common.ConfigureUtil) HashSet(java.util.HashSet) BuildStep(io.quarkus.deployment.annotations.BuildStep) CuratedApplicationShutdownBuildItem(io.quarkus.deployment.builditem.CuratedApplicationShutdownBuildItem) BuildException(io.quarkus.builder.BuildException) Duration(java.time.Duration) Map(java.util.Map) LoggingSetupBuildItem(io.quarkus.deployment.logging.LoggingSetupBuildItem) DevServicesResultBuildItem(io.quarkus.deployment.builditem.DevServicesResultBuildItem) StartupLogCompressor(io.quarkus.deployment.console.StartupLogCompressor) ContainerAddress(io.quarkus.devservices.common.ContainerAddress) GlobalDevServicesConfig(io.quarkus.deployment.dev.devservices.GlobalDevServicesConfig) Set(java.util.Set) ContainerLocator(io.quarkus.devservices.common.ContainerLocator) Feature(io.quarkus.deployment.Feature) List(java.util.List) LaunchModeBuildItem(io.quarkus.deployment.builditem.LaunchModeBuildItem) Optional(java.util.Optional) ElasticsearchContainer(org.testcontainers.elasticsearch.ElasticsearchContainer) DockerStatusBuildItem(io.quarkus.deployment.builditem.DockerStatusBuildItem) Collections(java.util.Collections) DevServicesResultBuildItem(io.quarkus.deployment.builditem.DevServicesResultBuildItem) BuildException(io.quarkus.builder.BuildException) ContainerAddress(io.quarkus.devservices.common.ContainerAddress) ElasticsearchContainer(org.testcontainers.elasticsearch.ElasticsearchContainer)

Example 4 with DockerStatusBuildItem

use of io.quarkus.deployment.builditem.DockerStatusBuildItem in project quarkus by quarkusio.

the class DevServicesDatasourceProcessor method launchDatabases.

@BuildStep(onlyIfNot = IsNormal.class, onlyIf = GlobalDevServicesConfig.Enabled.class)
DevServicesDatasourceResultBuildItem launchDatabases(CurateOutcomeBuildItem curateOutcomeBuildItem, DockerStatusBuildItem dockerStatusBuildItem, List<DefaultDataSourceDbKindBuildItem> installedDrivers, List<DevServicesDatasourceProviderBuildItem> devDBProviders, DataSourcesBuildTimeConfig dataSourceBuildTimeConfig, LaunchModeBuildItem launchMode, List<DevServicesDatasourceConfigurationHandlerBuildItem> configurationHandlerBuildItems, BuildProducer<DevServicesResultBuildItem> devServicesResultBuildItemBuildProducer, Optional<ConsoleInstalledBuildItem> consoleInstalledBuildItem, CuratedApplicationShutdownBuildItem closeBuildItem, LoggingSetupBuildItem loggingSetupBuildItem, GlobalDevServicesConfig globalDevServicesConfig) {
    // if not and the DB's have already started we just return
    if (databases != null) {
        boolean restartRequired = false;
        if (!restartRequired) {
            for (Map.Entry<String, String> entry : cachedProperties.entrySet()) {
                if (!Objects.equals(entry.getValue(), trim(ConfigProvider.getConfig().getOptionalValue(entry.getKey(), String.class).orElse(null)))) {
                    restartRequired = true;
                    break;
                }
            }
        }
        if (!restartRequired) {
            // devservices properties may have been added
            for (var name : ConfigProvider.getConfig().getPropertyNames()) {
                if (name.startsWith("quarkus.datasource.") && name.contains(".devservices.") && !cachedProperties.containsKey(name)) {
                    restartRequired = true;
                    break;
                }
            }
        }
        if (!restartRequired) {
            for (RunningDevService database : databases) {
                devServicesResultBuildItemBuildProducer.produce(database.toBuildItem());
            }
            // keep the previous behaviour of producing DevServicesDatasourceResultBuildItem only when the devservice first starts.
            return null;
        }
        for (Closeable i : databases) {
            try {
                i.close();
            } catch (Throwable e) {
                log.error("Failed to stop database", e);
            }
        }
        databases = null;
        cachedProperties = null;
    }
    DevServicesDatasourceResultBuildItem.DbResult defaultResult;
    Map<String, DevServicesDatasourceResultBuildItem.DbResult> namedResults = new HashMap<>();
    // now we need to figure out if we need to launch some databases
    // note that because we run in dev and test mode only we know the runtime
    // config at build time, as they both execute in the same JVM
    // to keep things simpler for now we are only going to support this for the default datasource
    // support for named datasources will come later
    Map<String, String> propertiesMap = new HashMap<>();
    List<RunningDevService> runningDevServices = new ArrayList<>();
    Map<String, List<DevServicesDatasourceConfigurationHandlerBuildItem>> configHandlersByDbType = configurationHandlerBuildItems.stream().collect(Collectors.toMap(DevServicesDatasourceConfigurationHandlerBuildItem::getDbKind, Collections::singletonList, (configurationHandlerBuildItems1, configurationHandlerBuildItems2) -> {
        List<DevServicesDatasourceConfigurationHandlerBuildItem> ret = new ArrayList<>();
        ret.addAll(configurationHandlerBuildItems1);
        ret.addAll(configurationHandlerBuildItems2);
        return ret;
    }));
    Map<String, DevServicesDatasourceProvider> devDBProviderMap = devDBProviders.stream().collect(Collectors.toMap(DevServicesDatasourceProviderBuildItem::getDatabase, DevServicesDatasourceProviderBuildItem::getDevServicesProvider));
    RunningDevService defaultDevService = startDevDb(null, curateOutcomeBuildItem, installedDrivers, !dataSourceBuildTimeConfig.namedDataSources.isEmpty(), devDBProviderMap, dataSourceBuildTimeConfig.defaultDataSource, configHandlersByDbType, propertiesMap, dockerStatusBuildItem, launchMode.getLaunchMode(), consoleInstalledBuildItem, loggingSetupBuildItem, globalDevServicesConfig);
    if (defaultDevService != null) {
        runningDevServices.add(defaultDevService);
    }
    defaultResult = toDbResult(defaultDevService);
    for (Map.Entry<String, DataSourceBuildTimeConfig> entry : dataSourceBuildTimeConfig.namedDataSources.entrySet()) {
        RunningDevService namedDevService = startDevDb(entry.getKey(), curateOutcomeBuildItem, installedDrivers, true, devDBProviderMap, entry.getValue(), configHandlersByDbType, propertiesMap, dockerStatusBuildItem, launchMode.getLaunchMode(), consoleInstalledBuildItem, loggingSetupBuildItem, globalDevServicesConfig);
        if (namedDevService != null) {
            runningDevServices.add(namedDevService);
            namedResults.put(entry.getKey(), toDbResult(namedDevService));
        }
    }
    if (first) {
        first = false;
        Runnable closeTask = new Runnable() {

            @Override
            public void run() {
                if (databases != null) {
                    for (Closeable i : databases) {
                        try {
                            i.close();
                        } catch (Throwable t) {
                            log.error("Failed to stop database", t);
                        }
                    }
                }
                first = true;
                databases = null;
                cachedProperties = null;
            }
        };
        closeBuildItem.addCloseTask(closeTask, true);
    }
    databases = runningDevServices;
    cachedProperties = propertiesMap;
    for (RunningDevService database : databases) {
        devServicesResultBuildItemBuildProducer.produce(database.toBuildItem());
    }
    return new DevServicesDatasourceResultBuildItem(defaultResult, namedResults);
}
Also used : IsNormal(io.quarkus.deployment.IsNormal) DataSourcesBuildTimeConfig(io.quarkus.datasource.runtime.DataSourcesBuildTimeConfig) DevServicesDatasourceProvider(io.quarkus.datasource.deployment.spi.DevServicesDatasourceProvider) Logger(org.jboss.logging.Logger) LaunchMode(io.quarkus.runtime.LaunchMode) HashMap(java.util.HashMap) DevServicesDatasourceConfigurationHandlerBuildItem(io.quarkus.datasource.deployment.spi.DevServicesDatasourceConfigurationHandlerBuildItem) RunningDevService(io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService) ConsoleInstalledBuildItem(io.quarkus.deployment.console.ConsoleInstalledBuildItem) CurateOutcomeBuildItem(io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem) DevServicesDatasourceResultBuildItem(io.quarkus.datasource.deployment.spi.DevServicesDatasourceResultBuildItem) BuildProducer(io.quarkus.deployment.annotations.BuildProducer) ArrayList(java.util.ArrayList) BuildStep(io.quarkus.deployment.annotations.BuildStep) CuratedApplicationShutdownBuildItem(io.quarkus.deployment.builditem.CuratedApplicationShutdownBuildItem) Map(java.util.Map) LoggingSetupBuildItem(io.quarkus.deployment.logging.LoggingSetupBuildItem) DevServicesResultBuildItem(io.quarkus.deployment.builditem.DevServicesResultBuildItem) StartupLogCompressor(io.quarkus.deployment.console.StartupLogCompressor) GlobalDevServicesConfig(io.quarkus.deployment.dev.devservices.GlobalDevServicesConfig) DataSourceBuildTimeConfig(io.quarkus.datasource.runtime.DataSourceBuildTimeConfig) Collectors(java.util.stream.Collectors) DevServicesDatasourceProviderBuildItem(io.quarkus.datasource.deployment.spi.DevServicesDatasourceProviderBuildItem) Objects(java.util.Objects) List(java.util.List) ConfigProvider(org.eclipse.microprofile.config.ConfigProvider) LaunchModeBuildItem(io.quarkus.deployment.builditem.LaunchModeBuildItem) Closeable(java.io.Closeable) DefaultDataSourceDbKindBuildItem(io.quarkus.datasource.deployment.spi.DefaultDataSourceDbKindBuildItem) Optional(java.util.Optional) DockerStatusBuildItem(io.quarkus.deployment.builditem.DockerStatusBuildItem) Collections(java.util.Collections) HashMap(java.util.HashMap) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) DevServicesDatasourceProvider(io.quarkus.datasource.deployment.spi.DevServicesDatasourceProvider) RunningDevService(io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService) ArrayList(java.util.ArrayList) List(java.util.List) DevServicesDatasourceResultBuildItem(io.quarkus.datasource.deployment.spi.DevServicesDatasourceResultBuildItem) DataSourceBuildTimeConfig(io.quarkus.datasource.runtime.DataSourceBuildTimeConfig) HashMap(java.util.HashMap) Map(java.util.Map) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 5 with DockerStatusBuildItem

use of io.quarkus.deployment.builditem.DockerStatusBuildItem in project quarkus by quarkusio.

the class DevServicesRedisProcessor method startContainer.

private RunningDevService startContainer(DockerStatusBuildItem dockerStatusBuildItem, String connectionName, DevServicesConfig devServicesConfig, LaunchMode launchMode, boolean useSharedNetwork, Optional<Duration> timeout) {
    if (!devServicesConfig.enabled) {
        // explicitly disabled
        log.debug("Not starting devservices for " + (isDefault(connectionName) ? "default redis client" : connectionName) + " as it has been disabled in the config");
        return null;
    }
    String configPrefix = getConfigPrefix(connectionName);
    boolean needToStart = !ConfigUtils.isPropertyPresent(configPrefix + RedisConfig.HOSTS_CONFIG_NAME);
    if (!needToStart) {
        log.debug("Not starting devservices for " + (isDefault(connectionName) ? "default redis client" : connectionName) + " as hosts have been provided");
        return null;
    }
    if (!dockerStatusBuildItem.isDockerAvailable()) {
        log.warn("Please configure quarkus.redis.hosts for " + (isDefault(connectionName) ? "default redis client" : connectionName) + " or get a working docker instance");
        return null;
    }
    DockerImageName dockerImageName = DockerImageName.parse(devServicesConfig.imageName.orElse(REDIS_6_ALPINE)).asCompatibleSubstituteFor(REDIS_6_ALPINE);
    Supplier<RunningDevService> defaultRedisServerSupplier = () -> {
        QuarkusPortRedisContainer redisContainer = new QuarkusPortRedisContainer(dockerImageName, devServicesConfig.port, launchMode == DEVELOPMENT ? devServicesConfig.serviceName : null, useSharedNetwork);
        timeout.ifPresent(redisContainer::withStartupTimeout);
        redisContainer.start();
        String redisHost = REDIS_SCHEME + redisContainer.getHost() + ":" + redisContainer.getPort();
        return new RunningDevService(Feature.REDIS_CLIENT.getName(), redisContainer.getContainerId(), redisContainer::close, configPrefix + RedisConfig.HOSTS_CONFIG_NAME, redisHost);
    };
    return redisContainerLocator.locateContainer(devServicesConfig.serviceName, devServicesConfig.shared, launchMode).map(containerAddress -> {
        String redisUrl = REDIS_SCHEME + containerAddress.getUrl();
        return new RunningDevService(Feature.REDIS_CLIENT.getName(), containerAddress.getId(), null, configPrefix + RedisConfig.HOSTS_CONFIG_NAME, redisUrl);
    }).orElseGet(defaultRedisServerSupplier);
}
Also used : IsNormal(io.quarkus.deployment.IsNormal) DevServiceConfiguration(io.quarkus.redis.client.deployment.RedisBuildTimeConfig.DevServiceConfiguration) ConfigUtils(io.quarkus.runtime.configuration.ConfigUtils) DockerImageName(org.testcontainers.utility.DockerImageName) DevServicesSharedNetworkBuildItem(io.quarkus.deployment.builditem.DevServicesSharedNetworkBuildItem) RedisClientUtil.isDefault(io.quarkus.redis.client.runtime.RedisClientUtil.isDefault) Logger(org.jboss.logging.Logger) LaunchMode(io.quarkus.runtime.LaunchMode) HashMap(java.util.HashMap) RunningDevService(io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService) ConsoleInstalledBuildItem(io.quarkus.deployment.console.ConsoleInstalledBuildItem) OptionalInt(java.util.OptionalInt) Supplier(java.util.function.Supplier) ConfigureUtil(io.quarkus.devservices.common.ConfigureUtil) ArrayList(java.util.ArrayList) BuildStep(io.quarkus.deployment.annotations.BuildStep) CuratedApplicationShutdownBuildItem(io.quarkus.deployment.builditem.CuratedApplicationShutdownBuildItem) Duration(java.time.Duration) Map(java.util.Map) LoggingSetupBuildItem(io.quarkus.deployment.logging.LoggingSetupBuildItem) GenericContainer(org.testcontainers.containers.GenericContainer) DevServicesResultBuildItem(io.quarkus.deployment.builditem.DevServicesResultBuildItem) StartupLogCompressor(io.quarkus.deployment.console.StartupLogCompressor) GlobalDevServicesConfig(io.quarkus.deployment.dev.devservices.GlobalDevServicesConfig) ContainerLocator(io.quarkus.devservices.common.ContainerLocator) Collectors(java.util.stream.Collectors) Feature(io.quarkus.deployment.Feature) List(java.util.List) LaunchModeBuildItem(io.quarkus.deployment.builditem.LaunchModeBuildItem) Closeable(java.io.Closeable) Entry(java.util.Map.Entry) Optional(java.util.Optional) RedisClientUtil(io.quarkus.redis.client.runtime.RedisClientUtil) RedisConfig(io.quarkus.redis.client.runtime.RedisConfig) DEVELOPMENT(io.quarkus.runtime.LaunchMode.DEVELOPMENT) DockerStatusBuildItem(io.quarkus.deployment.builditem.DockerStatusBuildItem) DockerImageName(org.testcontainers.utility.DockerImageName) RunningDevService(io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService)

Aggregations

IsNormal (io.quarkus.deployment.IsNormal)5 BuildStep (io.quarkus.deployment.annotations.BuildStep)5 CuratedApplicationShutdownBuildItem (io.quarkus.deployment.builditem.CuratedApplicationShutdownBuildItem)5 DevServicesResultBuildItem (io.quarkus.deployment.builditem.DevServicesResultBuildItem)5 DockerStatusBuildItem (io.quarkus.deployment.builditem.DockerStatusBuildItem)5 LaunchModeBuildItem (io.quarkus.deployment.builditem.LaunchModeBuildItem)5 ConsoleInstalledBuildItem (io.quarkus.deployment.console.ConsoleInstalledBuildItem)5 StartupLogCompressor (io.quarkus.deployment.console.StartupLogCompressor)5 GlobalDevServicesConfig (io.quarkus.deployment.dev.devservices.GlobalDevServicesConfig)5 LoggingSetupBuildItem (io.quarkus.deployment.logging.LoggingSetupBuildItem)5 HashMap (java.util.HashMap)5 List (java.util.List)5 Map (java.util.Map)5 Optional (java.util.Optional)5 Logger (org.jboss.logging.Logger)5 RunningDevService (io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService)4 DevServicesSharedNetworkBuildItem (io.quarkus.deployment.builditem.DevServicesSharedNetworkBuildItem)4 ConfigureUtil (io.quarkus.devservices.common.ConfigureUtil)4 ContainerLocator (io.quarkus.devservices.common.ContainerLocator)4 LaunchMode (io.quarkus.runtime.LaunchMode)4