diff --git a/pom-step-01.xml b/pom-step-01.xml index e604785..22b4cf7 100644 --- a/pom-step-01.xml +++ b/pom-step-01.xml @@ -101,7 +101,7 @@ - + org.codehaus.mojo exec-maven-plugin diff --git a/scripts/start-docker-and-wait.sh b/scripts/start-docker-and-wait.sh index be9eec8..7b27701 100644 --- a/scripts/start-docker-and-wait.sh +++ b/scripts/start-docker-and-wait.sh @@ -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" </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..." @@ -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 @@ -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."