Skip to content

Commit

Permalink
Adds feature to offload service and JEA actions to scheduled tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
LordHepipud committed Jul 31, 2023
1 parent f028ade commit 3029bca
Show file tree
Hide file tree
Showing 16 changed files with 262 additions and 118 deletions.
24 changes: 24 additions & 0 deletions jobs/RestartWindowsService.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
param (
[string]$ServiceName = '',
[string]$TmpFilePath = ''
);

Use-Icinga -Minimal;

[bool]$Success = $TRUE;
[string]$ErrMsg = "";

try {
Restart-Service "$ServiceName" -ErrorAction Stop;
} catch {
$Success = $FALSE;
$ErrMsg = [string]::Format('Failed to restart service "{0}": {1}', $ServiceName, $_.Exception.Message);
}

Write-IcingaFileSecure -File "$TmpFilePath" -Value (
@{
'Success' = $Success;
'Message' = [string]::Format('Service "{0}" successfully restarted', $ServiceName);
'ErrMsg' = $ErrMsg;
} | ConvertTo-Json -Depth 100
);
24 changes: 24 additions & 0 deletions jobs/StartWindowsService.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
param (
[string]$ServiceName = '',
[string]$TmpFilePath = ''
);

Use-Icinga -Minimal;

[bool]$Success = $TRUE;
[string]$ErrMsg = "";

try {
Start-Service "$ServiceName" -ErrorAction Stop;
} catch {
$Success = $FALSE;
$ErrMsg = [string]::Format('Failed to start service "{0}": {1}', $ServiceName, $_.Exception.Message);
}

Write-IcingaFileSecure -File "$TmpFilePath" -Value (
@{
'Success' = $Success;
'Message' = [string]::Format('Service "{0}" successfully started', $ServiceName);
'ErrMsg' = $ErrMsg;
} | ConvertTo-Json -Depth 100
);
24 changes: 24 additions & 0 deletions jobs/StopWindowsService.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
param (
[string]$ServiceName = '',
[string]$TmpFilePath = ''
);

Use-Icinga -Minimal;

[bool]$Success = $TRUE;
[string]$ErrMsg = "";

try {
Stop-Service "$ServiceName" -ErrorAction Stop;
} catch {
$Success = $FALSE;
$ErrMsg = [string]::Format('Failed to stop service "{0}": {1}', $ServiceName, $_.Exception.Message);
}

Write-IcingaFileSecure -File "$TmpFilePath" -Value (
@{
'Success' = $Success;
'Message' = [string]::Format('Service "{0}" successfully stopped', $ServiceName);
'ErrMsg' = $ErrMsg;
} | ConvertTo-Json -Depth 100
);
2 changes: 1 addition & 1 deletion lib/core/framework/Install-IcingaFrameworkComponent.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function Install-IcingaFrameworkComponent()

if ([string]::IsNullOrEmpty((Get-IcingaJEAContext)) -eq $FALSE) {
Write-IcingaConsoleNotice 'Updating Icinga JEA profile';
& powershell.exe -Command { Use-Icinga -Minimal; Install-IcingaJEAProfile; } | Out-Null;
Invoke-IcingaWindowsScheduledTask -JobType InstallJEA -Timeout 600 | Out-Null;
}

# Unload the module if it was loaded before
Expand Down
2 changes: 1 addition & 1 deletion lib/core/framework/Install-IcingaFrameworkUpdate.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function Install-IcingaFrameworkUpdate()
if ([string]::IsNullOrEmpty((Get-IcingaJEAContext)) -eq $FALSE) {
Remove-IcingaFrameworkDependencyFile;
Write-IcingaConsoleNotice 'Updating Icinga JEA profile';
& powershell.exe -Command { Use-Icinga -Minimal; Install-IcingaJEAProfile; } | Out-Null;
Invoke-IcingaWindowsScheduledTask -JobType InstallJEA -Timeout 600 | Out-Null;
}

