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));
}
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));
}
}
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);
}
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;
}
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();
}
Aggregations