Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
146 changes: 133 additions & 13 deletions scripts/start-docker-and-wait.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,126 @@
#!/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

# Candidate ports to try
PORTS=(1044 1045 1046 1047 1048 1049 1050)

# function to find a free port between 8000 and 9000
find_free_port() {
for port in $(seq 8000 9000); do
if ! lsof -i :"$port" >/dev/null 2>&1; then
echo "$port"
return
fi
done
echo "❌ No free port found in range 8000-9000" >&2
exit 1
}

# Pick a free port for Tomcat
TOMCAT_PORT=$(find_free_port)
echo "Using Tomcat port: $TOMCAT_PORT"

# Delete old server if it exists
echo "🧹 Cleaning old server..."
mvn openmrs-sdk:delete -DserverId="$SERVER_ID" -B || true
rm -rf "$HOME/openmrs/$SERVER_ID"
echo "✅ Old server cleaned."

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="$TOMCAT_PORT,1044,MySQL 8.4.1 and above in SDK docker container (requires pre-installed Docker),yes" \
-B

# Detect MySQL connection target
detect_mysql_connection() {
# Check if MySQL container is running with a published port
MYSQL_CONTAINER=$(docker ps --filter "name=openmrs-sdk-mysql" -q | head -n 1)

if [ -n "$MYSQL_CONTAINER" ] && nc -z 127.0.0.1 3308 >/dev/null 2>&1; then
# Use host-mapped port
echo "jdbc:mariadb://127.0.0.1:3308/openmrs-example?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"
elif nc -z 127.0.0.1 3306 >/dev/null 2>&1; then
# Maybe local MySQL running on 3306
echo "jdbc:mariadb://127.0.0.1:3306/openmrs-example?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"
else
echo "❌ No MySQL instance detected on host or Docker. Please start one." >&2
exit 1
fi
}

# Copy pre-created runtime properties
SERVER_DIR="$HOME/openmrs/$SERVER_ID"
if [ -f "$PROJECT_DIR/src/main/config/openmrs-runtime.properties" ]; then
DB_URL=$(detect_mysql_connection)
# Generate runtime properties
cat > "$SERVER_DIR/openmrs-runtime.properties" <<EOF
connection.url=$DB_URL
connection.username=root
connection.password=Admin123
auto_update_database=false
tomcatport=$TOMCAT_PORT
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


# Function to check if debug port is free
is_port_free() {
! lsof -i :$1 >/dev/null 2>&1
}

# Pick first free port
for p in "${PORTS[@]}"; do
if is_port_free "$p"; then
DEBUG_PORT=$p
break
fi
done

echo "Using debug port: $DEBUG_PORT"
# Start the server in the background
echo "▶️ Starting OpenMRS server..."
mvn openmrs-sdk:run -Ddebug="$DEBUG_PORT" -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 @@ -14,9 +129,9 @@ TIMEOUT=180

while true; do
if command -v curl &> /dev/null; then
curl -sf http://localhost:8080/openmrs && break
curl -sf http://localhost:$TOMCAT_PORT/openmrs && break
elif command -v wget &> /dev/null; then
wget -q --spider http://localhost:8080/openmrs && break
wget -q --spider http://localhost:$TOMCAT_PORT/openmrs && break
else
echo "❌ Neither curl nor wget found! Please install one of them." >&2
exit 1
Expand All @@ -32,17 +147,22 @@ 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

echo "🧹 Shutting down Docker containers..."
docker-compose -f "$DISTRO_DIR/docker-compose.yml" down
# echo "🧹 Stopping and deleting OpenMRS server..."
mvn openmrs-sdk:delete -DserverId="$SERVER_ID" -B
rm -rf $SERVER_DIR

echo "✅ Done. Server removed."
Loading