Write-IcingaConsoleNotice 'Framework update has been completed. Please start a new PowerShell instance now to complete the update';
Expand Down
21 changes: 4 additions & 17 deletions lib/core/framework/Restart-IcingaService.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,11 @@ function Restart-IcingaService()
$Service
);

if (Get-Service "$Service" -ErrorAction SilentlyContinue) {
Write-IcingaConsoleNotice ([string]::Format('Restarting service "{0}"', $Service));
$Result = Invoke-IcingaWindowsScheduledTask -JobType 'RestartWindowsService' -ObjectName $Service;

& powershell.exe -Command {
Use-Icinga -Minimal;

$Service = $args[0];
try {
Restart-Service "$Service" -ErrorAction Stop;
Start-Sleep -Seconds 2;
Optimize-IcingaForWindowsMemory;
} catch {
Write-IcingaConsoleError -Message 'Failed to restart service "{0}". Error: {1}' -Objects $Service, $_.Exception.Message;
}
} -Args $Service;
if ($Result.Success -eq $FALSE) {
Write-IcingaConsoleError $Result.ErrMsg
} else {
Write-IcingaConsoleWarning -Message 'The service "{0}" is not installed' -Objects $Service;
Write-IcingaConsoleNotice $Result.Message
}

Optimize-IcingaForWindowsMemory;
}
21 changes: 4 additions & 17 deletions lib/core/framework/Start-IcingaService.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,11 @@ function Start-IcingaService()
$Service
);

if (Get-Service $Service -ErrorAction SilentlyContinue) {
Write-IcingaConsoleNotice -Message 'Starting service "{0}"' -Objects $Service;
$Result = Invoke-IcingaWindowsScheduledTask -JobType 'StartWindowsService' -ObjectName $Service;

& powershell.exe -Command {
Use-Icinga -Minimal;

$Service = $args[0];
try {
Start-Service "$Service" -ErrorAction Stop;
Start-Sleep -Seconds 2;
Optimize-IcingaForWindowsMemory;
} catch {
Write-IcingaConsoleError -Message 'Failed to start service "{0}". Error: {1}' -Objects $Service, $_.Exception.Message;
}
} -Args $Service;
if ($Result.Success -eq $FALSE) {
Write-IcingaConsoleError $Result.ErrMsg
} else {
Write-IcingaConsoleWarning -Message 'The service "{0}" is not installed' -Objects $Service;
Write-IcingaConsoleNotice $Result.Message
}

Optimize-IcingaForWindowsMemory;
}
21 changes: 4 additions & 17 deletions lib/core/framework/Stop-IcingaService.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,11 @@ function Stop-IcingaService()
$Service
);

if (Get-Service "$Service" -ErrorAction SilentlyContinue) {
Write-IcingaConsoleNotice -Message 'Stopping service "{0}"' -Objects $Service;
$Result = Invoke-IcingaWindowsScheduledTask -JobType 'StopWindowsService' -ObjectName $Service;

& powershell.exe -Command {
Use-Icinga -Minimal;

$Service = $args[0];
try {
Stop-Service "$Service" -ErrorAction Stop;
Start-Sleep -Seconds 2;
Optimize-IcingaForWindowsMemory;
} catch {
Write-IcingaConsoleError -Message 'Failed to stop service "{0}". Error: {1}' -Objects $Service, $_.Exception.Message;
}
} -Args $Service;
if ($Result.Success -eq $FALSE) {
Write-IcingaConsoleError $Result.ErrMsg
} else {
Write-IcingaConsoleWarning -Message 'The service "{0}" is not installed' -Objects $Service;
Write-IcingaConsoleNotice $Result.Message
}

Optimize-IcingaForWindowsMemory;
}
13 changes: 1 addition & 12 deletions lib/core/icingaagent/installer/Install-IcingaAgent.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,7 @@ function Install-IcingaAgent()
}
}

