416 lines
10 KiB
Bash
416 lines
10 KiB
Bash
#!/bin/bash
|
|
|
|
# COBY Multi-Exchange Data Aggregation System Deployment Script
|
|
# This script handles deployment of the COBY system using Docker Compose
|
|
|
|
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
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
|
|
COMPOSE_FILE="$PROJECT_ROOT/docker-compose.yml"
|
|
DEV_COMPOSE_FILE="$PROJECT_ROOT/docker-compose.dev.yml"
|
|
ENV_FILE="$PROJECT_ROOT/docker/.env"
|
|
ENV_EXAMPLE="$PROJECT_ROOT/docker/.env.example"
|
|
|
|
# Default values
|
|
ENVIRONMENT="production"
|
|
PROFILE=""
|
|
SERVICES=""
|
|
ACTION="up"
|
|
DETACHED=true
|
|
BUILD=false
|
|
PULL=false
|
|
FORCE_RECREATE=false
|
|
REMOVE_ORPHANS=true
|
|
|
|
# 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 show usage
|
|
show_usage() {
|
|
cat << EOF
|
|
COBY Deployment Script
|
|
|
|
Usage: $0 [OPTIONS] [ACTION] [SERVICES...]
|
|
|
|
ACTIONS:
|
|
up Start services (default)
|
|
down Stop and remove services
|
|
restart Restart services
|
|
logs Show service logs
|
|
ps Show running services
|
|
build Build services
|
|
pull Pull latest images
|
|
exec Execute command in service
|
|
health Check service health
|
|
|
|
OPTIONS:
|
|
-e, --env ENV Environment (production|development) [default: production]
|
|
-p, --profile PROFILE Docker compose profile (monitoring|tools)
|
|
-d, --detach Run in detached mode [default: true]
|
|
-f, --foreground Run in foreground mode
|
|
-b, --build Build images before starting
|
|
--pull Pull latest images before starting
|
|
--force-recreate Force recreate containers
|
|
--no-remove-orphans Don't remove orphaned containers
|
|
-h, --help Show this help message
|
|
|
|
EXAMPLES:
|
|
$0 # Start production environment
|
|
$0 -e development # Start development environment
|
|
$0 -p monitoring up # Start with monitoring profile
|
|
$0 down # Stop all services
|
|
$0 logs coby-app # Show logs for coby-app service
|
|
$0 exec coby-app bash # Execute bash in coby-app container
|
|
$0 -b up # Build and start services
|
|
|
|
SERVICES:
|
|
coby-app Main application
|
|
timescaledb Database
|
|
redis Cache
|
|
coby-dashboard Web dashboard
|
|
prometheus Metrics collection (monitoring profile)
|
|
grafana Visualization (monitoring profile)
|
|
|
|
EOF
|
|
}
|
|
|
|
# Function to check prerequisites
|
|
check_prerequisites() {
|
|
print_status "Checking prerequisites..."
|
|
|
|
# Check if Docker is installed and running
|
|
if ! command -v docker &> /dev/null; then
|
|
print_error "Docker is not installed. Please install Docker first."
|
|
exit 1
|
|
fi
|
|
|
|
if ! docker info &> /dev/null; then
|
|
print_error "Docker is not running. Please start Docker first."
|
|
exit 1
|
|
fi
|
|
|
|
# Check if Docker Compose is available
|
|
if ! command -v docker-compose &> /dev/null && ! docker compose version &> /dev/null; then
|
|
print_error "Docker Compose is not available. Please install Docker Compose."
|
|
exit 1
|
|
fi
|
|
|
|
# Determine compose command
|
|
if command -v docker-compose &> /dev/null; then
|
|
COMPOSE_CMD="docker-compose"
|
|
else
|
|
COMPOSE_CMD="docker compose"
|
|
fi
|
|
|
|
print_success "Prerequisites check passed"
|
|
}
|
|
|
|
# Function to setup environment file
|
|
setup_env_file() {
|
|
if [[ ! -f "$ENV_FILE" ]]; then
|
|
print_warning "Environment file not found. Creating from example..."
|
|
cp "$ENV_EXAMPLE" "$ENV_FILE"
|
|
print_status "Please edit $ENV_FILE with your configuration"
|
|
print_warning "Using default configuration for now"
|
|
else
|
|
print_success "Environment file found"
|
|
fi
|
|
}
|
|
|
|
# Function to build compose command
|
|
build_compose_command() {
|
|
local cmd="$COMPOSE_CMD"
|
|
|
|
# Add compose files
|
|
cmd="$cmd -f $COMPOSE_FILE"
|
|
|
|
if [[ "$ENVIRONMENT" == "development" ]]; then
|
|
cmd="$cmd -f $DEV_COMPOSE_FILE"
|
|
fi
|
|
|
|
# Add environment file
|
|
cmd="$cmd --env-file $ENV_FILE"
|
|
|
|
# Add profile if specified
|
|
if [[ -n "$PROFILE" ]]; then
|
|
cmd="$cmd --profile $PROFILE"
|
|
fi
|
|
|
|
echo "$cmd"
|
|
}
|
|
|
|
# Function to start services
|
|
start_services() {
|
|
print_status "Starting COBY services in $ENVIRONMENT mode..."
|
|
|
|
local cmd=$(build_compose_command)
|
|
local up_cmd="$cmd up"
|
|
|
|
if [[ "$BUILD" == true ]]; then
|
|
up_cmd="$up_cmd --build"
|
|
fi
|
|
|
|
if [[ "$PULL" == true ]]; then
|
|
up_cmd="$up_cmd --pull always"
|
|
fi
|
|
|
|
if [[ "$FORCE_RECREATE" == true ]]; then
|
|
up_cmd="$up_cmd --force-recreate"
|
|
fi
|
|
|
|
if [[ "$REMOVE_ORPHANS" == true ]]; then
|
|
up_cmd="$up_cmd --remove-orphans"
|
|
fi
|
|
|
|
if [[ "$DETACHED" == true ]]; then
|
|
up_cmd="$up_cmd -d"
|
|
fi
|
|
|
|
if [[ -n "$SERVICES" ]]; then
|
|
up_cmd="$up_cmd $SERVICES"
|
|
fi
|
|
|
|
eval "$up_cmd"
|
|
|
|
if [[ "$DETACHED" == true ]]; then
|
|
print_success "Services started successfully"
|
|
show_service_status
|
|
fi
|
|
}
|
|
|
|
# Function to stop services
|
|
stop_services() {
|
|
print_status "Stopping COBY services..."
|
|
|
|
local cmd=$(build_compose_command)
|
|
eval "$cmd down --remove-orphans"
|
|
|
|
print_success "Services stopped successfully"
|
|
}
|
|
|
|
# Function to restart services
|
|
restart_services() {
|
|
print_status "Restarting COBY services..."
|
|
|
|
local cmd=$(build_compose_command)
|
|
if [[ -n "$SERVICES" ]]; then
|
|
eval "$cmd restart $SERVICES"
|
|
else
|
|
eval "$cmd restart"
|
|
fi
|
|
|
|
print_success "Services restarted successfully"
|
|
}
|
|
|
|
# Function to show logs
|
|
show_logs() {
|
|
local cmd=$(build_compose_command)
|
|
if [[ -n "$SERVICES" ]]; then
|
|
eval "$cmd logs -f $SERVICES"
|
|
else
|
|
eval "$cmd logs -f"
|
|
fi
|
|
}
|
|
|
|
# Function to show service status
|
|
show_service_status() {
|
|
print_status "Service status:"
|
|
local cmd=$(build_compose_command)
|
|
eval "$cmd ps"
|
|
}
|
|
|
|
# Function to build services
|
|
build_services() {
|
|
print_status "Building COBY services..."
|
|
|
|
local cmd=$(build_compose_command)
|
|
if [[ -n "$SERVICES" ]]; then
|
|
eval "$cmd build $SERVICES"
|
|
else
|
|
eval "$cmd build"
|
|
fi
|
|
|
|
print_success "Services built successfully"
|
|
}
|
|
|
|
# Function to pull images
|
|
pull_images() {
|
|
print_status "Pulling latest images..."
|
|
|
|
local cmd=$(build_compose_command)
|
|
eval "$cmd pull"
|
|
|
|
print_success "Images pulled successfully"
|
|
}
|
|
|
|
# Function to execute command in service
|
|
exec_command() {
|
|
if [[ -z "$SERVICES" ]]; then
|
|
print_error "Service name required for exec command"
|
|
exit 1
|
|
fi
|
|
|
|
local service=$(echo "$SERVICES" | cut -d' ' -f1)
|
|
local command=$(echo "$SERVICES" | cut -d' ' -f2-)
|
|
|
|
if [[ "$service" == "$command" ]]; then
|
|
command="bash"
|
|
fi
|
|
|
|
local cmd=$(build_compose_command)
|
|
eval "$cmd exec $service $command"
|
|
}
|
|
|
|
# Function to check service health
|
|
check_health() {
|
|
print_status "Checking service health..."
|
|
|
|
local cmd=$(build_compose_command)
|
|
local services=$(eval "$cmd ps --services")
|
|
|
|
for service in $services; do
|
|
local health=$(eval "$cmd ps $service" | grep -o "healthy\|unhealthy\|starting" | head -1)
|
|
if [[ -n "$health" ]]; then
|
|
if [[ "$health" == "healthy" ]]; then
|
|
print_success "$service: $health"
|
|
elif [[ "$health" == "unhealthy" ]]; then
|
|
print_error "$service: $health"
|
|
else
|
|
print_warning "$service: $health"
|
|
fi
|
|
else
|
|
print_warning "$service: no health check"
|
|
fi
|
|
done
|
|
}
|
|
|
|
# Parse command line arguments
|
|
while [[ $# -gt 0 ]]; do
|
|
case $1 in
|
|
-e|--env)
|
|
ENVIRONMENT="$2"
|
|
shift 2
|
|
;;
|
|
-p|--profile)
|
|
PROFILE="$2"
|
|
shift 2
|
|
;;
|
|
-d|--detach)
|
|
DETACHED=true
|
|
shift
|
|
;;
|
|
-f|--foreground)
|
|
DETACHED=false
|
|
shift
|
|
;;
|
|
-b|--build)
|
|
BUILD=true
|
|
shift
|
|
;;
|
|
--pull)
|
|
PULL=true
|
|
shift
|
|
;;
|
|
--force-recreate)
|
|
FORCE_RECREATE=true
|
|
shift
|
|
;;
|
|
--no-remove-orphans)
|
|
REMOVE_ORPHANS=false
|
|
shift
|
|
;;
|
|
-h|--help)
|
|
show_usage
|
|
exit 0
|
|
;;
|
|
up|down|restart|logs|ps|build|pull|exec|health)
|
|
ACTION="$1"
|
|
shift
|
|
;;
|
|
*)
|
|
SERVICES="$SERVICES $1"
|
|
shift
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Trim leading/trailing spaces from services
|
|
SERVICES=$(echo "$SERVICES" | xargs)
|
|
|
|
# Main execution
|
|
main() {
|
|
print_status "COBY Multi-Exchange Data Aggregation System Deployment"
|
|
print_status "Environment: $ENVIRONMENT"
|
|
if [[ -n "$PROFILE" ]]; then
|
|
print_status "Profile: $PROFILE"
|
|
fi
|
|
if [[ -n "$SERVICES" ]]; then
|
|
print_status "Services: $SERVICES"
|
|
fi
|
|
print_status "Action: $ACTION"
|
|
echo
|
|
|
|
check_prerequisites
|
|
setup_env_file
|
|
|
|
case $ACTION in
|
|
up)
|
|
start_services
|
|
;;
|
|
down)
|
|
stop_services
|
|
;;
|
|
restart)
|
|
restart_services
|
|
;;
|
|
logs)
|
|
show_logs
|
|
;;
|
|
ps)
|
|
show_service_status
|
|
;;
|
|
build)
|
|
build_services
|
|
;;
|
|
pull)
|
|
pull_images
|
|
;;
|
|
exec)
|
|
exec_command
|
|
;;
|
|
health)
|
|
check_health
|
|
;;
|
|
*)
|
|
print_error "Unknown action: $ACTION"
|
|
show_usage
|
|
exit 1
|
|
;;
|
|
esac
|
|
}
|
|
|
|
# Run main function
|
|
main |