Search in sources :

Example 1 with ContainerPort

use of com.github.dockerjava.api.model.ContainerPort in project kogito-runtimes by kiegroup.

the class KogitoDevServicesProcessor method startTrustyServiceDevService.

@SuppressWarnings("unused")
@BuildStep(onlyIf = { GlobalDevServicesConfig.Enabled.class, IsDevelopment.class })
public void startTrustyServiceDevService(final DevServicesConfig devServicesConfig, final BuildProducer<SystemPropertyBuildItem> systemProperties, final BuildProducer<TrustyServiceAvailableBuildItem> trustyServiceAvailableBuildItemBuildProducer, final LaunchModeBuildItem launchMode, final KogitoBuildTimeConfig buildTimeConfig, final List<DevServicesSharedNetworkBuildItem> devServicesSharedNetwork, final Optional<ConsoleInstalledBuildItem> consoleInstalled, final CuratedApplicationShutdownBuildItem applicationShutdown, final LoggingSetupBuildItem loggingSetup) {
    LOGGER.info("Docker Containers configuration...");
    DockerClientFactory.lazyClient().listContainersCmd().exec().forEach(c -> {
        LOGGER.debug("----> Image: " + c.getImage());
        if (Objects.nonNull(c.getNames())) {
            Arrays.stream(c.getNames()).forEach(n -> LOGGER.debug(String.format("----> Name: %s", n)));
        }
        if (Objects.nonNull(c.getLabels())) {
            c.getLabels().forEach((key, value) -> LOGGER.debug(String.format("----> Label: [%s]=[%s]", key, value)));
        }
        LOGGER.debug("----> Ports: " + Arrays.stream(c.getPorts()).map(p -> p.getPrivatePort() + ">>" + p.getPublicPort()).collect(Collectors.joining(", ")));
        LOGGER.debug("----> Network: " + (Objects.isNull(c.getNetworkSettings()) ? "" : c.getNetworkSettings().getNetworks().entrySet().stream().map(n -> String.format("%s=%s [%s]", n.getKey(), n.getValue(), n.getValue().getIpAddress())).collect(Collectors.joining(", "))));
    });
    final TrustyServiceDevServiceConfig configuration = getConfiguration(buildTimeConfig);
    if (closeable != null) {
        boolean shouldShutdown = !configuration.equals(cfg);
        if (!shouldShutdown) {
            // Signal the service is (still) available when DevServices may have restarted but the service not
            trustyServiceAvailableBuildItemBuildProducer.produce(new TrustyServiceAvailableBuildItem());
            return;
        }
        shutdownTrustyService();
        cfg = null;
    }
    final StartupLogCompressor compressor = new StartupLogCompressor((launchMode.isTest() ? "(test) " : "") + "Kogito TrustyService DevService starting:", consoleInstalled, loggingSetup);
    TrustyServiceInstance trustyService = null;
    try {
        trustyService = startTrustyService(configuration, devServicesConfig, launchMode, !devServicesSharedNetwork.isEmpty());
        if (trustyService != null) {
            // Signal the service is available
            trustyServiceAvailableBuildItemBuildProducer.produce(new TrustyServiceAvailableBuildItem());
            closeable = trustyService.getCloseable();
        }
        compressor.close();
    } catch (Throwable t) {
        compressor.closeAndDumpCaptured();
        throw new RuntimeException("Failed to start Kogito TrustyService DevServices", t);
    }
    // Discover TrustyService container
    LOGGER.info("Discovering TrustyService instance...");
    DockerClientFactory.lazyClient().listContainersCmd().exec().stream().filter(container -> isTrustyServiceImage(container, configuration)).findFirst().ifPresent(container -> {
        Optional<Integer> port = Optional.empty();
        Optional<String> ipAddress = Optional.empty();
        final ContainerPort[] containerPorts = container.getPorts();
        final ContainerNetworkSettings networkSettings = container.getNetworkSettings();
        if (Objects.nonNull(containerPorts)) {
            port = Arrays.stream(containerPorts).map(ContainerPort::getPrivatePort).filter(Objects::nonNull).findFirst();
        }
        if (Objects.nonNull(networkSettings)) {
            ipAddress = networkSettings.getNetworks().values().stream().map(ContainerNetwork::getIpAddress).filter(Objects::nonNull).findFirst();
        }
        LOGGER.debug(String.format("[TrustyService] Private Port: %s", port.orElse(0)));
        LOGGER.debug(String.format("[TrustyService] IP Address: %s", ipAddress.orElse("<None>")));
        if (ipAddress.isPresent() && port.isPresent()) {
            final String trustyServiceServer = String.format("http://%s:%s", ipAddress.get(), port.get());
            LOGGER.debug(String.format("Setting System Property '%s' to '%s'", KOGITO_TRUSTY_SERVICE, trustyServiceServer));
            systemProperties.produce(new SystemPropertyBuildItem(KOGITO_TRUSTY_SERVICE, trustyServiceServer));
        }
    });
    // Configure the watch dog
    if (first) {
        first = false;
        final Runnable closeTask = () -> {
            if (closeable != null) {
                shutdownTrustyService();
            }
            first = true;
            closeable = null;
            cfg = null;
        };
        applicationShutdown.addCloseTask(closeTask, true);
    }
    cfg = configuration;
    if (trustyService != null && trustyService.isOwner()) {
        LOGGER.info("DevServices for Kogito TrustyService started at {}", trustyService.getUrl());
    }
}
Also used : Arrays(java.util.Arrays) DockerImageName(org.testcontainers.utility.DockerImageName) DevServicesSharedNetworkBuildItem(io.quarkus.deployment.builditem.DevServicesSharedNetworkBuildItem) LoggerFactory(org.slf4j.LoggerFactory) Container(com.github.dockerjava.api.model.Container) SystemPropertyBuildItem(io.quarkus.deployment.builditem.SystemPropertyBuildItem) LaunchMode(io.quarkus.runtime.LaunchMode) ConsoleInstalledBuildItem(io.quarkus.deployment.console.ConsoleInstalledBuildItem) Supplier(java.util.function.Supplier) IsDevelopment(io.quarkus.deployment.IsDevelopment) IsDockerWorking(io.quarkus.deployment.IsDockerWorking) BuildProducer(io.quarkus.deployment.annotations.BuildProducer) BuildStep(io.quarkus.deployment.annotations.BuildStep) QuarkusDataSourcePassword(org.kie.kogito.tracing.decision.quarkus.deployment.DevServicesConfig.Property.QuarkusDataSourcePassword) CuratedApplicationShutdownBuildItem(io.quarkus.deployment.builditem.CuratedApplicationShutdownBuildItem) HibernateOrmDatabaseGeneration(org.kie.kogito.tracing.decision.quarkus.deployment.DevServicesConfig.Property.HibernateOrmDatabaseGeneration) QuarkusDataSourceDbKind(org.kie.kogito.tracing.decision.quarkus.deployment.DevServicesConfig.Property.QuarkusDataSourceDbKind) ContainerNetworkSettings(com.github.dockerjava.api.model.ContainerNetworkSettings) LoggingSetupBuildItem(io.quarkus.deployment.logging.LoggingSetupBuildItem) KafkaBootstrapServers(org.kie.kogito.tracing.decision.quarkus.deployment.DevServicesConfig.Property.KafkaBootstrapServers) TrustyServiceInMemoryContainer(org.kie.kogito.tracing.decision.quarkus.devservices.TrustyServiceInMemoryContainer) Logger(org.slf4j.Logger) StartupLogCompressor(io.quarkus.deployment.console.StartupLogCompressor) ContainerAddress(io.quarkus.devservices.common.ContainerAddress) TrustyServiceAvailableBuildItem(org.kie.kogito.quarkus.addons.common.deployment.TrustyServiceAvailableBuildItem) GlobalDevServicesConfig(io.quarkus.deployment.dev.devservices.GlobalDevServicesConfig) ContainerLocator(io.quarkus.devservices.common.ContainerLocator) Collectors(java.util.stream.Collectors) QuarkusDataSourceJdbcUrl(org.kie.kogito.tracing.decision.quarkus.deployment.DevServicesConfig.Property.QuarkusDataSourceJdbcUrl) QuarkusDataSourceUserName(org.kie.kogito.tracing.decision.quarkus.deployment.DevServicesConfig.Property.QuarkusDataSourceUserName) Objects(java.util.Objects) List(java.util.List) LaunchModeBuildItem(io.quarkus.deployment.builditem.LaunchModeBuildItem) Closeable(java.io.Closeable) Optional(java.util.Optional) ContainerPort(com.github.dockerjava.api.model.ContainerPort) DockerClientFactory(org.testcontainers.DockerClientFactory) KOGITO_TRUSTY_SERVICE(org.kie.kogito.tracing.decision.TrustyConstants.KOGITO_TRUSTY_SERVICE) ContainerNetwork(com.github.dockerjava.api.model.ContainerNetwork) SystemPropertyBuildItem(io.quarkus.deployment.builditem.SystemPropertyBuildItem) TrustyServiceAvailableBuildItem(org.kie.kogito.quarkus.addons.common.deployment.TrustyServiceAvailableBuildItem) StartupLogCompressor(io.quarkus.deployment.console.StartupLogCompressor) ContainerPort(com.github.dockerjava.api.model.ContainerPort) Objects(java.util.Objects) ContainerNetworkSettings(com.github.dockerjava.api.model.ContainerNetworkSettings) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Aggregations