$InstallProcess = & powershell.exe -Command {
Use-Icinga -Minimal;

$IcingaInstaller = $args[0];
$InstallTarget = $args[1];
$InstallProcess = Start-IcingaProcess -Executable 'MsiExec.exe' -Arguments ([string]::Format('/quiet /i "{0}" {1}', $IcingaInstaller.InstallerPath, $InstallTarget)) -FlushNewLines;

Start-Sleep -Seconds 2;
Optimize-IcingaForWindowsMemory;

return $InstallProcess;
} -Args $IcingaInstaller, $InstallTarget;
$InstallProcess = Start-IcingaProcess -Executable 'MsiExec.exe' -Arguments ([string]::Format('/quiet /i "{0}" {1}', $IcingaInstaller.InstallerPath, $InstallTarget)) -FlushNewLines;

if ($InstallProcess.ExitCode -ne 0) {
Write-IcingaConsoleError -Message 'Failed to install Icinga 2 Agent: {0}{1}' -Objects $InstallProcess.Message, $InstallProcess.Error;
Expand Down
12 changes: 1 addition & 11 deletions lib/core/icingaagent/installer/Uninstall-IcingaAgent.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,7 @@ function Uninstall-IcingaAgent()

Stop-IcingaService -Service 'icinga2';

$Uninstaller = & powershell.exe -Command {
Use-Icinga -Minimal;

$IcingaData = $args[0];
$Uninstaller = Start-IcingaProcess -Executable 'MsiExec.exe' -Arguments ([string]::Format('{0} /q', $IcingaData.Uninstaller)) -FlushNewLine;

Start-Sleep -Seconds 2;
Optimize-IcingaForWindowsMemory;

return $Uninstaller;
} -Args $IcingaData;
$Uninstaller = Invoke-IcingaWindowsScheduledTask -JobType UninstallAgent -FilePath $IcingaData.Uninstaller;

if ($Uninstaller.ExitCode -ne 0) {
Write-IcingaConsoleError ([string]::Format('Failed to remove Icinga Agent: {0}{1}', $Uninstaller.Message, $Uninstaller.Error));
Expand Down
37 changes: 15 additions & 22 deletions lib/core/installer/Start-IcingaForWindowsInstallation.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ function Start-IcingaForWindowsInstallation()
$PluginPackageRelease = $FALSE;
$PluginPackageSnapshot = $FALSE;
$ForceCertificateGen = $FALSE;
[bool]$InstallJEA = $FALSE;
[bool]$InstallRESTApi = $FALSE;

if ([string]::IsNullOrEmpty($IcingaStableRepo) -eq $FALSE) {
Expand Down Expand Up @@ -252,22 +251,6 @@ function Start-IcingaForWindowsInstallation()
Restart-IcingaWindowsService;
}

switch ($InstallJEAProfile) {
'0' {
Install-IcingaJEAProfile;
$InstallJEA = $TRUE;
break;
};
'1' {
Install-IcingaSecurity;
$InstallJEA = $TRUE;
break;
};
'2' {
# Do not install JEA profile
};
}

switch ($InstallApiChecks) {
'0' {
Disable-IcingaFrameworkApiChecks;
Expand Down Expand Up @@ -298,18 +281,28 @@ function Start-IcingaForWindowsInstallation()
};
}

# Install Icinga for Windows certificate if both, JEA and REST is installed
if ($InstallJEA -And $InstallRESTApi) {
Install-IcingaForWindowsCertificate;
}

# Update configuration and clear swap
$ConfigSwap = Get-IcingaPowerShellConfig -Path 'Framework.Config.Swap';
Set-IcingaPowerShellConfig -Path 'Framework.Config.Swap' -Value $null;
Set-IcingaPowerShellConfig -Path 'Framework.Config.Live' -Value $ConfigSwap;
$global:Icinga.InstallWizard.Config = @{ };
Set-IcingaPowerShellConfig -Path 'Framework.Installed' -Value $TRUE;

# Always install the JEA profile at the end
switch ($InstallJEAProfile) {
'0' {
Invoke-IcingaWindowsScheduledTask -JobType InstallJEA -Timeout 600 | Out-Null;
break;
};
'1' {
Install-IcingaSecurity -RunAsTask;
break;
};
'2' {
# Do not install JEA profile
};
}

if ($Automated -eq $FALSE) {
Write-IcingaConsoleNotice 'Icinga for Windows is installed. Returning to main menu in 5 seconds'
Start-Sleep -Seconds 5;
Expand Down
20 changes: 2 additions & 18 deletions lib/core/repository/Install-IcingaComponent.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,7 @@ function Install-IcingaComponent()
}
}

$MSIData = & powershell.exe -Command {
Use-Icinga -Minimal;

$DownloadDestination = $args[0];
return (Read-IcingaMSIMetadata -File $DownloadDestination);
} -Args $DownloadDestination;
$MSIData = Invoke-IcingaWindowsScheduledTask -JobType ReadMSIPackage -FilePath $DownloadDestination;

if ($InstalledVersion.Full -eq $MSIData.ProductVersion -And $Force -eq $FALSE) {
Write-IcingaConsoleWarning 'The package "agent" with version "{0}" is already installed. Use "-Force" to re-install the component' -Objects $InstalledVersion.Full;
Expand All @@ -373,18 +368,7 @@ function Install-IcingaComponent()
}
}

