Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDK-377: Prompt Users with Existing Docker Containers for Database Setup #327

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`) ";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you combining selecting with specify a container id/name/label or replacing the latter with the former?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If users select the other option, they are prompted with: 'Please specify your container ID...'"


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