- Pass service registry to load balancer for dependency injection - Remove dynamic imports of service registry in load balancer - Update service registration and health check logic - Enable token-service in docker-compose and service config - Add room names and rooms proxy endpoints - Improve logging for proxy requests and health checks - Update deploy script project name to sa4cps - Add test script for coroutine fix - Minor code cleanup and formatting
310 lines
7.8 KiB
Bash
Executable File
310 lines
7.8 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Energy Management Microservices Deployment Script
|
|
# This script handles deployment, startup, and management of all microservices
|
|
|
|
set -e # Exit on any error
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Configuration
|
|
COMPOSE_FILE="docker-compose.yml"
|
|
PROJECT_NAME="sa4cps"
|
|
|
|
# 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"
|
|
}
|
|
|
|
# Function to check if Docker and Docker Compose are installed
|
|
check_dependencies() {
|
|
print_status "Checking dependencies..."
|
|
|
|
if ! command -v docker &> /dev/null; then
|
|
print_error "Docker is not installed. Please install Docker first."
|
|
exit 1
|
|
fi
|
|
|
|
if ! command -v docker compose &> /dev/null; then
|
|
print_error "Docker Compose is not installed. Please install Docker Compose first."
|
|
exit 1
|
|
fi
|
|
|
|
print_success "Dependencies check passed"
|
|
}
|
|
|
|
# Function to create necessary directories and files
|
|
setup_environment() {
|
|
print_status "Setting up environment..."
|
|
|
|
# Create nginx configuration directory
|
|
mkdir -p nginx/ssl
|
|
|
|
# Create init-mongo directory for database initialization
|
|
mkdir -p init-mongo
|
|
|
|
# Create a simple nginx configuration if it doesn't exist
|
|
if [ ! -f "nginx/nginx.conf" ]; then
|
|
cat > nginx/nginx.conf << 'EOF'
|
|
events {
|
|
worker_connections 1024;
|
|
}
|
|
|
|
http {
|
|
upstream api_gateway {
|
|
server api-gateway:8000;
|
|
}
|
|
|
|
server {
|
|
listen 80;
|
|
|
|
location / {
|
|
proxy_pass http://api_gateway;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
}
|
|
|
|
location /ws {
|
|
proxy_pass http://api_gateway;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Upgrade $http_upgrade;
|
|
proxy_set_header Connection "upgrade";
|
|
proxy_set_header Host $host;
|
|
}
|
|
}
|
|
}
|
|
EOF
|
|
print_success "Created nginx configuration"
|
|
fi
|
|
|
|
# Create MongoDB initialization script if it doesn't exist
|
|
if [ ! -f "init-mongo/init.js" ]; then
|
|
cat > init-mongo/init.js << 'EOF'
|
|
// MongoDB initialization script
|
|
db = db.getSiblingDB('energy_dashboard');
|
|
db.createUser({
|
|
user: 'dashboard_user',
|
|
pwd: 'dashboard_pass',
|
|
roles: [
|
|
{ role: 'readWrite', db: 'energy_dashboard' },
|
|
{ role: 'readWrite', db: 'energy_dashboard_tokens' },
|
|
{ role: 'readWrite', db: 'energy_dashboard_batteries' },
|
|
{ role: 'readWrite', db: 'energy_dashboard_demand_response' },
|
|
{ role: 'readWrite', db: 'energy_dashboard_p2p' },
|
|
{ role: 'readWrite', db: 'energy_dashboard_forecasting' },
|
|
{ role: 'readWrite', db: 'energy_dashboard_iot' }
|
|
]
|
|
});
|
|
|
|
// Create initial collections and indexes
|
|
db.sensors.createIndex({ "sensor_id": 1 }, { unique: true });
|
|
db.sensor_readings.createIndex({ "sensor_id": 1, "timestamp": -1 });
|
|
db.room_metrics.createIndex({ "room": 1, "timestamp": -1 });
|
|
|
|
print("MongoDB initialization completed");
|
|
EOF
|
|
print_success "Created MongoDB initialization script"
|
|
fi
|
|
|
|
print_success "Environment setup completed"
|
|
}
|
|
|
|
# Function to build all services
|
|
build_services() {
|
|
print_status "Building all microservices..."
|
|
|
|
docker compose -f $COMPOSE_FILE build
|
|
|
|
if [ $? -eq 0 ]; then
|
|
print_success "All services built successfully"
|
|
else
|
|
print_error "Failed to build services"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Function to start all services
|
|
start_services() {
|
|
print_status "Starting all services..."
|
|
|
|
docker compose -f $COMPOSE_FILE up -d
|
|
|
|
if [ $? -eq 0 ]; then
|
|
print_success "All services started successfully"
|
|
else
|
|
print_error "Failed to start services"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Function to stop all services
|
|
stop_services() {
|
|
print_status "Stopping all services..."
|
|
|
|
docker compose -f $COMPOSE_FILE down
|
|
|
|
print_success "All services stopped"
|
|
}
|
|
|
|
# Function to restart all services
|
|
restart_services() {
|
|
stop_services
|
|
start_services
|
|
}
|
|
|
|
# Function to show service status
|
|
show_status() {
|
|
print_status "Service status:"
|
|
docker compose -f $COMPOSE_FILE ps
|
|
|
|
print_status "Service health checks:"
|
|
|
|
# Wait a moment for services to start
|
|
sleep 5
|
|
|
|
# services=("api-gateway:8000" "token-service:8001" "battery-service:8002" "demand-response-service:8003")
|
|
services=("api-gateway:8000" "token-service:8001")
|
|
for service in "${services[@]}"; do
|
|
name="${service%:*}"
|
|
port="${service#*:}"
|
|
|
|
if curl -f -s "http://localhost:$port/health" > /dev/null; then
|
|
print_success "$name is healthy"
|
|
else
|
|
print_warning "$name is not responding to health checks"
|
|
fi
|
|
done
|
|
}
|
|
|
|
# Function to view logs
|
|
view_logs() {
|
|
if [ -z "$2" ]; then
|
|
print_status "Showing logs for all services..."
|
|
docker compose -f $COMPOSE_FILE logs -f
|
|
else
|
|
print_status "Showing logs for $2..."
|
|
docker compose -f $COMPOSE_FILE logs -f $2
|
|
fi
|
|
}
|
|
|
|
# Function to clean up everything
|
|
cleanup() {
|
|
print_warning "This will remove all containers, images, and volumes. Are you sure? (y/N)"
|
|
read -r response
|
|
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
|
|
print_status "Cleaning up everything..."
|
|
docker compose -f $COMPOSE_FILE down -v --rmi all
|
|
docker system prune -f
|
|
print_success "Cleanup completed"
|
|
else
|
|
print_status "Cleanup cancelled"
|
|
fi
|
|
}
|
|
|
|
# Function to run database migrations or setup
|
|
setup_database() {
|
|
print_status "Setting up databases..."
|
|
|
|
# Wait for MongoDB to be ready
|
|
print_status "Waiting for MongoDB to be ready..."
|
|
sleep 10
|
|
|
|
# Run any additional setup scripts here
|
|
print_success "Database setup completed"
|
|
}
|
|
|
|
# Function to show help
|
|
show_help() {
|
|
echo "Energy Management Microservices Deployment Script"
|
|
echo ""
|
|
echo "Usage: $0 [COMMAND]"
|
|
echo ""
|
|
echo "Commands:"
|
|
echo " setup Setup environment and dependencies"
|
|
echo " build Build all microservices"
|
|
echo " start Start all services"
|
|
echo " stop Stop all services"
|
|
echo " restart Restart all services"
|
|
echo " status Show service status and health"
|
|
echo " logs Show logs for all services"
|
|
echo " logs <svc> Show logs for specific service"
|
|
echo " deploy Full deployment (setup + build + start)"
|
|
echo " db-setup Setup databases"
|
|
echo " cleanup Remove all containers, images, and volumes"
|
|
echo " help Show this help message"
|
|
echo ""
|
|
echo "Examples:"
|
|
echo " $0 deploy # Full deployment"
|
|
echo " $0 logs battery-service # Show battery service logs"
|
|
echo " $0 status # Check service health"
|
|
}
|
|
|
|
# Main script logic
|
|
case "${1:-help}" in
|
|
setup)
|
|
check_dependencies
|
|
setup_environment
|
|
;;
|
|
build)
|
|
check_dependencies
|
|
build_services
|
|
;;
|
|
start)
|
|
check_dependencies
|
|
start_services
|
|
;;
|
|
stop)
|
|
stop_services
|
|
;;
|
|
restart)
|
|
restart_services
|
|
;;
|
|
status)
|
|
show_status
|
|
;;
|
|
logs)
|
|
view_logs $@
|
|
;;
|
|
deploy)
|
|
check_dependencies
|
|
setup_environment
|
|
build_services
|
|
start_services
|
|
setup_database
|
|
show_status
|
|
;;
|
|
db-setup)
|
|
setup_database
|
|
;;
|
|
cleanup)
|
|
cleanup
|
|
;;
|
|
help|--help|-h)
|
|
show_help
|
|
;;
|
|
*)
|
|
print_error "Unknown command: $1"
|
|
show_help
|
|
exit 1
|
|
;;
|
|
esac
|