Skip to content

Commit

Permalink
SDK-377: Prompt Users with Existing Docker Containers for Database Setup
Browse files Browse the repository at this point in the history
  • Loading branch information
wikumChamith committed Jan 16, 2025
1 parent d732d7f commit fcba3f6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
24 changes: 24 additions & 0 deletions sdk-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<properties>
<sdk.groupId>org.openmrs.maven.plugins</sdk.groupId>
<sdk.artifactId>openmrs-sdk-maven-plugin</sdk.artifactId>
<docker-java.version>3.2.8</docker-java.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -71,6 +72,29 @@
<artifactId>lombok</artifactId>
</dependency>

<!-- Docker-->
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java</artifactId>
<version>${docker-java.version}</version>
<exclusions>
<exclusion>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-transport-jersey</artifactId>
</exclusion>
<exclusion>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-transport-netty</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-transport-httpclient5</artifactId>
<version>${docker-java.version}</version>
</dependency>


<!--Testing-->
<dependency>
<groupId>junit</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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");

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -144,4 +158,14 @@ public void runDbContainer(String container, String dbUri, String username, Stri
executionEnvironment(mavenProject, mavenSession, pluginManager)
);
}

public List<Container> getDockerContainers() {
return dockerClient.listContainersCmd().withShowAll(true).exec();
}

public List<String> getDockerContainerNames() {
return getDockerContainers().stream()
.map(container -> container.getNames()[0].replace("/", ""))
.collect(Collectors.toList());
}
}

0 comments on commit fcba3f6

Please sign in to comment.