Skip to content

Commit

Permalink
SDK-316: Enable deploying O3 from build-distro plugin (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
wikumChamith authored Nov 6, 2023
1 parent f7508aa commit 00a10cf
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import org.apache.commons.lang.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.openmrs.maven.plugins.model.Artifact;
import org.openmrs.maven.plugins.model.DistroProperties;
import org.openmrs.maven.plugins.model.Server;
Expand All @@ -29,6 +32,7 @@
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@Mojo(name = "build-distro", requiresProject = false)
Expand Down Expand Up @@ -64,6 +68,10 @@ public class BuildDistro extends AbstractTask {

private static final String DOCKER_COMPOSE_OVERRIDE_YML = "docker-compose.override.yml";

private static final String O2_DISTRIBUTION = "2.x Distribution";

private static final String O3_DISTRIBUTION = "O3 Distribution";

private static final Logger log = LoggerFactory.getLogger(BuildDistro.class);

@Parameter(property = "distro")
Expand Down Expand Up @@ -129,14 +137,42 @@ public void executeTask() throws MojoExecutionException, MojoFailureException {
if (distroProperties == null) {
Server server = new Server.ServerBuilder().build();

wizard.promptForRefAppVersionIfMissing(server, versionsHelper, DISTRIBUTION_VERSION_PROMPT);
if (DistroHelper.isRefapp2_3_1orLower(server.getDistroArtifactId(), server.getVersion())) {
distroProperties = new DistroProperties(server.getVersion());
} else {
distroProperties = distroHelper.downloadDistroProperties(buildDirectory, server);
distroArtifact = new Artifact(server.getDistroArtifactId(), server.getVersion(), server.getDistroGroupId(),
"jar");
List<String> options = new ArrayList<>();
options.add(O2_DISTRIBUTION);
options.add(O3_DISTRIBUTION);

String choice = wizard.promptForMissingValueWithOptions("You can setup following servers", null, null, options);
switch (choice) {
case O2_DISTRIBUTION:
wizard.promptForRefAppVersionIfMissing(server, versionsHelper, DISTRIBUTION_VERSION_PROMPT);
if (DistroHelper.isRefapp2_3_1orLower(server.getDistroArtifactId(), server.getVersion())) {
distroProperties = new DistroProperties(server.getVersion());
} else {
distroProperties = distroHelper.downloadDistroProperties(buildDirectory, server);
distroArtifact = new Artifact(server.getDistroArtifactId(), server.getVersion(), server.getDistroGroupId(),
"jar");
}
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;
}

}

if (distroProperties == null) {
Expand Down Expand Up @@ -264,8 +300,8 @@ private String buildDistro(File targetDirectory, Artifact distroArtifact, Distro

wizard.showMessage("Creating Docker Compose configuration...\n");
String distroVersion = adjustImageName(distroProperties.getVersion());
writeDockerCompose(targetDirectory, distroName, distroVersion);
writeReadme(targetDirectory, distroName, distroVersion);
writeDockerCompose(targetDirectory, distroVersion);
writeReadme(targetDirectory, distroVersion);
copyBuildDistroResource("setenv.sh", new File(web, "setenv.sh"));
copyBuildDistroResource("startup.sh", new File(web, "startup.sh"));
copyBuildDistroResource("wait-for-it.sh", new File(web, "wait-for-it.sh"));
Expand Down Expand Up @@ -351,18 +387,17 @@ public boolean accept(File dir, String name) {
}
}

private void writeDockerCompose(File targetDirectory, String distro, String version) throws MojoExecutionException {
writeTemplatedFile(targetDirectory, distro, version, DOCKER_COMPOSE_PATH, DOCKER_COMPOSE_YML);
writeTemplatedFile(targetDirectory, distro, version, DOCKER_COMPOSE_OVERRIDE_PATH, DOCKER_COMPOSE_OVERRIDE_YML);
writeTemplatedFile(targetDirectory, distro, version, DOCKER_COMPOSE_PROD_PATH, DOCKER_COMPOSE_PROD_YML);
private void writeDockerCompose(File targetDirectory, String version) throws MojoExecutionException {
writeTemplatedFile(targetDirectory, version, DOCKER_COMPOSE_PATH, DOCKER_COMPOSE_YML);
writeTemplatedFile(targetDirectory, version, DOCKER_COMPOSE_OVERRIDE_PATH, DOCKER_COMPOSE_OVERRIDE_YML);
writeTemplatedFile(targetDirectory, version, DOCKER_COMPOSE_PROD_PATH, DOCKER_COMPOSE_PROD_YML);
}

private void writeReadme(File targetDirectory, String distro, String version) throws MojoExecutionException {
writeTemplatedFile(targetDirectory, distro, version, README_PATH, "README.md");
private void writeReadme(File targetDirectory, String version) throws MojoExecutionException {
writeTemplatedFile(targetDirectory, version, README_PATH, "README.md");
}

private void writeTemplatedFile(File targetDirectory, String distro, String version,
String path, String filename) throws MojoExecutionException {
private void writeTemplatedFile(File targetDirectory, String version, String path, String filename) throws MojoExecutionException {
URL composeUrl = getClass().getClassLoader().getResource(path);
if (composeUrl == null) {
throw new MojoExecutionException("Failed to find file '" + path + "' in classpath");
Expand All @@ -371,8 +406,7 @@ private void writeTemplatedFile(File targetDirectory, String distro, String vers
if (!compose.exists()) {
try (InputStream inputStream = composeUrl.openStream(); FileWriter composeWriter = new FileWriter(compose)) {
String content = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
content = content.replaceAll("<distro>", distro);
content = content.replaceAll("<tag>", version);
content = content.replaceAll("\\$\\{TAG:-nightly}", version);
composeWriter.write(content);
}
catch (IOException e) {
Expand Down
40 changes: 21 additions & 19 deletions maven-plugin/src/main/resources/build-distro/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
# Generated automatically by openmrs SDK
version: '2'
version: "3.7"

services:
db:
image: mysql:5.6
image: mariadb:10.8.2
command: "mysqld --character-set-server=utf8 --collation-server=utf8_general_ci"
environment:
MYSQL_DATABASE: openmrs
MYSQL_ROOT_PASSWORD: Admin123
MYSQL_USER: openmrs
MYSQL_PASSWORD: Admin123
MYSQL_USER: ${OPENMRS_DB_USER:-openmrs}
MYSQL_PASSWORD: ${OPENMRS_DB_PASSWORD:-openmrs}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-openmrs}
volumes:
- ./dbdump:/docker-entrypoint-initdb.d
- db-data:/var/lib/mysql

web:
image: openmrs/openmrs-referenceapplication:${TAG:-nightly}
build: web
image: <distro>:<tag>
depends_on:
- db
ports:
- "8080:8080"
environment:
DB_DATABASE: openmrs
DB_HOST: db
DB_USERNAME: openmrs
DB_PASSWORD: Admin123
DB_CREATE_TABLES: 'false' # change to 'true' if using an empty database
DB_AUTO_UPDATE: 'false' # change to 'true' if using an empty database
MODULE_WEB_ADMIN: 'true' # allow web admin on OpenMRS
OMRS_CONFIG_MODULE_WEB_ADMIN: "true"
OMRS_CONFIG_AUTO_UPDATE_DATABASE: "true"
OMRS_CONFIG_CREATE_TABLES: "true"
OMRS_CONFIG_CONNECTION_SERVER: db
OMRS_CONFIG_CONNECTION_URL: ${OPENMRS_DB_URL:-jdbc:mysql://localhost:3306/openmrs}
OMRS_CONFIG_CONNECTION_DATABASE: ${OPENMRS_DB_NAME:-openmrs}
OMRS_CONFIG_CONNECTION_USERNAME: ${OPENMRS_DB_USER:-openmrs}
OMRS_CONFIG_CONNECTION_PASSWORD: ${OPENMRS_DB_PASSWORD:-openmrs}
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/openmrs"]
timeout: 5s
volumes:
- web-data:/usr/local/tomcat/.OpenMRS
- /usr/local/tomcat/.OpenMRS/modules/ # used to mount persistent docker volume for modules
- /usr/local/tomcat/.OpenMRS/owa/ # used to mount persistent docker volume for owa
- openmrs-data:/openmrs/data/

volumes:
db-data:
web-data:
openmrs-data:
20 changes: 10 additions & 10 deletions maven-plugin/src/main/resources/build-distro/web/startup.sh
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
#!/bin/bash -eux

DB_CREATE_TABLES=${DB_CREATE_TABLES:-false}
DB_AUTO_UPDATE=${DB_AUTO_UPDATE:-false}
MODULE_WEB_ADMIN=${MODULE_WEB_ADMIN:-true}
OMRS_CONFIG_CREATE_TABLES=${OMRS_CONFIG_CREATE_TABLES:-false}
OMRS_CONFIG_AUTO_UPDATE_DATABASE=${OMRS_CONFIG_AUTO_UPDATE_DATABASE:-false}
OMRS_CONFIG_MODULE_WEB_ADMIN=${OMRS_CONFIG_MODULE_WEB_ADMIN:-true}
DEBUG=${DEBUG:-false}

cat > /usr/local/tomcat/openmrs-server.properties << EOF
install_method=auto
connection.url=jdbc\:mysql\://${DB_HOST}\:3306/${DB_DATABASE}?autoReconnect\=true&sessionVariables\=default_storage_engine\=InnoDB&useUnicode\=true&characterEncoding\=UTF-8
connection.username=${DB_USERNAME}
connection.password=${DB_PASSWORD}
connection.url=jdbc\:mysql\://${OMRS_CONFIG_CONNECTION_SERVER}\:3306/${OMRS_CONFIG_CONNECTION_DATABASE}?autoReconnect\=true&sessionVariables\=default_storage_engine\=InnoDB&useUnicode\=true&characterEncoding\=UTF-8
connection.username=${OMRS_CONFIG_CONNECTION_USERNAME}
connection.password=${OMRS_CONFIG_CONNECTION_PASSWORD}
has_current_openmrs_database=true
create_database_user=false
module_web_admin=${MODULE_WEB_ADMIN}
create_tables=${DB_CREATE_TABLES}
auto_update_database=${DB_AUTO_UPDATE}
OMRS_CONFIG_MODULE_WEB_ADMIN=${OMRS_CONFIG_MODULE_WEB_ADMIN}
create_tables=${OMRS_CONFIG_CREATE_TABLES}
auto_update_database=${OMRS_CONFIG_AUTO_UPDATE_DATABASE}
EOF

echo "------ Starting distribution -----"
cat /root/openmrs-distro.properties
echo "-----------------------------------"

# wait for mysql to initialise
/usr/local/tomcat/wait-for-it.sh --timeout=3600 ${DB_HOST}:3306
/usr/local/tomcat/wait-for-it.sh --timeout=3600 ${OMRS_CONFIG_CONNECTION_SERVER}:3306

if [ $DEBUG ]; then
export JPDA_ADDRESS="1044"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public DistroProperties downloadDistroProperties(File path, Artifact artifact) t

while (entries.hasMoreElements()) {
ZipEntry zipEntry = entries.nextElement();
if ("openmrs-distro.properties".equals(zipEntry.getName())) {
if ("openmrs-distro.properties".equals(zipEntry.getName()) || "distro.properties".equals(zipEntry.getName())) {
Properties properties = new Properties();
properties.load(zipFile.getInputStream(zipEntry));
distroProperties = new DistroProperties(properties);
Expand All @@ -260,16 +260,20 @@ public DistroProperties downloadDistroProperties(File path, Artifact artifact) t
return distroProperties;
}

public DistroProperties downloadDistroProperties(File serverPath, Server server) throws MojoExecutionException {
public DistroProperties downloadDistroProperties(File serverPath, Server server, String fileType) throws MojoExecutionException {
Artifact artifact = new Artifact(server.getDistroArtifactId(), server.getVersion(), server.getDistroGroupId(),
"jar");
fileType);
if (StringUtils.isNotBlank(artifact.getArtifactId())) {
return downloadDistroProperties(serverPath, artifact);
} else {
return null;
}
}

public DistroProperties downloadDistroProperties(File serverPath, Server server) throws MojoExecutionException {
return downloadDistroProperties(serverPath, server, "jar");
}

/**
* Distro can be passed in two ways: either as maven artifact identifier or path to distro file
* Returns null if string is invalid as path or identifier
Expand Down

0 comments on commit 00a10cf

Please sign in to comment.