Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Adds support to offload service and JEA actions to scheduled tasks #657

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function Clear-IcingaInternalServiceInformation()
{
$Global:Icinga.Protected.ServiceRestartLock = $FALSE;
$Global:Icinga.Protected.IcingaServiceUser = '';
$Global:Icinga.Protected.IfWServiceUser = '';
$Global:Icinga.Protected.IcingaServiceState = '';
$Global:Icinga.Protected.IfWServiceState = '';
}
53 changes: 53 additions & 0 deletions lib/core/framework/Get-IcingaWindowsServiceStatus.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
function Get-IcingaWindowsServiceStatus()
{
param (
[string]$Service = '',
[switch]$Force = $FALSE
);

if ($Service -eq 'icinga2' -Or $Service -eq 'icingapowershell') {
if ($Service -eq 'icinga2') {
if ([string]::IsNullOrEmpty($Global:Icinga.Protected.IcingaServiceState) -eq $FALSE) {
if ($Global:Icinga.Protected.ServiceRestartLock -And $Force -eq $FALSE) {
return @{
'Status' = $Global:Icinga.Protected.IcingaServiceState;
'Present' = $TRUE;
'Name' = $Service;
'DisplayName' = $Service;
};
}
}
} elseif ($Service -eq 'icingapowershell') {
if ([string]::IsNullOrEmpty($Global:Icinga.Protected.IfWServiceState) -eq $FALSE) {
if ($Global:Icinga.Protected.ServiceRestartLock -And $Force -eq $FALSE) {
return @{
'Status' = $Global:Icinga.Protected.IfWServiceState;
'Present' = $TRUE;
'Name' = $Service;
'DisplayName' = $Service;
};
}
}
}
}

$ServiceData = Invoke-IcingaWindowsScheduledTask -JobType 'GetWindowsService' -ObjectName $Service;

if ($ServiceData.Service.Installed -eq $FALSE) {
Write-IcingaConsoleError $ServiceData.ErrMsg;
return @{
'Status' = '';
'Present' = $FALSE;
'Name' = 'Unknown';
'DisplayName' = 'Unknown';
};
}

if ($Service -eq 'icinga2') {
$Global:Icinga.Protected.IcingaServiceState = $ServiceData.Service.Status;
} elseif ($Service -eq 'icingapowershell') {
$Global:Icinga.Protected.IfWServiceState = $ServiceData.Service.Status;
}

return $ServiceData.Service;
}
6 changes: 3 additions & 3 deletions lib/core/framework/Install-IcingaForWindowsService.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ function Install-IcingaForWindowsService()

$UpdateFile = [string]::Format('{0}.update', $Path);

$ServiceStatus = (Get-Service 'icingapowershell' -ErrorAction SilentlyContinue).Status;
$ServiceStatus = Get-IcingaWindowsServiceStatus -Service 'icingapowershell';

