Files
Omika/infra/deploy/Install-OmikaDiagnosticsApi.ps1
T
2026-10-23 18:46:50 +02:00

134 lines
5.7 KiB
PowerShell

# ============================================================
# Omika - Installation du service Windows OmikaDiagnosticsApi (deploiement 5/6)
# ============================================================
# CIBLE : Windows Server 2025 (SRV-OMIKA), PowerShell ADMINISTRATEUR.
# NE PAS executer depuis le poste de developpement.
#
# A QUOI SERT CE SERVICE :
# API centrale qui RECOIT les lots de diagnostics envoyes par les serveurs
# clients distants (multi-serveur uniquement). Sur un SRV-OMIKA mono-serveur
# (l'API ecrit son JSONL localement), ce service est INUTILE : ne pas le
# deployer, ou le laisser arrete.
#
# CE QUE CE SCRIPT FAIT :
# 1. Cree le dossier du spool central + journalisation.
# 2. Pose les ACL NTFS minimales (Modifier sur le spool central).
# 3. Valide le compte de service (existence + droit de connexion).
# 4. Cree le service Windows OmikaDiagnosticsApi (idempotent, Manual).
# 5. Verifie ce qui a ete reellement cree.
#
# CE QU'IL NE FAIT PAS :
# - AUCUN demarrage de service, AUCUN secret ecrit, AUCUNE migration SQL.
# - AUCUNE cle de client Diagnostics (se cree dans l'interface Web).
#
# PREREQUIS MANUELS :
# - Compte local dedie + droit « Ouvrir une session en tant que service ».
# - Publication copiee dans <InstallRoot>\Apps\DiagnosticsApi.
# - La table des clients Diagnostics existe (migration 006).
# - Au moins un client enregistre (OmikaDiagnosticsWeb -> Clients Diagnostics).
#
# IDEMPOTENT - prend en charge -WhatIf.
#
# EXEMPLES :
# .\Install-OmikaDiagnosticsApi.ps1
# .\Install-OmikaDiagnosticsApi.ps1 -WhatIf
# .\Install-OmikaDiagnosticsApi.ps1 -Desinstaller
# ============================================================
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[string]$InstallRoot = 'C:\Omika',
[string]$ServiceName = 'OmikaDiagnosticsApi',
[string]$Account = '.\svc_omika_diagapi',
# Compte du service OmikaDiagnosticsWorker : lecteur du spool central.
[string]$AccountWorker = '.\svc_omika_diag',
[ValidateSet('Manual', 'Automatic', 'Disabled')]
[string]$StartupType = 'Manual',
[switch]$Desinstaller
)
$ErrorActionPreference = 'Stop'
Import-Module (Join-Path $PSScriptRoot 'Omika.Deploy.Common.psm1') -Force
# Le module a son propre etat de session : $WhatIfPreference ne s'y propage pas.
$Simulation = [bool]$WhatIfPreference
# --- Definition du service ---------------------------------------------------
$NomAffiche = 'Omika Diagnostics Api'
$DossierApps = 'DiagnosticsApi'
$Executable = 'Omika.Diagnostics.Api.exe'
$Description = 'Omika - API centrale de reception des lots de diagnostics (multi-serveur, cle client, spool central).'
$DossiersModifier = @(
'Logs\DiagnosticsApi',
'Data\DiagnosticsApi\Central'
)
try {
Assert-OmikaAdmin
$chemins = Get-OmikaPaths -InstallRoot $InstallRoot
$cheminExe = Join-Path (Join-Path $chemins.Apps $DossierApps) $Executable
# --- Rollback ------------------------------------------------------------
if ($Desinstaller) {
Write-OmikaStep "Desinstallation du service $ServiceName"
Remove-OmikaService -Name $ServiceName -WhatIf:$Simulation | Out-Null
Write-OmikaServiceSummary -Name $ServiceName -Supprime
exit 0
}
# --- 1. Dossiers ---------------------------------------------------------
Write-OmikaStep 'Dossiers du service'
foreach ($relatif in $DossiersModifier) {
New-OmikaDirectory -Path (Join-Path $InstallRoot $relatif) -WhatIf:$Simulation | Out-Null
}
# --- 2. ACL --------------------------------------------------------------
Write-OmikaStep 'ACL NTFS minimales'
# 2a. Spool central + logs : ce service ecrit.
foreach ($relatif in $DossiersModifier) {
Grant-OmikaAcl -Path (Join-Path $InstallRoot $relatif) -Account $Account -Right 'M' `
-RequireExisting -WhatIf:$Simulation | Out-Null
}
# 2b. Le Worker Diagnostics doit pouvoir LIRE le spool central.
# Le dossier vient d'etre cree ci-dessus, il existe donc forcement.
Grant-OmikaAcl -Path (Join-Path $InstallRoot 'Data\DiagnosticsApi\Central') `
-Account $AccountWorker -Right 'R' -RequireExisting -WhatIf:$Simulation | Out-Null
# --- 3. Compte de service ------------------------------------------------
Write-OmikaStep 'Compte de service'
Assert-OmikaServiceAccount -Account $Account | Out-Null
# --- 4. Service Windows --------------------------------------------------
Write-OmikaStep "Creation du service Windows ($StartupType)"
Install-OmikaService -Name $ServiceName -DisplayName $NomAffiche `
-ExePath $cheminExe -Account $Account -Description $Description `
-StartupType $StartupType -WhatIf:$Simulation | Out-Null
# --- 5. Recapitulatif ----------------------------------------------------
Write-OmikaServiceSummary -Name $ServiceName -ExePath $cheminExe -Account $Account -Rappels @(
'Injecter les secrets en variables MACHINE (aucun secret dans ce script) :'
' ConnectionStrings__DiagnosticsDatabase=<connexion PostgreSQL centrale>'
' Diagnostics__Central__Directory=<dossier du spool central>'
' ASPNETCORE_ENVIRONMENT=Production'
' ASPNETCORE_URLS=http://127.0.0.1:<PORT_DIAG_API>'
'ENREGISTRER LES CLIENTS : OmikaDiagnosticsWeb -> Clients Diagnostics -> Nouveau client.'
' La cle n''est affichee QU''UNE FOIS : la copier immediatement.'
'Cote serveur client, activer alors DiagnosticsForwarding__Enabled=true + Endpoint + ApiKey.'
'Exposer en HTTPS via le reverse proxy Apache : jamais Kestrel en direct.'
'Sur un serveur MONO-SERVEUR, ce service n''est pas necessaire.'
)
exit 0
}
catch {
Write-OmikaFail $_.Exception.Message
exit 1
}