From 591555f2c1c0ae12bdae176e7fc4c371541f1fbb Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Sat, 30 May 2020 14:42:31 +0200 Subject: [PATCH] Improves Agent installation/uninstallation stability Fixes #69 --- CHANGELOG.md | 1 + lib/core/framework/Restart-IcingaService.psm1 | 9 +++++-- lib/core/framework/Start-IcingaService.psm1 | 6 ++++- lib/core/framework/Stop-IcingaService.psm1 | 6 ++++- .../installer/Install-IcingaAgent.psm1 | 12 +++++++-- .../installer/Uninstall-IcingaAgent.psm1 | 27 ++++++++++++++----- 6 files changed, 48 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 021f242..62ceb2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic * [#67](https://github.com/Icinga/icinga-powershell-framework/issues/67) Adds support to flush entire Icinga 2 ProgamData directory on uninstallation * [#68](https://github.com/Icinga/icinga-powershell-framework/issues/68) Improves the setup wizard by providing better understandable prompts including examples and various smaller bugfixes * Console prints are now containing a severity message to better keep an eye on possible warnings/errors +* [#69](https://github.com/Icinga/icinga-powershell-framework/issues/69) Improves stability of installation/uninstallation of the Agent by using different PowerShell instances for service and installation/uninstallation handling ### Bugfixes diff --git a/lib/core/framework/Restart-IcingaService.psm1 b/lib/core/framework/Restart-IcingaService.psm1 index 1e18e4e..9e9dd73 100644 --- a/lib/core/framework/Restart-IcingaService.psm1 +++ b/lib/core/framework/Restart-IcingaService.psm1 @@ -24,9 +24,14 @@ function Restart-IcingaService() $Service ); - if (Get-Service $Service -ErrorAction SilentlyContinue) { + if (Get-Service "$Service" -ErrorAction SilentlyContinue) { Write-IcingaConsoleNotice ([string]::Format('Restarting service "{0}"', $Service)); - Restart-Service $Service; + powershell.exe -Command { + $Service = $args[0] + + Restart-Service "$Service"; + } -Args $Service; + } else { Write-IcingaConsoleWarning -Message 'The service "{0}" is not installed' -Objects $Service; } diff --git a/lib/core/framework/Start-IcingaService.psm1 b/lib/core/framework/Start-IcingaService.psm1 index 63487cb..b8a4980 100644 --- a/lib/core/framework/Start-IcingaService.psm1 +++ b/lib/core/framework/Start-IcingaService.psm1 @@ -26,7 +26,11 @@ function Start-IcingaService() if (Get-Service $Service -ErrorAction SilentlyContinue) { Write-IcingaConsoleNotice -Message 'Starting service "{0}"' -Objects $Service; - Start-Service $Service; + powershell.exe -Command { + $Service = $args[0] + + Start-Service "$Service"; + } -Args $Service; } else { Write-IcingaConsoleWarning -Message 'The service "{0}" is not installed' -Objects $Service; } diff --git a/lib/core/framework/Stop-IcingaService.psm1 b/lib/core/framework/Stop-IcingaService.psm1 index 8daaf52..4ede669 100644 --- a/lib/core/framework/Stop-IcingaService.psm1 +++ b/lib/core/framework/Stop-IcingaService.psm1 @@ -26,7 +26,11 @@ function Stop-IcingaService() if (Get-Service $Service -ErrorAction SilentlyContinue) { Write-IcingaConsoleNotice -Message 'Stopping service "{0}"' -Objects $Service; - Stop-Service $Service; + powershell.exe -Command { + $Service = $args[0] + + Stop-Service "$Service"; + } -Args $Service; } else { Write-IcingaConsoleWarning -Message 'The service "{0}" is not installed' -Objects $Service; } diff --git a/lib/core/icingaagent/installer/Install-IcingaAgent.psm1 b/lib/core/icingaagent/installer/Install-IcingaAgent.psm1 index 613faa6..97ccf06 100644 --- a/lib/core/icingaagent/installer/Install-IcingaAgent.psm1 +++ b/lib/core/icingaagent/installer/Install-IcingaAgent.psm1 @@ -71,13 +71,21 @@ function Install-IcingaAgent() } } - $InstallProcess = Start-IcingaProcess -Executable 'MsiExec.exe' -Arguments ([string]::Format('/quiet /i "{0}" {1}', $IcingaInstaller.InstallerPath, $InstallTarget)) -FlushNewLines; + $InstallProcess = powershell.exe -Command { + $IcingaInstaller = $args[0]; + $InstallTarget = $args[1]; + Use-Icinga; + + $InstallProcess = Start-IcingaProcess -Executable 'MsiExec.exe' -Arguments ([string]::Format('/quiet /i "{0}" {1}', $IcingaInstaller.InstallerPath, $InstallTarget)) -FlushNewLines; + + return $InstallProcess; + } -Args $IcingaInstaller, $InstallTarget; if ($InstallProcess.ExitCode -ne 0) { Write-IcingaConsoleError -Message 'Failed to install Icinga 2 Agent: {0}{1}' -Objects $InstallProcess.Message, $InstallProcess.Error; return $FALSE; } - + Write-IcingaConsoleNotice 'Icinga Agent was successfully installed'; return $TRUE; } diff --git a/lib/core/icingaagent/installer/Uninstall-IcingaAgent.psm1 b/lib/core/icingaagent/installer/Uninstall-IcingaAgent.psm1 index ef1d852..ba9b15f 100644 --- a/lib/core/icingaagent/installer/Uninstall-IcingaAgent.psm1 +++ b/lib/core/icingaagent/installer/Uninstall-IcingaAgent.psm1 @@ -4,18 +4,32 @@ function Uninstall-IcingaAgent() [switch]$RemoveDataFolder = $FALSE ); - $IcingaData = Get-IcingaAgentInstallation; + $IcingaData = Get-IcingaAgentInstallation; + [string]$IcingaProgramData = Join-Path -Path $Env:ProgramData -ChildPath 'icinga2'; if ($IcingaData.Installed -eq $FALSE) { - Write-IcingaConsoleError 'Unable to uninstall the Icinga Agent. The Agent is not installed'; - return; + Write-IcingaConsoleNotice 'Unable to uninstall the Icinga Agent. The Agent is not installed'; + if ($RemoveDataFolder) { + if (Test-Path $IcingaProgramData) { + Write-IcingaConsoleNotice -Message 'Removing Icinga Agent directory: "{0}"' -Objects $IcingaProgramData; + return ((Remove-ItemSecure -Path $IcingaProgramData -Recurse -Force) -eq $FALSE); + } else { + Write-IcingaConsoleNotice -Message 'Icinga Agent directory "{0}" does not exist' -Objects $IcingaProgramData; + } + } + return $FALSE; } - Write-IcingaConsoleNotice 'Removing current Icinga Agent'; + $Uninstaller = powershell.exe -Command { + $IcingaData = $args[0] + Use-Icinga; - Stop-IcingaService 'icinga2'; + Stop-Service 'icinga2' -ErrorAction SilentlyContinue | Out-Null; - $Uninstaller = Start-IcingaProcess -Executable 'MsiExec.exe' -Arguments ([string]::Format('{0} /q', $IcingaData.Uninstaller)) -FlushNewLine; + $Uninstaller = Start-IcingaProcess -Executable 'MsiExec.exe' -Arguments ([string]::Format('{0} /q', $IcingaData.Uninstaller)) -FlushNewLine; + + return $Uninstaller; + } -Args $IcingaData; if ($Uninstaller.ExitCode -ne 0) { Write-IcingaConsoleError ([string]::Format('Failed to remove Icinga Agent: {0}{1}', $Uninstaller.Message, $Uninstaller.Error)); @@ -23,7 +37,6 @@ function Uninstall-IcingaAgent() } if ($RemoveDataFolder) { - [string]$IcingaProgramData = Join-Path -Path $Env:ProgramData -ChildPath 'icinga2'; Write-IcingaConsoleNotice -Message 'Removing Icinga Agent directory: "{0}"' -Objects $IcingaProgramData; if ((Remove-ItemSecure -Path $IcingaProgramData -Recurse -Force) -eq $FALSE) { return $FALSE;