From 1638465b06666ee25ee5dd8c04e6083e66cad657 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Wed, 24 Feb 2021 13:27:11 +0100 Subject: [PATCH] Adds feature to uninstall Icinga for Windows --- doc/31-Changelog.md | 1 + .../framework/Uninstall-IcingaForWindows.psm1 | 72 +++++++++++++++++++ .../Uninstall-IcingaFrameworkComponent.psm1 | 48 +++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 lib/core/framework/Uninstall-IcingaForWindows.psm1 create mode 100644 lib/core/framework/Uninstall-IcingaFrameworkComponent.psm1 diff --git a/doc/31-Changelog.md b/doc/31-Changelog.md index eccf787..c140090 100644 --- a/doc/31-Changelog.md +++ b/doc/31-Changelog.md @@ -20,6 +20,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic * [#205](https://github.com/Icinga/icinga-powershell-framework/pull/205) Ensure Icinga for Windows configuration file is opened as read-only for every single task besides actually modifying configuration content * [#207](https://github.com/Icinga/icinga-powershell-framework/pull/207) Adds new Argument `-LabelName` to `New-IcingaCheck`, allowing the developer to provide custom label names for checks and override the default based on the check name. * [#210](https://github.com/Icinga/icinga-powershell-framework/pull/210) Updates the Icinga DSL for building PowerShell arrays to ensure all string values are properly escaped with `'`. In case the user already wrapped commands with `'` by himself, this will not have an effect as we only add single quotes for escaping if they are not present already +* [#211](https://github.com/Icinga/icinga-powershell-framework/pull/211) Adds feature to uninstall single components for Icinga for Windows or to uninstall everything and start entirely from new ### Bugfixes diff --git a/lib/core/framework/Uninstall-IcingaForWindows.psm1 b/lib/core/framework/Uninstall-IcingaForWindows.psm1 new file mode 100644 index 0000000..db9da30 --- /dev/null +++ b/lib/core/framework/Uninstall-IcingaForWindows.psm1 @@ -0,0 +1,72 @@ +<# +.SYNOPSIS + Uninstalls every PowerShell module within the icinga-powershell-* namespace + including the Icinga Agent with all components (like certificates) as well as + the Icinga for Windows service and the Icinga PowerShell Framework. +.DESCRIPTION + Uninstalls every PowerShell module within the icinga-powershell-* namespace + including the Icinga Agent with all components (like certificates) as well as + the Icinga for Windows service and the Icinga PowerShell Framework. +.FUNCTIONALITY + Uninstalls every PowerShell module within the icinga-powershell-* namespace + including the Icinga Agent with all components (like certificates) as well as + the Icinga for Windows service and the Icinga PowerShell Framework. +.PARAMETER Force + Suppress the question if you are sure to uninstall everything +.INPUTS + System.String +.OUTPUTS + Null +.LINK + https://github.com/Icinga/icinga-powershell-framework +#> + +function Uninstall-IcingaForWindows() +{ + param ( + [switch]$Force = $FALSE + ); + + $ModuleList = Get-Module 'icinga-powershell-*' -ListAvailable; + [string]$Modules = [string]::Join(', ', $ModuleList.Name); + + if ($Force -eq $FALSE) { + Write-IcingaConsoleWarning -Message 'You are about to uninstall the Icinga Agent with all components (including certificates) and all Icinga for Windows Components: {0}{1}Are you sure you want to proceed? (y/N)' -Objects $Modules, (New-IcingaNewLine); + $Input = Read-Host 'Confirm uninstall'; + if ($input -ne 'y') { + return; + } + } + + $CurrentLocation = Get-Location; + + if ($CurrentLocation -eq (Join-Path -Path (Get-IcingaFrameworkRootPath) -ChildPath 'icinga-powershell-framework')) { + Set-Location -Path (Get-IcingaFrameworkRootPath); + } + + Write-IcingaConsoleNotice 'Uninstalling Icinga for Windows from this host'; + Write-IcingaConsoleNotice 'Uninstalling Icinga Agent'; + Uninstall-IcingaAgent -RemoveDataFolder | Out-Null; + Write-IcingaConsoleNotice 'Uninstalling Icinga for Windows service'; + Uninstall-IcingaFrameworkService | Out-Null; + + $HasErrors = $FALSE; + + foreach ($module in $ModuleList.Name) { + [string]$ModuleName = $module.Replace('icinga-powershell-', ''); + + if ((Uninstall-IcingaFrameworkComponent -Component $ModuleName)) { + continue; + } + + $HasErrors = $TRUE; + } + + Remove-Module 'icinga-powershell-framework' -Force -ErrorAction SilentlyContinue; + + if ($HasErrors) { + Write-IcingaConsoleWarning 'Not all components could be removed. Please ensure no other PowerShell/Application is currently open and accessing Icinga for Windows files'; + } else { + Write-IcingaConsoleNotice 'Icinga for Windows was removed from this host.'; + } +} diff --git a/lib/core/framework/Uninstall-IcingaFrameworkComponent.psm1 b/lib/core/framework/Uninstall-IcingaFrameworkComponent.psm1 new file mode 100644 index 0000000..5ea436e --- /dev/null +++ b/lib/core/framework/Uninstall-IcingaFrameworkComponent.psm1 @@ -0,0 +1,48 @@ +<# +.SYNOPSIS + Uninstalls a specific module within the icinga-powershell-* namespace + inside your PowerShell module folder +.DESCRIPTION + Uninstalls a specific module within the icinga-powershell-* namespace + inside your PowerShell module folder +.FUNCTIONALITY + Uninstalls a specific module within the icinga-powershell-* namespace + inside your PowerShell module folder +.PARAMETER Component + The component you want to uninstall, like 'plugins' or 'mssql' +.INPUTS + System.String +.OUTPUTS + Null +.LINK + https://github.com/Icinga/icinga-powershell-framework +#> + +function Uninstall-IcingaFrameworkComponent() +{ + param ( + [string]$Component + ); + + $ModuleBase = Get-IcingaFrameworkRootPath; + $UninstallComponent = [string]::Format('icinga-powershell-{0}', $Component); + $UninstallPath = Join-Path -Path $ModuleBase -ChildPath $UninstallComponent; + + if ((Test-Path $UninstallPath) -eq $FALSE) { + Write-IcingaConsoleNotice -Message 'The Icinga for Windows component "{0}" at "{1}" could not ne found.' -Objects $UninstallComponent, $UninstallPath; + return $FALSE; + } + + Write-IcingaConsoleNotice -Message 'Uninstalling Icinga for Windows component "{0}" from "{1}"' -Objects $UninstallComponent, $UninstallPath; + if (Remove-ItemSecure -Path $UninstallPath -Recurse -Force) { + Write-IcingaConsoleNotice -Message 'Successfully removed Icinga for Windows component "{0}" from "{1}"' -Objects $UninstallComponent, $UninstallPath; + if ($UninstallComponent -ne 'icinga-powershell-framework') { + Remove-Module $UninstallComponent -Force -ErrorAction SilentlyContinue; + } + return $TRUE; + } else { + Write-IcingaConsoleError -Message 'Unable to uninstall Icinga for Windows component "{0}" from "{1}"' -Objects $UninstallComponent, $UninstallPath; + } + + return $FALSE; +}