Search in sources :

Example 1 with DevServicesResultBuildItem

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

the class DevServicesConfigBuildStep method setup.

@BuildStep
@Produce(ServiceStartBuildItem.class)
DevServicesLauncherConfigResultBuildItem setup(BuildProducer<RunTimeConfigurationDefaultBuildItem> runtimeConfig, List<DevServicesConfigResultBuildItem> devServicesConfigResultBuildItems, List<DevServicesResultBuildItem> devServicesResultBuildItems, CuratedApplicationShutdownBuildItem shutdownBuildItem) {
    Map<String, String> newProperties = new HashMap<>(devServicesConfigResultBuildItems.stream().collect(Collectors.toMap(DevServicesConfigResultBuildItem::getKey, DevServicesConfigResultBuildItem::getValue)));
    for (DevServicesResultBuildItem resultBuildItem : devServicesResultBuildItems) {
        newProperties.putAll(resultBuildItem.getConfig());
    }
    Config config = ConfigProvider.getConfig();
    // we also check the current config, as the dev service may have been disabled by explicit config
    if (oldConfig != null) {
        for (Map.Entry<String, String> entry : oldConfig.entrySet()) {
            if (!newProperties.containsKey(entry.getKey()) && config.getOptionalValue(entry.getKey(), String.class).isEmpty()) {
                newProperties.put(entry.getKey(), entry.getValue());
            }
        }
    } else {
        shutdownBuildItem.addCloseTask(new Runnable() {

            @Override
            public void run() {
                oldConfig = null;
            }
        }, true);
    }
    for (Map.Entry<String, String> entry : newProperties.entrySet()) {
        runtimeConfig.produce(new RunTimeConfigurationDefaultBuildItem(entry.getKey(), entry.getValue()));
    }
    oldConfig = newProperties;
    return new DevServicesLauncherConfigResultBuildItem(Collections.unmodifiableMap(newProperties));
}
Also used : DevServicesLauncherConfigResultBuildItem(io.quarkus.deployment.builditem.DevServicesLauncherConfigResultBuildItem) DevServicesConfigResultBuildItem(io.quarkus.deployment.builditem.DevServicesConfigResultBuildItem) HashMap(java.util.HashMap) Config(org.eclipse.microprofile.config.Config) DevServicesResultBuildItem(io.quarkus.deployment.builditem.DevServicesResultBuildItem) HashMap(java.util.HashMap) Map(java.util.Map) RunTimeConfigurationDefaultBuildItem(io.quarkus.deployment.builditem.RunTimeConfigurationDefaultBuildItem) Produce(io.quarkus.deployment.annotations.Produce) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 2 with DevServicesResultBuildItem

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

the class DevServicesLambdaProcessor method startEventServer.

@Produce(ServiceStartBuildItem.class)
@BuildStep(onlyIfNot = IsNormal.class)
public void startEventServer(LaunchModeBuildItem launchMode, LambdaConfig config, Optional<EventServerOverrideBuildItem> override, BuildProducer<DevServicesResultBuildItem> devServicePropertiesProducer, BuildProducer<RuntimeApplicationShutdownBuildItem> runtimeApplicationShutdownBuildItemBuildProducer) throws Exception {
    if (!launchMode.getLaunchMode().isDevOrTest())
        return;
    if (legacyTestingEnabled())
        return;
    if (server != null) {
        return;
    }
    Supplier<MockEventServer> supplier = null;
    if (override.isPresent()) {
        supplier = override.get().getServer();
    } else {
        supplier = () -> new MockEventServer();
    }
    server = supplier.get();
    int port = launchMode.getLaunchMode() == LaunchMode.TEST ? config.mockEventServer.testPort : config.mockEventServer.devPort;
    startMode = launchMode.getLaunchMode();
    server.start(port);
    int actualPort = server.getPort();
    String baseUrl = "localhost:" + actualPort + MockEventServer.BASE_PATH;
    Map<String, String> properties = new HashMap<>();
    properties.put(AmazonLambdaApi.QUARKUS_INTERNAL_AWS_LAMBDA_TEST_API, baseUrl);
    if (actualPort != port) {
        String portPropertyValue = String.valueOf(actualPort);
        String portPropertySuffix = launchMode.getLaunchMode() == LaunchMode.TEST ? "test-port" : "dev-port";
        String propName = "quarkus.lambda.mock-event-server." + portPropertySuffix;
        System.setProperty(propName, portPropertyValue);
    }
    devServicePropertiesProducer.produce(new DevServicesResultBuildItem(Feature.AMAZON_LAMBDA.getName(), null, properties));
    Runnable closeTask = () -> {
        if (server != null) {
            try {
                server.close();
            } catch (Throwable e) {
                log.error("Failed to stop the Lambda Mock Event Server", e);
            } finally {
                server = null;
            }
        }
        startMode = null;
        server = null;
    };
    QuarkusClassLoader cl = (QuarkusClassLoader) Thread.currentThread().getContextClassLoader();
    ((QuarkusClassLoader) cl.parent()).addCloseTask(closeTask);
    if (launchMode.isTest()) {
        runtimeApplicationShutdownBuildItemBuildProducer.produce(new RuntimeApplicationShutdownBuildItem(closeTask));
    }
}
Also used : HashMap(java.util.HashMap) DevServicesResultBuildItem(io.quarkus.deployment.builditem.DevServicesResultBuildItem) MockEventServer(io.quarkus.amazon.lambda.runtime.MockEventServer) QuarkusClassLoader(io.quarkus.bootstrap.classloading.QuarkusClassLoader) RuntimeApplicationShutdownBuildItem(io.quarkus.deployment.builditem.RuntimeApplicationShutdownBuildItem) Produce(io.quarkus.deployment.annotations.Produce) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Example 3 with DevServicesResultBuildItem

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

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

