Skip to content

Commit

Permalink
SDK-306 - Fix to enable the config-prefixed distro properties to succ… (
Browse files Browse the repository at this point in the history
  • Loading branch information
mseaton authored Nov 8, 2024
1 parent 9ee828f commit b4160cf
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Settings;
import org.openmrs.maven.plugins.model.Server;
import org.openmrs.maven.plugins.utility.ConfigurationInstaller;
import org.openmrs.maven.plugins.utility.DefaultJira;
import org.openmrs.maven.plugins.utility.DistroHelper;
import org.openmrs.maven.plugins.git.DefaultGitHelper;
Expand Down Expand Up @@ -119,6 +120,11 @@ public abstract class AbstractTask extends AbstractMojo {
*/
SpaInstaller spaInstaller;

/**
* installs configuration artifacts
*/
ConfigurationInstaller configurationInstaller;

/**
* handles github and provides basic git utilities
*/
Expand Down Expand Up @@ -181,6 +187,9 @@ public void initTask() {
if (spaInstaller == null) {
spaInstaller = new SpaInstaller(distroHelper, new NodeHelper(mavenProject, mavenSession, pluginManager));
}
if (configurationInstaller == null) {
configurationInstaller = new ConfigurationInstaller(distroHelper);
}
if (dockerHelper == null) {
dockerHelper = new DockerHelper(mavenProject, mavenSession, pluginManager, wizard);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* Create docker configuration for distributions.
Expand Down Expand Up @@ -322,8 +321,7 @@ private String buildDistro(File targetDirectory, Artifact distroArtifact, Distro
frontendDir.mkdir();

File configDir = new File(web, SDKConstants.OPENMRS_SERVER_CONFIGURATION);
configDir.mkdir();
setConfigFolder(configDir, distroProperties, distroArtifact);
configurationInstaller.installToDirectory(configDir, distroProperties);

ContentHelper.downloadAndMoveContentBackendConfig(web, distroProperties, moduleInstaller, wizard);

Expand Down Expand Up @@ -371,48 +369,6 @@ private String buildDistro(File targetDirectory, Artifact distroArtifact, Distro
return distroName;
}

private void setConfigFolder(File configDir, DistroProperties distroProperties, Artifact distroArtifact) throws MojoExecutionException {
if (distroProperties.getConfigArtifacts().isEmpty()) {
return;
}

downloadConfigs(distroProperties, configDir);

File refappConfigFile = new File(configDir, distroArtifact.getArtifactId() + "-" + distroArtifact.getVersion() + ".zip");

// Handle O2 configuration
if (!refappConfigFile.exists() && Artifact.GROUP_DISTRO.equals(distroArtifact.getGroupId()) && "referenceapplication-distro".equals(distroArtifact.getArtifactId())) {
refappConfigFile = new File(configDir, "referenceapplication-distro.owa");
}

if (!refappConfigFile.exists()) {
wizard.showError("No Configuration file found at " + refappConfigFile.getAbsolutePath());
return;
}

try {
ZipFile zipFile = new ZipFile(refappConfigFile);
zipFile.extractAll(configDir.getPath());
for (File file : Objects.requireNonNull(configDir.listFiles())) {
if (file.getName().equals("openmrs_config")) {
FileUtils.copyDirectory(file, configDir);
}
FileUtils.deleteQuietly(file);
}
FileUtils.deleteQuietly(refappConfigFile);
} catch (ZipException | IOException e) {
throw new RuntimeException(e);
}
}

private void downloadConfigs(DistroProperties distroProperties, File configDir) throws MojoExecutionException {
List<Artifact> configs = distroProperties.getConfigArtifacts();
wizard.showMessage("Downloading Configs...\n");
if (!configs.isEmpty()) {
moduleInstaller.installModules(configs, configDir.getAbsolutePath());
}
}

private void downloadOWAs(File targetDirectory, DistroProperties distroProperties, File owasDir)
throws MojoExecutionException {
List<Artifact> owas = distroProperties.getOwaArtifacts(distroHelper, targetDirectory);
Expand Down
65 changes: 1 addition & 64 deletions maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.openmrs.maven.plugins;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.output.NullOutputStream;
import org.apache.commons.lang.StringUtils;
Expand All @@ -23,7 +21,6 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
Expand All @@ -38,7 +35,6 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
Expand Down Expand Up @@ -287,8 +283,7 @@ public void setup(Server server, DistroProperties distroProperties) throws MojoE
}

installOWAs(server, distroProperties);

setConfigFolder(server, distroProperties);
configurationInstaller.installToServer(server, distroProperties);
} else {
moduleInstaller.installDefaultModules(server);
}
Expand Down Expand Up @@ -350,64 +345,6 @@ private void downloadOWAs(File targetDirectory, DistroProperties distroPropertie
}
}

/**
* Sets the configuration folder for the specified server using the provided distro properties.
*
* @param server The server for which to set the configuration folder.
* @param distroProperties The distro properties containing the configuration information.
*/
private void setConfigFolder(Server server, DistroProperties distroProperties) throws MojoExecutionException {
if (distroProperties.getConfigArtifacts().isEmpty()) {
return;
}

File configDir = new File(server.getServerDirectory(), SDKConstants.OPENMRS_SERVER_CONFIGURATION);
configDir.mkdir();

downloadConfigs(distroProperties, configDir);

File refappConfigFile = new File(configDir, server.getDistroArtifactId() + "-" + server.getVersion() + ".zip");

// Handle O2 configuration
if (!refappConfigFile.exists() && Artifact.GROUP_DISTRO.equals(server.getDistroGroupId()) && "referenceapplication-distro".equals(server.getDistroArtifactId())) {
refappConfigFile = new File(configDir, "referenceapplication-distro.owa");
}

if (!refappConfigFile.exists()) {
wizard.showError("No Configuration file found at " + refappConfigFile.getAbsolutePath());
return;
}

try {
ZipFile zipFile = new ZipFile(refappConfigFile);
zipFile.extractAll(configDir.getPath());
for (File file : Objects.requireNonNull(configDir.listFiles())) {
if (file.getName().equals("openmrs_config")) {
FileUtils.copyDirectory(file, configDir);
}
FileUtils.deleteQuietly(file);
}
FileUtils.deleteQuietly(refappConfigFile);
} catch (ZipException | IOException e) {
throw new RuntimeException(e);
}
}

/**
* Downloads the configuration artifact specified in the distro properties and saves them in the provided config directory.
*
* @param distroProperties The distro properties containing the configuration artifacts to download.
* @param configDir The directory where the configuration files will be saved.
* @throws MojoExecutionException If an error occurs while downloading the configuration files.
*/
private void downloadConfigs(DistroProperties distroProperties, File configDir) throws MojoExecutionException {
List<Artifact> configs = distroProperties.getConfigArtifacts();
wizard.showMessage("Downloading Configs...\n");
if (!configs.isEmpty()) {
moduleInstaller.installModules(configs, configDir.getAbsolutePath());
}
}

private void wipeDatabase(Server server) throws MojoExecutionException {
String uri = getUriWithoutDb(server);
try (DBConnector connector = new DBConnector(uri, server.getDbUser(), server.getDbPassword(), server.getDbName())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.openmrs.maven.plugins.utility;

import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.openmrs.maven.plugins.model.Artifact;
import org.openmrs.maven.plugins.model.DistroProperties;
import org.openmrs.maven.plugins.model.Server;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
* Supports retrieving the configuration artifacts specified in the distribution
* and installing them into an SDK server or directory
*/
public class ConfigurationInstaller {

private final Wizard wizard;

private final ModuleInstaller moduleInstaller;

public ConfigurationInstaller(DistroHelper distroHelper) {
this.wizard = distroHelper.wizard;
this.moduleInstaller = new ModuleInstaller(distroHelper.mavenProject, distroHelper.mavenSession, distroHelper.pluginManager, distroHelper.versionHelper);
}

/**
* Installs the configuration packages defined in the distro properties to the given server
* @param server the server to deploy to
* @param distroProperties the distro properties containing "config" elements
* @throws MojoExecutionException
*/
public void installToServer(Server server, DistroProperties distroProperties) throws MojoExecutionException {
File directory = new File(server.getServerDirectory(), SDKConstants.OPENMRS_SERVER_CONFIGURATION);
File tempDir = new File(server.getServerTmpDirectory(), UUID.randomUUID().toString());
installToDirectory(directory, tempDir, distroProperties);
}

public void installToDirectory(File installDir, DistroProperties distroProperties) throws MojoExecutionException {
File tempDir = new File(installDir.getParentFile(), UUID.randomUUID().toString());
installToDirectory(installDir, tempDir, distroProperties);
}

protected void installToDirectory(File installDir, File tempDir, DistroProperties distroProperties) throws MojoExecutionException {
if (distroProperties.getConfigArtifacts().isEmpty()) {
return;
}
if (installDir.mkdirs()) {
wizard.showMessage("Created configuration directory: " + installDir.getAbsolutePath());
}

wizard.showMessage("Downloading Configuration...\n");

List<Artifact> configs = distroProperties.getConfigArtifacts();
for (Artifact configArtifact : configs) {
// Some config artifacts have their configuration packaged in an "openmrs_config" subfolder within the zip
// If such a folder is found in the downloaded artifact, use it. Otherwise, use the entire zip contents
try {
if (!tempDir.mkdirs()) {
throw new MojoExecutionException("Unable to create temporary directory " + tempDir + "\n");
}
moduleInstaller.installAndUnpackModule(configArtifact, tempDir.getAbsolutePath());
File directoryToCopy = tempDir;
for (File f : Objects.requireNonNull(tempDir.listFiles())) {
if (f.isDirectory() && f.getName().equals("openmrs_config")) {
directoryToCopy = f;
}
}
try {
FileUtils.copyDirectory(directoryToCopy, installDir);
} catch (IOException e) {
throw new MojoExecutionException("Unable to copy config: " + directoryToCopy + "\n");
}
}
finally {
FileUtils.deleteQuietly(tempDir);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,12 @@ public Artifact getParentArtifact() {

int missingCount = 0;
if (StringUtils.isBlank(parentArtifactId)) {
log.warn("parent.artifactId missing");
missingCount++;
}
if (StringUtils.isBlank(parentGroupId)) {
log.warn("parent.groupId is missing");
missingCount++;
}
if (StringUtils.isBlank(parentVersion)) {
log.warn("parent.version is missing");
missingCount++;
}

Expand Down

0 comments on commit b4160cf

Please sign in to comment.