From 393b7baa8782f9710bde0cc46b38ab49e49fc161 Mon Sep 17 00:00:00 2001 From: Wikum Weerakutti Date: Tue, 30 Jul 2024 21:58:56 +0530 Subject: [PATCH] O3-3426: Update build-distro command to use Maven artifacts --- .../openmrs/maven/plugins/BuildDistro.java | 74 +++++++++++++++---- .../build-distro/web/Dockerfile-tomcat8 | 2 + 2 files changed, 60 insertions(+), 16 deletions(-) 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 17b590700..40f3ccf21 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 @@ -34,6 +34,7 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; +import java.util.Objects; /** * Create docker configuration for distributions. @@ -120,6 +121,9 @@ public class BuildDistro extends AbstractTask { @Parameter(defaultValue = "false", property = "reset") private boolean reset; + @Parameter(property = "appShellVersion") + private String appShellVersion; + @Override public void executeTask() throws MojoExecutionException, MojoFailureException { File buildDirectory = getBuildDirectory(); @@ -176,22 +180,9 @@ public void executeTask() throws MojoExecutionException, MojoFailureException { break; case O3_DISTRIBUTION: wizard.promptForO3RefAppVersionIfMissing(server, versionsHelper); - Artifact artifact = new Artifact(server.getDistroArtifactId(), server.getVersion(), server.getDistroGroupId(), "zip"); - try { - String gitHubUrl = "https://github.com/openmrs/openmrs-distro-referenceapplication"; - CloneCommand cloneCommand = Git.cloneRepository().setURI(gitHubUrl) - .setDirectory(new File(dir)); - if (!server.getVersion().equals(versionsHelper.getLatestSnapshotVersion(artifact))) { - cloneCommand.setBranch(server.getVersion()); - } - wizard.showMessage("Cloning from " + gitHubUrl); - cloneCommand.call(); - } catch (GitAPIException e) { - throw new RuntimeException(e); - } - wizard.showMessage("The '"+ artifact.getArtifactId() +" " + artifact.getVersion() + - "' distribution created! To start up the server run 'docker-compose up' from" + buildDirectory.getAbsolutePath()); - return; + server.setServerDirectory(buildDirectory); + distroArtifact = new Artifact(server.getDistroArtifactId(), server.getVersion(), server.getDistroGroupId(), "zip"); + distroProperties = new DistroProperties(distroHelper.getArtifactProperties(distroArtifact, server, appShellVersion)); } } @@ -312,8 +303,16 @@ private String buildDistro(File targetDirectory, Artifact distroArtifact, Distro moduleInstaller.installModules(distroProperties.getModuleArtifacts(distroHelper, targetDirectory), modulesDir.getAbsolutePath()); + File frontendDir = new File(web, "frontend"); + frontendDir.mkdir(); + + File configDir = new File(web, SDKConstants.OPENMRS_SERVER_CONFIGURATION); + configDir.mkdir(); + setConfigFolder(configDir, distroProperties, distroArtifact); + spaInstaller.installFromDistroProperties(web, distroProperties, ignorePeerDependencies, overrideReuseNodeCache); + File owasDir = new File(web, "owa"); owasDir.mkdir(); downloadOWAs(targetDirectory, distroProperties, owasDir); @@ -341,6 +340,49 @@ 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/resources/build-distro/web/Dockerfile-tomcat8 b/maven-plugin/src/main/resources/build-distro/web/Dockerfile-tomcat8 index 5d629537c..6897374a3 100644 --- a/maven-plugin/src/main/resources/build-distro/web/Dockerfile-tomcat8 +++ b/maven-plugin/src/main/resources/build-distro/web/Dockerfile-tomcat8 @@ -5,6 +5,8 @@ FROM tomcat:8-jre8 COPY openmrs.war /usr/local/tomcat/webapps/openmrs.war COPY modules /usr/local/tomcat/.OpenMRS/modules COPY owa /usr/local/tomcat/.OpenMRS/owa +COPY frontend /usr/local/tomcat/.OpenMRS/frontend +COPY configuration /usr/local/tomcat/.OpenMRS/configuration COPY setenv.sh /usr/local/tomcat/bin/setenv.sh