icinga-powershell-framework/lib/daemon/Start-IcingaPowerShellDaemon.psm1

100 lines
4.1 KiB
PowerShell
Raw Normal View History

function Start-IcingaPowerShellDaemon()
{
2021-08-06 12:12:27 -04:00
param (
[switch]$RunAsService = $FALSE,
[switch]$JEARestart = $FALSE
);
2021-12-09 11:42:06 -05:00
Start-IcingaForWindowsDaemon -RunAsService:$RunAsService -JEARestart:$JEARestart;
}
2021-12-09 11:42:06 -05:00
function Start-IcingaForWindowsDaemon()
{
param (
[switch]$RunAsService = $FALSE,
[switch]$JEARestart = $FALSE
);
$Global:Icinga.Protected.RunAsDaemon = $TRUE;
2021-08-06 12:12:27 -04:00
[string]$MainServicePidFile = (Join-Path -Path (Get-IcingaCacheDir) -ChildPath 'service.pid');
[string]$JeaPidFile = (Join-Path -Path (Get-IcingaCacheDir) -ChildPath 'jea.pid');
[string]$JeaProfile = Get-IcingaPowerShellConfig -Path 'Framework.JEAProfile';
[Security.Cryptography.X509Certificates.X509Certificate2]$Certificate = Get-IcingaForWindowsCertificate;
[string]$JeaPid = '';
2021-08-06 12:12:27 -04:00
if (Test-IcingaJEAServiceRunning) {
Write-IcingaEventMessage -EventId 1503 -Namespace 'Framework';
exit 1;
}
2021-08-06 12:12:27 -04:00
Write-IcingaFileSecure -File ($MainServicePidFile) -Value $PID;
2021-08-06 12:12:27 -04:00
if ([string]::IsNullOrEmpty($JeaProfile)) {
Write-IcingaDebugMessage -Message 'Starting Icinga for Windows service without JEA context' -Objects $RunAsService, $JEARestart, $JeaProfile;
2021-12-09 11:42:06 -05:00
$Global:Icinga.Protected.RunAsDaemon = $TRUE;
2021-08-06 12:12:27 -04:00
# Todo: Add config for active background tasks. Set it to 20 for the moment
2021-12-09 11:42:06 -05:00
Add-IcingaThreadPool -Name 'MainPool' -MaxInstances 20;
$Global:Icinga.Public.Add('SSLCertificate', $Certificate);
2021-08-06 12:12:27 -04:00
2021-12-09 11:42:06 -05:00
New-IcingaThreadInstance -Name "Main" -ThreadPool (Get-IcingaThreadPool -Name 'MainPool') -Command 'Add-IcingaForWindowsDaemon' -Start;
2021-08-06 12:12:27 -04:00
} else {
Write-IcingaDebugMessage -Message 'Starting Icinga for Windows service inside JEA context' -Objects $RunAsService, $JEARestart, $JeaProfile;
& powershell.exe -NoProfile -NoLogo -ConfigurationName $JeaProfile -Command {
try {
Use-Icinga -Daemon;
2021-08-06 12:12:27 -04:00
Write-IcingaFileSecure -File ($args[1]) -Value $PID;
2021-12-09 11:42:06 -05:00
$Global:Icinga.Protected.JEAContext = $TRUE;
$Global:Icinga.Protected.RunAsDaemon = $TRUE;
2021-08-06 12:12:27 -04:00
# Todo: Add config for active background tasks. Set it to 20 for the moment
2021-12-09 11:42:06 -05:00
Add-IcingaThreadPool -Name 'MainPool' -MaxInstances 20;
$Global:Icinga.Public.Add('SSLCertificate', $args[0]);
2021-08-06 12:12:27 -04:00
2021-12-09 11:42:06 -05:00
New-IcingaThreadInstance -Name "Main" -ThreadPool (Get-IcingaThreadPool -Name 'MainPool') -Command 'Add-IcingaForWindowsDaemon' -Start;
2021-08-06 12:12:27 -04:00
while ($TRUE) {
Start-Sleep -Seconds 100;
}
} catch {
2021-12-09 11:42:06 -05:00
Write-IcingaEventMessage -EventId 1600 -Namespace 'Framework' -ExceptionObject $_;
2021-08-06 12:12:27 -04:00
}
} -Args $Certificate, $JeaPidFile;
}
if ($JEARestart) {
return;
}
if ($RunAsService) {
2021-08-06 12:12:27 -04:00
[int]$JeaRestartCounter = 1;
while ($TRUE) {
2021-08-06 12:12:27 -04:00
if ([string]::IsNullOrEmpty($JeaProfile) -eq $FALSE) {
if ([string]::IsNullOrEmpty($JeaPid)) {
[string]$JeaPid = Get-IcingaJEAServicePid;
}
if ((Test-IcingaJEAServiceRunning -JeaPid $JeaPid) -eq $FALSE) {
if ($JeaRestartCounter -gt 5) {
Write-IcingaEventMessage -EventId 1504 -Namespace Framework;
exit 1;
}
Write-IcingaFileSecure -File $JeaPidFile -Value '';
Write-IcingaEventMessage -EventId 1505 -Namespace Framework -Objects ([string]::Format('{0}/5', $JeaRestartCounter));
2021-12-09 11:42:06 -05:00
Start-IcingaForWindowsDaemon -RunAsService:$RunAsService -JEARestart;
2021-08-06 12:12:27 -04:00
$JeaRestartCounter += 1;
$JeaPid = '';
}
Start-Sleep -Seconds 5;
continue;
}
Start-Sleep -Seconds 100;
}
}
}