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-361 and SDK-356 - Update wizard to support custom distributions and more platform and distribution versions #315

Merged
merged 7 commits into from
Nov 26, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ public void deploy_shouldReplaceDistroPlatform() throws Exception {
public void deploy_shouldUpgradeDistroTo2_3_1() throws Exception {
testServerId = setupTestServer("referenceapplication:2.2");
addAnswer(testServerId);
addAnswer("Distribution");
addAnswer("referenceapplication:2.3.1");
addTaskParam("distro", "referenceapplication:2.3.1");
addAnswer("y");
executeTask("deploy");
assertSuccess();
Expand All @@ -81,8 +80,7 @@ public void deploy_shouldUpgradeDistroTo2_3_1() throws Exception {
public void deploy_shouldDowngradeDistroTo2_1() throws Exception {
testServerId = setupTestServer("referenceapplication:2.2");
addAnswer(testServerId);
addAnswer("Distribution");
addAnswer("referenceapplication:2.1");
addTaskParam("distro", "referenceapplication:2.1");
addAnswer("y");
executeTask("deploy");
assertSuccess();
Expand All @@ -100,8 +98,7 @@ public void deploy_shouldDowngradeDistroTo2_1() throws Exception {
public void deploy_shouldUpgradeDistroTo2_13_0() throws Exception {
testServerId = setupTestServer("referenceapplication:2.3.1");
addAnswer(testServerId);
addAnswer("Distribution");
addAnswer("referenceapplication:2.13.0");
addTaskParam("distro", "referenceapplication:2.13.0");
addAnswer("y");
executeTask("deploy");
assertSuccess();
Expand All @@ -124,8 +121,7 @@ public void deploy_shouldUpgradeDistroTo2_13_0() throws Exception {
public void deploy_shouldUpgradeDistroTo3_0_0() throws Exception {
testServerId = setupTestServer("referenceapplication:2.2");
addAnswer(testServerId);
addAnswer("Distribution");
addAnswer("referenceapplication:3.0.0");
addTaskParam("distro", "referenceapplication:3.0.0");
addAnswer("y");
executeTask("deploy");
assertSuccess();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.openmrs.maven.plugins.model.Artifact;
import org.openmrs.maven.plugins.model.DistroProperties;

import org.openmrs.maven.plugins.utility.NpmVersionHelper;
import org.openmrs.maven.plugins.utility.SDKConstants;

import java.io.File;
import java.nio.file.Paths;
Expand Down Expand Up @@ -133,10 +131,8 @@ public void executeTask() throws MojoExecutionException, MojoFailureException {
distroProperties.addProperty("owa." + artifactId, version);
break;
case WAR_OPTION:
Artifact platformArtifact = new Artifact(SDKConstants.PLATFORM_ARTIFACT_ID,
SDKConstants.SETUP_DEFAULT_PLATFORM_VERSION, Artifact.GROUP_DISTRO);
if (StringUtils.isBlank(version)) {
version = wizard.promptForPlatformVersion(versionsHelper.getSuggestedVersions(platformArtifact, 5));
version = wizard.promptForPlatformVersion(versionsHelper);
}
distroProperties.addProperty("war.openmrs", version);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@
import java.util.ArrayList;
import java.util.List;

import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_ARTIFACT_ID;
import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_GROUP_ID;
import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_TYPE;
import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_ARTIFACT_ID;
import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_GROUP_ID;
import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_TYPE;
import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_PROMPT;
import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_PROMPT;

/**
* Create docker configuration for distributions.
Expand Down Expand Up @@ -78,10 +74,6 @@ public class BuildDistro extends AbstractTask {

private static final String DOCKER_COMPOSE_OVERRIDE_YML = "docker-compose.override.yml";

private static final String O2_DISTRIBUTION = "2.x Distribution";

private static final String O3_DISTRIBUTION = "O3 Distribution";

private static final Logger log = LoggerFactory.getLogger(BuildDistro.class);

/**
Expand Down Expand Up @@ -160,22 +152,21 @@ else if (StringUtils.isNotBlank(distro)) {
}

if (distribution == null) {
Server server = new Server.ServerBuilder().build();

List<String> options = new ArrayList<>();
options.add(O2_DISTRIBUTION);
options.add(O3_DISTRIBUTION);
options.add(REFAPP_2X_PROMPT);
options.add(REFAPP_3X_PROMPT);

Artifact artifact = null;
String choice = wizard.promptForMissingValueWithOptions("You can setup following servers", null, null, options);
switch (choice) {
case O2_DISTRIBUTION:
wizard.promptForRefAppVersionIfMissing(server, versionsHelper, DISTRIBUTION_VERSION_PROMPT);
distribution = builder.buildFromArtifact(new Artifact(REFAPP_2X_ARTIFACT_ID, server.getVersion(), REFAPP_2X_GROUP_ID, REFAPP_2X_TYPE));
case REFAPP_2X_PROMPT:
artifact = wizard.promptForRefApp2xArtifact(versionsHelper);
break;
case O3_DISTRIBUTION:
wizard.promptForO3RefAppVersionIfMissing(server, versionsHelper);
distribution = builder.buildFromArtifact(new Artifact(REFAPP_3X_ARTIFACT_ID, server.getVersion(), REFAPP_3X_GROUP_ID, REFAPP_3X_TYPE));
case REFAPP_3X_PROMPT:
artifact = wizard.promptForRefApp3xArtifact(versionsHelper);
}
distribution = builder.buildFromArtifact(artifact);
}

if (distribution == null) {
Expand Down
37 changes: 19 additions & 18 deletions maven-plugin/src/main/java/org/openmrs/maven/plugins/Deploy.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,8 @@ public void executeTask() throws MojoExecutionException, MojoFailureException {
}
}

private void runInteractiveMode(Server server, ServerUpgrader upgrader)
throws MojoExecutionException, MojoFailureException {
DistroProperties distroProperties;
private void runInteractiveMode(Server server, ServerUpgrader upgrader) throws MojoExecutionException, MojoFailureException {

List<String> options = new ArrayList<>(Arrays.asList(
DEPLOY_MODULE_OPTION,
DEPLOY_OWA_OPTION,
Expand All @@ -178,27 +177,29 @@ private void runInteractiveMode(Server server, ServerUpgrader upgrader)
server.getName(),
server.getVersion()));


Artifact artifact;

// If its impossible to define distro, prompt refapp distro versions
if (server.getName().equals("Platform") || server.getDistroGroupId() == null || server.getDistroArtifactId() == null) {
// If its impossible to define distro, prompt refapp distro versions
distro = wizard.promptForRefAppVersion(versionsHelper);
} else {
// If its possible to define distro, prompt that distro's versions
distro = wizard.promptForDistroVersion(server.getDistroGroupId(), server.getDistroArtifactId(),
server.getVersion(), server.getName(), versionsHelper);
artifact = wizard.promptForRefApp2xArtifact(versionsHelper);
}
wizard.showMessage("Deploying distribution: " + distro);
Distribution distribution = distroHelper.resolveDistributionForStringSpecifier(distro, versionsHelper);
else {
// If it is possible to define distro, prompt that distro's versions
artifact = new Artifact(server.getDistroArtifactId(), server.getVersion(), server.getDistroGroupId());
artifact.setVersion(wizard.promptForArtifactVersion("Please choose a " + server.getName() + " version", artifact, null, versionsHelper));
}

DistributionBuilder builder = new DistributionBuilder(getMavenEnvironment());
Distribution distribution = builder.buildFromArtifact(artifact);

wizard.showMessage("Deploying distribution: " + distribution.getName() + " " + distribution.getVersion());
upgrader.upgradeToDistro(server, distribution, ignorePeerDependencies, overrideReuseNodeCache);
break;
}
case (DEPLOY_PLATFORM_OPTION): {
wizard.showMessage(String.format(
TEMPLATE_CURRENT_VERSION,
"platform",
server.getPlatformVersion()));
Artifact webapp = new Artifact(SDKConstants.PLATFORM_ARTIFACT_ID,
SDKConstants.SETUP_DEFAULT_PLATFORM_VERSION, Artifact.GROUP_DISTRO);
platform = wizard.promptForPlatformVersion(versionsHelper.getSuggestedVersions(webapp, 3));
wizard.showMessage(String.format(TEMPLATE_CURRENT_VERSION, "platform", server.getPlatformVersion()));
platform = wizard.promptForPlatformVersion(versionsHelper);
deployOpenmrs(server, platform);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.openmrs.maven.plugins.model.DistroProperties;
import org.openmrs.maven.plugins.model.Server;
import org.openmrs.maven.plugins.utility.DistributionBuilder;
import org.openmrs.maven.plugins.utility.SDKConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -18,23 +19,14 @@
import java.util.ArrayList;
import java.util.List;

import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_ARTIFACT_ID;
import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_GROUP_ID;
import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_TYPE;
import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_ARTIFACT_ID;
import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_GROUP_ID;
import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_TYPE;
import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_PROMPT;

/**
* Generates an openmrs-distro.properties file for a specific version of OpenMRS Distro
*/
@Mojo(name = "generate-distro", requiresProject = false)
public class GenerateDistro extends AbstractTask {

private static final String O2_DISTRIBUTION = "2.x Distribution";

private static final String O3_DISTRIBUTION = "O3 Distribution";

private static final String GENERATE_DISTRO_PROMPT = "You can generate the distro.properties file for the following distributions";

private static final String GET_VERSION_PROMPT = "You can generate the distro.properties file for the following versions";
Expand All @@ -51,8 +43,8 @@ public class GenerateDistro extends AbstractTask {
@Override
public void executeTask() throws MojoExecutionException, MojoFailureException {
List<String> options = new ArrayList<>();
options.add(O2_DISTRIBUTION);
options.add(O3_DISTRIBUTION);
options.add(SDKConstants.REFAPP_2X_PROMPT);
options.add(SDKConstants.REFAPP_3X_PROMPT);
String choice = wizard.promptForMissingValueWithOptions(GENERATE_DISTRO_PROMPT, null, null, options);

DistributionBuilder builder = new DistributionBuilder(getMavenEnvironment());
Expand All @@ -67,25 +59,19 @@ public void executeTask() throws MojoExecutionException, MojoFailureException {
}

File outputFile = outputLocation != null ? new File(outputLocation) : new File(System.getProperty("user.dir"));
Artifact artifact = null;
switch (choice) {
case O2_DISTRIBUTION:
wizard.promptForRefAppVersionIfMissing(server, versionsHelper, GET_VERSION_PROMPT);
distribution = builder.buildFromArtifact(new Artifact(REFAPP_2X_ARTIFACT_ID, server.getVersion(), REFAPP_2X_GROUP_ID, REFAPP_2X_TYPE));
break;

case O3_DISTRIBUTION:
wizard.promptForO3RefAppVersionIfMissing(server, versionsHelper, GET_VERSION_PROMPT);
distribution = builder.buildFromArtifact(new Artifact(REFAPP_3X_ARTIFACT_ID, server.getVersion(), REFAPP_3X_GROUP_ID, REFAPP_3X_TYPE));
break;
case SDKConstants.REFAPP_2X_PROMPT:
artifact = wizard.promptForRefApp2xArtifact(versionsHelper);
break;
case REFAPP_3X_PROMPT:
artifact = wizard.promptForRefApp3xArtifact(versionsHelper);
}

distribution = builder.buildFromArtifact(artifact);
if (distribution == null) {
throw new MojoFailureException("Distribution could not be generated, the specified distribution could not be found");
}

distribution.getEffectiveProperties().saveTo(outputFile);
logger.info(String.format("openmrs-distro.properties file created successfully at %s", outputFile.getAbsolutePath() + File.separator + DistroProperties.DISTRO_FILE_NAME));


}
}
63 changes: 33 additions & 30 deletions maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.openmrs.maven.plugins.utility.ContentHelper;
import org.openmrs.maven.plugins.utility.DBConnector;
import org.openmrs.maven.plugins.utility.DistributionBuilder;
import org.openmrs.maven.plugins.utility.DistroHelper;
import org.openmrs.maven.plugins.utility.SDKConstants;
import org.openmrs.maven.plugins.utility.ServerHelper;

Expand All @@ -40,12 +39,8 @@

import static org.openmrs.maven.plugins.model.Artifact.GROUP_DISTRO;
import static org.openmrs.maven.plugins.utility.SDKConstants.PLATFORM_ARTIFACT_ID;
import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_ARTIFACT_ID;
import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_GROUP_ID;
import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_TYPE;
import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_ARTIFACT_ID;
import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_GROUP_ID;
import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_TYPE;
import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_2X_PROMPT;
import static org.openmrs.maven.plugins.utility.SDKConstants.REFAPP_3X_PROMPT;
import static org.openmrs.maven.plugins.utility.SDKConstants.SETUP_DEFAULT_PLATFORM_VERSION;


Expand All @@ -57,18 +52,14 @@ public class Setup extends AbstractServerTask {

public static final String SETTING_UP_A_NEW_SERVER = "Setting up a new server...";

public static final String SETUP_SERVERS_PROMPT = "You can setup the following servers";
public static final String SETUP_SERVERS_PROMPT = "You can setup the following distributions";

public static final String ENABLE_DEBUGGING_DEFAULT_MESSAGE =
"If you want to enable remote debugging by default when running the server, "
+ "\nspecify the %s here (e.g. 1044). Leave blank to disable debugging. \n(Do not do this on a production server)";

private static final String O2_DISTRIBUTION = "2.x Distribution";

private static final String PLATFORM = "Platform";

private static final String O3_DISTRIBUTION = "O3 Distribution";

private static final String CLASSPATH_SCRIPT_PREFIX = "classpath://";

private static final String NO_DEBUGGING_DEFAULT_ANSWER = "no debugging";
Expand Down Expand Up @@ -205,53 +196,65 @@ private DistroProperties resolveDistroProperties(Server server) throws MojoExecu

List<String> options = new ArrayList<>();

String currentDirectoryOption = null;
Distribution currentDirectoryDistribution = null;
File distroPropertiesFile = distroHelper.getDistroPropertiesFileFromDir();
if (distroPropertiesFile != null) {
try {
currentDirectoryDistribution = builder.buildFromFile(distroPropertiesFile);
if (currentDirectoryDistribution != null) {
options.add(currentDirectoryDistribution.getName() + " " + currentDirectoryDistribution.getVersion() + " from current directory");
currentDirectoryOption = currentDirectoryDistribution.getName() + " " + currentDirectoryDistribution.getVersion() + " from current directory";
options.add(currentDirectoryOption);
}
}
catch (Exception e) {
wizard.showWarning("Found " + distroPropertiesFile.getAbsolutePath() + " but unable to load this into a distribution: " + e.getMessage());
}
}

options.add(O3_DISTRIBUTION);
options.add(O2_DISTRIBUTION);
options.add(REFAPP_2X_PROMPT);
options.add(REFAPP_3X_PROMPT);
options.add(PLATFORM);
String choice = wizard.promptForMissingValueWithOptions(SETUP_SERVERS_PROMPT, null, null, options);

String customDistroMessage = "Please specify distribution artifact";
String customDistroDefault = SDKConstants.REFERENCEAPPLICATION_2_4;
Copy link
Member Author

Choose a reason for hiding this comment

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

By adding these to this top-level wizard step, we move the option to specify a custom distribution out from under the RefApp 2.x section and into the top-level section where it belongs. This then allows us to focus the refapp 2.x section on paging through the published versions in Maven


String choice = wizard.promptForMissingValueWithOptions(SETUP_SERVERS_PROMPT, null, null, options, customDistroMessage, customDistroDefault);

// Deploy from current directory if chosen
if (currentDirectoryOption != null && currentDirectoryOption.equals(choice)) {
return currentDirectoryDistribution.getEffectiveProperties();
}

// Deploy platform
if (PLATFORM.equals(choice)) {
return resolveDistroPropertiesForPlatform(server, platform);
}

if (O2_DISTRIBUTION.equals(choice)) {
wizard.promptForRefAppVersionIfMissing(server, versionsHelper);
Distribution distribution = builder.buildFromArtifact(new Artifact(REFAPP_2X_ARTIFACT_ID, server.getVersion(), REFAPP_2X_GROUP_ID, REFAPP_2X_TYPE));
if (REFAPP_2X_PROMPT.equals(choice)) {
Artifact artifact = wizard.promptForRefApp2xArtifact(versionsHelper);
Distribution distribution = builder.buildFromArtifact(artifact);
return distribution.getEffectiveProperties();
}

if (O3_DISTRIBUTION.equals(choice)) {
wizard.promptForO3RefAppVersionIfMissing(server, versionsHelper);
Distribution distribution = builder.buildFromArtifact(new Artifact(REFAPP_3X_ARTIFACT_ID, server.getVersion(), REFAPP_3X_GROUP_ID, REFAPP_3X_TYPE));
if (REFAPP_3X_PROMPT.equals(choice)) {
Artifact artifact = wizard.promptForRefApp3xArtifact(versionsHelper);
Distribution distribution = builder.buildFromArtifact(artifact);
return distribution.getEffectiveProperties();
}

// If here, it is because the option to set up from current directory was chosen, and these properties were already loaded, just return them
if (currentDirectoryDistribution == null) {
throw new MojoExecutionException("No valid distribution could be found");
}
return currentDirectoryDistribution.getEffectiveProperties();
// If here, it is because custom distribution was chosen and the choice reflects the Maven coordinates
Distribution distribution = distroHelper.resolveDistributionForStringSpecifier(choice, versionsHelper);
return distribution.getEffectiveProperties();
}

private DistroProperties resolveDistroPropertiesForPlatform(Server server, String version) throws MojoExecutionException {
Artifact platformArtifact = new Artifact(PLATFORM_ARTIFACT_ID, SETUP_DEFAULT_PLATFORM_VERSION, GROUP_DISTRO);
version = wizard.promptForPlatformVersionIfMissing(version, versionsHelper.getSuggestedVersions(platformArtifact, 6));
platformArtifact = DistroHelper.parseDistroArtifact(GROUP_DISTRO + ":" + PLATFORM_ARTIFACT_ID + ":" + version, versionsHelper);
server.setPlatformVersion(platformArtifact.getVersion());
if (StringUtils.isBlank(version)) {
version = wizard.promptForPlatformVersion(versionsHelper);
}
platformArtifact.setVersion(version);
server.setPlatformVersion(version);
try {
DistributionBuilder builder = new DistributionBuilder(getMavenEnvironment());
Distribution distribution = builder.buildFromArtifact(platformArtifact);
Expand Down
Loading