compliance/fix-existing-package.bat
2025-09-30 10:15:35 +08:00

140 lines
4.2 KiB
Batchfile

@echo off
setlocal enabledelayedexpansion
echo === Fix Existing FastAPI Package ===
echo This script fixes the missing assets directory issue in existing packages
echo.
REM Find the most recent package directory
for /f "delims=" %%i in ('dir /b /ad /o-d dms-compliance-fastapi-amd64-windows-* 2^>nul') do (
set "PACKAGE_DIR=%%i"
goto :found
)
echo [ERROR] No package directory found matching pattern: dms-compliance-fastapi-amd64-windows-*
echo Please make sure you have a generated package directory.
pause
exit /b 1
:found
echo [INFO] Found package directory: %PACKAGE_DIR%
echo.
REM Check if assets directory exists in current location
if not exist "assets" (
echo [ERROR] Assets directory not found in current location
echo Please run this script from the same directory where you ran the build script
pause
exit /b 1
)
echo [INFO] Assets directory found, proceeding with fix...
echo.
REM Stop any running containers
echo [INFO] Stopping any running containers...
cd /d "%PACKAGE_DIR%"
docker compose down >nul 2>&1
cd /d ".."
REM Load the existing image to check if it exists
echo [INFO] Checking existing Docker image...
cd /d "%PACKAGE_DIR%"
if exist "docker-image.tar" (
docker load -i docker-image.tar >nul 2>&1
echo [INFO] Existing image loaded
) else (
echo [WARNING] No docker-image.tar found
)
cd /d ".."
REM Create a new build directory with assets
echo [INFO] Creating fixed build directory...
set "FIX_DIR=%PACKAGE_DIR%-fixed"
if exist "%FIX_DIR%" rmdir /s /q "%FIX_DIR%"
mkdir "%FIX_DIR%"
REM Copy everything from the original package
echo [INFO] Copying original package files...
robocopy "%PACKAGE_DIR%" "%FIX_DIR%" /E /XF docker-image.tar >nul 2>&1
REM Copy the assets directory
echo [INFO] Adding missing assets directory...
robocopy "assets" "%FIX_DIR%\assets" /E >nul 2>&1
REM Copy other required files that might be missing
if exist "ddms_compliance_suite" robocopy "ddms_compliance_suite" "%FIX_DIR%\ddms_compliance_suite" /E /XD __pycache__ /XF *.pyc >nul 2>&1
if exist "custom_stages" robocopy "custom_stages" "%FIX_DIR%\custom_stages" /E /XD __pycache__ /XF *.pyc >nul 2>&1
if exist "custom_testcases" robocopy "custom_testcases" "%FIX_DIR%\custom_testcases" /E /XD __pycache__ /XF *.pyc >nul 2>&1
REM Rebuild the Docker image
echo [INFO] Rebuilding Docker image with assets...
cd /d "%FIX_DIR%"
REM Check if Dockerfile exists
if not exist "Dockerfile" (
echo [ERROR] Dockerfile not found in package directory
cd /d ".."
pause
exit /b 1
)
echo [INFO] Building fixed Docker image...
set DOCKER_SCOUT_NO_ANALYTICS=true
set DOCKER_BUILDX_NO_DEFAULT_ATTESTATIONS=true
docker build -t compliance-dms-windows:latest . 2>&1
if errorlevel 1 (
echo [ERROR] Docker build failed
cd /d ".."
pause
exit /b 1
)
echo [SUCCESS] Docker image rebuilt successfully!
REM Save the new image
echo [INFO] Saving fixed Docker image...
docker save compliance-dms-windows:latest -o docker-image.tar
REM Test the fixed image
echo [INFO] Testing the fixed image...
docker run --rm -d -p 5052:5050 --name test-fixed-container compliance-dms-windows:latest
timeout /t 10 /nobreak >nul
REM Check if container is running and test the API
docker ps | findstr test-fixed-container >nul
if errorlevel 1 (
echo [WARNING] Test container failed to start
) else (
echo [SUCCESS] Test container is running
REM Test if assets are accessible
docker exec test-fixed-container ls -la /app/assets/doc/dms/ >nul 2>&1
if errorlevel 1 (
echo [WARNING] Assets directory not found in container
) else (
echo [SUCCESS] Assets directory found in container
docker exec test-fixed-container cat /app/assets/doc/dms/domain.json | head -5
)
)
REM Clean up test container
docker stop test-fixed-container >nul 2>&1
docker rm test-fixed-container >nul 2>&1
cd /d ".."
echo.
echo === Fix Complete ===
echo [SUCCESS] Fixed package created: %FIX_DIR%
echo.
echo To use the fixed package:
echo 1. cd %FIX_DIR%
echo 2. start.bat
echo 3. Test with: curl -X POST http://localhost:5050/run -H "Content-Type: application/json" -d "{\"mode\":\"dms\",\"base_url\":\"http://127.0.0.1:5001\"}"
echo.
echo The fixed package should now be able to find the assets/doc/dms/domain.json file.
echo.
pause