Skip to content

Commit 7a3a2d6

Browse files
committed
WIP
1 parent cdd04c5 commit 7a3a2d6

16 files changed

+129
-38
lines changed

jobs/RestartWindowsService.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ Use-Icinga -Minimal;
77

88
[bool]$Success = $TRUE;
99
[string]$ErrMsg = "";
10+
[string]$Status = '';
1011

1112
try {
1213
Restart-Service "$ServiceName" -ErrorAction Stop;
14+
$Status = [string](Get-Service "$ServiceName").Status;
1315
} catch {
1416
$Success = $FALSE;
1517
$ErrMsg = [string]::Format('Failed to restart service "{0}": {1}', $ServiceName, $_.Exception.Message);
@@ -20,5 +22,6 @@ Write-IcingaFileSecure -File "$TmpFilePath" -Value (
2022
'Success' = $Success;
2123
'Message' = [string]::Format('Service "{0}" successfully restarted', $ServiceName);
2224
'ErrMsg' = $ErrMsg;
25+
'Status' = $Status;
2326
} | ConvertTo-Json -Depth 100
2427
);

jobs/StartWindowsService.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ Use-Icinga -Minimal;
77

88
[bool]$Success = $TRUE;
99
[string]$ErrMsg = "";
10+
[string]$Status = '';
1011

1112
try {
1213
Start-Service "$ServiceName" -ErrorAction Stop;
14+
$Status = [string](Get-Service "$ServiceName").Status;
1315
} catch {
1416
$Success = $FALSE;
1517
$ErrMsg = [string]::Format('Failed to start service "{0}": {1}', $ServiceName, $_.Exception.Message);
@@ -20,5 +22,6 @@ Write-IcingaFileSecure -File "$TmpFilePath" -Value (
2022
'Success' = $Success;
2123
'Message' = [string]::Format('Service "{0}" successfully started', $ServiceName);
2224
'ErrMsg' = $ErrMsg;
25+
'Status' = $Status;
2326
} | ConvertTo-Json -Depth 100
2427
);

jobs/StopWindowsService.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ Use-Icinga -Minimal;
77

88
[bool]$Success = $TRUE;
99
[string]$ErrMsg = "";
10+
[string]$Status = '';
1011

1112
try {
1213
Stop-Service "$ServiceName" -ErrorAction Stop;
14+
$Status = [string](Get-Service "$ServiceName").Status;
1315
} catch {
1416
$Success = $FALSE;
1517
$ErrMsg = [string]::Format('Failed to stop service "{0}": {1}', $ServiceName, $_.Exception.Message);
@@ -20,5 +22,6 @@ Write-IcingaFileSecure -File "$TmpFilePath" -Value (
2022
'Success' = $Success;
2123
'Message' = [string]::Format('Service "{0}" successfully stopped', $ServiceName);
2224
'ErrMsg' = $ErrMsg;
25+
'Status' = $Status;
2326
} | ConvertTo-Json -Depth 100
2427
);
Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,40 @@
11
function Get-IcingaWindowsServiceStatus()
22
{
33
param (
4-
[string]$Service = ''
4+
[string]$Service = '',
5+
[switch]$Force = $FALSE
56
);
67

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+
734
$ServiceData = Invoke-IcingaWindowsScheduledTask -JobType 'GetWindowsService' -ObjectName $Service;
835

936
if ($ServiceData.Service.Installed -eq $FALSE) {
10-
Write-IcingaConsoleError .\certificate$ServiceData.ErrMsg;
37+
Write-IcingaConsoleError $ServiceData.ErrMsg;
1138
return @{
1239
'Status' = 'Unknown';
1340
'Present' = $FALSE;
@@ -16,5 +43,11 @@ function Get-IcingaWindowsServiceStatus()
1643
};
1744
}
1845

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+
1952
return $ServiceData.Service;
2053
}

lib/core/framework/New-IcingaEnvironmentVariable.psm1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ function New-IcingaEnvironmentVariable()
7070
$Global:Icinga.Protected.Add('ThreadName', '');
7171
$Global:Icinga.Protected.Add('IcingaServiceUser', '');
7272
$Global:Icinga.Protected.Add('IfWServiceUser', '');
73+
$Global:Icinga.Protected.Add('ServiceRestartLock', $FALSE);
74+
$Global:Icinga.Protected.Add('IcingaServiceState', '');
75+
$Global:Icinga.Protected.Add('IfWServiceState', '');
7376
$Global:Icinga.Protected.Add('GarbageCollector', @{ });
7477
}
7578
}

lib/core/framework/Restart-IcingaService.psm1

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

28+
if ($Global:Icinga.Protected.ServiceRestartLock -And $Force -eq $FALSE) {
29+
return;
30+
}
31+
2732
$Result = Invoke-IcingaWindowsScheduledTask -JobType 'RestartWindowsService' -ObjectName $Service;
2833

2934
if ($Result.Success -eq $FALSE) {
30-
Write-IcingaConsoleError $Result.ErrMsg
35+
Write-IcingaConsoleError $Result.ErrMsg;
3136
} else {
32-
Write-IcingaConsoleNotice $Result.Message
37+
Write-IcingaConsoleNotice $Result.Message;
38+
}
39+
40+
if ($Service -eq 'icinga2') {
41+
$Global:Icinga.Protected.IcingaServiceState = $Result.Status;
42+
} elseif ($Service -eq 'icingapowershell') {
43+
$Global:Icinga.Protected.IfWServiceState = $Result.Status;
3344
}
3445
}

