|
| 1 | +package io.quarkus.narayana.lra.deployment.devservice; |
| 2 | + |
| 3 | +import static io.quarkus.devservices.common.ContainerLocator.locateContainerWithLabels; |
| 4 | + |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +import org.jboss.logging.Logger; |
| 9 | +import org.testcontainers.utility.DockerImageName; |
| 10 | + |
| 11 | +import io.quarkus.deployment.Feature; |
| 12 | +import io.quarkus.deployment.IsDevServicesSupportedByLaunchMode; |
| 13 | +import io.quarkus.deployment.annotations.BuildStep; |
| 14 | +import io.quarkus.deployment.annotations.BuildSteps; |
| 15 | +import io.quarkus.deployment.builditem.DevServicesComposeProjectBuildItem; |
| 16 | +import io.quarkus.deployment.builditem.DevServicesResultBuildItem; |
| 17 | +import io.quarkus.deployment.builditem.DevServicesSharedNetworkBuildItem; |
| 18 | +import io.quarkus.deployment.builditem.DockerStatusBuildItem; |
| 19 | +import io.quarkus.deployment.builditem.LaunchModeBuildItem; |
| 20 | +import io.quarkus.deployment.builditem.Startable; |
| 21 | +import io.quarkus.deployment.dev.devservices.DevServicesConfig; |
| 22 | +import io.quarkus.devservices.common.ComposeLocator; |
| 23 | +import io.quarkus.devservices.common.ContainerLocator; |
| 24 | +import io.quarkus.narayana.lra.deployment.LRABuildTimeConfiguration; |
| 25 | +import io.quarkus.runtime.configuration.ConfigUtils; |
| 26 | + |
| 27 | +/** |
| 28 | + * Start Narayana LRA coordinator as a dev service. |
| 29 | + */ |
| 30 | +@BuildSteps(onlyIf = { IsDevServicesSupportedByLaunchMode.class, DevServicesConfig.Enabled.class }) |
| 31 | +public class DevServicesLRAProcessor { |
| 32 | + |
| 33 | + private static final Logger log = Logger.getLogger(DevServicesLRAProcessor.class); |
| 34 | + private static final String LRA_COORDINATOR_URL_PROPERTY = "quarkus.lra.coordinator-url"; |
| 35 | + |
| 36 | + static final String DEV_SERVICE_LABEL = "quarkus-dev-service-lra-coordinator"; |
| 37 | + static final int LRA_COORDINATOR_CONTAINER_PORT = 8080; |
| 38 | + |
| 39 | + private static final ContainerLocator lraCoordinatorContainerLocator = locateContainerWithLabels( |
| 40 | + LRA_COORDINATOR_CONTAINER_PORT, DEV_SERVICE_LABEL); |
| 41 | + |
| 42 | + @BuildStep |
| 43 | + public DevServicesResultBuildItem lraCoordinatorDevService( |
| 44 | + LRABuildTimeConfiguration lraBuildTimeConfiguration, |
| 45 | + DevServicesComposeProjectBuildItem compose, |
| 46 | + DockerStatusBuildItem dockerStatusBuildItem, |
| 47 | + LaunchModeBuildItem launchMode, |
| 48 | + List<DevServicesSharedNetworkBuildItem> sharedNetwork, |
| 49 | + DevServicesConfig devServicesConfig) { |
| 50 | + LRACoordinatorDevServicesBuildTimeConfig config = lraBuildTimeConfiguration.devservices(); |
| 51 | + if (isDevServiceDisabled(dockerStatusBuildItem, config)) { |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + boolean useSharedNetwork = DevServicesSharedNetworkBuildItem.isSharedNetworkRequired(devServicesConfig, sharedNetwork); |
| 56 | + |
| 57 | + if (config.logWarning()) { |
| 58 | + log.warn("Dev Services for LRA requires exposing your application on the 0.0.0.0 host address. " + |
| 59 | + "Your application will be accessible from your network. You can disable this warning by setting " + |
| 60 | + "quarkus.lra.devservices.log-warning=false."); |
| 61 | + } |
| 62 | + |
| 63 | + return lraCoordinatorContainerLocator |
| 64 | + .locateContainer(config.serviceName(), config.shared(), launchMode.getLaunchMode()) |
| 65 | + .or(() -> ComposeLocator.locateContainer(compose, |
| 66 | + List.of(config.imageName(), "lra-coordinator"), |
| 67 | + LRA_COORDINATOR_CONTAINER_PORT, launchMode.getLaunchMode(), useSharedNetwork)) |
| 68 | + .map(containerAddress -> DevServicesResultBuildItem.discovered() |
| 69 | + .feature(Feature.NARAYANA_LRA) |
| 70 | + .containerId(containerAddress.getId()) |
| 71 | + .config(Map.of(LRA_COORDINATOR_URL_PROPERTY, "http://" + containerAddress.getUrl() + "/lra-coordinator", |
| 72 | + "quarkus.http.host", "0.0.0.0", // Required since the container needs to call the host application |
| 73 | + "quarkus.lra.base-uri", |
| 74 | + "http://host.containers.internal:" |
| 75 | + + (launchMode.isTest() ? "${quarkus.http.test-port}" : "${quarkus.http.port}"))) |
| 76 | + .build()) |
| 77 | + .orElseGet(() -> DevServicesResultBuildItem.owned() |
| 78 | + .feature(Feature.NARAYANA_LRA) |
| 79 | + .serviceName(config.serviceName()) |
| 80 | + .serviceConfig(config) |
| 81 | + .startable(() -> createContainer(compose, config, useSharedNetwork, launchMode)) |
| 82 | + .postStartHook(s -> logDevServiceStarted(s.getConnectionInfo())) |
| 83 | + .configProvider(Map.of(LRA_COORDINATOR_URL_PROPERTY, Startable::getConnectionInfo, |
| 84 | + "quarkus.http.host", s -> "0.0.0.0", // Required since the container needs to call the host application |
| 85 | + "quarkus.lra.base-uri", |
| 86 | + s -> "http://host.containers.internal:" |
| 87 | + + (launchMode.isTest() ? "${quarkus.http.test-port}" : "${quarkus.http.port}"))) |
| 88 | + .build()); |
| 89 | + } |
| 90 | + |
| 91 | + private void logDevServiceStarted(String connectionInfo) { |
| 92 | + log.infof("Dev Services for the LRA coordinator started. Other applications in dev mode will find the " + |
| 93 | + "LRA coordinator automatically. For Quarkus application in production mode, you can connect to " + |
| 94 | + "this coordinator by starting you application with -D%s=%s\n", LRA_COORDINATOR_URL_PROPERTY, connectionInfo); |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + private Startable createContainer(DevServicesComposeProjectBuildItem compose, |
| 99 | + LRACoordinatorDevServicesBuildTimeConfig config, boolean useSharedNetwork, LaunchModeBuildItem launchMode) { |
| 100 | + return new LRACoordinatorContainer(DockerImageName.parse(config.imageName()), |
| 101 | + config.port().orElse(0), |
| 102 | + compose.getDefaultNetworkId(), |
| 103 | + useSharedNetwork) |
| 104 | + .withEnv(config.containerEnv()) |
| 105 | + .withSharedServiceLabel(launchMode.getLaunchMode(), config.serviceName()); |
| 106 | + } |
| 107 | + |
| 108 | + private boolean isDevServiceDisabled(DockerStatusBuildItem dockerStatusBuildItem, |
| 109 | + LRACoordinatorDevServicesBuildTimeConfig config) { |
| 110 | + // explicitly disable |
| 111 | + if (!config.enabled()) { |
| 112 | + log.debug("Not starting dev services for the LRA coordinator, as it has been disabled in the config."); |
| 113 | + return true; |
| 114 | + } |
| 115 | + |
| 116 | + // defined LRA coordinator URL |
| 117 | + if (ConfigUtils.isPropertyNonEmpty(LRA_COORDINATOR_URL_PROPERTY)) { |
| 118 | + log.debugf("Not starting dev services for the LRA coordinator, the \"%s\" is configured", |
| 119 | + LRA_COORDINATOR_URL_PROPERTY); |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + // missing docker environment |
| 124 | + if (!dockerStatusBuildItem.isContainerRuntimeAvailable()) { |
| 125 | + log.warnf("Couldn't find valid Docker environment, please configure the \"%s\" configuration property.", |
| 126 | + LRA_COORDINATOR_URL_PROPERTY); |
| 127 | + return true; |
| 128 | + } |
| 129 | + |
| 130 | + return false; |
| 131 | + } |
| 132 | +} |
0 commit comments