Files
rpa_vision_v3/deploy/installer/configure_embed.ps1
Dom a210e5ee32 feat(update): swap atomique + rollback (Lea.bat) + confirmation boot (main.py)
Implémente le SWAP réel de la MAJ silencieuse (DETTE-022), remplace les stubs :
- updater.apply_update : ARME le swap (extrait le ZIP -> agent_v1_new/ +
  marqueur UPDATE_READY, garde-fou zip-slip). N'écrase JAMAIS le vivant.
- updater.write_boot_ok_marker : désarme le rollback (retire PENDING_BOOT).
- Lea.bat (template + embed généré par configure_embed.ps1) : swap ATOMIQUE
  par renames (agent_v1 -> agent_v1_prev backup ; agent_v1_new -> agent_v1)
  + rollback auto si PENDING_BOOT persiste (boot précédent non confirmé).
- main.py : confirme le boot après 90 s de liveness locale OU quit propre
  (évite un faux rollback ; RPA_BOOT_CONFIRM_DELAY_S surchargeable pour les tests).

Testable (Python) : 45 tests verts. Le swap OS (renames Lea.bat) + le câblage
main.py seront validés par le test Win 11 (step 0 pré-canary, dont le rollback).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-01 14:10:34 +02:00

135 lines
4.6 KiB
PowerShell

# ============================================================
# configure_embed.ps1 — Configure le runtime Python embedded
# ------------------------------------------------------------
#
# Quand le composant 'pythonembed' est installe, on a :
# <AppDir>\python-embed\ <-- runtime Python 3.12 embedded
#
# Ce script :
# 1. Active l'import des packages (patch de python312._pth)
# 2. Installe pip dans le runtime embedded
# 3. Installe les dependances requirements_agent.txt
# 4. Reecrit Lea.bat pour pointer sur python-embed\pythonw.exe
#
# Doit etre execute avec le CWD = <AppDir>
# ============================================================
$ErrorActionPreference = 'Stop'
$AppDir = Get-Location
$EmbedDir = Join-Path $AppDir "python-embed"
$PythonExe = Join-Path $EmbedDir "python.exe"
if (-not (Test-Path $PythonExe)) {
Write-Host "Python embedded introuvable, abandon."
exit 1
}
Write-Host "Configuration de Python embedded..."
# ---------------------------------------------------------------
# 1. Decommenter la ligne 'import site' dans python312._pth
# (necessaire pour que pip puisse fonctionner)
# ---------------------------------------------------------------
$PthFile = Get-ChildItem -Path $EmbedDir -Filter "python*._pth" | Select-Object -First 1
if ($PthFile) {
$Content = Get-Content $PthFile.FullName
$NewContent = $Content -replace '^#import site', 'import site'
Set-Content -Path $PthFile.FullName -Value $NewContent
Write-Host " python._pth patche (import site active)"
}
# ---------------------------------------------------------------
# 2-3. Verification des dependances embarquees (runtime 100% autonome)
# L'embed DOIT contenir toutes les dependances runtime.
# AUCUN pip, AUCUN reseau : si une dependance manque -> echec explicite.
# ---------------------------------------------------------------
$RequiredModules = @('socketio','tkinter','mss','pynput','pystray','plyer','requests','httpx','PIL','win32api')
$Missing = @()
foreach ($m in $RequiredModules) {
& $PythonExe -c "import $m" 2>$null
if ($LASTEXITCODE -ne 0) { $Missing += $m }
}
if ($Missing.Count -gt 0) {
Write-Host " ERREUR : runtime Lea incomplet. Modules manquants : $($Missing -join ', ')"
Write-Host " L'embed doit etre livre complet (aucune installation reseau en POC)."
exit 1
}
Write-Host " Dependances embarquees verifiees ($($RequiredModules.Count) modules) - offline OK."
# ---------------------------------------------------------------
# 4. Reecrire Lea.bat pour utiliser python-embed
# ---------------------------------------------------------------
$LeaBat = Join-Path $AppDir "Lea.bat"
$NewLeaBat = @"
@echo off
chcp 65001 >nul 2>&1
title Lea - Assistante IA
cd /d "%~dp0"
if exist "lea_agent.lock" (
for /f "usebackq tokens=* delims=" %%i in ("lea_agent.lock") do (
taskkill /F /PID %%i >nul 2>&1
)
del /f /q "lea_agent.lock" >nul 2>&1
timeout /t 2 >nul
)
:: MAJ SILENCIEUSE swap atomique + rollback (renames uniquement)
if exist "PENDING_BOOT" (
echo [MAJ] Boot precedent non confirme : retour a la version precedente.
if exist "agent_v1_prev" (
if exist "agent_v1_echec" rmdir /s /q "agent_v1_echec" >nul 2>&1
if exist "agent_v1" move "agent_v1" "agent_v1_echec" >nul 2>&1
move "agent_v1_prev" "agent_v1" >nul 2>&1
)
del /f /q "PENDING_BOOT" >nul 2>&1
) else if exist "UPDATE_READY" (
if exist "agent_v1_new" (
echo [MAJ] Application de la mise a jour...
if exist "agent_v1" (
if exist "agent_v1_prev" rmdir /s /q "agent_v1_prev" >nul 2>&1
move "agent_v1" "agent_v1_prev" >nul 2>&1
)
move "agent_v1_new" "agent_v1" >nul 2>&1
move "UPDATE_READY" "PENDING_BOOT" >nul 2>&1
) else (
del /f /q "UPDATE_READY" >nul 2>&1
)
)
if exist "config.txt" (
for /f "usebackq eol=# tokens=1,* delims==" %%a in ("config.txt") do (
if not "%%a"=="" if not "%%b"=="" set "%%a=%%b"
)
)
echo.
echo Demarrage de Lea (runtime embedded)...
echo.
start "" /b "%~dp0python-embed\pythonw.exe" run_agent_v1.py
timeout /t 3 >nul
set "LEA_ALIVE=0"
if exist "lea_agent.lock" (
for /f "usebackq tokens=* delims=" %%i in ("lea_agent.lock") do (
tasklist /FI "PID eq %%i" /NH 2>nul | findstr /I "pythonw" >nul && set "LEA_ALIVE=1"
)
)
if "%LEA_ALIVE%"=="0" (
echo.
echo Lea n'a pas demarre correctement. Affichage des erreurs :
echo.
"%~dp0python-embed\python.exe" run_agent_v1.py
pause
)
"@
Set-Content -Path $LeaBat -Value $NewLeaBat -Encoding ASCII
Write-Host " Lea.bat reecrit pour runtime embedded"
Write-Host "Configuration terminee."
exit 0