Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

O3-3640: SDK to use any content packages specified in distribution properties #288

Merged
merged 37 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
384e7b3
Pull and extract the content package set in distro.prop
Aug 22, 2024
5476188
Revert "Pull and extract the content package set in distro.prop"
Aug 26, 2024
ed94bcf
Merge branch 'openmrs:master' into master
nravilla Aug 26, 2024
024cffc
To handle content packages
nravilla Aug 27, 2024
6052404
Formatting fix
nravilla Aug 27, 2024
05e7edf
Revert "Formatting fix"
nravilla Aug 27, 2024
e46fcac
Formatting fix
nravilla Aug 27, 2024
c2edc36
Formatting fix
nravilla Aug 27, 2024
6d89453
Formatting fix
nravilla Aug 27, 2024
44d620c
Formatting fix
nravilla Aug 27, 2024
3e699ce
Formatting fix
nravilla Aug 27, 2024
fbc954e
Removed unused variables
nravilla Aug 29, 2024
12fb282
Formatting fix
nravilla Aug 29, 2024
46ac43c
Formatting fix
nravilla Aug 29, 2024
cd35a01
Formatting fix
nravilla Aug 29, 2024
41c4654
Merge remote-tracking branch 'upstream/master'
nravilla Aug 29, 2024
405b4dd
Merge branch 'master' of https://github.com/nravilla/openmrs-sdk into…
nravilla Aug 29, 2024
b32c4d6
Formatting fix
nravilla Aug 30, 2024
6a68fe2
Formatting fix
nravilla Aug 30, 2024
1e50aed
Formatting fix
nravilla Aug 30, 2024
a86913c
Formatting fix
nravilla Aug 30, 2024
392fc71
Formatting fix
nravilla Aug 30, 2024
5c34131
Merge remote-tracking branch 'upstream/master' into O3-3640-new
nravilla Sep 9, 2024
776b404
Added getContentArtifacts
nravilla Sep 9, 2024
89120ef
Changed Content to ContentConfiguration
nravilla Sep 9, 2024
befffc3
Merge branch 'master' into O3-3640-new
nravilla Sep 18, 2024
37c0442
Removed serverDirectory as an instance variable from ContentHelper
nravilla Sep 19, 2024
6b5041a
removed empty method - cleanupContentTempFolder()
nravilla Sep 19, 2024
78a39b7
Changes based on PR code review
nravilla Sep 23, 2024
7db72c4
Changes based on PR code review-formatting fix
nravilla Sep 23, 2024
57484db
formatting fix
nravilla Sep 23, 2024
aa55b35
Merge branch 'O3-3640-new' of https://github.com/nravilla/openmrs-sdk…
nravilla Sep 23, 2024
8ddadf5
Removed accidently added eclipse files
nravilla Sep 23, 2024
c5e8d41
Changes based on PR code review
nravilla Sep 23, 2024
2e8af3f
Reverted changes regarding OS temp directory
nravilla Sep 23, 2024
9e9e4d2
Formatting fix
nravilla Sep 23, 2024
1fefe44
Code review and some improvements
ibacher Sep 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.openmrs.maven.plugins.model.Project;
import org.openmrs.maven.plugins.model.Server;
import org.openmrs.maven.plugins.model.Version;
import org.openmrs.maven.plugins.utility.ContentHelper;
import org.openmrs.maven.plugins.utility.DistroHelper;
import org.openmrs.maven.plugins.utility.SDKConstants;
import org.slf4j.Logger;
Expand Down Expand Up @@ -294,7 +295,6 @@ private String buildDistro(File targetDirectory, Artifact distroArtifact, Distro
File owasDir = new File(tempDir, "bundledOwas");
owasDir.mkdir();
downloadOWAs(targetDirectory, distroProperties, owasDir);

spaInstaller.installFromDistroProperties(tempDir, distroProperties, ignorePeerDependencies, overrideReuseNodeCache);
File frontendDir = new File(tempDir, "frontend");
if(frontendDir.exists()) {
Expand Down Expand Up @@ -325,8 +325,9 @@ private String buildDistro(File targetDirectory, Artifact distroArtifact, Distro
configDir.mkdir();
setConfigFolder(configDir, distroProperties, distroArtifact);

spaInstaller.installFromDistroProperties(web, distroProperties, ignorePeerDependencies, overrideReuseNodeCache);
ContentHelper.downloadAndMoveContentBackendConfig(web, distroProperties, moduleInstaller, wizard);

spaInstaller.installFromDistroProperties(web, distroProperties, ignorePeerDependencies, overrideReuseNodeCache);

File owasDir = new File(web, "owa");
owasDir.mkdir();
Expand Down Expand Up @@ -375,7 +376,6 @@ private void setConfigFolder(File configDir, DistroProperties distroProperties,
return;
}


downloadConfigs(distroProperties, configDir);

File refappConfigFile = new File(configDir, distroArtifact.getArtifactId() + "-" + distroArtifact.getVersion() + ".zip");
Expand Down Expand Up @@ -514,7 +514,7 @@ private void writeTemplatedFile(File targetDirectory, String version, String pat
}

private String adjustImageName(String part) {
return part.replaceAll("\\s+", "").toLowerCase();
return part != null ? part.replaceAll("\\s+", "").toLowerCase() : "";
}

private void copyDbDump(File targetDirectory, InputStream stream) throws MojoExecutionException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.openmrs.maven.plugins.model.DistroProperties;
import org.openmrs.maven.plugins.model.Server;
import org.openmrs.maven.plugins.model.Version;
import org.openmrs.maven.plugins.utility.ContentHelper;
import org.openmrs.maven.plugins.utility.DBConnector;
import org.openmrs.maven.plugins.utility.DistroHelper;
import org.openmrs.maven.plugins.utility.SDKConstants;
Expand Down Expand Up @@ -278,11 +279,16 @@ public void setup(Server server, DistroProperties distroProperties) throws MojoE
setServerVersionsFromDistroProperties(server, distroProperties);
distroHelper.parseContentProperties(distroProperties);
moduleInstaller.installModulesForDistro(server, distroProperties, distroHelper);
setConfigFolder(server, distroProperties);

ContentHelper.downloadAndMoveContentBackendConfig(server.getServerDirectory(), distroProperties, moduleInstaller, wizard);

if (spaInstaller != null) {
spaInstaller.installFromDistroProperties(server.getServerDirectory(), distroProperties, ignorePeerDependencies, overrideReuseNodeCache);
}

installOWAs(server, distroProperties);

setConfigFolder(server, distroProperties);
} else {
moduleInstaller.installDefaultModules(server);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.openmrs.maven.plugins.utility;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

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;

/**
* This class is downloads and moves content backend config to respective configuration folders
*/
public class ContentHelper {

public static final String FRONTEND_CONFIG_FOLDER = Paths.get("configs", "frontend_config").toString();
public static final String BACKEND_CONFIG_FOLDER = Paths.get("configs", "backend_config").toString();

public static void downloadContent(Artifact contentArtifact, ModuleInstaller moduleInstaller, File targetDir) throws MojoExecutionException {
String artifactId = contentArtifact.getArtifactId();
// create a temporary artifact folder
File sourceDir;
try {
sourceDir = Files.createTempDirectory("openmrs-sdk-" + artifactId + "-").toFile();
} catch (IOException e) {
throw new MojoExecutionException("Exception while trying to create temporary directory", e);
}

moduleInstaller.installAndUnpackModule(contentArtifact, sourceDir.getAbsolutePath());
moveBackendConfig(artifactId, sourceDir, targetDir);

FileUtils.deleteQuietly(sourceDir);
}

private static void moveBackendConfig(String artifactId, File sourceDir, File targetDir) throws MojoExecutionException {
try {
File backendConfigFiles = sourceDir.toPath().resolve(BACKEND_CONFIG_FOLDER).toFile();
Path targetPath = targetDir.toPath().toAbsolutePath();

if (backendConfigFiles.exists()) {
File[] configDirectories = backendConfigFiles.listFiles(File::isDirectory);
if (configDirectories != null) {
for (File config : configDirectories) {
Path destDir = targetPath.resolve(config.getName()).resolve(artifactId);
Files.createDirectories(destDir);

//copy config files to the matching configuration folder
FileUtils.copyDirectory(config, destDir.toFile());
}
}
}
}
catch (IOException e) {
throw new MojoExecutionException("Error copying backend configuration: " + e.getMessage(), e);
}
}

public static void downloadAndMoveContentBackendConfig(File serverDirectory, DistroProperties distroProperties, ModuleInstaller moduleInstaller, Wizard wizard) throws MojoExecutionException {
if (distroProperties != null) {
File targetDir = new File(serverDirectory, SDKConstants.OPENMRS_SERVER_CONFIGURATION);
List<Artifact> contents = distroProperties.getContentArtifacts();

if (contents != null) {
for (Artifact content : contents) {
wizard.showMessage("Downloading Content: " + content + "\n");
ContentHelper.downloadContent(content, moduleInstaller, targetDir);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.openmrs.maven.plugins.utility;

import org.apache.commons.io.FileUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.BuildPluginManager;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -10,7 +11,11 @@
import org.twdata.maven.mojoexecutor.MojoExecutor;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.twdata.maven.mojoexecutor.MojoExecutor.Element;
Expand All @@ -29,6 +34,8 @@
*/
public class ModuleInstaller {

private static final String GOAL_COPY = "copy";

private static final String GOAL_UNPACK = "unpack";

final MavenProject mavenProject;
Expand Down Expand Up @@ -74,18 +81,31 @@ public void installModulesForDistro(Server server, DistroProperties properties,
}

public void installModules(List<Artifact> artifacts, String outputDir) throws MojoExecutionException {
final String goal = "copy";
prepareModules(artifacts.toArray(new Artifact[0]), outputDir, goal);
prepareModules(artifacts.toArray(new Artifact[0]), outputDir, GOAL_COPY);
}

public void installModules(Artifact[] artifacts, String outputDir) throws MojoExecutionException {
final String goal = "copy";
prepareModules(artifacts, outputDir, goal);
prepareModules(artifacts, outputDir, GOAL_COPY);
}

public void installModule(Artifact artifact, String outputDir) throws MojoExecutionException {
final String goal = "copy";
prepareModules(new Artifact[] { artifact }, outputDir, goal);
prepareModules(new Artifact[] { artifact }, outputDir, GOAL_COPY);
}

public void installAndUnpackModule(Artifact artifact, String outputDir) throws MojoExecutionException {
Path markersDirectory;
try {
markersDirectory = Files.createTempDirectory("openmrs-sdk-markers");
} catch (IOException e) {
throw new MojoExecutionException("Error creating markers directory", e);
}

prepareModules(new Artifact[] { artifact }, outputDir, GOAL_UNPACK,
element("overWriteSnapshots", "true"),
element("overWriteReleases", "true"),
element("markersDirectory", markersDirectory.toAbsolutePath().toString()));

FileUtils.deleteQuietly(markersDirectory.toFile());
}

/**
Expand All @@ -95,18 +115,17 @@ public void installModule(Artifact artifact, String outputDir) throws MojoExecut
* @param goal
* @throws MojoExecutionException
*/
private void prepareModules(Artifact[] artifacts, String outputDir, String goal) throws MojoExecutionException {
private void prepareModules(Artifact[] artifacts, String outputDir, String goal, MojoExecutor.Element... additionalConfiguration) throws MojoExecutionException {
MojoExecutor.Element[] artifactItems = new MojoExecutor.Element[artifacts.length];
for (int index = 0; index < artifacts.length; index++) {
artifactItems[index] = artifacts[index].toElement(outputDir);
}

List<MojoExecutor.Element> configuration = new ArrayList<>();
configuration.add(element("artifactItems", artifactItems));
if (goal.equals(GOAL_UNPACK)) {
configuration.add(element("overWriteSnapshots", "true"));
configuration.add(element("overWriteReleases", "true"));
}

configuration.addAll(Arrays.asList(additionalConfiguration));

executeMojo(
plugin(
groupId(SDKConstants.DEPENDENCY_PLUGIN_GROUP_ID),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void setVersion(String version){
}

public String getName(){
return getParam("name");
return getParam("name", "openmrs");
}

public void setName(String name){
Expand Down Expand Up @@ -150,6 +150,18 @@ public List<Artifact> getConfigArtifacts() {
return artifactList;
}

public List<Artifact> getContentArtifacts() {
List<Artifact> artifacts = new ArrayList<>();
for (Object keyObject : getAllKeys()) {
String key = keyObject.toString();
if (key.startsWith(TYPE_CONTENT + ".")) {
artifacts.add(new Artifact(checkIfOverwritten(key, ARTIFACT_ID), getParam(key),
checkIfOverwritten(key, GROUP_ID), checkIfOverwritten(key, TYPE)));
}
}
return artifacts;
}

protected Set<Object> getAllKeys() {
return properties.keySet();
}
Expand Down Expand Up @@ -237,7 +249,18 @@ else if (key.equals("distro.referenceapplication")) {
* @param key
* @return
*/
public String getParam(String key) {return properties.getProperty(key); }
public String getParam(String key) {
return properties.getProperty(key);
}

/**
* get param from properties
* @param key
* @return
*/
public String getParam(String key, String defaultValue) {
return properties.getProperty(key, defaultValue);
}

public Artifact getModuleArtifact(String artifactId){
String key = TYPE_OMOD + "." + artifactId;
Expand Down
Loading