lib/core/framework/Start-IcingaService.psm1

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

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

28+
if ($Global:Icinga.Protected.ServiceRestartLock -And $Force -eq $FALSE) {
29+
return;
30+
}
31+
2732
$Result = Invoke-IcingaWindowsScheduledTask -JobType 'StartWindowsService' -ObjectName $Service;
2833

2934
if ($Result.Success -eq $FALSE) {
30-
Write-IcingaConsoleError $Result.ErrMsg
35+
Write-IcingaConsoleError $Result.ErrMsg;
3136
} else {
32-
Write-IcingaConsoleNotice $Result.Message
37+
Write-IcingaConsoleNotice $Result.Message;
38+
}
39+
40+
if ($Service -eq 'icinga2') {
41+
$Global:Icinga.Protected.IcingaServiceState = $Result.Status;
42+
} elseif ($Service -eq 'icingapowershell') {
43+
$Global:Icinga.Protected.IfWServiceState = $Result.Status;
3344
}
3445
}

lib/core/framework/Stop-IcingaService.psm1

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

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

28+
if ($Global:Icinga.Protected.ServiceRestartLock -And $Force -eq $FALSE) {
29+
return;
30+
}
31+
2732
$Result = Invoke-IcingaWindowsScheduledTask -JobType 'StopWindowsService' -ObjectName $Service;
2833

2934
if ($Result.Success -eq $FALSE) {
30-
Write-IcingaConsoleError $Result.ErrMsg
35+
Write-IcingaConsoleError $Result.ErrMsg;
3136
} else {
32-
Write-IcingaConsoleNotice $Result.Message
37+
Write-IcingaConsoleNotice $Result.Message;
38+
}
39+
40+
if ($Service -eq 'icinga2') {
41+
$Global:Icinga.Protected.IcingaServiceState = $Result.Status;
42+
} elseif ($Service -eq 'icingapowershell') {
43+
$Global:Icinga.Protected.IfWServiceState = $Result.Status;
3344
}
3445
}

lib/core/icingaagent/misc/Start-IcingaAgentInstallWizard.psm1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,9 +752,9 @@ function Start-IcingaAgentInstallWizard()
752752
}
753753
Test-IcingaAgent;
754754
if ($InstallFrameworkService) {
755-
Restart-IcingaWindowsService;
755+
Restart-IcingaWindowsService -Force;
756756
}
757-
Restart-IcingaService 'icinga2';
757+
Restart-IcingaService 'icinga2' -Force;
758758
}
759759
}
760760
}

lib/core/installer/Install-Icinga.psm1

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ function Install-Icinga()
1313
# Always ensure we use the proper TLS Version
1414
Set-IcingaTLSVersion;
1515

16+
$Global:Icinga.Protected.ServiceRestartLock = $TRUE;
17+
1618
# Ignore SSL validation in case we set the flag
1719
if ($NoSSLValidation) {
1820
Enable-IcingaUntrustedCertificateValidation;
@@ -68,6 +70,7 @@ function Install-Icinga()
6870
$JsonInstallCmd = ConvertFrom-Json -InputObject $InstallCommand -ErrorAction Stop;
6971
} catch {
7072
Write-IcingaConsoleError 'Failed to deserialize the provided JSON from file or command: {0}' -Objects $_.Exception.Message;
73+
$Global:Icinga.Protected.ServiceRestartLock = $FALSE;
7174
return;
7275
}
7376

@@ -81,6 +84,7 @@ function Install-Icinga()
8184
$Success = Invoke-IcingaForWindowsManagementConsoleCustomConfig -IcingaConfiguration $IcingaConfiguration;
8285

8386
if ($Success -eq $FALSE) {
87+
$Global:Icinga.Protected.ServiceRestartLock = $FALSE;
8488
return;
8589
}
8690

@@ -101,6 +105,7 @@ function Install-Icinga()
101105
$Success = Invoke-IcingaForWindowsManagementConsoleCustomConfig -IcingaConfiguration $IcingaConfiguration;
102106

103107
if ($Success -eq $FALSE) {
108+
$Global:Icinga.Protected.ServiceRestartLock = $FALSE;
104109
return;
105110
}
106111

@@ -111,7 +116,7 @@ function Install-Icinga()
111116
# Set our "old" swap live again. By doing so, we can still continue our old
112117
# configuration
113118
Set-IcingaPowerShellConfig -Path 'Framework.Config.Swap' -Value $OldConfigSwap;
114-
119+
$Global:Icinga.Protected.ServiceRestartLock = $FALSE;
115120
return;
116121
}
117122

@@ -186,6 +191,8 @@ function Install-Icinga()
186191
}
187192
}
188193

194+
$Global:Icinga.Protected.ServiceRestartLock = $FALSE;
195+
189196
if ($null -ne (Get-Command -Name 'Set-IcingaForWindowsManagementConsoleClosing' -ErrorAction SilentlyContinue)) {
190197
Set-IcingaForWindowsManagementConsoleClosing -Completed;
191198
}

0 commit comments

Comments
 (0)