$InstallProcess = & powershell.exe -Command {
Use-Icinga -Minimal;

$DownloadDestination = $args[0];
$InstallTarget = $args[1];
$InstallProcess = Start-IcingaProcess -Executable 'MsiExec.exe' -Arguments ([string]::Format('/quiet /i "{0}" {1}', $DownloadDestination, $InstallTarget)) -FlushNewLines;

Start-Sleep -Seconds 2;
Optimize-IcingaForWindowsMemory;

return $InstallProcess;
} -Args $DownloadDestination, $InstallTarget;
$InstallProcess = Start-IcingaProcess -Executable 'MsiExec.exe' -Arguments ([string]::Format('/quiet /i "{0}" {1}', $DownloadDestination, $InstallTarget)) -FlushNewLines;

if ($InstallProcess.ExitCode -ne 0) {
Write-IcingaConsoleError -Message 'Failed to install component "agent": {0}{1}' -Objects $InstallProcess.Message, $InstallProcess.Error;
Expand Down
10 changes: 8 additions & 2 deletions lib/core/windows/Install-IcingaSecurity.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ function Install-IcingaSecurity()
[string]$IcingaUser = 'icinga',
[switch]$RebuildFramework = $FALSE,
[switch]$AllowScriptBlocks = $FALSE,
[switch]$ConstrainedLanguage = $FALSE
[switch]$ConstrainedLanguage = $FALSE,
[switch]$RunAsTask = $FALSE
);

if ($PSVersionTable.PSVersion -lt (New-IcingaVersionObject -Version 5, 0)) {
Expand All @@ -25,7 +26,12 @@ function Install-IcingaSecurity()
}

Install-IcingaServiceUser -IcingaUser $IcingaUser;
Install-IcingaJEAProfile -IcingaUser $IcingaUser -RebuildFramework:$RebuildFramework -AllowScriptBlocks:$AllowScriptBlocks -ConstrainedLanguage:$ConstrainedLanguage;

if ($RunAsTask) {
Invoke-IcingaWindowsScheduledTask -JobType InstallJEA -Timeout 600 | Out-Null;
} else {
Install-IcingaJEAProfile -IcingaUser $IcingaUser -RebuildFramework:$RebuildFramework -AllowScriptBlocks:$AllowScriptBlocks -ConstrainedLanguage:$ConstrainedLanguage;
}

Restart-IcingaWindowsService;
}
Loading

0 comments on commit 3029bca

Please sign in to comment.