if ((Test-Path $UpdateFile)) {

Write-IcingaConsoleNotice 'Updating Icinga PowerShell Service binary';

if ($ServiceStatus -eq 'Running') {
if ($ServiceStatus.Status -eq 'Running') {
Write-IcingaConsoleNotice 'Stopping Icinga PowerShell service';
Stop-IcingaWindowsService;
Start-Sleep -Seconds 1;
Expand All @@ -68,7 +68,7 @@ function Install-IcingaForWindowsService()
(Get-IcingaPowerShellModuleFile)
);

if ($null -eq $ServiceStatus) {
if ($ServiceStatus.Present -eq $FALSE) {
$ServiceCreation = Start-IcingaProcess -Executable 'sc.exe' -Arguments ([string]::Format('create icingapowershell binPath= "{0}" DisplayName= "Icinga PowerShell Service" start= auto', $Path));

if ($ServiceCreation.ExitCode -ne 0) {
Expand Down
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
6 changes: 3 additions & 3 deletions lib/core/framework/Install-IcingaFrameworkUpdate.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ function Install-IcingaFrameworkUpdate()

Write-IcingaConsoleNotice ([string]::Format('Using content of folder "{0}" for updates', $ModuleContent));

$ServiceStatus = (Get-Service 'icingapowershell' -ErrorAction SilentlyContinue).Status;
$AgentStatus = (Get-Service 'icinga2' -ErrorAction SilentlyContinue).Status;
$ServiceStatus = (Get-IcingaWindowsServiceStatus -Service 'icingapowershell').Status;
$AgentStatus = (Get-IcingaWindowsServiceStatus -Service 'icinga2').Status;

if ($ServiceStatus -eq 'Running') {
Write-IcingaConsoleNotice 'Stopping Icinga PowerShell service';
Expand Down 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
4 changes: 2 additions & 2 deletions lib/core/framework/Invoke-IcingaForWindowsMigration.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function Invoke-IcingaForWindowsMigration()

# Upgrade to v1.8.0
if (Test-IcingaForWindowsMigration -MigrationVersion (New-IcingaVersionObject -Version '1.8.0')) {
$ServiceStatus = (Get-Service 'icingapowershell' -ErrorAction SilentlyContinue).Status;
$ServiceStatus = (Get-IcingaWindowsServiceStatus -Service 'icingapowershell').Status;

Write-IcingaConsoleNotice 'Applying pending migrations required for Icinga for Windows v1.8.0';
if ($ServiceStatus -eq 'Running') {
Expand Down Expand Up @@ -43,7 +43,7 @@ function Invoke-IcingaForWindowsMigration()
if (Test-IcingaForWindowsMigration -MigrationVersion (New-IcingaVersionObject -Version '1.10.0')) {
Write-IcingaConsoleNotice 'Applying pending migrations required for Icinga for Windows v1.10.0';

$ServiceStatus = (Get-Service 'icingapowershell' -ErrorAction SilentlyContinue).Status;
$ServiceStatus = (Get-IcingaWindowsServiceStatus -Service 'icingapowershell').Status;

if ($ServiceStatus -eq 'Running') {
Stop-IcingaWindowsService;
Expand Down
2 changes: 1 addition & 1 deletion lib/core/framework/Invoke-IcingaInternalServiceCall.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function Invoke-IcingaInternalServiceCall()
}

# Test our Icinga for Windows service. If the service is not installed or not running, execute the plugin locally
$IcingaForWindowsService = (Get-Service 'icingapowershell' -ErrorAction SilentlyContinue);
$IcingaForWindowsService = Get-Service 'icingapowershell' -ErrorAction SilentlyContinue;

if ($null -eq $IcingaForWindowsService -Or $IcingaForWindowsService.Status -ne 'Running') {
return $NULL;
Expand Down
5 changes: 5 additions & 0 deletions lib/core/framework/New-IcingaEnvironmentVariable.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ function New-IcingaEnvironmentVariable()
$Global:Icinga.Protected.Add('RunAsDaemon', $FALSE);
$Global:Icinga.Protected.Add('Minimal', $FALSE);
$Global:Icinga.Protected.Add('ThreadName', '');
$Global:Icinga.Protected.Add('IcingaServiceUser', '');
$Global:Icinga.Protected.Add('IfWServiceUser', '');
$Global:Icinga.Protected.Add('ServiceRestartLock', $FALSE);
$Global:Icinga.Protected.Add('IcingaServiceState', '');
$Global:Icinga.Protected.Add('IfWServiceState', '');
$Global:Icinga.Protected.Add('GarbageCollector', @{ });
}
}
30 changes: 14 additions & 16 deletions lib/core/framework/Restart-IcingaService.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,25 @@
function Restart-IcingaService()
{
param (
$Service
$Service,
[switch]$Force = $FALSE
);

if (Get-Service "$Service" -ErrorAction SilentlyContinue) {
Write-IcingaConsoleNotice ([string]::Format('Restarting service "{0}"', $Service));
if ($Global:Icinga.Protected.ServiceRestartLock -And $Force -eq $FALSE) {
return;
}

& powershell.exe -Command {
Use-Icinga -Minimal;
$Result = Invoke-IcingaWindowsScheduledTask -JobType 'RestartWindowsService' -ObjectName $Service;

$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;
if ($Service -eq 'icinga2') {
$Global:Icinga.Protected.IcingaServiceState = $Result.Status;
} elseif ($Service -eq 'icingapowershell') {
$Global:Icinga.Protected.IfWServiceState = $Result.Status;
}
}
32 changes: 15 additions & 17 deletions lib/core/framework/Start-IcingaService.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,26 @@

function Start-IcingaService()
{
param(
$Service
param (
$Service,
[switch]$Force = $FALSE
);

if (Get-Service $Service -ErrorAction SilentlyContinue) {
Write-IcingaConsoleNotice -Message 'Starting service "{0}"' -Objects $Service;
if ($Global:Icinga.Protected.ServiceRestartLock -And $Force -eq $FALSE) {
return;
}

& powershell.exe -Command {
Use-Icinga -Minimal;
$Result = Invoke-IcingaWindowsScheduledTask -JobType 'StartWindowsService' -ObjectName $Service;

$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;
if ($Service -eq 'icinga2') {
$Global:Icinga.Protected.IcingaServiceState = $Result.Status;
} elseif ($Service -eq 'icingapowershell') {
$Global:Icinga.Protected.IfWServiceState = $Result.Status;
}
}
32 changes: 15 additions & 17 deletions lib/core/framework/Stop-IcingaService.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,26 @@

function Stop-IcingaService()
{
param(
$Service
param (
$Service,
[switch]$Force = $FALSE
);

if (Get-Service "$Service" -ErrorAction SilentlyContinue) {
Write-IcingaConsoleNotice -Message 'Stopping service "{0}"' -Objects $Service;
if ($Global:Icinga.Protected.ServiceRestartLock -And $Force -eq $FALSE) {
return;
}

& powershell.exe -Command {
Use-Icinga -Minimal;
$Result = Invoke-IcingaWindowsScheduledTask -JobType 'StopWindowsService' -ObjectName $Service;

$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;
if ($Service -eq 'icinga2') {
$Global:Icinga.Protected.IcingaServiceState = $Result.Status;
} elseif ($Service -eq 'icingapowershell') {
$Global:Icinga.Protected.IfWServiceState = $Result.Status;
}
}
11 changes: 2 additions & 9 deletions lib/core/icingaagent/getters/Get-IcingaAgentInstallation.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ function Get-IcingaAgentInstallation()
}
}

$IcingaService = Get-IcingaServices -Service 'icinga2';
$ServiceUser = 'NT AUTHORITY\NetworkService';

if ($null -ne $IcingaService) {
$ServiceUser = $IcingaService.icinga2.configuration.ServiceUser;
}

if ($null -eq $IcingaData) {
return @{
'Installed' = $FALSE;
Expand All @@ -33,7 +26,7 @@ function Get-IcingaAgentInstallation()
'Architecture' = $architecture;
'Uninstaller' = '';
'InstallDate' = '';
'User' = $ServiceUser;
'User' = (Get-IcingaServiceUser);
};
}

Expand All @@ -44,6 +37,6 @@ function Get-IcingaAgentInstallation()
'Architecture' = $architecture;
'Uninstaller' = $IcingaData.UninstallString.Replace("MsiExec.exe ", "");
'InstallDate' = $IcingaData.InstallDate;
'User' = $ServiceUser;
'User' = (Get-IcingaServiceUser);
};
}
26 changes: 19 additions & 7 deletions lib/core/icingaagent/getters/Get-IcingaServiceUser.psm1
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
function Get-IcingaServiceUser()
{
$Services = Get-IcingaServices -Service 'icinga2';
if ($null -eq $Services) {
$Services = Get-IcingaServices -Service 'icingapowershell';
if ($null -eq $Services) {
return $null;
if ([string]::IsNullOrEmpty($Global:Icinga.Protected.IcingaServiceUser) -eq $FALSE) {
return $Global:Icinga.Protected.IcingaServiceUser;
}

$Services = Get-IcingaWindowsServiceStatus -Service 'icinga2';
if ($Services.Present -eq $FALSE) {
$Services = Get-IcingaWindowsServiceStatus -Service 'icingapowershell';
if ($Services.Present -eq $FALSE) {
return 'NT Authority\NetworkService';
}
}

$Services = $Services.GetEnumerator() | Select-Object -First 1;
$ServiceUser = ($Services.Value.configuration.ServiceUser).Replace('.\', '');
$ServiceUser = (Get-IcingaWindowsInformation Win32_Service |
ForEach-Object {
if ($_.Name -Like $Services.Name) {
return $_;
}
} | Select-Object StartName).StartName;

$ServiceUser = $ServiceUser.Replace('.\', '');

if ($ServiceUser -eq 'LocalSystem') {
$ServiceUser = 'NT Authority\SYSTEM';
}

$Global:Icinga.Protected.IcingaServiceUser = $ServiceUser;

return $ServiceUser;
}
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 /norestart /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 /norestart', $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
Loading