From f881029cd57038ae5e793d1d097ea78438971ce7 Mon Sep 17 00:00:00 2001 From: Michael Seaton Date: Tue, 26 Nov 2024 21:10:46 -0500 Subject: [PATCH] SDK-347 - Follow-up fix to update of owas and server properties file on deploy --- .../openmrs/maven/plugins/ServerUpgrader.java | 21 +++++++++++-- .../maven/plugins/utility/OwaHelper.java | 28 +++++++++-------- .../plugins/model/BaseSdkProperties.java | 30 ++++++++++++------- 3 files changed, 52 insertions(+), 27 deletions(-) diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/ServerUpgrader.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/ServerUpgrader.java index 4766c9a45..46161585d 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/ServerUpgrader.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/ServerUpgrader.java @@ -19,6 +19,8 @@ import java.util.List; import java.util.Map; +import static org.openmrs.maven.plugins.utility.OwaHelper.OWA_PACKAGE_EXTENSION; + public class ServerUpgrader { private final AbstractTask parentTask; @@ -96,11 +98,12 @@ public void upgradeToDistro(Server server, Distribution distribution, boolean ig parentTask.wizard.showMessage("Created directory: " + owaDir.getName()); } for (Artifact artifact : owaChanges.getArtifactsToRemove()) { - File owaFile = new File(owaDir, artifact.getDestFileName()); + String owaBaseName = parentTask.owaHelper.getOwaBaseName(artifact); + File owaFile = new File(owaDir, owaBaseName + OWA_PACKAGE_EXTENSION); if (owaFile.delete()) { parentTask.wizard.showMessage("Removed owa file: " + owaFile.getName()); } - File owaExpandedDir = new File(owaDir, artifact.getArtifactId()); + File owaExpandedDir = new File(owaDir, owaBaseName); if (owaExpandedDir.exists()) { try { FileUtils.deleteDirectory(owaExpandedDir); @@ -124,6 +127,7 @@ public void upgradeToDistro(Server server, Distribution distribution, boolean ig boolean updateSpa = spaArtifactChanges.hasChanges() || spaBuildChanges.hasChanges(); if (updateSpa) { parentTask.spaInstaller.installFromDistroProperties(server.getServerDirectory(), distroProperties, ignorePeerDependencies, overrideReuseNodeCache); + server.replaceSpaProperties(distroProperties.getSpaProperties()); } // Upgrade config and content @@ -148,13 +152,24 @@ public void upgradeToDistro(Server server, Distribution distribution, boolean ig if (configChanges.hasChanges()) { parentTask.configurationInstaller.installToServer(server, distroProperties); + for (Artifact artifact : configChanges.getArtifactsToRemove()) { + server.removePropertiesForArtifact(BaseSdkProperties.TYPE_CONFIG, artifact); + } + for (Artifact artifact : configChanges.getArtifactsToAdd()) { + server.addPropertiesForArtifact(BaseSdkProperties.TYPE_CONFIG, artifact); + } } if (contentChanges.hasChanges()) { ContentHelper.downloadAndMoveContentBackendConfig(server.getServerDirectory(), distroProperties, parentTask.moduleInstaller, parentTask.wizard); // TODO: Where is the frontend config installation? + for (Artifact artifact : contentChanges.getArtifactsToRemove()) { + server.removePropertiesForArtifact(BaseSdkProperties.TYPE_CONTENT, artifact); + } + for (Artifact artifact : contentChanges.getArtifactsToAdd()) { + server.addPropertiesForArtifact(BaseSdkProperties.TYPE_CONTENT, artifact); + } } - } server.setVersion(distroProperties.getVersion()); diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/OwaHelper.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/OwaHelper.java index d5723ac65..b339700d1 100644 --- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/OwaHelper.java +++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/OwaHelper.java @@ -22,6 +22,7 @@ import org.slf4j.LoggerFactory; import org.twdata.maven.mojoexecutor.MojoExecutor; +import javax.annotation.Nullable; import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -45,15 +46,13 @@ import static org.twdata.maven.mojoexecutor.MojoExecutor.plugin; import static org.twdata.maven.mojoexecutor.MojoExecutor.version; -import javax.annotation.Nullable; - public class OwaHelper { public static final String PACKAGE_JSON_FILENAME = "package.json"; public static final String NODE_VERSION_KEY = "node"; public static final String NPM_VERSION_KEY = "npm"; - private static final String OWA_PACKAGE_EXTENSION = ".owa"; + public static final String OWA_PACKAGE_EXTENSION = ".owa"; private MavenSession session; @@ -98,12 +97,10 @@ public void setSession(MavenSession session) { this.session = session; } - public void downloadOwa(File owaDir, Artifact owa, ModuleInstaller moduleInstaller) - throws MojoExecutionException { - boolean isSysAdmin = false; + public void downloadOwa(File owaDir, Artifact owa, ModuleInstaller moduleInstaller) throws MojoExecutionException { + if (owa.getArtifactId().startsWith("openmrs-owa-")) { owa.setArtifactId(owa.getArtifactId().substring(12)); - isSysAdmin = owa.getArtifactId().equalsIgnoreCase("sysadmin"); } moduleInstaller.installModule(owa, owaDir.getAbsolutePath()); @@ -112,12 +109,7 @@ public void downloadOwa(File owaDir, Artifact owa, ModuleInstaller moduleInstall throw new MojoExecutionException("Unable to download OWA " + owa + " from Maven"); } - File renamedFile; - if (!isSysAdmin) { - renamedFile = new File(owaDir, owa.getArtifactId() + OWA_PACKAGE_EXTENSION); - } else { - renamedFile = new File(owaDir, "SystemAdministration" + OWA_PACKAGE_EXTENSION); - } + File renamedFile = new File(owaDir, getOwaBaseName(owa) + OWA_PACKAGE_EXTENSION); if (renamedFile.exists()) { renamedFile.delete(); @@ -131,6 +123,16 @@ public void downloadOwa(File owaDir, Artifact owa, ModuleInstaller moduleInstall } } + public String getOwaBaseName(Artifact owa) { + if (owa.getArtifactId().startsWith("openmrs-owa-")) { + owa.setArtifactId(owa.getArtifactId().substring(12)); + } + if (owa.getArtifactId().equalsIgnoreCase("sysadmin")) { + return "SystemAdministration"; + } + return owa.getArtifactId(); + } + public void createOwaProject() throws MojoExecutionException { File owaDir = installationDir != null ? installationDir : prepareOwaDir(); owaDir.mkdirs(); diff --git a/sdk-commons/src/main/java/org/openmrs/maven/plugins/model/BaseSdkProperties.java b/sdk-commons/src/main/java/org/openmrs/maven/plugins/model/BaseSdkProperties.java index 9bafe2251..a4d225059 100644 --- a/sdk-commons/src/main/java/org/openmrs/maven/plugins/model/BaseSdkProperties.java +++ b/sdk-commons/src/main/java/org/openmrs/maven/plugins/model/BaseSdkProperties.java @@ -106,15 +106,15 @@ public List getModuleArtifacts() { } public List getOwaArtifacts() { - List artifacts = new ArrayList<>(); + List artifactList = new ArrayList<>(); for (Object keyObject: getAllKeys()) { String key = keyObject.toString(); - if (key.startsWith(TYPE_OWA + ".")) { - String artifactId = key.substring(TYPE_OWA.length() + 1); - artifacts.add(new Artifact(artifactId, getParam(key), Artifact.GROUP_OWA, Artifact.TYPE_ZIP)); + String artifactType = getArtifactType(key); + if(artifactType.equals(TYPE_OWA)) { + artifactList.add(new Artifact(checkIfOverwritten(key, ARTIFACT_ID), getParam(key), checkIfOverwritten(key, GROUP_ID), checkIfOverwritten(key, TYPE))); } } - return artifacts; + return artifactList; } public Map getSpaProperties() { @@ -237,10 +237,12 @@ protected String checkIfOverwritten(String key, String param) { return Artifact.GROUP_WEB; case TYPE_OMOD: return Artifact.GROUP_MODULE; + case TYPE_OWA: + return Artifact.GROUP_OWA; case TYPE_DISTRO: - case TYPE_CONFIG: - return properties.getProperty(PROPERTY_DISTRO_GROUP_ID, Artifact.GROUP_DISTRO); - case TYPE_CONTENT: + case TYPE_CONFIG: + return properties.getProperty(PROPERTY_DISTRO_GROUP_ID, Artifact.GROUP_DISTRO); + case TYPE_CONTENT: return Artifact.GROUP_CONTENT; default: return ""; @@ -252,6 +254,7 @@ protected String checkIfOverwritten(String key, String param) { return TYPE_JAR; case TYPE_WAR: return TYPE_WAR; + case TYPE_OWA: case TYPE_CONFIG: case TYPE_CONTENT: return TYPE_ZIP; @@ -340,10 +343,9 @@ public void removeModuleProperties(Artifact artifact) { public void removePropertiesForArtifact(String type, Artifact artifact) { Properties newProperties = new Properties(); - for (Object keyObject : properties.keySet()) { - String key = keyObject.toString(); + for (String key : properties.stringPropertyNames()) { if (!key.startsWith(type + "." + artifact.getArtifactId())) { - properties.put(key, properties.get(key)); + newProperties.put(key, properties.get(key)); } } properties = newProperties; @@ -356,6 +358,12 @@ public void addPropertiesForArtifact(String type, Artifact artifact) { properties.put(base + "." + TYPE, artifact.getType()); } + public void replaceSpaProperties(Map spaProperties) { + Set existingSpaProperties = getSpaProperties().keySet(); + properties.keySet().removeAll(existingSpaProperties); + properties.putAll(spaProperties); + } + /** * Removes `-omod` or `-webapp` suffix from artifact ID. *