Skip to content

Commit d62f75d

Browse files
committed
Adds feature to offload service and JEA actions to scheduled tasks
1 parent f028ade commit d62f75d

16 files changed

+260
-118
lines changed

jobs/RestartWindowsService.ps1

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
param (
2+
[string]$ServiceName = '',
3+
[string]$TmpFilePath = ''
4+
);
5+
6+
Use-Icinga -Minimal;
7+
8+
[bool]$Success = $TRUE;
9+
[string]$ErrMsg = "";
10+
11+
try {
12+
Restart-Service "$ServiceName" -ErrorAction Stop;
13+
} catch {
14+
$Success = $FALSE;
15+
$ErrMsg = [string]::Format('Failed to restart service "{0}": {1}', $ServiceName, $_.Exception.Message);
16+
}
17+
18+
Write-IcingaFileSecure -File "$TmpFilePath" -Value (
19+
@{
20+
'Success' = $Success;
21+
'Message' = [string]::Format('Service "{0}" successfully restarted', $ServiceName);
22+
'ErrMsg' = $ErrMsg;
23+
} | ConvertTo-Json -Depth 100
24+
);

jobs/StartWindowsService.ps1

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
param (
2+
[string]$ServiceName = '',
3+
[string]$TmpFilePath = ''
4+
);
5+
6+
Use-Icinga -Minimal;
7+
8+
[bool]$Success = $TRUE;
9+
[string]$ErrMsg = "";
10+
11+
try {
12+
Start-Service "$ServiceName" -ErrorAction Stop;
13+
} catch {
14+
$Success = $FALSE;
15+
$ErrMsg = [string]::Format('Failed to start service "{0}": {1}', $ServiceName, $_.Exception.Message);
16+
}
17+
18+
Write-IcingaFileSecure -File "$TmpFilePath" -Value (
19+
@{
20+
'Success' = $Success;
21+
'Message' = [string]::Format('Service "{0}" successfully started', $ServiceName);
22+
'ErrMsg' = $ErrMsg;
23+
} | ConvertTo-Json -Depth 100
24+
);

jobs/StopWindowsService.ps1

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
param (
2+
[string]$ServiceName = '',
3+
[string]$TmpFilePath = ''
4+
);
5+
6+
Use-Icinga -Minimal;
7+
8+
[bool]$Success = $TRUE;
9+
[string]$ErrMsg = "";
10+
11+
try {
12+
Stop-Service "$ServiceName" -ErrorAction Stop;
13+
} catch {
14+
$Success = $FALSE;
15+
$ErrMsg = [string]::Format('Failed to stop service "{0}": {1}', $ServiceName, $_.Exception.Message);
16+
}
17+
18+
Write-IcingaFileSecure -File "$TmpFilePath" -Value (
19+
@{
20+
'Success' = $Success;
21+
'Message' = [string]::Format('Service "{0}" successfully stopped', $ServiceName);
22+
'ErrMsg' = $ErrMsg;
23+
} | ConvertTo-Json -Depth 100
24+
);

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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/Restart-IcingaService.psm1

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,11 @@ function Restart-IcingaService()
2424
$Service
2525
);
2626

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

30-
& powershell.exe -Command {
31-
Use-Icinga -Minimal;
32-
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;
29+
if ($Result.Success -eq $FALSE) {
30+
Write-IcingaConsoleError $Result.ErrMsg
4231
} else {
43-
Write-IcingaConsoleWarning -Message 'The service "{0}" is not installed' -Objects $Service;
32+
Write-IcingaConsoleNotice $Result.Message
4433
}
45-
46-
Optimize-IcingaForWindowsMemory;
4734
}

lib/core/framework/Start-IcingaService.psm1

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,11 @@ function Start-IcingaService()
2424
$Service
2525
);
2626

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

