17. docker deployment

This commit is contained in:
Dobromir Popov
2025-08-05 01:22:27 +03:00
parent fa972ace8a
commit 71442f766c
17 changed files with 2925 additions and 269 deletions

View File

@ -1,112 +1,416 @@
#!/bin/bash
# Deployment script for market data infrastructure
# Run this on your Docker host at 192.168.0.10
# COBY Multi-Exchange Data Aggregation System Deployment Script
# This script handles deployment of the COBY system using Docker Compose
set -e
set -e # Exit on any error
echo "🚀 Deploying Market Data Infrastructure..."
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Check if Docker and Docker Compose are available
if ! command -v docker &> /dev/null; then
echo "❌ Docker is not installed or not in PATH"
exit 1
fi
# 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"
if ! command -v docker-compose &> /dev/null && ! docker compose version &> /dev/null; then
echo "❌ Docker Compose is not installed or not in PATH"
exit 1
fi
# Default values
ENVIRONMENT="production"
PROFILE=""
SERVICES=""
ACTION="up"
DETACHED=true
BUILD=false
PULL=false
FORCE_RECREATE=false
REMOVE_ORPHANS=true
# Set Docker Compose command
if docker compose version &> /dev/null; then
DOCKER_COMPOSE="docker compose"
else
DOCKER_COMPOSE="docker-compose"
fi
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
# Create necessary directories
echo "📁 Creating directories..."
mkdir -p ./data/timescale
mkdir -p ./data/redis
mkdir -p ./logs
mkdir -p ./backups
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
# Set proper permissions
echo "🔐 Setting permissions..."
chmod 755 ./data/timescale
chmod 755 ./data/redis
chmod 755 ./logs
chmod 755 ./backups
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
# Copy environment file if it doesn't exist
if [ ! -f .env ]; then
echo "📋 Creating .env file..."
cp .env.example .env
echo "⚠️ Please edit .env file with your specific configuration"
echo "⚠️ Default passwords are set - change them for production!"
fi
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Pull latest images
echo "📥 Pulling Docker images..."
$DOCKER_COMPOSE -f timescaledb-compose.yml pull
# Function to show usage
show_usage() {
cat << EOF
COBY Deployment Script
# Stop existing containers if running
echo "🛑 Stopping existing containers..."
$DOCKER_COMPOSE -f timescaledb-compose.yml down
Usage: $0 [OPTIONS] [ACTION] [SERVICES...]
# Start the services
echo "🏃 Starting services..."
$DOCKER_COMPOSE -f timescaledb-compose.yml up -d
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
# Wait for services to be ready
echo "⏳ Waiting for services to be ready..."
sleep 30
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
# Check service health
echo "🏥 Checking service health..."
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
# Check TimescaleDB
if docker exec market_data_timescaledb pg_isready -U market_user -d market_data; then
echo "✅ TimescaleDB is ready"
else
echo "❌ TimescaleDB is not ready"
exit 1
fi
SERVICES:
coby-app Main application
timescaledb Database
redis Cache
coby-dashboard Web dashboard
prometheus Metrics collection (monitoring profile)
grafana Visualization (monitoring profile)
# Check Redis
if docker exec market_data_redis redis-cli -a market_data_redis_2024 ping | grep -q PONG; then
echo "✅ Redis is ready"
else
echo "❌ Redis is not ready"
exit 1
fi
EOF
}
# Display connection information
echo ""
echo "🎉 Deployment completed successfully!"
echo ""
echo "📊 Connection Information:"
echo " TimescaleDB:"
echo " Host: 192.168.0.10"
echo " Port: 5432"
echo " Database: market_data"
echo " Username: market_user"
echo " Password: (check .env file)"
echo ""
echo " Redis:"
echo " Host: 192.168.0.10"
echo " Port: 6379"
echo " Password: (check .env file)"
echo ""
echo "📝 Next steps:"
echo " 1. Update your application configuration to use these connection details"
echo " 2. Test the connection from your application"
echo " 3. Set up monitoring and alerting"
echo " 4. Configure backup schedules"
echo ""
echo "🔍 To view logs:"
echo " docker-compose -f timescaledb-compose.yml logs -f"
echo ""
echo "🛑 To stop services:"
echo " docker-compose -f timescaledb-compose.yml down"
# 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