Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
2 changes: 1 addition & 1 deletion pom-step-01.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
</execution>
</executions>
</plugin>
<!-- Start Docker Compose and wait to copy checksums-->
<!-- Start openMRS SDK and wait to copy checksums-->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
Expand Down
96 changes: 85 additions & 11 deletions scripts/start-docker-and-wait.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,72 @@
#!/bin/bash
set -e

# Directory where docker-compose.yml lives; default to target/distro if not passed
# Directory where the distro is located; default to target/distro
DISTRO_DIR="${1:-../target/distro}"
SERVER_ID="${2:-openmrs-example}"
# Project directory (one level up from script location)
PROJECT_DIR="$(cd "$(dirname "$0")/../" && pwd)"

echo "🚀 Starting OpenMRS in Docker from $DISTRO_DIR..."
docker-compose -f "$DISTRO_DIR/docker-compose.yml" up -d web
# Ensure JAVA_HOME is set
if [ -z "$JAVA_HOME" ]; then
echo "❌ JAVA_HOME is not set. Please set it to your Java installation."
exit 1
fi

# Delete old server if it exists
mvn openmrs-sdk:delete -DserverId="$SERVER_ID" -B || true

echo "🚀 Starting OpenMRS using the SDK from $DISTRO_DIR..."


# Run SDK setup (automated, Docker-based MySQL if Docker available)
mvn openmrs-sdk:setup \
-DserverId="$SERVER_ID" \
-Ddistro="$DISTRO_DIR/web/openmrs-distro.properties" \
-DjavaHome="$JAVA_HOME" \
-DbatchAnswers="8080,1044,MySQL 8.4.1 and above in SDK docker container (requires pre-installed Docker),yes" \
-B

# Copy pre-created runtime properties
SERVER_DIR="$HOME/openmrs/$SERVER_ID"
if [ -f "$PROJECT_DIR/src/main/config/openmrs-runtime.properties" ]; then
# Generate runtime properties
cat > "$SERVER_DIR/openmrs-runtime.properties" <<EOF
connection.url=jdbc:mariadb://127.0.0.1:3308/openmrs-example?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
connection.username=root
connection.password=Admin123
Copy link
Member

@Ruhanga Ruhanga Sep 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to detect whether a MySQL instance is running on the host machine or inside a Docker container, so the script can dynamically choose the correct connection URL—either jdbc:mariadb://127.0.0.1:* for a host-based instance, or jdbc:mariadb://<mysql-service-name>:* for a Docker-based instance?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure @Ruhanga let me address it

auto_update_database=false
tomcatport=8080
EOF

echo "✅ Generated openmrs-runtime.properties at $SERVER_DIR"
else
echo "⚠️ No openmrs-runtime.properties found. SDK will run first-time setup wizard."
fi

# Import pre-populated database dump
DB_DUMP=$(ls "$PROJECT_DIR/src/main/db/demo-db-"*.sql 2>/dev/null | head -n 1)
if [ -f "$DB_DUMP" ]; then
echo "📦 Importing pre-populated database dump into SDK MySQL container..."
MYSQL_CONTAINER=$(docker ps --filter "name=openmrs-sdk-mysql" -q | head -n 1)
if [ -n "$MYSQL_CONTAINER" ]; then
docker exec -i "$MYSQL_CONTAINER" sh -c "mysql -u root -pAdmin123 openmrs-example" < "$DB_DUMP"
echo "✅ Database imported successfully."
else
echo "❌ MySQL SDK container not running. Database import skipped."
fi
else
echo "⚠️ No pre-populated database dump found at $DB_DUMP. SDK will create empty DB."
fi

# Start the server in the background
echo "▶️ Starting OpenMRS server..."
mvn openmrs-sdk:run -DserverId="$SERVER_ID" \
-DjvmArgs='--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED' -B \
> "$SERVER_DIR/openmrs.log" 2>&1 &

SERVER_PID=$!
echo "🔹 OpenMRS SDK started in background (PID: $SERVER_PID). Logs: $SERVER_DIR/openmrs.log"

# Wait for OpenMRS to start (max 180 seconds)
echo "⏳ Waiting for OpenMRS to initialize..."
Expand All @@ -32,17 +93,30 @@ while true; do
sleep 5
done

echo "✅ OpenMRS is up. Proceeding to copy configuration checksums..."
echo "✅ OpenMRS is up and running."

CONTAINER_ID=$(docker-compose -f "$DISTRO_DIR/docker-compose.yml" ps -q web)
# SDK stores servers under $SERVER_DIR. Openmrs-sdk generates config files automatically.
CHECKSUM_DIR="$SERVER_DIR/configuration_checksums"

# Try to copy the checksum file
echo "📦 Attempting to extract openmrs_config_checksums..."
if docker cp "$CONTAINER_ID":/openmrs/data/configuration_checksums "$DISTRO_DIR/web/openmrs_config_checksums"; then
if [ -d "$CHECKSUM_DIR" ]; then
echo "📦 Copying configuration checksums to $DISTRO_DIR/web/openmrs_config_checksums..."
mkdir -p "$DISTRO_DIR/web"
cp -r "$CHECKSUM_DIR" "$DISTRO_DIR/web/openmrs_config_checksums"
echo "✅ Checksums copied successfully."
else
echo "❌ Failed to copy checksums file. File may not exist yet."
echo "⚠️ Checksums directory not found yet. It may be created later by the SDK."
fi

# Stop any existing server using 8080
echo "🔍 Checking if port 8080 is in use..."
PID=$(lsof -ti:8080 || true)
if [ -n "$PID" ]; then
echo "⚠️ Port 8080 is in use by PID $PID. Stopping it..."
kill -9 $PID
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works if I don’t care about what’s running on port 8080. But what if I do have a process I want to keep there? Killing it blindly might stop something important. Maybe we should add a check or a prompt before killing, or even allow overriding the port instead of always freeing 8080.

Something like:

  echo "Port $PORT is in use by PID $PID."
  read -p "Do you want to stop this process? (y/N) " choice

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to use a free port within a specific range, such as 8000 to 9000.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh that makes sense @mherman22 .... let me update it to go as @Ruhanga suggests

echo "✅ Freed up port 8080."
fi
# echo "🧹 Stopping and deleting OpenMRS server..."
mvn openmrs-sdk:delete -DserverId="$SERVER_ID" -B
rm -rf $SERVER_DIR

echo "🧹 Shutting down Docker containers..."
docker-compose -f "$DISTRO_DIR/docker-compose.yml" down
echo "✅ Done. Server removed."
Loading