the class DevServicesProcessor method buildServiceDescriptions.

private List<DevServiceDescriptionBuildItem> buildServiceDescriptions(DockerStatusBuildItem dockerStatusBuildItem, List<DevServicesResultBuildItem> devServicesResults, Optional<DevServicesLauncherConfigResultBuildItem> devServicesLauncherConfig) {
    // Fetch container infos
    Set<String> containerIds = devServicesResults.stream().map(DevServicesResultBuildItem::getContainerId).filter(Objects::nonNull).collect(Collectors.toSet());
    Map<String, Container> containerInfos = fetchContainerInfos(dockerStatusBuildItem, containerIds);
    // Build descriptions
    Set<String> configKeysFromDevServices = new HashSet<>();
    List<DevServiceDescriptionBuildItem> descriptions = new ArrayList<>();
    for (DevServicesResultBuildItem buildItem : devServicesResults) {
        configKeysFromDevServices.addAll(buildItem.getConfig().keySet());
        descriptions.add(toDevServiceDescription(buildItem, containerInfos.get(buildItem.getContainerId())));
    }
    // Sort descriptions by name
    descriptions.sort(Comparator.comparing(DevServiceDescriptionBuildItem::getName));
    // Add description from other dev service configs as last
    if (devServicesLauncherConfig.isPresent()) {
        Map<String, String> config = new HashMap<>(devServicesLauncherConfig.get().getConfig());
        for (String key : configKeysFromDevServices) {
            config.remove(key);
        }
        if (!config.isEmpty()) {
            descriptions.add(new DevServiceDescriptionBuildItem("Other Dev Services", null, config));
        }
    }
    return descriptions;
}
Also used : Container(com.github.dockerjava.api.model.Container) DevServicesResultBuildItem(io.quarkus.deployment.builditem.DevServicesResultBuildItem) DevServiceDescriptionBuildItem(io.quarkus.deployment.dev.devservices.DevServiceDescriptionBuildItem)

Example 5 with DevServicesResultBuildItem

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

the class KeycloakDevServicesProcessor method startKeycloakContainer.

