Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Always enforce Framework code caching #318

Merged
merged 1 commit into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ cache/*
*.log

!cache/README.md
!cache/framework_cache.psm1
15 changes: 15 additions & 0 deletions cache/framework_cache.psm1
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions doc/31-Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
42 changes: 9 additions & 33 deletions doc/frameworkusage/05-Enable-Framework-Code-Caching.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 0 additions & 1 deletion doc/frameworkusage/06-Update-Framework-And-Components.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 1 addition & 27 deletions icinga-powershell-framework.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -52,7 +27,6 @@
'Get-IcingaPowerShellModuleFile',
'Start-IcingaShellAsUser',
'Get-IcingaPowerShellConfig',
'Get-IcingaFrameworkCodeCache',
'Read-IcingaPowerShellConfig',
'Test-IcingaPowerShellConfigItem',
'Write-IcingaConsoleOutput',
Expand Down
24 changes: 11 additions & 13 deletions icinga-powershell-framework.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
24 changes: 0 additions & 24 deletions lib/core/framework/Disable-IcingaFrameworkCodeCache.psm1

This file was deleted.

21 changes: 0 additions & 21 deletions lib/core/framework/Enable-IcingaFrameworkCodeCache.psm1

This file was deleted.

28 changes: 0 additions & 28 deletions lib/core/framework/Get-IcingaFrameworkCodeCache.psm1

This file was deleted.

19 changes: 1 addition & 18 deletions lib/core/installer/Start-IcingaForWindowsInstallation.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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 = @();
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion lib/core/installer/menu/installation/AdvancedEntries.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

This file was deleted.

20 changes: 4 additions & 16 deletions lib/core/installer/menu/manage/framework/ManageFramework.psm1
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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' = @{ };
}
Expand Down

This file was deleted.