30-
& powershell.exe -Command {
31-
Use-Icinga -Minimal;
32-
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;
29+
if ($Result.Success -eq $FALSE) {
30+
Write-IcingaConsoleError $Result.ErrMsg
4231
} else {
43-
Write-IcingaConsoleWarning -Message 'The service "{0}" is not installed' -Objects $Service;
32+
Write-IcingaConsoleNotice $Result.Message
4433
}
45-
46-
Optimize-IcingaForWindowsMemory;
4734
}

lib/core/framework/Stop-IcingaService.psm1

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,11 @@ function Stop-IcingaService()
2424
$Service
2525
);
2626

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

30-
& powershell.exe -Command {
31-
Use-Icinga -Minimal;
32-
33-
$Service = $args[0];
34-
try {
35-
Stop-Service "$Service" -ErrorAction Stop;
36-
Start-Sleep -Seconds 2;
37-
Optimize-IcingaForWindowsMemory;
38-
} catch {
39-
Write-IcingaConsoleError -Message 'Failed to stop service "{0}". Error: {1}' -Objects $Service, $_.Exception.Message;
40-
}
41-
} -Args $Service;
29+
if ($Result.Success -eq $FALSE) {
30+
Write-IcingaConsoleError $Result.ErrMsg
4231
} else {
43-
Write-IcingaConsoleWarning -Message 'The service "{0}" is not installed' -Objects $Service;
32+
Write-IcingaConsoleNotice $Result.Message
4433
}
45-
46-
Optimize-IcingaForWindowsMemory;
4734
}

lib/core/icingaagent/installer/Install-IcingaAgent.psm1

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,7 @@ function Install-IcingaAgent()
7373
}
7474
}
7575

76-
$InstallProcess = & powershell.exe -Command {
77-
Use-Icinga -Minimal;
78-
79-
$IcingaInstaller = $args[0];
80-
$InstallTarget = $args[1];
81-
$InstallProcess = Start-IcingaProcess -Executable 'MsiExec.exe' -Arguments ([string]::Format('/quiet /i "{0}" {1}', $IcingaInstaller.InstallerPath, $InstallTarget)) -FlushNewLines;
82-
83-
Start-Sleep -Seconds 2;
84-
Optimize-IcingaForWindowsMemory;
85-
86-
return $InstallProcess;
87-
} -Args $IcingaInstaller, $InstallTarget;
76+
$InstallProcess = Start-IcingaProcess -Executable 'MsiExec.exe' -Arguments ([string]::Format('/quiet /i "{0}" {1}', $IcingaInstaller.InstallerPath, $InstallTarget)) -FlushNewLines;
8877

8978
if ($InstallProcess.ExitCode -ne 0) {
9079
Write-IcingaConsoleError -Message 'Failed to install Icinga 2 Agent: {0}{1}' -Objects $InstallProcess.Message, $InstallProcess.Error;

lib/core/icingaagent/installer/Uninstall-IcingaAgent.psm1

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,7 @@ function Uninstall-IcingaAgent()
2222

2323
Stop-IcingaService -Service 'icinga2';
2424

25-
$Uninstaller = & powershell.exe -Command {
26-
Use-Icinga -Minimal;
27-
28-
$IcingaData = $args[0];
29-
$Uninstaller = Start-IcingaProcess -Executable 'MsiExec.exe' -Arguments ([string]::Format('{0} /q', $IcingaData.Uninstaller)) -FlushNewLine;
30-
31-
Start-Sleep -Seconds 2;
32-
Optimize-IcingaForWindowsMemory;
33-
34-
return $Uninstaller;
35-
} -Args $IcingaData;
25+
$Uninstaller = Invoke-IcingaWindowsScheduledTask -JobType UninstallAgent -FilePath $IcingaData.Uninstaller;
3626

3727
if ($Uninstaller.ExitCode -ne 0) {
3828
Write-IcingaConsoleError ([string]::Format('Failed to remove Icinga Agent: {0}{1}', $Uninstaller.Message, $Uninstaller.Error));

0 commit comments

Comments
 (0)