From c7c7b6ec62982029e908d778cd19c96506f2dc57 Mon Sep 17 00:00:00 2001 From: Patryk Najda Date: Fri, 25 Jun 2021 13:05:11 +0200 Subject: [PATCH] Refactor redis-client devservices to use a common container locator --- bom/application/pom.xml | 5 +++ extensions/devservices/common/pom.xml | 35 +++++++++++++++++++ .../devservices/common}/ContainerLocator.java | 17 ++++----- extensions/devservices/pom.xml | 1 + extensions/redis-client/deployment/pom.xml | 4 +++ .../deployment/DevServicesProcessor.java | 4 ++- 6 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 extensions/devservices/common/pom.xml rename extensions/{redis-client/deployment/src/main/java/io/quarkus/redis/client/deployment => devservices/common/src/main/java/io/quarkus/devservices/common}/ContainerLocator.java (87%) diff --git a/bom/application/pom.xml b/bom/application/pom.xml index 70da5255e42207..3ede076a3dcf9e 100644 --- a/bom/application/pom.xml +++ b/bom/application/pom.xml @@ -619,6 +619,11 @@ quarkus-devservices-mssql ${project.version} + + io.quarkus + quarkus-devservices-common + ${project.version} + io.quarkus quarkus-elasticsearch-rest-client-common diff --git a/extensions/devservices/common/pom.xml b/extensions/devservices/common/pom.xml new file mode 100644 index 00000000000000..9b92912a4b1935 --- /dev/null +++ b/extensions/devservices/common/pom.xml @@ -0,0 +1,35 @@ + + + + quarkus-devservices-parent + io.quarkus + 999-SNAPSHOT + + 4.0.0 + + quarkus-devservices-common + Quarkus - DevServices - Common + + + + com.github.docker-java + docker-java-api + + + org.jboss.logging + jboss-logging + + + org.testcontainers + testcontainers + + + junit + junit + + + + + \ No newline at end of file diff --git a/extensions/redis-client/deployment/src/main/java/io/quarkus/redis/client/deployment/ContainerLocator.java b/extensions/devservices/common/src/main/java/io/quarkus/devservices/common/ContainerLocator.java similarity index 87% rename from extensions/redis-client/deployment/src/main/java/io/quarkus/redis/client/deployment/ContainerLocator.java rename to extensions/devservices/common/src/main/java/io/quarkus/devservices/common/ContainerLocator.java index 90b2300cee2ae8..b78902f915b4d4 100644 --- a/extensions/redis-client/deployment/src/main/java/io/quarkus/redis/client/deployment/ContainerLocator.java +++ b/extensions/devservices/common/src/main/java/io/quarkus/devservices/common/ContainerLocator.java @@ -1,16 +1,16 @@ -package io.quarkus.redis.client.deployment; +package io.quarkus.devservices.common; + +import java.util.List; -import com.github.dockerjava.api.model.Container; -import com.github.dockerjava.api.model.ContainerPort; -import io.smallrye.common.constraint.Nullable; import org.jboss.logging.Logger; import org.testcontainers.DockerClientFactory; -import java.util.List; +import com.github.dockerjava.api.model.Container; +import com.github.dockerjava.api.model.ContainerPort; public class ContainerLocator { - private static final Logger log = Logger.getLogger(DevServicesProcessor.class); + private static final Logger log = Logger.getLogger(ContainerLocator.class); private final String devServiceLabel; private final int port; @@ -20,7 +20,6 @@ public ContainerLocator(String devServiceLabel, int port) { this.port = port; } - @Nullable private Container lookup(String expectedLabelValue) { List containers = DockerClientFactory.lazyClient().listContainersCmd().exec(); for (Container container : containers) { @@ -32,7 +31,6 @@ private Container lookup(String expectedLabelValue) { return null; } - @Nullable private ContainerPort getMappedPort(Container container, int port) { for (ContainerPort p : container.getPorts()) { Integer mapped = p.getPrivatePort(); @@ -44,7 +42,6 @@ private ContainerPort getMappedPort(Container container, int port) { return null; } - @Nullable public String locateContainer(String serviceName) { Container container = lookup(serviceName); if (container != null) { @@ -52,7 +49,7 @@ public String locateContainer(String serviceName) { if (containerPort != null) { String url = containerPort.getIp() + ":" + containerPort.getPublicPort(); log.infof("Dev Services container locator found: %s (%s). " - + "Connecting to: %s.", + + "Connecting to: %s.", container.getId(), container.getImage(), url); return url; diff --git a/extensions/devservices/pom.xml b/extensions/devservices/pom.xml index 28b06fd4554078..bcdb8478fd6474 100644 --- a/extensions/devservices/pom.xml +++ b/extensions/devservices/pom.xml @@ -25,6 +25,7 @@ derby mssql db2 + common diff --git a/extensions/redis-client/deployment/pom.xml b/extensions/redis-client/deployment/pom.xml index 93badcfd0a3bca..daf0dd932ab0aa 100644 --- a/extensions/redis-client/deployment/pom.xml +++ b/extensions/redis-client/deployment/pom.xml @@ -19,6 +19,10 @@ io.quarkus quarkus-vertx-deployment + + io.quarkus + quarkus-devservices-common + io.quarkus quarkus-redis-client diff --git a/extensions/redis-client/deployment/src/main/java/io/quarkus/redis/client/deployment/DevServicesProcessor.java b/extensions/redis-client/deployment/src/main/java/io/quarkus/redis/client/deployment/DevServicesProcessor.java index 987eeaef61e02b..14fc42368b18c0 100644 --- a/extensions/redis-client/deployment/src/main/java/io/quarkus/redis/client/deployment/DevServicesProcessor.java +++ b/extensions/redis-client/deployment/src/main/java/io/quarkus/redis/client/deployment/DevServicesProcessor.java @@ -25,6 +25,7 @@ import io.quarkus.deployment.builditem.LaunchModeBuildItem; import io.quarkus.deployment.builditem.RunTimeConfigurationDefaultBuildItem; import io.quarkus.deployment.builditem.ServiceStartBuildItem; +import io.quarkus.devservices.common.ContainerLocator; import io.quarkus.redis.client.deployment.RedisBuildTimeConfig.DevServiceConfiguration; import io.quarkus.redis.client.runtime.RedisClientUtil; import io.quarkus.redis.client.runtime.RedisConfig; @@ -147,7 +148,8 @@ private StartResult startContainer(String connectionName, DevServicesConfig devS return new StartResult(url, null); } - FixedPortRedisContainer redisContainer = new FixedPortRedisContainer(dockerImageName, devServicesConfig.port, launchMode == DEVELOPMENT ? devServicesConfig.serviceName : null); + FixedPortRedisContainer redisContainer = new FixedPortRedisContainer(dockerImageName, devServicesConfig.port, + launchMode == DEVELOPMENT ? devServicesConfig.serviceName : null); redisContainer.start(); String redisHost = REDIS_SCHEME + redisContainer.getHost() + ":" + redisContainer.getPort(); return new StartResult(redisHost,