VAH6002's picture
Upload 18 files
43e1802 verified
Raw History Blame Contribute Delete
5.24 kB
@echo off
setlocal enabledelayedexpansion
:: Configuration
set IMAGE_NAME=autonomous-demand-forecasting
set CONTAINER_NAME=autonomous-demand-forecasting
set PORT_UI=7860
:: Colors (limited in Windows batch)
set "GREEN=[92m"
set "RED=[91m"
set "YELLOW=[93m"
set "BLUE=[94m"
set "NC=[0m"
:: Functions
goto :main
:log
echo %BLUE%[%date% %time%]%NC% %~1
goto :eof
:error
echo %RED%[ERROR]%NC% %~1 >&2
goto :eof
:success
echo %GREEN%[SUCCESS]%NC% %~1
goto :eof
:warn
echo %YELLOW%[WARNING]%NC% %~1
goto :eof
:show_help
echo Autonomous Demand Forecasting Agent - Deployment Script
echo.
echo Usage: %~nx0 [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 %~nx0 deploy # Build and run the application
echo %~nx0 logs # View application logs
echo %~nx0 restart # Restart the application
goto :eof
:build_image
call :log "Building Docker image: %IMAGE_NAME%"
docker build -t %IMAGE_NAME% .
if !errorlevel! equ 0 (
call :success "Docker image built successfully!"
) else (
call :error "Failed to build Docker image"
exit /b 1
)
goto :eof
:run_container
call :log "Starting container: %CONTAINER_NAME%"
:: Stop existing container if running
docker ps -q -f name=%CONTAINER_NAME% >nul 2>&1
if !errorlevel! equ 0 (
call :warn "Container %CONTAINER_NAME% is already running. Stopping it first..."
docker stop %CONTAINER_NAME% >nul 2>&1
docker rm %CONTAINER_NAME% >nul 2>&1
)
:: Create necessary directories
if not exist logs mkdir logs
if not exist data mkdir data
:: Run the container
docker run -d ^
--name %CONTAINER_NAME% ^
-p %PORT_UI%:7860 ^
-v "%cd%/logs:/app/logs" ^
-v "%cd%/data:/app/data" ^
-v "%cd%/autonomous_demand_forecasting/database:/app/autonomous_demand_forecasting/database" ^
-e PORT=7860 ^
-e PYTHONPATH=/app ^
-e PYTHONUNBUFFERED=1 ^
--restart unless-stopped ^
%IMAGE_NAME%
if !errorlevel! equ 0 (
call :success "Container started successfully!"
call :log "Streamlit UI (Main App): http://localhost:%PORT_UI%"
) else (
call :error "Failed to start container"
exit /b 1
)
goto :eof
:stop_container
call :log "Stopping container: %CONTAINER_NAME%"
docker ps -q -f name=%CONTAINER_NAME% >nul 2>&1
if !errorlevel! equ 0 (
docker stop %CONTAINER_NAME% >nul 2>&1
docker rm %CONTAINER_NAME% >nul 2>&1
call :success "Container stopped and removed!"
) else (
call :warn "Container %CONTAINER_NAME% is not running"
)
goto :eof
:restart_container
call :log "Restarting container: %CONTAINER_NAME%"
call :stop_container
call :run_container
goto :eof
:show_logs
call :log "Showing logs for container: %CONTAINER_NAME%"
docker ps -q -f name=%CONTAINER_NAME% >nul 2>&1
if !errorlevel! equ 0 (
docker logs -f %CONTAINER_NAME%
) else (
call :error "Container %CONTAINER_NAME% is not running"
exit /b 1
)
goto :eof
:show_status
call :log "Container status:"
docker ps -q -f name=%CONTAINER_NAME% >nul 2>&1
if !errorlevel! equ 0 (
call :success "Container %CONTAINER_NAME% is running"
docker ps -f name=%CONTAINER_NAME%
echo.
call :log "Health check:"
docker exec %CONTAINER_NAME% python health_check.py
if !errorlevel! neq 0 call :warn "Health check failed"
) else (
call :warn "Container %CONTAINER_NAME% is not running"
)
goto :eof
:clean_up
call :log "Cleaning up Docker resources"
:: Stop and remove container
docker ps -aq -f name=%CONTAINER_NAME% >nul 2>&1
if !errorlevel! equ 0 (
docker stop %CONTAINER_NAME% >nul 2>&1
docker rm %CONTAINER_NAME% >nul 2>&1
)
:: Remove image
docker images -q %IMAGE_NAME% >nul 2>&1
if !errorlevel! equ 0 (
docker rmi %IMAGE_NAME% >nul 2>&1
)
:: Clean up unused Docker resources
docker system prune -f >nul 2>&1
call :success "Cleanup completed!"
goto :eof
:deploy
call :log "Starting full deployment..."
call :build_image
if !errorlevel! neq 0 exit /b 1
call :run_container
if !errorlevel! neq 0 exit /b 1
:: Wait a moment for container to start
timeout /t 5 /nobreak >nul
call :show_status
call :success "Deployment completed successfully!"
call :log "Access your application at:"
call :log " • Streamlit UI (Main App): http://localhost:%PORT_UI%"
goto :eof
:main
if "%~1"=="" goto :show_help
if "%~1"=="build" goto :build_image
if "%~1"=="run" goto :run_container
if "%~1"=="stop" goto :stop_container
if "%~1"=="restart" goto :restart_container
if "%~1"=="logs" goto :show_logs
if "%~1"=="status" goto :show_status
if "%~1"=="clean" goto :clean_up
if "%~1"=="deploy" goto :deploy
if "%~1"=="help" goto :show_help
if "%~1"=="--help" goto :show_help
if "%~1"=="-h" goto :show_help
call :error "Unknown command: %~1"
call :show_help
exit /b 1