taler-windows

Windows packaging
Log | Files | Refs

setup-wsl-debian.bat (11740B)


      1 @echo off
      2 :: This file is part of GNU Taler.
      3 :: Copyright (C) 2025 Taler Systems SA
      4 ::
      5 :: TALER is free software; you can redistribute it and/or modify it under the
      6 :: terms of the GNU Lesser General Public License as published by the Free Software
      7 :: Foundation; either version 2.1, or (at your option) any later version.
      8 ::
      9 :: TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10 :: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11 :: A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
     12 ::
     13 :: You should have received a copy of the GNU Lesser General Public License along with
     14 :: TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 ::
     16 :: Author: Léa Oualli
     17 
     18 setlocal enabledelayedexpansion
     19 
     20 REM -------------------------- LOGFILE GLOBALE ---------------------------
     21 set LOGFILE=C:\Users\Public\taler-bat.log
     22 
     23 echo [%DATE% %TIME%] === [START] GNU Taler-Merchant Installer === > %LOGFILE%
     24 echo [%DATE% %TIME%] Arguments : STEP="%1" DOMAIN="%2" DISTRO="%3" PROXY_MODE="%4" >> %LOGFILE%
     25 
     26 set WSL_USER=taleruser
     27 set STEP=%1
     28 set DOMAIN=%2
     29 set DISTRO=%3
     30 set WSL=wsl.exe
     31 if exist "%WINDIR%\Sysnative\wsl.exe" set WSL=%WINDIR%\Sysnative\wsl.exe
     32 set PROXY_MODE=%4
     33 
     34 if "%STEP%"=="" (
     35     set STEP=ALL
     36 )
     37 
     38 if "%DISTRO%"=="" (
     39     set DISTRO=Debian
     40 )
     41 
     42 echo [%DATE% %TIME%] Initial STEP="%STEP%", DOMAIN="%DOMAIN%", DISTRO="%DISTRO%", PROXY_MODE="%PROXY_MODE%" >> %LOGFILE%
     43 
     44 if "%STEP%"=="1" goto step1
     45 if "%STEP%"=="2" goto step2
     46 if "%STEP%"=="3" goto step3
     47 if "%STEP%"=="4" goto step4
     48 if "%STEP%"=="5" goto step5
     49 if "%STEP%"=="6" goto step6
     50 if "%STEP%"=="7" goto step7
     51 if "%STEP%"=="8" goto step8
     52 
     53 :step1
     54 echo [%DATE% %TIME%] [1/8] Checking WSL and VirtualMachinePlatform... >> %LOGFILE%
     55 
     56 :: Check if WSL is enabled
     57 powershell -Command "(Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux).State" | find /I "Enabled" >nul
     58 set WSL_ENABLED=%errorlevel%
     59 :: Check if VirtualMachinePlatform is enabled
     60 powershell -Command "(Get-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform).State" | find /I "Enabled" >nul
     61 set VMP_ENABLED=%errorlevel%
     62 
     63 if %WSL_ENABLED% equ 0 (
     64      echo [%DATE% %TIME%] WSL already enabled. >> %LOGFILE%
     65 ) else (
     66      echo [%DATE% %TIME%] Enabling WSL feature... >> %LOGFILE%
     67     powershell.exe -Command "Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux -NoRestart"
     68     set NEED_REBOOT=1
     69 )
     70 
     71 if %VMP_ENABLED% equ 0 (
     72     echo [%DATE% %TIME%] VirtualMachinePlatform already enabled. >> %LOGFILE%
     73 ) else (
     74     echo [%DATE% %TIME%] Enabling VirtualMachinePlatform feature... >> %LOGFILE%
     75     powershell.exe -Command "Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform -NoRestart"
     76     set NEED_REBOOT=1
     77 )
     78 
     79 if defined NEED_REBOOT (
     80     echo [%DATE% %TIME%] [INFO] REBOOT REQUIRED! >> %LOGFILE%
     81     echo [INFO] A restart is required to finish enabling WSL features. Please restart your computer and run the installer again.
     82     pause
     83     exit /b 1
     84 ) else (
     85     echo [%DATE% %TIME%] [INFO] WSL and VirtualMachinePlatform are ready. >> %LOGFILE%
     86 )
     87 
     88 if not "%STEP%"=="ALL" exit /b
     89 
     90 echo [%DATE% %TIME%] Updating WSL to the latest version if needed... >> %LOGFILE%
     91 echo [INFO] If a black window appears, please press any key when prompted, then wait until the window closes automatically.
     92 echo [INFO] The installation will continue automatically after the update.
     93 timeout /t 3 /nobreak
     94 
     95 :: Use start /wait to ensure the script waits for the update to finish (including if user has to press a key)
     96 start /wait "" %WSL% --update
     97 
     98 if not "%STEP%"=="ALL" exit /b
     99 
    100 :step2
    101 echo [%DATE% %TIME%] [2/8] Installing %DISTRO%... >> %LOGFILE%
    102 %WSL% --install -d %DISTRO%
    103 
    104 
    105 echo [%DATE% %TIME%] Initializing %DISTRO% first launch... >> %LOGFILE%
    106 %WSL% -d %DISTRO% -- bash -c "echo %DISTRO% ready"
    107 if not "%STEP%"=="ALL" exit /b
    108 
    109 :step3
    110 echo [%DATE% %TIME%] [3/8] Creating Debian user if needed... >> %LOGFILE%
    111 echo [%DATE% %TIME%] DISTRO="%DISTRO%", WSL_USER="%WSL_USER%" >> %LOGFILE%
    112 %WSL% -d %DISTRO% -- bash -c "id %WSL_USER% >/dev/null 2>&1 || sudo useradd -m %WSL_USER%"
    113 
    114 echo [%DATE% %TIME%] Waiting for user "%WSL_USER%" to be present... >> %LOGFILE%
    115 :wait_for_taleruser
    116 %WSL% -d %DISTRO% -- bash -c "id %WSL_USER%" >nul 2>&1
    117 if %errorlevel% neq 0 (
    118     timeout /t 2 >nul
    119     goto wait_for_taleruser
    120 )
    121 echo [%DATE% %TIME%] User "%WSL_USER%" exists in Debian! >> %LOGFILE%
    122 
    123 %WSL% -d %DISTRO% -- bash -c "echo '%WSL_USER% ALL=(ALL) NOPASSWD:ALL' | sudo tee /etc/sudoers.d/%WSL_USER%"
    124 %WSL% -d %DISTRO% -- bash -c "sudo chmod 0440 /etc/sudoers.d/%WSL_USER%"
    125 echo [%DATE% %TIME%] Sudoers file updated. >> %LOGFILE%
    126 if not "%STEP%"=="ALL" exit /b
    127 
    128 REM Nettoie /etc/wsl.conf avant d’ajouter
    129 %WSL% -d %DISTRO% -- bash -c "sudo sed -i '/^\[user\]/d;/^default *=/d' /etc/wsl.conf"
    130 %WSL% -d %DISTRO% -- bash -c "echo -e '[user]\ndefault = %WSL_USER%' | sudo tee -a /etc/wsl.conf > /dev/null"
    131 echo [%DATE% %TIME%] wsl.conf set default user. >> %LOGFILE%
    132 
    133 :: Pour appliquer immédiatement, termine la distro :
    134 %WSL% --terminate %DISTRO%
    135 echo [%DATE% %TIME%] Distro %DISTRO% terminated for user change. >> %LOGFILE%
    136 
    137 :step4
    138 echo [%DATE% %TIME%] [4/8] Adding user to www-data group... >> %LOGFILE%echo [DEBUG] DISTRO="%DISTRO%", WSL_USER="%WSL_USER%", DOMAIN="%DOMAIN%" >> %LOGFILE%
    139 %WSL% -d %DISTRO% -- bash -c "sudo usermod -aG www-data %WSL_USER% || echo 'Warning: Could not add user to www-data (might already be a member)'"
    140 echo [%DATE% %TIME%] User %WSL_USER% added to www-data (or already a member). >> %LOGFILE%
    141 if not "%STEP%"=="ALL" exit /b
    142 
    143 :step5
    144 echo [%DATE% %TIME%] [5/8] Preparing and copying scripts... >> %LOGFILE%
    145 
    146 REM 1. Installer dos2unix (si pas déjà installé)
    147 %WSL% -d %DISTRO% -- bash -c "sudo apt-get update && sudo apt-get install -y dos2unix"
    148 
    149 REM 2. Prepare the folder
    150 %WSL% -d %DISTRO% -- bash -c "sudo mkdir -p /home/%WSL_USER%/taler-installer"
    151 
    152 REM 3. Remove the old scripts
    153 %WSL% -d %DISTRO% -- bash -c "sudo rm -f /home/%WSL_USER%/taler-installer/install-taler-from-apt.sh"
    154 %WSL% -d %DISTRO% -- bash -c "sudo rm -f /home/%WSL_USER%/taler-installer/test-db.sh"
    155 %WSL% -d %DISTRO% -- bash -c "sudo rm -f /home/%WSL_USER%/taler-installer/set-db.sh"
    156 
    157 REM 4. Past the script form  Windows to %TEMP%
    158 copy /Y "%~dp0install-taler-from-apt.sh" "%TEMP%\install-taler-from-apt.sh"
    159 copy /Y "%~dp0test-db.sh" "%TEMP%\test-db.sh"
    160 copy /Y "%~dp0set-db.sh" "%TEMP%\set-db.sh"
    161 
    162 REM 5. copy the script into WSL (via tee)
    163 type "%TEMP%\install-taler-from-apt.sh" | %WSL% -d %DISTRO% -- bash -c "sudo tee /home/%WSL_USER%/taler-installer/install-taler-from-apt.sh > /dev/null"
    164 type "%TEMP%\test-db.sh" | %WSL% -d %DISTRO% -- bash -c "sudo tee /home/%WSL_USER%/taler-installer/test-db.sh > /dev/null"
    165 type "%TEMP%\set-db.sh"  | %WSL% -d %DISTRO% -- bash -c "sudo tee /home/%WSL_USER%/taler-installer/set-db.sh > /dev/null"
    166 echo [%DATE% %TIME%] Scripts copied to WSL. >> %LOGFILE%
    167 
    168 REM 6. Convert all the scripts into UNIX format
    169 %WSL% -d %DISTRO% -- bash -c "sudo dos2unix /home/%WSL_USER%/taler-installer/*.sh"
    170 
    171 REM 7. Make the script executable
    172 %WSL% -d %DISTRO% -- bash -c "sudo chmod +x /home/%WSL_USER%/taler-installer/*.sh"
    173 
    174 REM ------------------------------------------------------------------------------
    175 
    176 if not "%STEP%"=="ALL" exit /b
    177 
    178 :step6
    179 echo [%DATE% %TIME%] [6/8] Checks permissions... >> %LOGFILE%
    180 
    181 REM Check the version dos2unix (pour debug)
    182 %WSL% -d %DISTRO% -- bash -c "dos2unix --version"
    183 
    184 REM Display the format .sh ( ASCII text, Not CRLF)
    185 %WSL% -d %DISTRO% -- bash -c "file /home/%WSL_USER%/taler-installer/*.sh"
    186 
    187 REM Show the 5 firs lines of the code
    188 %WSL% -d %DISTRO% -- bash -c "for f in /home/%WSL_USER%/taler-installer/*.sh; do echo '----- $f'; head -n 5 \$f; done"
    189 
    190 REM Vérifier les droits d'exécution
    191 %WSL% -d %DISTRO% -- bash -c "ls -l /home/%WSL_USER%/taler-installer/*.sh"
    192 
    193 REM Fin du step
    194 echo [%DATE% %TIME%] Scripts verified (permissions/format/headers). >> %LOGFILE%
    195 if not "%STEP%"=="ALL" exit /b
    196 
    197 :step7
    198 echo [%DATE% %TIME%] [7/8] Restarting WSL... >> %LOGFILE%
    199 echo [DEBUG] DISTRO="%DISTRO%" >> %LOGFILE%
    200 %WSL% --shutdown
    201 timeout /t 3
    202 
    203 echo [%DATE% %TIME%] Running final installation script... >> %LOGFILE%
    204 %WSL% -d %DISTRO% -- bash -c "sudo -u %WSL_USER% bash /home/%WSL_USER%/taler-installer/install-taler-from-apt.sh | tee /home/%WSL_USER%/taler-installer/install-bat.log"
    205 %WSL% -d %DISTRO% -- bash -c "sudo cp /home/%WSL_USER%/taler-installer/install-bat.log /mnt/c/Users/Public/install-bat.log"
    206 echo [%DATE% %TIME%] Final install script run, log copied to Windows. >> %LOGFILE%
    207 if not "%STEP%"=="ALL" exit /b
    208 
    209 :step8
    210 echo [%DATE% %TIME%] [8/8] Configuring reverse proxy... >> %LOGFILE%
    211 echo [%DATE% %TIME%] DISTRO="%DISTRO%", DOMAIN="%DOMAIN%", PROXY_MODE="%PROXY_MODE%" >> %LOGFILE%
    212 
    213 echo [%DATE% %TIME%] --- Démarrage configuration reverse proxy --- >> %LOGFILE%
    214 
    215 if "%DOMAIN%"=="" (
    216     set DOMAIN=localhost
    217     echo [%DATE% %TIME%] Domaine vide, fallback sur localhost >> %LOGFILE%
    218 )
    219 
    220 if "%PROXY_MODE%"=="" (
    221     set PROXY_MODE=http
    222     echo [%DATE% %TIME%] Mode proxy vide, fallback sur HTTP >> %LOGFILE%
    223 )
    224 
    225 REM Installe Apache (dans tous les cas)
    226 echo [%DATE% %TIME%] Installation Apache2... >> %LOGFILE%
    227 %WSL% -d %DISTRO% -- bash -c "sudo apt-get update && sudo apt-get install -y apache2" >> %LOGFILE% 2>&1
    228 
    229 if /I "%PROXY_MODE%"=="https" (
    230     echo [%DATE% %TIME%] Mode HTTPS sélectionné pour %DOMAIN% >> %LOGFILE%
    231     echo [%DATE% %TIME%] Setting up HTTPS with Let's Encrypt for %DOMAIN% ... >> %LOGFILE%
    232     %WSL% -d %DISTRO% -- bash -c "sudo taler-merchant-rproxy-setup --acme --merchant-url='https://%DOMAIN%:8888/' --apache" >> %LOGFILE% 2>&1
    233     set CERT_STATUS=%errorlevel%
    234     echo [%DATE% %TIME%] HTTPS return code : %CERT_STATUS% >> %LOGFILE%
    235     if not %CERT_STATUS%==0 (
    236         echo [%DATE% %TIME%] [ERREUR] Certificat HTTPS échec pour %DOMAIN% >> %LOGFILE%
    237         echo [ERREUR] Impossible d’obtenir un certificat HTTPS valide pour %DOMAIN%.
    238         echo [ERREUR] Veuillez vérifier que le domaine pointe bien sur cette machine et soit accessible publiquement.
    239         timeout /t 5 /nobreak
    240     )
    241 ) else (
    242     echo [%DATE% %TIME%] Mode HTTP sélectionné pour %DOMAIN% >> %LOGFILE%
    243    echo [%DATE% %TIME%] Setting up HTTP only for %DOMAIN% ... >> %LOGFILE%
    244     %WSL% -d %DISTRO% -- bash -c "sudo taler-merchant-rproxy-setup --domain %DOMAIN% --httponly --apache" >> %LOGFILE% 2>&1
    245     echo [%DATE% %TIME%] HTTP only proxy finished for %DOMAIN% >> %LOGFILE%
    246 )
    247 
    248 echo [%DATE% %TIME%] --- Fin configuration reverse proxy --- >> %LOGFILE%
    249 
    250 
    251 echo.
    252 echo Verifying domain configured in Apache...
    253 %WSL% -d %DISTRO% -- bash -c "grep 'ServerName' /etc/apache2/sites-available/taler-merchant.conf || echo 'ServerName not found!'"
    254 
    255 REM Start Apache service
    256 %WSL% -d %DISTRO% -- bash -c "sudo service apache2 start"
    257 
    258 REM Scheduled task (on next boot): Apache will start automatically
    259 powershell -Command "Register-ScheduledTask -Action (New-ScheduledTaskAction -Execute '%WINDIR%\System32\wsl.exe' -Argument '-d %DISTRO% -- bash -c \"sudo service apache2 start && while true; do sleep 3600; done\"') -Trigger (New-ScheduledTaskTrigger -AtLogOn) -TaskName 'StartTalerApacheWSL' -User '$env:USERNAME' -RunLevel Highest -Force"
    260 
    261 echo [%DATE% %TIME%] GNU Taler-Merchant installed successfully! >> %LOGFILE%
    262 echo [%DATE% %TIME%] === [END] GNU Taler-Merchant Installer === >> %LOGFILE%
    263 
    264 timeout /t 5 /nobreak >nul
    265 exit /b