Merge pull request #322 from Icinga:deprecated/remove_import_from_files

Deprecated: Remove legacy import feature from files

In the past we used different ways to import certain information into Icinga for Windows. With the new version, we can get rid of most of the handling and reduce CPU impact in this matter.
This commit is contained in:
Lord Hepipud 2021-08-07 14:28:21 +02:00 committed by GitHub
commit 5f86b2d2e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 15 additions and 161 deletions

View file

@ -24,6 +24,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
* [#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 * [#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 * [#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 * [#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
* [#322](https://github.com/Icinga/icinga-powershell-framework/pull/322) Remove legacy import feature from Framework and replace it with a dummy function, as no longer required by Icinga for Windows
## 1.5.2 (2021-07-09) ## 1.5.2 (2021-07-09)

View file

@ -32,11 +32,6 @@ function Use-Icinga()
$global:Icinga.Add('Minimal', $TRUE); $global:Icinga.Add('Minimal', $TRUE);
} }
# If we load the minimal Framework files, we have to ensure our enums are loaded
Import-Module ([string]::Format('{0}\lib\icinga\exception\Icinga_IcingaExceptionEnums.psm1', $PSScriptRoot)) -Global;
Import-Module ([string]::Format('{0}\lib\icinga\enums\Icinga_IcingaEnums.psm1', $PSScriptRoot)) -Global;
Import-Module ([string]::Format('{0}\lib\core\logging\Icinga_EventLog_Enums.psm1', $PSScriptRoot)) -Global;
return; return;
} }
@ -46,12 +41,6 @@ function Use-Icinga()
Use-IcingaPlugins; Use-IcingaPlugins;
} }
# This function will allow us to load this entire module including possible
# actions, making it available within our shell environment
# First load our custom modules
Import-IcingaLib '\' -Init -Custom;
Import-IcingaLib '\' -Init;
if ($LibOnly -eq $FALSE) { if ($LibOnly -eq $FALSE) {
$global:IcingaThreads = [hashtable]::Synchronized(@{}); $global:IcingaThreads = [hashtable]::Synchronized(@{});
$global:IcingaThreadContent = [hashtable]::Synchronized(@{}); $global:IcingaThreadContent = [hashtable]::Synchronized(@{});
@ -107,150 +96,27 @@ function Get-IcingaFrameworkCodeCacheFile()
return (Join-Path -Path (Get-IcingaCacheDir) -ChildPath 'framework_cache.psm1'); return (Join-Path -Path (Get-IcingaCacheDir) -ChildPath 'framework_cache.psm1');
} }
function Write-IcingaFrameworkCodeCache()
{
Import-IcingaLib '\' -Init -CompileCache;
}
function Import-IcingaLib() function Import-IcingaLib()
{ {
param( # Do nothing, just leave it here as compatibility layer until we
[String]$Lib, # cleaned every other repository
# The Force Reload will remove the module in case it's loaded and reload it to track
# possible development changes without having to create new PowerShell environments
[Switch]$ForceReload,
[switch]$Init,
[switch]$Custom,
[switch]$WriteManifests,
[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 ($CompileCache -eq $FALSE) {
Import-Module 'icinga-powershell-framework' -Global -Force;
return;
}
[array]$ImportModules = @();
[array]$RemoveModules = @();
if ($Custom) {
[string]$directory = Join-Path -Path $PSScriptRoot -ChildPath 'custom\';
} else {
[string]$directory = Join-Path -Path $PSScriptRoot -ChildPath 'lib\';
}
[string]$module = Join-Path -Path $directory -ChildPath $Lib;
[string]$moduleName = '';
$ListOfLoadedModules = Get-Module | Select-Object Name;
# Load modules from directory
if ((Test-Path $module -PathType Container)) {
Get-ChildItem -Path $module -Recurse -Filter *.psm1 |
ForEach-Object {
[string]$modulePath = $_.FullName;
$moduleName = $_.Name.Replace('.psm1', '');
if ($ListOfLoadedModules -like "*$moduleName*") {
if ($ForceReload) {
$RemoveModules += $moduleName;
}
$ImportModules += $modulePath;
} else {
$ImportModules += $modulePath;
if ($WriteManifests) {
Publish-IcingaModuleManifest -Module $moduleName;
}
}
}
} else {
$module = $module.Replace('.psm1', ''); # Cut possible .psm1 ending
$moduleName = $module.Split('\')[-1]; # Get the last element
if ($ForceReload) {
if ($ListOfLoadedModules -Like "*$moduleName*") {
$RemoveModules += $moduleName;
}
}
$ImportModules += ([string]::Format('{0}.psm1', $module));
if ($WriteManifests) {
Publish-IcingaModuleManifest -Module $moduleName;
}
}
if ($RemoveModules.Count -ne 0) {
Remove-Module $RemoveModules;
}
if ($ImportModules.Count -ne 0) {
if ($CompileCache) {
$CacheContent = '';
foreach ($module in $ImportModules) {
$Content = Get-Content $module -Raw;
$CacheContent += $Content + "`r`n";
}
$CacheContent += $Content + "Export-ModuleMember -Function @( '*' )";
Set-Content -Path $CacheFile -Value $CacheContent;
} else {
Import-Module $ImportModules -Global;
}
}
} }
function Publish-IcingaModuleManifest() function Write-IcingaFrameworkCodeCache()
{ {
param( [string]$CacheFile = Get-IcingaFrameworkCodeCacheFile;
[string]$Module [string]$directory = Join-Path -Path $PSScriptRoot -ChildPath 'lib\';
); [string]$CacheContent = '';
[string]$ManifestDir = Join-Path -Path $PSScriptRoot -ChildPath 'manifests'; # Load modules from directory
[string]$ModuleFile = [string]::Format('{0}.psd1', $Module); Get-ChildItem -Path $directory -Recurse -Filter '*.psm1' |
[string]$PSDFile = Join-Path -Path $ManifestDir -ChildPath $ModuleFile; ForEach-Object {
$CacheContent += (Get-Content -Path $_.FullName -Raw);
if (Test-Path $PSDFile) { $CacheContent += "`r`n";
return;
}
New-ModuleManifest -Path $PSDFile -ModuleVersion 1.0 -Author $env:USERNAME -CompanyName 'Icinga GmbH' -Copyright '(c) 2019 Icinga GmbH. All rights reserved.' -PowerShellVersion 4.0;
$Content = Get-Content $PSDFile;
$NewContent = @();
foreach ($line in $Content) {
if ([string]::IsNullOrEmpty($line)) {
continue;
} }
if ($line[0] -eq '#') { $CacheContent += "Export-ModuleMember -Function @( '*' )";
continue; Set-Content -Path $CacheFile -Value $CacheContent;
}
if ($line.Contains('#')) {
$line = $line.Substring(0, $line.IndexOf('#'));
}
$tmpLine = $line;
while ($tmpLine.Contains(' ')) {
$tmpLine = $tmpLine.Replace(' ', '');
}
if ([string]::IsNullOrEmpty($tmpLine)) {
continue;
}
$NewContent += $line;
}
Set-Content -Path $PSDFile -Value $NewContent;
} }
function Publish-IcingaEventlogDocumentation() function Publish-IcingaEventlogDocumentation()

View file

@ -1,5 +1,3 @@
Import-IcingaLib core\tools;
function ConvertFrom-TimeSpan() function ConvertFrom-TimeSpan()
{ {
param ( param (

View file

@ -1,5 +1,3 @@
Import-IcingaLib core\tools;
<# <#
.SYNOPSIS .SYNOPSIS
Converts unit to seconds. Converts unit to seconds.

View file

@ -7,8 +7,7 @@ function New-IcingaCheckCommand()
'Critical', 'Critical',
'[switch]NoPerfData', '[switch]NoPerfData',
'[int]Verbose' '[int]Verbose'
), )
[array]$ImportLib = @()
); );
if ([string]::IsNullOrEmpty($Name) -eq $TRUE) { if ([string]::IsNullOrEmpty($Name) -eq $TRUE) {
@ -49,12 +48,6 @@ function New-IcingaCheckCommand()
New-Item -Path $ModuleFolder -ItemType Directory | Out-Null; New-Item -Path $ModuleFolder -ItemType Directory | Out-Null;
Add-Content -Path $ScriptFile -Value 'Import-IcingaLib icinga\plugin;';
foreach ($Library in $ImportLib) {
Add-Content -Path $ScriptFile -Value "Import-IcingaLib $Library;";
}
Add-Content -Path $ScriptFile -Value ''; Add-Content -Path $ScriptFile -Value '';
Add-Content -Path $ScriptFile -Value "function $CommandName()"; Add-Content -Path $ScriptFile -Value "function $CommandName()";
Add-Content -Path $ScriptFile -Value "{"; Add-Content -Path $ScriptFile -Value "{";

View file

@ -1,5 +1,3 @@
Import-IcingaLib icinga\plugin;
function Get-IcingaHelpThresholds() function Get-IcingaHelpThresholds()
{ {
param ( param (