@BuildStep(onlyIfNot = IsNormal.class, onlyIf = { IsEnabled.class, GlobalDevServicesConfig.Enabled.class })
public DevServicesResultBuildItem startKeycloakContainer(DockerStatusBuildItem dockerStatusBuildItem, BuildProducer<KeycloakDevServicesConfigBuildItem> keycloakBuildItemBuildProducer, List<DevServicesSharedNetworkBuildItem> devServicesSharedNetworkBuildItem, Optional<OidcDevServicesBuildItem> oidcProviderBuildItem, KeycloakBuildTimeConfig config, CuratedApplicationShutdownBuildItem closeBuildItem, LaunchModeBuildItem launchMode, Optional<ConsoleInstalledBuildItem> consoleInstalledBuildItem, LoggingSetupBuildItem loggingSetupBuildItem, GlobalDevServicesConfig devServicesConfig) {
    if (oidcProviderBuildItem.isPresent()) {
        // Dev Services for the alternative OIDC provider are enabled
        return null;
    }
    DevServicesConfig currentDevServicesConfiguration = config.devservices;
    // if not and the Keycloak container has already started we just return
    if (devService != null) {
        boolean restartRequired = !currentDevServicesConfiguration.equals(capturedDevServicesConfiguration);
        if (!restartRequired) {
            FileTime currentRealmFileLastModifiedDate = getRealmFileLastModifiedDate(currentDevServicesConfiguration.realmPath);
            if (currentRealmFileLastModifiedDate != null && !currentRealmFileLastModifiedDate.equals(capturedRealmFileLastModifiedDate)) {
                restartRequired = true;
                capturedRealmFileLastModifiedDate = currentRealmFileLastModifiedDate;
            }
        }
        if (!restartRequired) {
            DevServicesResultBuildItem result = devService.toBuildItem();
            String usersString = result.getConfig().get(OIDC_USERS);
            Map<String, String> users = (usersString == null || usersString.isBlank()) ? Map.of() : Arrays.stream(usersString.split(",")).map(s -> s.split("=")).collect(Collectors.toMap(s -> s[0], s -> s[1]));
            keycloakBuildItemBuildProducer.produce(new KeycloakDevServicesConfigBuildItem(result.getConfig(), Map.of(OIDC_USERS, users)));
            return result;
        }
        try {
            devService.close();
        } catch (Throwable e) {
            LOG.error("Failed to stop Keycloak container", e);
        }
        devService = null;
        capturedDevServicesConfiguration = null;
    }
    capturedDevServicesConfiguration = currentDevServicesConfiguration;
    StartupLogCompressor compressor = new StartupLogCompressor((launchMode.isTest() ? "(test) " : "") + "KeyCloak Dev Services Starting:", consoleInstalledBuildItem, loggingSetupBuildItem);
    if (vertxInstance == null) {
        vertxInstance = Vertx.vertx();
    }
    try {
        RunningDevService newDevService = startContainer(dockerStatusBuildItem, keycloakBuildItemBuildProducer, !devServicesSharedNetworkBuildItem.isEmpty(), devServicesConfig.timeout);
        if (newDevService == null) {
            compressor.close();
            return null;
        }
        devService = newDevService;
        if (first) {
            first = false;
            Runnable closeTask = new Runnable() {

                @Override
                public void run() {
                    if (devService != null) {
                        try {
                            devService.close();
                        } catch (Throwable t) {
                            LOG.error("Failed to stop Keycloak container", t);
                        }
                    }
                    if (vertxInstance != null) {
                        try {
                            vertxInstance.close();
                        } catch (Throwable t) {
                            LOG.error("Failed to close Vertx instance", t);
                        }
                    }
                    first = true;
                    devService = null;
                    capturedDevServicesConfiguration = null;
                    vertxInstance = null;
                    capturedRealmFileLastModifiedDate = null;
                }
            };
            closeBuildItem.addCloseTask(closeTask, true);
        }
        capturedRealmFileLastModifiedDate = getRealmFileLastModifiedDate(capturedDevServicesConfiguration.realmPath);
        if (devService == null) {
            compressor.closeAndDumpCaptured();
        } else {
            compressor.close();
        }
    } catch (Throwable t) {
        compressor.closeAndDumpCaptured();
        throw new RuntimeException(t);
    }
    LOG.info("Dev Services for Keycloak started.");
    return devService.toBuildItem();
}
Also used : StartupLogCompressor(io.quarkus.deployment.console.StartupLogCompressor) DevServicesResultBuildItem(io.quarkus.deployment.builditem.DevServicesResultBuildItem) RunningDevService(io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService) GlobalDevServicesConfig(io.quarkus.deployment.dev.devservices.GlobalDevServicesConfig) FileTime(java.nio.file.attribute.FileTime) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Aggregations

DevServicesResultBuildItem (io.quarkus.deployment.builditem.DevServicesResultBuildItem)5 BuildStep (io.quarkus.deployment.annotations.BuildStep)4 HashMap (java.util.HashMap)3 Produce (io.quarkus.deployment.annotations.Produce)2 RunningDevService (io.quarkus.deployment.builditem.DevServicesResultBuildItem.RunningDevService)2 StartupLogCompressor (io.quarkus.deployment.console.StartupLogCompressor)2 GlobalDevServicesConfig (io.quarkus.deployment.dev.devservices.GlobalDevServicesConfig)2 Map (java.util.Map)2 Container (com.github.dockerjava.api.model.Container)1 MockEventServer (io.quarkus.amazon.lambda.runtime.MockEventServer)1 QuarkusClassLoader (io.quarkus.bootstrap.classloading.QuarkusClassLoader)1 DefaultDataSourceDbKindBuildItem (io.quarkus.datasource.deployment.spi.DefaultDataSourceDbKindBuildItem)1 DevServicesDatasourceConfigurationHandlerBuildItem (io.quarkus.datasource.deployment.spi.DevServicesDatasourceConfigurationHandlerBuildItem)1 DevServicesDatasourceProvider (io.quarkus.datasource.deployment.spi.DevServicesDatasourceProvider)1 DevServicesDatasourceProviderBuildItem (io.quarkus.datasource.deployment.spi.DevServicesDatasourceProviderBuildItem)1 DevServicesDatasourceResultBuildItem (io.quarkus.datasource.deployment.spi.DevServicesDatasourceResultBuildItem)1 DataSourceBuildTimeConfig (io.quarkus.datasource.runtime.DataSourceBuildTimeConfig)1 DataSourcesBuildTimeConfig (io.quarkus.datasource.runtime.DataSourcesBuildTimeConfig)1 IsNormal (io.quarkus.deployment.IsNormal)1 BuildProducer (io.quarkus.deployment.annotations.BuildProducer)1