#!/bin/bash # Deployment script for Observation App # This script handles the complete deployment process set -e # Exit on any error echo "🚀 Starting Observation App Deployment..." # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Function to print colored output print_status() { echo -e "${BLUE}[INFO]${NC} $1" } print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } # Check if .env file exists if [ ! -f .env ]; then print_error ".env file not found!" echo "Please create .env file before deploying." exit 1 fi # Check if public directory exists if [ ! -d public ]; then print_error "public directory not found!" echo "Please ensure public directory exists with required files." exit 1 fi # Check if public/db directory exists if [ ! -d public/db ]; then print_warning "public/db directory not found. Creating empty directory..." mkdir -p public/db fi # Stop existing containers if running print_status "Stopping existing containers..." docker-compose down 2>/dev/null || true # Build and start containers print_status "Building and starting containers..." docker-compose up --build -d # Wait for database to be ready print_status "Waiting for database to be ready..." sleep 10 # Check if database is accessible print_status "Checking database connection..." docker-compose exec observation_db mongosh --eval "db.adminCommand('ping')" > /dev/null 2>&1 if [ $? -eq 0 ]; then print_success "Database is ready!" else print_error "Database connection failed!" exit 1 fi # Import database if files exist in public/db if [ "$(ls -A public/db/*.json 2>/dev/null)" ]; then print_status "Found database files in public/db. Starting import..." # Import each JSON file for file in public/db/*.json; do filename=$(basename "$file" .json) print_status "Importing $filename..." # Copy file to container and import docker cp "$file" observation_app:/tmp/"$(basename "$file")" docker-compose exec observation_app curl -X POST -F "file=@/tmp/$(basename "$file")" http://localhost:3000/api/import-json # Clean up docker-compose exec observation_app rm "/tmp/$(basename "$file")" print_success "Imported $filename" done else print_warning "No database files found in public/db" fi # Check application health print_status "Checking application health..." sleep 5 if curl -f http://localhost:3000/api/health > /dev/null 2>&1; then print_success "Application is healthy!" else print_warning "Application health check failed, but deployment may still be successful" fi # Display deployment information echo "" print_success "🎉 Deployment completed successfully!" echo "" echo "📋 Service Information:" echo " • Application URL: http://localhost:3000" echo " • Database URL: mongodb://admin:password123@localhost:27017" echo " • Database Name: beanstalk" echo "" echo "🔧 Useful Commands:" echo " • View logs: docker-compose logs -f" echo " • Stop services: docker-compose down" echo " • Restart services: docker-compose restart" echo " • Access database: docker-compose exec observation_db mongosh" echo "" echo "📁 Data Locations:" echo " • Images: ./public/observations/ (copied to container)" echo " • Database files: ./public/db/ (imported to MongoDB)" echo " • MongoDB data: Docker volume 'mongo_data'" echo "" # Show container status print_status "Container Status:" docker-compose ps