#!/bin/bash set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Logging functions log() { echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1" } error() { echo -e "${RED}[ERROR]${NC} $1" >&2 } success() { echo -e "${GREEN}[SUCCESS]${NC} $1" } warn() { echo -e "${YELLOW}[WARNING]${NC} $1" } # Configuration IMAGE_NAME="autonomous-demand-forecasting" CONTAINER_NAME="autonomous-demand-forecasting" PORT_UI=7860 # Functions show_help() { echo "Autonomous Demand Forecasting Agent - Deployment Script" echo "" echo "Usage: $0 [COMMAND]" echo "" echo "Commands:" echo " build Build the Docker image" echo " run Run the container" echo " stop Stop the container" echo " restart Restart the container" echo " logs Show container logs" echo " status Show container status" echo " clean Remove container and image" echo " deploy Full deployment (build + run)" echo " help Show this help message" echo "" echo "Examples:" echo " $0 deploy # Build and run the application" echo " $0 logs # View application logs" echo " $0 restart # Restart the application" } build_image() { log "๐Ÿ”จ Building Docker image: $IMAGE_NAME" docker build -t $IMAGE_NAME . success "Docker image built successfully!" } run_container() { log "๐Ÿš€ Starting container: $CONTAINER_NAME" # Stop existing container if running if docker ps -q -f name=$CONTAINER_NAME | grep -q .; then warn "Container $CONTAINER_NAME is already running. Stopping it first..." docker stop $CONTAINER_NAME docker rm $CONTAINER_NAME fi # Create necessary directories mkdir -p logs data # Run the container docker run -d \ --name $CONTAINER_NAME \ -p $PORT_UI:7860 \ -v "$(pwd)/logs:/app/logs" \ -v "$(pwd)/data:/app/data" \ -v "$(pwd)/autonomous_demand_forecasting/database:/app/autonomous_demand_forecasting/database" \ -e PORT=7860 \ -e PYTHONPATH=/app \ -e PYTHONUNBUFFERED=1 \ --restart unless-stopped \ $IMAGE_NAME success "Container started successfully!" log "๐ŸŒ Streamlit UI (Main App): http://localhost:$PORT_UI" } stop_container() { log "๐Ÿ›‘ Stopping container: $CONTAINER_NAME" if docker ps -q -f name=$CONTAINER_NAME | grep -q .; then docker stop $CONTAINER_NAME docker rm $CONTAINER_NAME success "Container stopped and removed!" else warn "Container $CONTAINER_NAME is not running" fi } restart_container() { log "๐Ÿ”„ Restarting container: $CONTAINER_NAME" stop_container run_container } show_logs() { log "๐Ÿ“‹ Showing logs for container: $CONTAINER_NAME" if docker ps -q -f name=$CONTAINER_NAME | grep -q .; then docker logs -f $CONTAINER_NAME else error "Container $CONTAINER_NAME is not running" exit 1 fi } show_status() { log "๐Ÿ“Š Container status:" if docker ps -q -f name=$CONTAINER_NAME | grep -q .; then success "Container $CONTAINER_NAME is running" docker ps -f name=$CONTAINER_NAME echo "" log "๐Ÿ” Health check:" docker exec $CONTAINER_NAME python health_check.py || warn "Health check failed" else warn "Container $CONTAINER_NAME is not running" fi } clean_up() { log "๐Ÿงน Cleaning up Docker resources" # Stop and remove container if docker ps -aq -f name=$CONTAINER_NAME | grep -q .; then docker stop $CONTAINER_NAME 2>/dev/null || true docker rm $CONTAINER_NAME 2>/dev/null || true fi # Remove image if docker images -q $IMAGE_NAME | grep -q .; then docker rmi $IMAGE_NAME fi # Clean up unused Docker resources docker system prune -f success "Cleanup completed!" } deploy() { log "๐Ÿš€ Starting full deployment..." build_image run_container # Wait a moment for container to start sleep 5 show_status success "๐ŸŽ‰ Deployment completed successfully!" log "๐Ÿ“ฑ Access your application at:" log " โ€ข Streamlit UI (Main App): http://localhost:$PORT_UI" } # Main script logic case "${1:-help}" in build) build_image ;; run) run_container ;; stop) stop_container ;; restart) restart_container ;; logs) show_logs ;; status) show_status ;; clean) clean_up ;; deploy) deploy ;; help|--help|-h) show_help ;; *) error "Unknown command: $1" show_help exit 1 ;; esac