From b4160cf3e92eca8a981bf510adaffaf54a12c846 Mon Sep 17 00:00:00 2001 From: Michael Seaton Date: Thu, 7 Nov 2024 19:39:49 -0500 Subject: [PATCH] =?UTF-8?q?SDK-306=20-=20Fix=20to=20enable=20the=20config-?= =?UTF-8?q?prefixed=20distro=20properties=20to=20succ=E2=80=A6=20(#301)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../openmrs/maven/plugins/AbstractTask.java | 9 ++ .../openmrs/maven/plugins/BuildDistro.java | 46 +--------- .../java/org/openmrs/maven/plugins/Setup.java | 65 +-------------- .../utility/ConfigurationInstaller.java | 83 +++++++++++++++++++ .../maven/plugins/model/DistroProperties.java | 3 - 5 files changed, 94 insertions(+), 112 deletions(-) create mode 100644 maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/ConfigurationInstaller.java diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/AbstractTask.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/AbstractTask.java index 61a39775..b7e19161 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/AbstractTask.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/AbstractTask.java @@ -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; @@ -119,6 +120,11 @@ public abstract class AbstractTask extends AbstractMojo { */ SpaInstaller spaInstaller; + /** + * installs configuration artifacts + */ + ConfigurationInstaller configurationInstaller; + /** * handles github and provides basic git utilities */ @@ -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); } diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/BuildDistro.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/BuildDistro.java index 91ef6bde..a4d5205d 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/BuildDistro.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/BuildDistro.java @@ -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. @@ -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); @@ -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 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 owas = distroProperties.getOwaArtifacts(distroHelper, targetDirectory); diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java index b71f17fa..fc1da801 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java @@ -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; @@ -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; @@ -38,7 +35,6 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -import java.util.Objects; /** @@ -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); } @@ -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 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())) { diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/ConfigurationInstaller.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/ConfigurationInstaller.java new file mode 100644 index 00000000..eb247b48 --- /dev/null +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/ConfigurationInstaller.java @@ -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 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); + } + } + } +} diff --git a/sdk-commons/src/main/java/org/openmrs/maven/plugins/model/DistroProperties.java b/sdk-commons/src/main/java/org/openmrs/maven/plugins/model/DistroProperties.java index 4c887d34..e32d6eec 100644 --- a/sdk-commons/src/main/java/org/openmrs/maven/plugins/model/DistroProperties.java +++ b/sdk-commons/src/main/java/org/openmrs/maven/plugins/model/DistroProperties.java @@ -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++; }