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