Skip to content

Commit

Permalink
SDK-306 - Move common code into installer
Browse files Browse the repository at this point in the history
  • Loading branch information
mseaton committed Oct 31, 2024
1 parent 59a76b8 commit 62603c2
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 92 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,8 +32,6 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

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

File configDir = new File(web, SDKConstants.OPENMRS_SERVER_CONFIGURATION);
if (!configDir.mkdir()) {
throw new MojoExecutionException("Failed to create " + configDir.getAbsolutePath() + " directory");
}
installConfiguration(configDir, distroProperties);
configurationInstaller.installToDirectory(configDir, distroProperties);

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

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

/**
* Installs configuration based on config.xyz distro properties
*
* @param configDir The directory into which to load the configuration
* @param distroProperties The distro properties containing the configuration information.
*/
private void installConfiguration(File configDir, DistroProperties distroProperties) throws MojoExecutionException {
if (distroProperties.getConfigArtifacts().isEmpty()) {
return;
}

wizard.showMessage("Downloading Configs...\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
File tempConfigDir = new File(configDir.getParentFile(), UUID.randomUUID().toString());
try {
if (!tempConfigDir.mkdir()) {
throw new MojoExecutionException("Unable to create temporary directory " + tempConfigDir.getAbsolutePath() + "\n");
}
moduleInstaller.installAndUnpackModule(configArtifact, tempConfigDir.getAbsolutePath());
File directoryToCopy = tempConfigDir;
for (File f : Objects.requireNonNull(tempConfigDir.listFiles())) {
if (f.isDirectory() && f.getName().equals("openmrs_config")) {
directoryToCopy = f;
}
}
try {
FileUtils.copyDirectory(directoryToCopy, configDir);
} catch (IOException e) {
throw new MojoExecutionException("Unable to copy config: " + directoryToCopy.getAbsolutePath() + "\n");
}
}
finally {
FileUtils.deleteQuietly(tempConfigDir);
}
}
}

private void downloadOWAs(File targetDirectory, DistroProperties distroProperties, File owasDir)
throws MojoExecutionException {
List<Artifact> owas = distroProperties.getOwaArtifacts(distroHelper, targetDirectory);
Expand Down
47 changes: 1 addition & 46 deletions maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public void setup(Server server, DistroProperties distroProperties) throws MojoE
}

installOWAs(server, distroProperties);
installConfiguration(server, distroProperties);
configurationInstaller.installToServer(server, distroProperties);
} else {
moduleInstaller.installDefaultModules(server);
}
Expand Down Expand Up @@ -348,51 +348,6 @@ private void downloadOWAs(File targetDirectory, DistroProperties distroPropertie
}
}

/**
* Installs configuration based on config.xyz distro properties
*
* @param server The server for which to set the configuration folder.
* @param distroProperties The distro properties containing the configuration information.
*/
private void installConfiguration(Server server, DistroProperties distroProperties) throws MojoExecutionException {
if (distroProperties.getConfigArtifacts().isEmpty()) {
return;
}

wizard.showMessage("Downloading Configs...\n");
File configDir = new File(server.getServerDirectory(), SDKConstants.OPENMRS_SERVER_CONFIGURATION);
if (configDir.mkdir()) {
wizard.showMessage("Created directory " + configDir.getAbsolutePath() + "\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
File tempConfigDir = new File(server.getServerTmpDirectory(), UUID.randomUUID().toString());
try {
if (!tempConfigDir.mkdirs()) {
throw new MojoExecutionException("Unable to create temporary directory " + tempConfigDir.getAbsolutePath() + "\n");
}
moduleInstaller.installAndUnpackModule(configArtifact, tempConfigDir.getAbsolutePath());
File directoryToCopy = tempConfigDir;
for (File f : Objects.requireNonNull(tempConfigDir.listFiles())) {
if (f.isDirectory() && f.getName().equals("openmrs_config")) {
directoryToCopy = f;
}
}
try {
FileUtils.copyDirectory(directoryToCopy, configDir);
} catch (IOException e) {
throw new MojoExecutionException("Unable to copy config: " + directoryToCopy.getAbsolutePath() + "\n");
}
}
finally {
FileUtils.deleteQuietly(tempConfigDir);
}
}
}

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,89 @@
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);
if (directory.exists()) {
throw new MojoExecutionException("Server configuration directory already exists");
}
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.exists()) {
throw new MojoExecutionException("Server configuration directory already exists");
}
if (!installDir.mkdirs()) {
throw new MojoExecutionException("Unable to create directory: " + installDir);
}

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

0 comments on commit 62603c2

Please sign in to comment.