Skip to content

Commit

Permalink
SDK-347 - Follow-up fix to update of owas and server properties file …
Browse files Browse the repository at this point in the history
…on deploy
  • Loading branch information
mseaton committed Nov 27, 2024
1 parent 1a79cfe commit f881029
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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());
Expand All @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ public List<Artifact> getModuleArtifacts() {
}

public List<Artifact> getOwaArtifacts() {
List<Artifact> artifacts = new ArrayList<>();
List<Artifact> 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<String, String> getSpaProperties() {
Expand Down Expand Up @@ -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 "";
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -356,6 +358,12 @@ public void addPropertiesForArtifact(String type, Artifact artifact) {
properties.put(base + "." + TYPE, artifact.getType());
}

public void replaceSpaProperties(Map<String, String> spaProperties) {
Set<String> existingSpaProperties = getSpaProperties().keySet();
properties.keySet().removeAll(existingSpaProperties);
properties.putAll(spaProperties);
}

/**
* Removes `-omod` or `-webapp` suffix from artifact ID.
*
Expand Down

0 comments on commit f881029

Please sign in to comment.