namespace ZA.CoreService.ESBCertificateManager.Models;
///
/// Verbindungskonfiguration für eine Progress Sonic ESB Management Instanz.
///
public sealed class SonicConnection
{
///
/// Eindeutiger Bezeichner; wird in referenziert.
///
public required string Name { get; init; }
/// Sonic-Domain-Name, z.B. "proalpha-test".
public required string DomainName { get; init; }
///
/// Sonic-Broker-/Management-URL im Sonic-Format, z.B. "tcp://dekun-painwbdet:13070".
/// Der Hostname wird daraus extrahiert (WinRM-Ziel / HTTP-Basis).
///
public required string ConnectionUrl { get; init; }
/// Benutzername für WinRM / Management-Konsole.
public required string Username { get; init; }
/// Passwort für WinRM / Management-Konsole.
public required string Password { get; init; }
///
/// Management-Modus:
/// - WinRm: PowerShell Remoting auf dem Sonic-Server (Standard)
/// - LocalCmd: CMD/PowerShell lokal (zum Testen direkt auf dem Sonic-PC)
/// - HttpApi: REST, falls vorhanden
///
public SonicManagementMode ManagementMode { get; init; } = SonicManagementMode.WinRm;
///
/// Installationsroot von Sonic MQ / Management Console, z.B. "C:\Sonic\MQ10.0".
/// Wird für Default-Scripts (stopcontainer/startcontainer) benötigt.
///
public string SonicHome { get; init; } = @"C:\Sonic\MQ10.0";
// ---------------------------------------------------------------
// HTTP REST API (ManagementMode = HttpApi)
// ---------------------------------------------------------------
public int ManagementHttpPort { get; init; } = 8080;
public string ApiBasePath { get; init; } = "/api/v1";
public string ContainerListPath { get; init; } = string.Empty;
public string ContainerRestartPath { get; init; } = string.Empty;
public string ContainerStopPath { get; init; } = string.Empty;
public string ContainerStartPath { get; init; } = string.Empty;
// ---------------------------------------------------------------
// WinRM / LocalCmd
// ---------------------------------------------------------------
public int WinRmPort { get; init; } = 5985;
///
/// PowerShell-Script zum Neustarten. Leer = Default über Sonic bin\stop/startcontainer.bat.
/// Platzhalter: {container}, {domain}, {sonicHome}
///
public string WinRmRestartScript { get; init; } = string.Empty;
///
/// PowerShell-Script zum Auflisten der Container. Leer = Default (Services + SonicHome).
///
public string WinRmContainerListScript { get; init; } = string.Empty;
public string WinRmXapiImportScript { get; init; } = string.Empty;
public int TimeoutSeconds { get; init; } = 60;
public int PostRestartDelaySeconds { get; init; } = 15;
/// Effektives Restart-Script inkl. Default, wenn leer.
public string ResolveRestartScript()
=> string.IsNullOrWhiteSpace(WinRmRestartScript)
? DefaultRestartScript
: WinRmRestartScript;
/// Effektives List-Script inkl. Default, wenn leer.
public string ResolveContainerListScript()
=> string.IsNullOrWhiteSpace(WinRmContainerListScript)
? DefaultContainerListScript
: WinRmContainerListScript;
///
/// Default: Sonic Management Console Tools per CMD auf dem Ziel-PC.
/// stopcontainer.bat / startcontainer.bat unter SonicHome\bin.
///
public const string DefaultRestartScript =
"""
$ErrorActionPreference = 'Stop'
$sonicHome = '{sonicHome}'
$domain = '{domain}'
$container = '{container}'
$bin = Join-Path $sonicHome 'bin'
$stop = Join-Path $bin 'stopcontainer.bat'
$start = Join-Path $bin 'startcontainer.bat'
$fullName = if ($container -like '*.*') { $container } else { "$domain.$container" }
if (-not (Test-Path -LiteralPath $stop)) {
throw "Sonic stopcontainer.bat nicht gefunden: $stop (SonicHome prüfen)"
}
if (-not (Test-Path -LiteralPath $start)) {
throw "Sonic startcontainer.bat nicht gefunden: $start (SonicHome prüfen)"
}
Write-Output "STOP $fullName via $stop"
$stopProc = Start-Process -FilePath 'cmd.exe' -ArgumentList @('/c', "`"$stop`" `"$fullName`"") -Wait -PassThru -NoNewWindow
if ($stopProc.ExitCode -ne 0) {
Write-Warning "stopcontainer ExitCode=$($stopProc.ExitCode) – starte trotzdem neu"
}
Start-Sleep -Seconds 5
Write-Output "START $fullName via $start"
$startProc = Start-Process -FilePath 'cmd.exe' -ArgumentList @('/c', "`"$start`" `"$fullName`"") -Wait -PassThru -NoNewWindow
if ($startProc.ExitCode -ne 0) {
throw "startcontainer fehlgeschlagen, ExitCode=$($startProc.ExitCode)"
}
Write-Output "Neustart ok: $fullName"
""";
public const string DefaultContainerListScript =
"""
$ErrorActionPreference = 'Continue'
$sonicHome = '{sonicHome}'
$names = @()
# Windows-Dienste mit Sonic/ESB im Namen
$names += @(Get-Service -ErrorAction SilentlyContinue |
Where-Object { $_.DisplayName -match 'Sonic|ESB|MQ' -or $_.Name -match 'Sonic|ESB|MQ' } |
Select-Object -ExpandProperty Name)
# Container-Cache-Ordner unter SonicHome (Domain.Container.cache)
if (Test-Path -LiteralPath $sonicHome) {
$names += @(Get-ChildItem -LiteralPath $sonicHome -Directory -ErrorAction SilentlyContinue |
Where-Object { $_.Name -like '*.cache' } |
ForEach-Object { $_.Name -replace '\.cache$', '' })
}
$names | Where-Object { $_ } | Sort-Object -Unique
""";
}
public enum SonicManagementMode
{
/// HTTP REST API (wenn vom Sonic-Server bereitgestellt).
HttpApi = 0,
///
/// PowerShell Remoting (WinRM) – führt Scripts auf dem Sonic-Server aus.
///
WinRm = 1,
///
/// CMD/PowerShell lokal auf diesem PC – zum Testen direkt auf dem Sonic-Rechner.
///
LocalCmd = 2
}