File size: 5,236 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
201
202
203
204
@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