diff --git a/sdk-commons/pom.xml b/sdk-commons/pom.xml index e52a1e4b..925e25dc 100644 --- a/sdk-commons/pom.xml +++ b/sdk-commons/pom.xml @@ -12,6 +12,7 @@ org.openmrs.maven.plugins openmrs-sdk-maven-plugin + 3.2.8 @@ -71,6 +72,29 @@ lombok + + + com.github.docker-java + docker-java + ${docker-java.version} + + + com.github.docker-java + docker-java-transport-jersey + + + com.github.docker-java + docker-java-transport-netty + + + + + com.github.docker-java + docker-java-transport-httpclient5 + ${docker-java.version} + + + junit diff --git a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java index cc2ea55d..3a43b4a2 100644 --- a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java +++ b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java @@ -904,8 +904,11 @@ public void promptForDockerizedDb(Server server, DockerHelper dockerHelper, Stri throws MojoExecutionException { promptForDockerHostIfMissing(dockerHelper, dockerHost); - String containerId = prompt( - "Please specify your container id/name/label (you can get it using command `docker ps -a --no-trunc`)"); + List containerNames = dockerHelper.getDockerContainerNames(); + String customMessage = "Please specify your container id/name/label (you can get it using command `docker ps -a --no-trunc`) "; + + String containerId = promptForMissingValueWithOptions("Select the docker container: ", null, null, + containerNames, customMessage, ""); String username = prompt("Please specify DB username"); String password = promptForPassword("Please specify DB password"); diff --git a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DockerHelper.java b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DockerHelper.java index a860293a..2fc05d4f 100644 --- a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DockerHelper.java +++ b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DockerHelper.java @@ -1,5 +1,11 @@ package org.openmrs.maven.plugins.utility; +import com.github.dockerjava.api.DockerClient; +import com.github.dockerjava.api.model.Container; +import com.github.dockerjava.core.DefaultDockerClientConfig; +import com.github.dockerjava.core.DockerClientBuilder; +import com.github.dockerjava.core.DockerClientConfig; +import com.github.dockerjava.httpclient5.ApacheDockerHttpClient; import org.apache.commons.lang.SystemUtils; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.BuildPluginManager; @@ -12,7 +18,9 @@ import java.io.IOException; import java.io.OutputStream; import java.nio.file.Files; +import java.util.List; import java.util.Properties; +import java.util.stream.Collectors; import static org.openmrs.maven.plugins.utility.PropertiesUtils.loadPropertiesFromFile; import static org.twdata.maven.mojoexecutor.MojoExecutor.artifactId; @@ -51,12 +59,18 @@ public class DockerHelper { private final MavenSession mavenSession; private final BuildPluginManager pluginManager; private final Wizard wizard; + private final DockerClient dockerClient; public DockerHelper(MavenEnvironment mavenEnvironment) { this.mavenProject = mavenEnvironment.getMavenProject(); this.mavenSession = mavenEnvironment.getMavenSession(); this.pluginManager = mavenEnvironment.getPluginManager(); this.wizard = mavenEnvironment.getWizard(); + DefaultDockerClientConfig.Builder configBuilder = DefaultDockerClientConfig.createDefaultConfigBuilder(); + DockerClientConfig config = configBuilder.build(); + dockerClient = DockerClientBuilder.getInstance(config).withDockerHttpClient( + new ApacheDockerHttpClient.Builder().dockerHost(config.getDockerHost()).sslConfig(config.getSSLConfig()).build() + ).build(); } private Properties getSdkProperties() throws MojoExecutionException { @@ -144,4 +158,14 @@ public void runDbContainer(String container, String dbUri, String username, Stri executionEnvironment(mavenProject, mavenSession, pluginManager) ); } + + public List getDockerContainers() { + return dockerClient.listContainersCmd().withShowAll(true).exec(); + } + + public List getDockerContainerNames() { + return getDockerContainers().stream() + .map(container -> container.getNames()[0].replace("/", "")) + .collect(Collectors.toList()); + } }