Skip to content

Commit 0b1de9e

Browse files
committed
Adds feature to offload service and JEA actions to scheduled tasks
1 parent 117d20f commit 0b1de9e

34 files changed

+237
-168
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function Clear-IcingaInternalServiceInformation()
2+
{
3+
$Global:Icinga.Protected.ServiceRestartLock = $FALSE;
4+
$Global:Icinga.Protected.IcingaServiceUser = '';
5+
$Global:Icinga.Protected.IfWServiceUser = '';
6+
$Global:Icinga.Protected.IcingaServiceState = '';
7+
$Global:Icinga.Protected.IfWServiceState = '';
8+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
function Get-IcingaWindowsServiceStatus()
2+
{
3+
param (
4+
[string]$Service = '',
5+
[switch]$Force = $FALSE
6+
);
7+
8+
if ($Service -eq 'icinga2' -Or $Service -eq 'icingapowershell') {
9+
if ($Service -eq 'icinga2') {
10+
if ([string]::IsNullOrEmpty($Global:Icinga.Protected.IcingaServiceState) -eq $FALSE) {
11+
if ($Global:Icinga.Protected.ServiceRestartLock -And $Force -eq $FALSE) {
12+
return @{
13+
'Status' = $Global:Icinga.Protected.IcingaServiceState;
14+
'Present' = $TRUE;
15+
'Name' = $Service;
16+
'DisplayName' = $Service;
17+
};
18+
}
19+
}
20+
} elseif ($Service -eq 'icingapowershell') {
21+
if ([string]::IsNullOrEmpty($Global:Icinga.Protected.IfWServiceState) -eq $FALSE) {
22+
if ($Global:Icinga.Protected.ServiceRestartLock -And $Force -eq $FALSE) {
23+
return @{
24+
'Status' = $Global:Icinga.Protected.IfWServiceState;
25+
'Present' = $TRUE;
26+
'Name' = $Service;
27+
'DisplayName' = $Service;
28+
};
29+
}
30+
}
31+
}
32+
}
33+
34+
$ServiceData = Invoke-IcingaWindowsScheduledTask -JobType 'GetWindowsService' -ObjectName $Service;
35+
36+
if ($ServiceData.Service.Installed -eq $FALSE) {
37+
Write-IcingaConsoleError $ServiceData.ErrMsg;
38+
return @{
39+
'Status' = '';
40+
'Present' = $FALSE;
41+
'Name' = 'Unknown';
42+
'DisplayName' = 'Unknown';
43+
};
44+
}
45+
46+
if ($Service -eq 'icinga2') {
47+
$Global:Icinga.Protected.IcingaServiceState = $ServiceData.Service.Status;
48+
} elseif ($Service -eq 'icingapowershell') {
49+
$Global:Icinga.Protected.IfWServiceState = $ServiceData.Service.Status;
50+
}
51+
52+
return $ServiceData.Service;
53+
}

lib/core/framework/Install-IcingaForWindowsService.psm1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ function Install-IcingaForWindowsService()
3939

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

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

4444
if ((Test-Path $UpdateFile)) {
4545

4646
Write-IcingaConsoleNotice 'Updating Icinga PowerShell Service binary';
4747

48-
if ($ServiceStatus -eq 'Running') {
48+
if ($ServiceStatus.Status -eq 'Running') {
4949
Write-IcingaConsoleNotice 'Stopping Icinga PowerShell service';
5050
Stop-IcingaWindowsService;
5151
Start-Sleep -Seconds 1;
@@ -68,7 +68,7 @@ function Install-IcingaForWindowsService()
6868
(Get-IcingaPowerShellModuleFile)
6969
);
7070

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

7474
if ($ServiceCreation.ExitCode -ne 0) {

lib/core/framework/Install-IcingaFrameworkComponent.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ function Install-IcingaFrameworkComponent()
112112

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

118118
# Unload the module if it was loaded before

lib/core/framework/Install-IcingaFrameworkUpdate.psm1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ function Install-IcingaFrameworkUpdate()
5252

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

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

5858
if ($ServiceStatus -eq 'Running') {
5959
Write-IcingaConsoleNotice 'Stopping Icinga PowerShell service';
@@ -109,7 +109,7 @@ function Install-IcingaFrameworkUpdate()
109109
if ([string]::IsNullOrEmpty((Get-IcingaJEAContext)) -eq $FALSE) {
110110
Remove-IcingaFrameworkDependencyFile;
111111
Write-IcingaConsoleNotice 'Updating Icinga JEA profile';
112-
& powershell.exe -Command { Use-Icinga -Minimal; Install-IcingaJEAProfile; } | Out-Null;
112+
Invoke-IcingaWindowsScheduledTask -JobType InstallJEA -Timeout 600 | Out-Null;
113113
}
114114

115115
Write-IcingaConsoleNotice 'Framework update has been completed. Please start a new PowerShell instance now to complete the update';

lib/core/framework/Invoke-IcingaForWindowsMigration.psm1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function Invoke-IcingaForWindowsMigration()
1313

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

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

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

4848
if ($ServiceStatus -eq 'Running') {
4949
Stop-IcingaWindowsService;

lib/core/framework/Invoke-IcingaInternalServiceCall.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function Invoke-IcingaInternalServiceCall()
1717
}
1818

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

2222
if ($null -eq $IcingaForWindowsService -Or $IcingaForWindowsService.Status -ne 'Running') {
2323
return $NULL;

lib/core/framework/New-IcingaEnvironmentVariable.psm1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ function New-IcingaEnvironmentVariable()
6868
$Global:Icinga.Protected.Add('RunAsDaemon', $FALSE);
6969
$Global:Icinga.Protected.Add('Minimal', $FALSE);
7070
$Global:Icinga.Protected.Add('ThreadName', '');
71+
$Global:Icinga.Protected.Add('IcingaServiceUser', '');
72+
$Global:Icinga.Protected.Add('IfWServiceUser', '');
73+
$Global:Icinga.Protected.Add('ServiceRestartLock', $FALSE);
74+
$Global:Icinga.Protected.Add('IcingaServiceState', '');
75+
$Global:Icinga.Protected.Add('IfWServiceState', '');
7176
$Global:Icinga.Protected.Add('GarbageCollector', @{ });
7277
}
7378
}

lib/core/framework/Restart-IcingaService.psm1

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,25 @@
2121
function Restart-IcingaService()
2222
{
2323
param (
24-
$Service
24+
$Service,
25+
[switch]$Force = $FALSE
2526
);
2627

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

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

33-
$Service = $args[0];
34-
try {
35-
Restart-Service "$Service" -ErrorAction Stop;
36-
Start-Sleep -Seconds 2;
37-
Optimize-IcingaForWindowsMemory;
38-
} catch {
39-
Write-IcingaConsoleError -Message 'Failed to restart service "{0}". Error: {1}' -Objects $Service, $_.Exception.Message;
40-
}
41-
} -Args $Service;
34+
if ($Result.Success -eq $FALSE) {
35+
Write-IcingaConsoleError $Result.ErrMsg;
4236
} else {
43-
Write-IcingaConsoleWarning -Message 'The service "{0}" is not installed' -Objects $Service;
37+
Write-IcingaConsoleNotice $Result.Message;
4438
}
4539

46-
Optimize-IcingaForWindowsMemory;
40+
if ($Service -eq 'icinga2') {
41+
$Global:Icinga.Protected.IcingaServiceState = $Result.Status;
42+
} elseif ($Service -eq 'icingapowershell') {
43+
$Global:Icinga.Protected.IfWServiceState = $Result.Status;
44+
}
4745
}

lib/core/framework/Start-IcingaService.psm1

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,26 @@
2020

2121
function Start-IcingaService()
2222
{
23-
param(
24-
$Service
23+
param (
24+
$Service,
25+
[switch]$Force = $FALSE
2526
);
2627

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

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

33-
$Service = $args[0];
34-
try {
35-
Start-Service "$Service" -ErrorAction Stop;
36-
Start-Sleep -Seconds 2;
37-
Optimize-IcingaForWindowsMemory;
38-
} catch {
39-
Write-IcingaConsoleError -Message 'Failed to start service "{0}". Error: {1}' -Objects $Service, $_.Exception.Message;
40-
}
41-
} -Args $Service;
34+
if ($Result.Success -eq $FALSE) {
35+
Write-IcingaConsoleError $Result.ErrMsg;
4236
} else {
43-
Write-IcingaConsoleWarning -Message 'The service "{0}" is not installed' -Objects $Service;
37+
Write-IcingaConsoleNotice $Result.Message;
4438
}
4539

46-
Optimize-IcingaForWindowsMemory;
40+
if ($Service -eq 'icinga2') {
41+
$Global:Icinga.Protected.IcingaServiceState = $Result.Status;
42+
} elseif ($Service -eq 'icingapowershell') {
43+
$Global:Icinga.Protected.IfWServiceState = $Result.Status;
44+
}
4745
}

0 commit comments

Comments
 (0)