Spaces:
Runtime error
Runtime error
File size: 4,867 Bytes
d8ac98f 43e1802 d8ac98f 43e1802 d8ac98f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | #!/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 |