Container (com.github.dockerjava.api.model.Container)1 ContainerNetwork (com.github.dockerjava.api.model.ContainerNetwork)1 ContainerNetworkSettings (com.github.dockerjava.api.model.ContainerNetworkSettings)1 ContainerPort (com.github.dockerjava.api.model.ContainerPort)1 IsDevelopment (io.quarkus.deployment.IsDevelopment)1 IsDockerWorking (io.quarkus.deployment.IsDockerWorking)1 BuildProducer (io.quarkus.deployment.annotations.BuildProducer)1 BuildStep (io.quarkus.deployment.annotations.BuildStep)1 CuratedApplicationShutdownBuildItem (io.quarkus.deployment.builditem.CuratedApplicationShutdownBuildItem)1 DevServicesSharedNetworkBuildItem (io.quarkus.deployment.builditem.DevServicesSharedNetworkBuildItem)1 LaunchModeBuildItem (io.quarkus.deployment.builditem.LaunchModeBuildItem)1 SystemPropertyBuildItem (io.quarkus.deployment.builditem.SystemPropertyBuildItem)1 ConsoleInstalledBuildItem (io.quarkus.deployment.console.ConsoleInstalledBuildItem)1 StartupLogCompressor (io.quarkus.deployment.console.StartupLogCompressor)1 GlobalDevServicesConfig (io.quarkus.deployment.dev.devservices.GlobalDevServicesConfig)1 LoggingSetupBuildItem (io.quarkus.deployment.logging.LoggingSetupBuildItem)1 ContainerAddress (io.quarkus.devservices.common.ContainerAddress)1 ContainerLocator (io.quarkus.devservices.common.ContainerLocator)1 LaunchMode (io.quarkus.runtime.LaunchMode)1 Closeable (java.io.Closeable)1