|
| 1 | +@echo off |
| 2 | +setlocal EnableDelayedExpansion |
| 3 | + |
| 4 | +rem ============================================================================ |
| 5 | +rem Local Image Builder for Demo Environment (Windows Batch Version) |
| 6 | +rem ============================================================================ |
| 7 | +rem This script builds Docker images for the demo environment on your local machine. |
| 8 | +rem The script automatically detects the platform (arm64/amd64) based on your machine. |
| 9 | +rem By default, images are only built locally. Use --push to push to AWS ECR. |
| 10 | +rem |
| 11 | +rem Usage: |
| 12 | +rem .\scripts\demo\build-demo-images-local.bat [IMAGE_NAME] [--push] |
| 13 | +rem |
| 14 | +rem Examples: |
| 15 | +rem .\scripts\demo\build-demo-images-local.bat # Build all images locally |
| 16 | +rem .\scripts\demo\build-demo-images-local.bat --push # Build and push all images |
| 17 | +rem .\scripts\demo\build-demo-images-local.bat mono-engine # Build mono-engine locally |
| 18 | +rem ============================================================================ |
| 19 | + |
| 20 | +rem Configuration |
| 21 | +set "REGISTRY_ALIAS=m8q5m4u3" |
| 22 | +set "REPOSITORY=mega" |
| 23 | +set "REGISTRY=public.ecr.aws" |
| 24 | +set "SHOULD_PUSH=false" |
| 25 | + |
| 26 | +rem Auto-detect platform if not explicitly set |
| 27 | +if "%TARGET_PLATFORMS%"=="" ( |
| 28 | + if /i "%PROCESSOR_ARCHITECTURE%"=="AMD64" ( |
| 29 | + set "TARGET_PLATFORMS=linux/amd64" |
| 30 | + set "ARCH_SUFFIX=amd64" |
| 31 | + ) else if /i "%PROCESSOR_ARCHITECTURE%"=="ARM64" ( |
| 32 | + set "TARGET_PLATFORMS=linux/arm64" |
| 33 | + set "ARCH_SUFFIX=arm64" |
| 34 | + ) else ( |
| 35 | + rem Default to arm64 for compatibility |
| 36 | + set "TARGET_PLATFORMS=linux/arm64" |
| 37 | + set "ARCH_SUFFIX=arm64" |
| 38 | + echo [WARN] Unknown machine architecture: %PROCESSOR_ARCHITECTURE%, defaulting to !TARGET_PLATFORMS! |
| 39 | + ) |
| 40 | + echo [INFO] Auto-detected platform: !TARGET_PLATFORMS! (machine: %PROCESSOR_ARCHITECTURE%) |
| 41 | +) else ( |
| 42 | + echo [INFO] Using explicit TARGET_PLATFORMS: %TARGET_PLATFORMS% |
| 43 | +) |
| 44 | + |
| 45 | +rem Get script directory and repo root |
| 46 | +set "SCRIPT_DIR=%~dp0" |
| 47 | +rem Remove trailing backslash |
| 48 | +if "%SCRIPT_DIR:~-1%"=="\" set "SCRIPT_DIR=%SCRIPT_DIR:~0,-1%" |
| 49 | + |
| 50 | +if exist "%SCRIPT_DIR%\..\..\Cargo.toml" ( |
| 51 | + pushd "%SCRIPT_DIR%\..\.." |
| 52 | + set "REPO_ROOT=!CD!" |
| 53 | + popd |
| 54 | +) else if exist "%SCRIPT_DIR%\..\..\moon\package.json" ( |
| 55 | + pushd "%SCRIPT_DIR%\..\.." |
| 56 | + set "REPO_ROOT=!CD!" |
| 57 | + popd |
| 58 | +) else if exist "Cargo.toml" ( |
| 59 | + set "REPO_ROOT=%CD%" |
| 60 | +) else ( |
| 61 | + set "REPO_ROOT=" |
| 62 | +) |
| 63 | + |
| 64 | +rem Image Definitions |
| 65 | +set "IMAGE_ORDER=mono-engine mega-ui orion-server orion-client" |
| 66 | + |
| 67 | +set "IMAGES_mono-engine_DOCKERFILE=mono/Dockerfile" |
| 68 | +set "IMAGES_mono-engine_CONTEXT=." |
| 69 | +set "TAGS_mono-engine=mono-0.1.0-pre-release" |
| 70 | + |
| 71 | +set "IMAGES_mega-ui_DOCKERFILE=moon/apps/web/Dockerfile" |
| 72 | +set "IMAGES_mega-ui_CONTEXT=moon" |
| 73 | +set "TAGS_mega-ui=mega-ui-demo-0.1.0-pre-release" |
| 74 | + |
| 75 | +set "IMAGES_orion-server_DOCKERFILE=orion-server/Dockerfile" |
| 76 | +set "IMAGES_orion-server_CONTEXT=." |
| 77 | +set "TAGS_orion-server=orion-server-0.1.0-pre-release" |
| 78 | + |
| 79 | +set "IMAGES_orion-client_DOCKERFILE=orion/Dockerfile" |
| 80 | +set "IMAGES_orion-client_CONTEXT=." |
| 81 | +set "TAGS_orion-client=orion-client-0.1.0-pre-release" |
| 82 | + |
| 83 | +rem Parse Arguments |
| 84 | +set "TARGET_IMAGE=" |
| 85 | +:parse_args |
| 86 | +if "%~1"=="" goto :args_done |
| 87 | +if "%~1"=="--push" ( |
| 88 | + set "SHOULD_PUSH=true" |
| 89 | +) else ( |
| 90 | + set "TARGET_IMAGE=%~1" |
| 91 | +) |
| 92 | +shift |
| 93 | +goto :parse_args |
| 94 | +:args_done |
| 95 | + |
| 96 | +rem Main Logic |
| 97 | +call :check_prerequisites |
| 98 | +if errorlevel 1 exit /b 1 |
| 99 | + |
| 100 | +call :setup_buildx |
| 101 | +if errorlevel 1 exit /b 1 |
| 102 | + |
| 103 | +if "%SHOULD_PUSH%"=="true" ( |
| 104 | + call :login_ecr |
| 105 | + if errorlevel 1 exit /b 1 |
| 106 | +) |
| 107 | + |
| 108 | +if not "%TARGET_IMAGE%"=="" ( |
| 109 | + call :build_and_push "%TARGET_IMAGE%" |
| 110 | + if errorlevel 1 exit /b 1 |
| 111 | +) else ( |
| 112 | + call :build_all |
| 113 | + if errorlevel 1 exit /b 1 |
| 114 | +) |
| 115 | + |
| 116 | +echo [INFO] Done! |
| 117 | +exit /b 0 |
| 118 | + |
| 119 | +rem ============================================================================ |
| 120 | +rem Functions |
| 121 | +rem ============================================================================ |
| 122 | + |
| 123 | +:check_prerequisites |
| 124 | + echo [INFO] Checking prerequisites... |
| 125 | + if "%REPO_ROOT%"=="" ( |
| 126 | + echo [ERROR] Could not determine repository root. |
| 127 | + echo [ERROR] Please run this script from the mega repository root, or ensure Cargo.toml or moon/package.json exists. |
| 128 | + exit /b 1 |
| 129 | + ) |
| 130 | + echo [INFO] Repository root: %REPO_ROOT% |
| 131 | + |
| 132 | + where docker >nul 2>nul |
| 133 | + if errorlevel 1 ( |
| 134 | + echo [ERROR] Docker is not installed. Please install Docker Desktop. |
| 135 | + exit /b 1 |
| 136 | + ) |
| 137 | + |
| 138 | + docker info >nul 2>nul |
| 139 | + if errorlevel 1 ( |
| 140 | + echo [ERROR] Docker daemon is not running. Please start Docker Desktop. |
| 141 | + exit /b 1 |
| 142 | + ) |
| 143 | + |
| 144 | + docker buildx version >nul 2>nul |
| 145 | + if errorlevel 1 ( |
| 146 | + echo [ERROR] Docker Buildx is not available. Please enable it in Docker Desktop. |
| 147 | + exit /b 1 |
| 148 | + ) |
| 149 | + |
| 150 | + if "%SHOULD_PUSH%"=="true" ( |
| 151 | + where aws >nul 2>nul |
| 152 | + if errorlevel 1 ( |
| 153 | + echo [ERROR] AWS CLI is not installed. |
| 154 | + exit /b 1 |
| 155 | + ) |
| 156 | + call aws sts get-caller-identity >nul 2>nul |
| 157 | + if errorlevel 1 ( |
| 158 | + echo [ERROR] AWS credentials are not configured. Please run: aws configure |
| 159 | + exit /b 1 |
| 160 | + ) |
| 161 | + ) |
| 162 | + echo [INFO] All prerequisites met. |
| 163 | + exit /b 0 |
| 164 | + |
| 165 | +:setup_buildx |
| 166 | + echo [INFO] Setting up Docker Buildx... |
| 167 | + set "builder_name=mega-builder" |
| 168 | + |
| 169 | + docker buildx inspect "!builder_name!" >nul 2>nul |
| 170 | + if errorlevel 1 ( |
| 171 | + echo [INFO] Creating new buildx builder: !builder_name! |
| 172 | + docker buildx create --name "!builder_name!" --driver docker-container --use >nul |
| 173 | + if errorlevel 1 ( |
| 174 | + echo [ERROR] Failed to create buildx builder: !builder_name! |
| 175 | + exit /b 1 |
| 176 | + ) |
| 177 | + ) else ( |
| 178 | + echo [INFO] Using existing buildx builder: !builder_name! |
| 179 | + docker buildx use "!builder_name!" >nul |
| 180 | + ) |
| 181 | + |
| 182 | + docker buildx inspect "!builder_name!" --bootstrap >nul 2>nul |
| 183 | + if errorlevel 1 ( |
| 184 | + echo [ERROR] Failed to bootstrap buildx builder: !builder_name! |
| 185 | + exit /b 1 |
| 186 | + ) |
| 187 | + |
| 188 | + rem Check for missing platforms (simplified check for Windows) |
| 189 | + docker buildx inspect "!builder_name!" --bootstrap 2>nul | findstr /i "platforms" >nul |
| 190 | + |
| 191 | + echo [INFO] Buildx setup complete. |
| 192 | + exit /b 0 |
| 193 | + |
| 194 | +:login_ecr |
| 195 | + echo [INFO] Logging in to Amazon ECR Public... |
| 196 | + |
| 197 | + rem Capture password into variable |
| 198 | + set "LOGIN_PASSWORD=" |
| 199 | + for /f "tokens=*" %%i in ('aws ecr-public get-login-password --region us-east-1 2^>nul') do set "LOGIN_PASSWORD=%%i" |
| 200 | + |
| 201 | + if "!LOGIN_PASSWORD!"=="" ( |
| 202 | + echo [ERROR] Failed to get ECR login password. Please check your AWS credentials. |
| 203 | + exit /b 1 |
| 204 | + ) |
| 205 | + |
| 206 | + echo !LOGIN_PASSWORD! | docker login --username AWS --password-stdin "%REGISTRY%/%REGISTRY_ALIAS%" >nul 2>nul |
| 207 | + if errorlevel 1 ( |
| 208 | + echo [ERROR] Failed to login to ECR Public. |
| 209 | + exit /b 1 |
| 210 | + ) |
| 211 | + echo [INFO] ECR login successful. |
| 212 | + exit /b 0 |
| 213 | + |
| 214 | +:build_and_push |
| 215 | + set "img_name=%~1" |
| 216 | + |
| 217 | + if not defined IMAGES_!img_name!_DOCKERFILE ( |
| 218 | + echo [ERROR] Unknown image: !img_name! |
| 219 | + exit /b 1 |
| 220 | + ) |
| 221 | + |
| 222 | + set "dockerfile=!IMAGES_%img_name%_DOCKERFILE!" |
| 223 | + set "context=!IMAGES_%img_name%_CONTEXT!" |
| 224 | + set "tag=!TAGS_%img_name%!" |
| 225 | + |
| 226 | + rem Check for multiple platforms |
| 227 | + echo "%TARGET_PLATFORMS%" | findstr "," >nul |
| 228 | + if not errorlevel 1 ( |
| 229 | + echo [ERROR] Multiple platforms detected in TARGET_PLATFORMS: %TARGET_PLATFORMS% |
| 230 | + echo [ERROR] This local script only supports single platform builds. |
| 231 | + exit /b 1 |
| 232 | + ) |
| 233 | + |
| 234 | + rem Ensure ARCH_SUFFIX is set |
| 235 | + if "%ARCH_SUFFIX%"=="" ( |
| 236 | + for /f "tokens=2 delims=/" %%a in ("%TARGET_PLATFORMS%") do set "ARCH_SUFFIX=%%a" |
| 237 | + ) |
| 238 | + if "%ARCH_SUFFIX%"=="" ( |
| 239 | + echo [ERROR] Failed to extract architecture suffix from TARGET_PLATFORMS: %TARGET_PLATFORMS% |
| 240 | + exit /b 1 |
| 241 | + ) |
| 242 | + |
| 243 | + set "full_tag=!tag!-!ARCH_SUFFIX!" |
| 244 | + set "image_base=%REGISTRY%/%REGISTRY_ALIAS%/%REPOSITORY%" |
| 245 | + set "full_image=!image_base!:!full_tag!" |
| 246 | + |
| 247 | + if "%SHOULD_PUSH%"=="true" ( |
| 248 | + echo [INFO] Building and pushing !img_name! ^(!full_tag!^)... |
| 249 | + ) else ( |
| 250 | + echo [INFO] Building !img_name! ^(!full_tag!^)... |
| 251 | + ) |
| 252 | + echo [INFO] Dockerfile: !dockerfile! |
| 253 | + echo [INFO] Context: !context! |
| 254 | + echo [INFO] Platforms: %TARGET_PLATFORMS% |
| 255 | + |
| 256 | + pushd "%REPO_ROOT%" |
| 257 | + |
| 258 | + set "CACHE_ARGS=" |
| 259 | + if "%SHOULD_PUSH%"=="true" ( |
| 260 | + rem Simplified cache check: always attempt to use registry cache if pushing |
| 261 | + set "CACHE_ARGS=--cache-from type=registry,ref=!full_image!" |
| 262 | + ) |
| 263 | + |
| 264 | + rem Build command |
| 265 | + set "BUILD_CMD=docker buildx build --builder mega-builder --platform %TARGET_PLATFORMS% --file !dockerfile! --tag !full_image! --progress=plain --build-arg BUILDKIT_INLINE_CACHE=1 --load" |
| 266 | + |
| 267 | + if "!img_name!"=="mega-ui" ( |
| 268 | + set "BUILD_CMD=!BUILD_CMD! --build-arg APP_ENV=demo" |
| 269 | + ) |
| 270 | + |
| 271 | + if defined CACHE_ARGS ( |
| 272 | + set "BUILD_CMD=!BUILD_CMD! !CACHE_ARGS!" |
| 273 | + ) |
| 274 | + |
| 275 | + set "BUILD_CMD=!BUILD_CMD! --cache-to type=inline !context!" |
| 276 | + |
| 277 | + rem Execute Build |
| 278 | + call !BUILD_CMD! |
| 279 | + if errorlevel 1 ( |
| 280 | + echo [ERROR] Failed to build !img_name! |
| 281 | + popd |
| 282 | + exit /b 1 |
| 283 | + ) |
| 284 | + |
| 285 | + if "%SHOULD_PUSH%"=="true" ( |
| 286 | + if not errorlevel 1 ( |
| 287 | + docker push "!full_image!" |
| 288 | + if errorlevel 1 ( |
| 289 | + echo [ERROR] Failed to push !img_name! |
| 290 | + popd |
| 291 | + exit /b 1 |
| 292 | + ) |
| 293 | + echo [INFO] !img_name! built and pushed successfully. |
| 294 | + echo [INFO] Image: !full_image! |
| 295 | + ) |
| 296 | + ) else ( |
| 297 | + echo [INFO] !img_name! built successfully ^(local only^). |
| 298 | + echo [INFO] Image: !full_image! |
| 299 | + ) |
| 300 | + |
| 301 | + popd |
| 302 | + exit /b 0 |
| 303 | + |
| 304 | +:build_all |
| 305 | + if "%SHOULD_PUSH%"=="true" ( |
| 306 | + echo [INFO] Building and pushing all demo images... |
| 307 | + ) else ( |
| 308 | + echo [INFO] Building all demo images... |
| 309 | + ) |
| 310 | + |
| 311 | + set "FAILED_IMAGES=" |
| 312 | + for %%i in (%IMAGE_ORDER%) do ( |
| 313 | + echo. |
| 314 | + echo [INFO] ========================================== |
| 315 | + echo [INFO] Building: %%i |
| 316 | + echo [INFO] ========================================== |
| 317 | + call :build_and_push "%%i" |
| 318 | + if errorlevel 1 ( |
| 319 | + echo [ERROR] Failed to build %%i |
| 320 | + set "FAILED_IMAGES=!FAILED_IMAGES! %%i" |
| 321 | + ) |
| 322 | + ) |
| 323 | + |
| 324 | + echo. |
| 325 | + if not "!FAILED_IMAGES!"=="" ( |
| 326 | + echo [ERROR] ========================================== |
| 327 | + echo [ERROR] Some images failed to build: |
| 328 | + echo [ERROR] !FAILED_IMAGES! |
| 329 | + echo [ERROR] ========================================== |
| 330 | + exit /b 1 |
| 331 | + ) |
| 332 | + |
| 333 | + echo [INFO] ========================================== |
| 334 | + if "%SHOULD_PUSH%"=="true" ( |
| 335 | + echo [INFO] All images built and pushed successfully! |
| 336 | + ) else ( |
| 337 | + echo [INFO] All images built successfully! |
| 338 | + ) |
| 339 | + echo [INFO] ========================================== |
| 340 | + exit /b 0 |
0 commit comments