243 lines
7.9 KiB
Batchfile
243 lines
7.9 KiB
Batchfile
@echo off
|
|
setlocal enabledelayedexpansion
|
|
|
|
echo ========================================
|
|
echo Quality Label Print Service Installer
|
|
echo Complete Self-Contained Version
|
|
echo ========================================
|
|
echo.
|
|
|
|
REM Check for administrator privileges
|
|
net session >nul 2>&1
|
|
if %errorLevel% neq 0 (
|
|
echo ERROR: This installer requires Administrator privileges.
|
|
echo Please right-click this file and select "Run as administrator"
|
|
echo.
|
|
pause
|
|
exit /b 1
|
|
)
|
|
|
|
echo [1/6] Administrator privileges confirmed ✓
|
|
echo.
|
|
|
|
REM Get current directory and set variables
|
|
set CURRENT_DIR=%~dp0
|
|
set SERVICE_NAME=QualityPrintService
|
|
set SERVICE_DISPLAY_NAME=Quality Label Print Service
|
|
set INSTALL_DIR=C:\QualityPrintService
|
|
set PYTHON_SCRIPT=%INSTALL_DIR%\print_service_complete.py
|
|
set LOG_DIR=%USERPROFILE%\PrintService\logs
|
|
|
|
echo [2/6] Checking Python installation...
|
|
|
|
REM Check if Python is available
|
|
python --version >nul 2>&1
|
|
if %errorLevel% neq 0 (
|
|
echo WARNING: Python not found in PATH
|
|
echo.
|
|
echo Installing portable Python interpreter...
|
|
|
|
REM Download portable Python (this would need to be included in the zip)
|
|
if exist "%CURRENT_DIR%python_portable" (
|
|
echo Using included portable Python ✓
|
|
set PYTHON_EXE=%CURRENT_DIR%python_portable\python.exe
|
|
) else (
|
|
echo ERROR: Portable Python not found in package
|
|
echo Please ensure python_portable folder is included
|
|
pause
|
|
exit /b 1
|
|
)
|
|
) else (
|
|
echo Python found in system PATH ✓
|
|
set PYTHON_EXE=python
|
|
)
|
|
|
|
echo [3/6] Creating installation directories...
|
|
|
|
REM Create installation directory
|
|
if not exist "%INSTALL_DIR%" (
|
|
mkdir "%INSTALL_DIR%"
|
|
echo Created: %INSTALL_DIR% ✓
|
|
)
|
|
|
|
REM Create log directory
|
|
if not exist "%LOG_DIR%" (
|
|
mkdir "%LOG_DIR%"
|
|
echo Created: %LOG_DIR% ✓
|
|
)
|
|
|
|
echo [4/6] Installing service files...
|
|
|
|
REM Copy service files
|
|
copy "%CURRENT_DIR%print_service_complete.py" "%INSTALL_DIR%\" >nul
|
|
if %errorLevel% equ 0 (
|
|
echo Copied print service script ✓
|
|
) else (
|
|
echo ERROR: Failed to copy service script
|
|
pause
|
|
exit /b 1
|
|
)
|
|
|
|
REM Copy additional files if they exist
|
|
if exist "%CURRENT_DIR%requirements_complete.txt" (
|
|
copy "%CURRENT_DIR%requirements_complete.txt" "%INSTALL_DIR%\" >nul
|
|
echo Copied requirements file ✓
|
|
)
|
|
|
|
REM Copy portable Python if included
|
|
if exist "%CURRENT_DIR%python_portable" (
|
|
echo Copying portable Python interpreter...
|
|
xcopy "%CURRENT_DIR%python_portable" "%INSTALL_DIR%\python_portable\" /E /I /H /Y >nul
|
|
if %errorLevel% equ 0 (
|
|
echo Portable Python installed ✓
|
|
set PYTHON_EXE=%INSTALL_DIR%\python_portable\python.exe
|
|
)
|
|
)
|
|
|
|
echo [5/6] Installing Windows service...
|
|
|
|
REM Remove existing service if it exists
|
|
sc query "%SERVICE_NAME%" >nul 2>&1
|
|
if %errorLevel% equ 0 (
|
|
echo Stopping existing service...
|
|
sc stop "%SERVICE_NAME%" >nul 2>&1
|
|
timeout /t 3 >nul
|
|
echo Removing existing service...
|
|
sc delete "%SERVICE_NAME%" >nul 2>&1
|
|
timeout /t 2 >nul
|
|
)
|
|
|
|
REM Try to install with NSSM first (if available)
|
|
if exist "%CURRENT_DIR%nssm.exe" (
|
|
echo Installing with NSSM (Non-Sucking Service Manager)...
|
|
|
|
"%CURRENT_DIR%nssm.exe" install "%SERVICE_NAME%" "%PYTHON_EXE%" "%PYTHON_SCRIPT%"
|
|
"%CURRENT_DIR%nssm.exe" set "%SERVICE_NAME%" DisplayName "%SERVICE_DISPLAY_NAME%"
|
|
"%CURRENT_DIR%nssm.exe" set "%SERVICE_NAME%" Description "Quality Label Printing Service for Windows"
|
|
"%CURRENT_DIR%nssm.exe" set "%SERVICE_NAME%" Start SERVICE_AUTO_START
|
|
"%CURRENT_DIR%nssm.exe" set "%SERVICE_NAME%" AppStdout "%LOG_DIR%\service_output.log"
|
|
"%CURRENT_DIR%nssm.exe" set "%SERVICE_NAME%" AppStderr "%LOG_DIR%\service_error.log"
|
|
"%CURRENT_DIR%nssm.exe" set "%SERVICE_NAME%" AppRotateFiles 1
|
|
"%CURRENT_DIR%nssm.exe" set "%SERVICE_NAME%" AppRotateOnline 1
|
|
"%CURRENT_DIR%nssm.exe" set "%SERVICE_NAME%" AppRotateBytes 1048576
|
|
|
|
echo Windows service installed with NSSM ✓
|
|
|
|
) else (
|
|
echo Installing with Windows SC command (Enhanced Service Wrapper)...
|
|
|
|
REM Copy service wrapper
|
|
copy "%CURRENT_DIR%service_wrapper.py" "%INSTALL_DIR%\" >nul
|
|
if %errorLevel% neq 0 (
|
|
echo WARNING: Service wrapper not found, creating basic wrapper...
|
|
)
|
|
|
|
REM Create enhanced service wrapper batch file
|
|
set WRAPPER_BAT=%INSTALL_DIR%\service_wrapper.bat
|
|
|
|
echo @echo off > "%WRAPPER_BAT%"
|
|
echo REM Windows Print Service Wrapper - Fixed for Error 1053 >> "%WRAPPER_BAT%"
|
|
echo cd /d "%INSTALL_DIR%" >> "%WRAPPER_BAT%"
|
|
echo. >> "%WRAPPER_BAT%"
|
|
echo REM Check if Python service wrapper exists >> "%WRAPPER_BAT%"
|
|
echo if exist "%INSTALL_DIR%\service_wrapper.py" ( >> "%WRAPPER_BAT%"
|
|
echo echo Starting service with wrapper... >> "%WRAPPER_BAT%"
|
|
echo "%PYTHON_EXE%" "%INSTALL_DIR%\service_wrapper.py" >> "%WRAPPER_BAT%"
|
|
echo ^) else ( >> "%WRAPPER_BAT%"
|
|
echo echo Starting service directly... >> "%WRAPPER_BAT%"
|
|
echo "%PYTHON_EXE%" "%PYTHON_SCRIPT%" --service >> "%WRAPPER_BAT%"
|
|
echo ^) >> "%WRAPPER_BAT%"
|
|
|
|
REM Install service using sc command with enhanced wrapper
|
|
sc create "%SERVICE_NAME%" binPath= "\"%WRAPPER_BAT%\"" DisplayName= "%SERVICE_DISPLAY_NAME%" start= auto
|
|
|
|
if %errorLevel% equ 0 (
|
|
echo Windows service installed with Enhanced SC Wrapper ✓
|
|
) else (
|
|
echo ERROR: Failed to install Windows service
|
|
echo Trying scheduled task fallback...
|
|
goto :install_scheduled_task
|
|
)
|
|
)
|
|
|
|
REM Configure service recovery options
|
|
echo Configuring service recovery options...
|
|
sc failure "%SERVICE_NAME%" reset= 86400 actions= restart/5000/restart/5000/restart/5000 >nul 2>&1
|
|
|
|
goto :start_service
|
|
|
|
:install_scheduled_task
|
|
echo [5/6] Installing as scheduled task (fallback)...
|
|
|
|
REM Create scheduled task as fallback
|
|
schtasks /create /tn "%SERVICE_NAME%" /tr "\"%PYTHON_EXE%\" \"%PYTHON_SCRIPT%\"" /sc onstart /ru SYSTEM /f >nul 2>&1
|
|
|
|
if %errorLevel% equ 0 (
|
|
echo Scheduled task installed ✓
|
|
|
|
REM Start the task
|
|
schtasks /run /tn "%SERVICE_NAME%" >nul 2>&1
|
|
echo Scheduled task started ✓
|
|
) else (
|
|
echo ERROR: Failed to install scheduled task
|
|
pause
|
|
exit /b 1
|
|
)
|
|
|
|
goto :install_chrome_extension
|
|
|
|
:start_service
|
|
echo [6/6] Starting service...
|
|
|
|
REM Start the service
|
|
sc start "%SERVICE_NAME%" >nul 2>&1
|
|
|
|
if %errorLevel% equ 0 (
|
|
echo Service started successfully ✓
|
|
) else (
|
|
echo WARNING: Service may not have started properly
|
|
echo This is normal on first install - the service will auto-start on next boot
|
|
)
|
|
|
|
:install_chrome_extension
|
|
echo.
|
|
echo ========================================
|
|
echo Installation Complete! ✓
|
|
echo ========================================
|
|
echo.
|
|
echo Service Details:
|
|
echo Name: %SERVICE_NAME%
|
|
echo Location: %INSTALL_DIR%
|
|
echo Logs: %LOG_DIR%
|
|
echo URL: http://localhost:8765
|
|
echo.
|
|
|
|
REM Test service connectivity
|
|
echo Testing service connectivity...
|
|
timeout /t 3 >nul
|
|
|
|
powershell -Command "try { $response = Invoke-RestMethod -Uri 'http://localhost:8765/health' -TimeoutSec 10; if ($response.status -eq 'healthy') { Write-Host 'Service is responding ✓' -ForegroundColor Green } else { Write-Host 'Service responded but may have issues' -ForegroundColor Yellow } } catch { Write-Host 'Service not yet responding (this is normal on first install)' -ForegroundColor Yellow }"
|
|
|
|
echo.
|
|
echo Next Steps:
|
|
echo 1. Install Chrome Extension:
|
|
echo - Open Chrome and go to chrome://extensions/
|
|
echo - Enable 'Developer mode'
|
|
echo - Click 'Load unpacked' and select the chrome_extension folder
|
|
echo.
|
|
echo 2. Test the installation:
|
|
echo - Visit: http://localhost:8765/health
|
|
echo - Expected response: {"status": "healthy"}
|
|
echo.
|
|
echo 3. The service will automatically start with Windows
|
|
echo.
|
|
|
|
if exist "%CURRENT_DIR%chrome_extension\" (
|
|
echo Opening Chrome extension folder...
|
|
explorer "%CURRENT_DIR%chrome_extension"
|
|
)
|
|
|
|
echo Installation completed successfully!
|
|
echo The Quality Print Service is now ready to use.
|
|
echo.
|
|
pause |