From 40aecbd6a93b5c9f9f983d61372f2402545d44d4 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Fri, 6 Aug 2021 15:44:31 +0200 Subject: [PATCH] Enforces Framework code caching --- .gitignore | 1 + cache/framework_cache.psm1 | 15 +++++++ doc/31-Changelog.md | 1 + .../05-Enable-Framework-Code-Caching.md | 42 ++++--------------- .../06-Update-Framework-And-Components.md | 1 - icinga-powershell-framework.psd1 | 28 +------------ icinga-powershell-framework.psm1 | 24 +++++------ .../Disable-IcingaFrameworkCodeCache.psm1 | 24 ----------- .../Enable-IcingaFrameworkCodeCache.psm1 | 21 ---------- .../Get-IcingaFrameworkCodeCache.psm1 | 28 ------------- .../Start-IcingaForWindowsInstallation.psm1 | 19 +-------- .../menu/installation/AdvancedEntries.psm1 | 1 - .../framework/EnableCodeCache.psm1 | 32 -------------- .../manage/framework/ManageFramework.psm1 | 20 ++------- .../framework/ToogleFrameworkCodeCache.psm1 | 9 ---- 15 files changed, 43 insertions(+), 223 deletions(-) create mode 100644 cache/framework_cache.psm1 delete mode 100644 lib/core/framework/Disable-IcingaFrameworkCodeCache.psm1 delete mode 100644 lib/core/framework/Enable-IcingaFrameworkCodeCache.psm1 delete mode 100644 lib/core/framework/Get-IcingaFrameworkCodeCache.psm1 delete mode 100644 lib/core/installer/menu/installation/framework/EnableCodeCache.psm1 delete mode 100644 lib/core/installer/menu/manage/framework/ToogleFrameworkCodeCache.psm1 diff --git a/.gitignore b/.gitignore index 95d338cf..8c93afb1 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ cache/* *.log !cache/README.md +!cache/framework_cache.psm1 \ No newline at end of file diff --git a/cache/framework_cache.psm1 b/cache/framework_cache.psm1 new file mode 100644 index 00000000..dfd6284d --- /dev/null +++ b/cache/framework_cache.psm1 @@ -0,0 +1,15 @@ +<# + ### Note ### + + This file is shipping plain with Icinga for Windows for each version. + Once the module is loaded, this content will entirely be replaced with + all modules and components shipped by the Icinga PowerShell Framework. + + Manually enabling the feature is no longer required. +#> + +$Global:Icinga = @{ + 'RebuildCache' = $TRUE; +}; + +Write-IcingaFrameworkCodeCache; diff --git a/doc/31-Changelog.md b/doc/31-Changelog.md index a97eeff8..114d8dfe 100644 --- a/doc/31-Changelog.md +++ b/doc/31-Changelog.md @@ -23,6 +23,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic * [#306](https://github.com/Icinga/icinga-powershell-framework/pull/306) Adds new Cmdlet `Exit-IcingaThrowCritical` to throw critical exit with a custom message, either by force or by using string filtering and adds storing of plugin exit codes internally * [#314](https://github.com/Icinga/icinga-powershell-framework/pull/314) Adds support to configure on which address TCP sockets are created on, defaults to `loopback` interface * [#316](https://github.com/Icinga/icinga-powershell-framework/pull/316) The reconfigure menu was previously present inside the Icinga Agent sub-menu and is now moved to the main installation menu for the Management Console +* [#318](https://github.com/Icinga/icinga-powershell-framework/pull/318) We always enforce the Icinga Framework Code caching now and ship a plain file to build the cache on first loading ## 1.5.2 (2021-07-09) diff --git a/doc/frameworkusage/05-Enable-Framework-Code-Caching.md b/doc/frameworkusage/05-Enable-Framework-Code-Caching.md index b2d298d3..61b5da97 100644 --- a/doc/frameworkusage/05-Enable-Framework-Code-Caching.md +++ b/doc/frameworkusage/05-Enable-Framework-Code-Caching.md @@ -1,45 +1,21 @@ -# Enable Framework Code Caching +# Framework Code Caching -On certain systems with fewer CPU cores, there might be an impact while running Icinga for Windows because of long loading times for the Icinga PowerShell Framework. To mitigate this issue, we added the possibility to create a code cache file for the entire Icinga PowerShell Framework. +By default, Icinga for Windows will compile all module files into a single cache file for quicker and easier loading. This ensures, that during startup all functions are available and can be used in combination with JEA profiles. -What it does is to load every single module and content file into one single `cache.psm1` file which is loaded in case the caching is enabled. +The location of the cache file is at -## Pre-Cautions - -By enabling this feature, you will have to generate a new cache file whenever you apply changes to any code for the Icinga PowerShell Framework. This can be done by running the Cmdlet - -```powershell -Write-IcingaFrameworkCodeCache ``` - -Please note that the code cache feature must be enabled first. - -In case you upgrade to a newer version of the Icinga PowerShell Framework, you will only require to manually proceed in case the code cache feature was disabled beforehand. In case the code cache feature is enabled during the upgrade, the cache file will be generated and updated automatically. - -## Enable Icinga Framework Code Cache - -To enable the Icinga PowerShell Framework code cache, simply run the following command within an Icinga Shell: - -```powershell -Enable-IcingaFrameworkCodeCache +.\cache\framework_cache.psm1 ``` -Once activated, you should make sure to generate a new cache file before using the Framework: - -```powershell -Write-IcingaFrameworkCodeCache -``` - -If you leave the code caching feature enabled, future updates of the Framework will automatically generate a new cache file. If you disabled the feature in-between, please write the cache file manually. +## Pre-Cautions -In case no cache file is present while the feature is activated, a cache file is generated on the first use of `Use-Icinga` or `icinga`. +In case you are running custom modifications to the Framework or apply manual patches, you will **always** have to re-write the Icinga for Windows cache file! During upgrades by using the Icinga for Windows Cmdlets, the cache file is updated automatically. -## Disable Icinga Framework Code Cache +## Updating Cache File -To disable the code caching feature again, you can simply run +To re-write the cache file and update it to the latest version manually, you can use the following command: ```powershell -Disable-IcingaFrameworkCodeCache +Write-IcingaFrameworkCodeCache ``` - -Please note that even though the cache file is no longer loaded it still remains. Therefor you will have to manually use `Write-IcingaFrameworkCodeCache` in case you activate the feature later again. This is especially required if you update the Icinga PowerShell Framework while the feature was disabled. diff --git a/doc/frameworkusage/06-Update-Framework-And-Components.md b/doc/frameworkusage/06-Update-Framework-And-Components.md index 7cee6dc2..fd002a08 100644 --- a/doc/frameworkusage/06-Update-Framework-And-Components.md +++ b/doc/frameworkusage/06-Update-Framework-And-Components.md @@ -43,7 +43,6 @@ Which version of the "Icinga Framework" do you want to install? (release/snapsho [Notice]: Unblocking Icinga PowerShell Files [Notice]: Cleaning temporary content [Notice]: Updating Framework cache file -[Notice]: The code caching feature is currently not enabled. You can enable it with "Enable-IcingaFrameworkCodeCache" [Notice]: Framework update has been completed. Please start a new PowerShell instance now to complete the update [Passed]: Icinga Agent service is installed [Passed]: The specified user "NT Authority\NetworkService" is allowed to run as service diff --git a/icinga-powershell-framework.psd1 b/icinga-powershell-framework.psd1 index a51a83ad..157e7f0f 100644 --- a/icinga-powershell-framework.psd1 +++ b/icinga-powershell-framework.psd1 @@ -8,32 +8,7 @@ Description = 'Icinga for Windows module which allows to entirely monitor the Windows Host system.' PowerShellVersion = '4.0' NestedModules = @( - '.\lib\core\framework\Get-IcingaFrameworkCodeCache.psm1', - '.\lib\config\Get-IcingaPowerShellConfig.psm1', - '.\lib\config\Read-IcingaPowerShellConfig.psm1', - '.\lib\config\Test-IcingaPowerShellConfigItem.psm1', - '.\lib\core\logging\Write-IcingaConsoleOutput.psm1', - '.\lib\core\logging\Write-IcingaConsoleNotice.psm1', - '.\lib\core\logging\Write-IcingaConsoleWarning.psm1', - '.\lib\core\tools\Read-IcingaFileContent.psm1', - '.\lib\core\framework\Invoke-IcingaInternalServiceCall.psm1', - '.\lib\core\framework\Get-IcingaFrameworkApiChecks.psm1', - '.\lib\daemon\Get-IcingaBackgroundDaemons.psm1', - '.\lib\webserver\Enable-IcingaUntrustedCertificateValidation.psm1', - '.\lib\core\logging\Write-IcingaEventMessage.psm1', - '.\lib\icinga\plugin\Exit-IcingaExecutePlugin.psm1', - '.\lib\icinga\exception\Exit-IcingaPluginNotInstalled.psm1', - '.\lib\icinga\exception\Exit-IcingaThrowException.psm1', - '.\lib\web\Set-IcingaTLSVersion.psm1', - '.\lib\web\Disable-IcingaProgressPreference.psm1', - '.\lib\core\tools\New-IcingaNewLine.psm1', - '.\lib\core\logging\Write-IcingaConsolePlain.psm1', - '.\lib\core\tools\Test-IcingaFunction.psm1', - '.\lib\core\tools\Write-IcingaConsoleHeader.psm1', - '.\lib\core\framework\Test-IcingaFrameworkConsoleOutput.psm1', - '.\lib\core\tools\ConvertTo-IcingaSecureString.psm1', - '.\lib\core\tools\ConvertTo-JsonUTF8Bytes.psm1', - '.\lib\core\tools\ConvertFrom-JsonUTF8.psm1' + '.\cache\framework_cache.psm1' ) FunctionsToExport = @( 'Use-Icinga', @@ -52,7 +27,6 @@ 'Get-IcingaPowerShellModuleFile', 'Start-IcingaShellAsUser', 'Get-IcingaPowerShellConfig', - 'Get-IcingaFrameworkCodeCache', 'Read-IcingaPowerShellConfig', 'Test-IcingaPowerShellConfigItem', 'Write-IcingaConsoleOutput', diff --git a/icinga-powershell-framework.psm1 b/icinga-powershell-framework.psm1 index 502fd310..80fb272d 100644 --- a/icinga-powershell-framework.psm1 +++ b/icinga-powershell-framework.psm1 @@ -17,6 +17,11 @@ function Use-Icinga() [switch]$Minimal = $FALSE ); + if ($null -ne $Global:Icinga -And $Global:Icinga.ContainsKey('RebuildCache') -And $Global:Icinga.RebuildCache) { + Remove-Module 'icinga-powershell-framework'; + Import-Module (Get-IcingaFrameworkCodeCacheFile) -Global -Force; + } + Disable-IcingaProgressPreference; if ($Minimal) { @@ -42,9 +47,7 @@ function Use-Icinga() Use-IcingaPlugins; } - if ((Test-Path (Get-IcingaFrameworkCodeCacheFile)) -eq $FALSE -And (Get-IcingaFrameworkCodeCache)) { - Write-IcingaFrameworkCodeCache; - } + Write-IcingaFrameworkCodeCache; # This function will allow us to load this entire module including possible # actions, making it available within our shell environment @@ -109,11 +112,7 @@ function Get-IcingaFrameworkCodeCacheFile() function Write-IcingaFrameworkCodeCache() { - if (Get-IcingaFrameworkCodeCache) { - Import-IcingaLib '\' -Init -CompileCache; - } else { - Write-IcingaConsoleNotice 'The code caching feature is currently not enabled. You can enable it with "Enable-IcingaFrameworkCodeCache"'; - } + Import-IcingaLib '\' -Init -CompileCache; } function Import-IcingaLib() @@ -129,16 +128,15 @@ function Import-IcingaLib() [switch]$CompileCache ); - # This is just to only allow a global loading of the module. Import-IcingaLib is ignored on every other # location. It is just there to give a basic idea within commands, of which functions are used if ($Init -eq $FALSE) { return; } - + $CacheFile = Get-IcingaFrameworkCodeCacheFile; - if ($Custom -eq $FALSE -And $CompileCache -eq $FALSE -And (Test-Path $CacheFile) -And (Get-IcingaFrameworkCodeCache)) { + if ($Custom -eq $FALSE -And $CompileCache -eq $FALSE -And (Test-Path $CacheFile)) { Import-Module $CacheFile -Global; return; } @@ -361,8 +359,8 @@ function Invoke-IcingaCommand() 'User environment $UserDomain\$Username' ); - if (Get-IcingaFrameworkCodeCache) { - $Headers += [string]::Format('Note: Icinga Framework Code Caching is enabled'); + if ($null -eq (Get-Command -Name 'Write-IcingaConsoleHeader' -ErrorAction SilentlyContinue)) { + Use-Icinga; } Write-IcingaConsoleHeader -HeaderLines $Headers; diff --git a/lib/core/framework/Disable-IcingaFrameworkCodeCache.psm1 b/lib/core/framework/Disable-IcingaFrameworkCodeCache.psm1 deleted file mode 100644 index 4db06f6b..00000000 --- a/lib/core/framework/Disable-IcingaFrameworkCodeCache.psm1 +++ /dev/null @@ -1,24 +0,0 @@ -<# -.SYNOPSIS - Disables the feature to cache all functions into a single file, - allowing quicker loading times of the Icinga PowerShell Framework -.DESCRIPTION - Disables the feature to cache all functions into a single file, - allowing quicker loading times of the Icinga PowerShell Framework -.FUNCTIONALITY - Disables the Icinga for Windows code caching -.EXAMPLE - PS>Disable-IcingaFrameworkCodeCache; -.LINK - https://github.com/Icinga/icinga-powershell-framework -#> - -function Disable-IcingaFrameworkCodeCache() -{ - Set-IcingaPowerShellConfig -Path 'Framework.CodeCaching' -Value $FALSE; - - # Remove the cache file in case it exists and the feature is disabled - if (Test-Path -Path (Get-IcingaFrameworkCodeCacheFile)) { - Remove-ItemSecure -Path (Get-IcingaFrameworkCodeCacheFile) -Force | Out-Null; - } -} diff --git a/lib/core/framework/Enable-IcingaFrameworkCodeCache.psm1 b/lib/core/framework/Enable-IcingaFrameworkCodeCache.psm1 deleted file mode 100644 index 5eceef27..00000000 --- a/lib/core/framework/Enable-IcingaFrameworkCodeCache.psm1 +++ /dev/null @@ -1,21 +0,0 @@ -<# -.SYNOPSIS - Enables the feature to cache all functions into a single file, - allowing quicker loading times of the Icinga PowerShell Framework -.DESCRIPTION - Enables the feature to cache all functions into a single file, - allowing quicker loading times of the Icinga PowerShell Framework -.FUNCTIONALITY - Enables the Icinga for Windows code caching -.EXAMPLE - PS>Enable-IcingaFrameworkCodeCache; -.LINK - https://github.com/Icinga/icinga-powershell-framework -#> - -function Enable-IcingaFrameworkCodeCache() -{ - Set-IcingaPowerShellConfig -Path 'Framework.CodeCaching' -Value $TRUE; - - Write-IcingaConsoleWarning 'Please run "Write-IcingaFrameworkCodeCache" in addition to ensure your cache is updated. This should be done after each update of the Framework in case the feature was disabled during the update run.'; -} diff --git a/lib/core/framework/Get-IcingaFrameworkCodeCache.psm1 b/lib/core/framework/Get-IcingaFrameworkCodeCache.psm1 deleted file mode 100644 index d340a9ab..00000000 --- a/lib/core/framework/Get-IcingaFrameworkCodeCache.psm1 +++ /dev/null @@ -1,28 +0,0 @@ -<# -.SYNOPSIS - Fetches the current enable/disable state of the feature - for caching the Icinga PowerShell Framework code -.DESCRIPTION - Fetches the current enable/disable state of the feature - for caching the Icinga PowerShell Framework code -.FUNCTIONALITY - Get the current code caching configuration of the - Icinga PowerShell Framework -.EXAMPLE - PS>Get-IcingaFrameworkCodeCache; -.LINK - https://github.com/Icinga/icinga-powershell-framework -.OUTPUTS - System.Boolean -#> - -function Get-IcingaFrameworkCodeCache() -{ - $CodeCaching = Get-IcingaPowerShellConfig -Path 'Framework.CodeCaching'; - - if ($null -eq $CodeCaching) { - return $FALSE; - } - - return $CodeCaching; -} diff --git a/lib/core/installer/Start-IcingaForWindowsInstallation.psm1 b/lib/core/installer/Start-IcingaForWindowsInstallation.psm1 index cf10f11f..22bb8262 100644 --- a/lib/core/installer/Start-IcingaForWindowsInstallation.psm1 +++ b/lib/core/installer/Start-IcingaForWindowsInstallation.psm1 @@ -43,9 +43,6 @@ function Start-IcingaForWindowsInstallation() $IcingaEndpoints = Get-IcingaForWindowsInstallerValuesFromStep -InstallerStep 'Show-IcingaForWindowsInstallerMenuEnterIcingaParentNodes'; $IcingaPort = Get-IcingaForWindowsInstallerValuesFromStep -InstallerStep 'Show-IcingaForWindowsInstallationMenuEnterIcingaPort'; - # Icinga for Windows PowerShell Framework - $CodeCacheType = Get-IcingaForWindowsInstallerStepSelection -InstallerStep 'Show-IcingaForWindowsManagementConsoleEnableCodeCache'; - $Hostname = ''; $GlobalZones = @(); $IcingaParentAddresses = @(); @@ -224,21 +221,7 @@ function Start-IcingaForWindowsInstallation() } } - switch ($CodeCacheType) { - '0' { - # Enable Code Cache - Enable-IcingaFrameworkCodeCache; - Write-IcingaConsoleNotice 'Writing Icinga Framework Code Cache file'; - Write-IcingaFrameworkCodeCache; - break; - }; - '1' { - # Disable Code Cache - Disable-IcingaFrameworkCodeCache; - break; - } - } - + Write-IcingaFrameworkCodeCache; Test-IcingaAgent; if ($InstallAgent) { diff --git a/lib/core/installer/menu/installation/AdvancedEntries.psm1 b/lib/core/installer/menu/installation/AdvancedEntries.psm1 index 7cd60f33..943d0b2c 100644 --- a/lib/core/installer/menu/installation/AdvancedEntries.psm1 +++ b/lib/core/installer/menu/installation/AdvancedEntries.psm1 @@ -21,7 +21,6 @@ function Add-IcingaForWindowsInstallationAdvancedEntries() Show-IcingaForWindowsInstallerMenuSelectIcingaPluginsSource -Automated -Advanced; Show-IcingaForWindowsInstallerMenuSelectWindowsServiceSource -Automated -Advanced; Show-IcingaForWindowsInstallationMenuEnterWindowsServiceDirectory -Automated -Advanced; - Show-IcingaForWindowsManagementConsoleEnableCodeCache -Automated -Advanced; Enable-IcingaFrameworkConsoleOutput; diff --git a/lib/core/installer/menu/installation/framework/EnableCodeCache.psm1 b/lib/core/installer/menu/installation/framework/EnableCodeCache.psm1 deleted file mode 100644 index 6f345e5d..00000000 --- a/lib/core/installer/menu/installation/framework/EnableCodeCache.psm1 +++ /dev/null @@ -1,32 +0,0 @@ -function Show-IcingaForWindowsManagementConsoleEnableCodeCache() -{ - param ( - [array]$Value = @(), - [string]$DefaultInput = '0', - [switch]$JumpToSummary = $FALSE, - [switch]$Automated = $FALSE, - [switch]$Advanced = $FALSE - ); - - Show-IcingaForWindowsInstallerMenu ` - -Header 'Do you want to enable the Icinga Framework Code Cache?' ` - -Entries @( - @{ - 'Caption' = 'Enable Icinga Framework Code Cache '; - 'Command' = 'Show-IcingaForWindowsInstallerConfigurationSummary'; - 'Help' = 'Enables the Icinga Framework Code Cache feature during installation to decrease the loading time of the Icinga Framework. Please note that for each custom modification you do on the Icinga Framework afterwards, you will have to call "Write-IcingaFrameworkCodeCache" to rebuild the cache'; - }, - @{ - 'Caption' = 'Disable Icinga Framework Code Cache'; - 'Command' = 'Show-IcingaForWindowsInstallerConfigurationSummary'; - 'Help' = 'Does not enable the Icinga Framework Code Cache and disables it during the installation process'; - } - ) ` - -DefaultIndex $DefaultInput ` - -JumpToSummary:$FALSE ` - -ConfigElement ` - -Automated:$Automated ` - -Advanced:$Advanced; -} - -Set-Alias -Name 'IfW-CodeCache' -Value 'Show-IcingaForWindowsManagementConsoleEnableCodeCache'; diff --git a/lib/core/installer/menu/manage/framework/ManageFramework.psm1 b/lib/core/installer/menu/manage/framework/ManageFramework.psm1 index 1d10a337..88139802 100644 --- a/lib/core/installer/menu/manage/framework/ManageFramework.psm1 +++ b/lib/core/installer/menu/manage/framework/ManageFramework.psm1 @@ -1,7 +1,6 @@ function Show-IcingaForWindowsManagementConsoleManageFramework() { $FrameworkDebug = Get-IcingaFrameworkDebugMode; - $FrameworkCodeCache = Get-IcingaFrameworkCodeCache; $IcingaService = Get-Service 'icingapowershell' -ErrorAction SilentlyContinue; $AdminShell = $global:Icinga.InstallWizard.AdminShell; $ServiceStatus = $null; @@ -30,21 +29,10 @@ function Show-IcingaForWindowsManagementConsoleManageFramework() } }, @{ - 'Caption' = ([string]::Format('Framework Code Cache: {0}', (& { if ($FrameworkCodeCache) { 'Enabled' } else { 'Disabled' } } ))); - 'Command' = 'Show-IcingaForWindowsManagementConsoleManageFramework'; - 'Help' = 'Disable or enable the Icinga PowerShell Framework Code cache feature. The code cache is written automatically once it is enabled'; - 'Disabled' = $FALSE; - 'Action' = @{ - 'Command' = 'Invoke-IcingaForWindowsMangementConsoleToogleFrameworkCodeCache'; - 'Arguments' = @{ }; - } - }, - @{ - 'Caption' = 'Update Framework Code Cache'; - 'Command' = 'Show-IcingaForWindowsManagementConsoleManageFramework'; - 'Help' = 'Updates the Icinga PowerShell Framework Code Cache'; - 'Disabled' = (-Not ($FrameworkCodeCache)); - 'Action' = @{ + 'Caption' = 'Update Framework Code Cache'; + 'Command' = 'Show-IcingaForWindowsManagementConsoleManageFramework'; + 'Help' = 'Updates the Icinga PowerShell Framework Code Cache'; + 'Action' = @{ 'Command' = 'Write-IcingaFrameworkCodeCache'; 'Arguments' = @{ }; } diff --git a/lib/core/installer/menu/manage/framework/ToogleFrameworkCodeCache.psm1 b/lib/core/installer/menu/manage/framework/ToogleFrameworkCodeCache.psm1 deleted file mode 100644 index e6e44a12..00000000 --- a/lib/core/installer/menu/manage/framework/ToogleFrameworkCodeCache.psm1 +++ /dev/null @@ -1,9 +0,0 @@ -function Invoke-IcingaForWindowsMangementConsoleToogleFrameworkCodeCache() -{ - if (Get-IcingaFrameworkCodeCache) { - Disable-IcingaFrameworkCodeCache; - } else { - Enable-IcingaFrameworkCodeCache; - Write-IcingaFrameworkCodeCache; - } -}