Skip to content

Commit

Permalink
Merge pull request #322 from Icinga:deprecated/remove_import_from_files
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LordHepipud authored Aug 7, 2021
2 parents 6a82bf0 + e9ba874 commit 5f86b2d
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 161 deletions.
1 change: 1 addition & 0 deletions doc/31-Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
* [#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
* [#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)

Expand Down
160 changes: 13 additions & 147 deletions icinga-powershell-framework.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ function Use-Icinga()
$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;
}

Expand All @@ -46,12 +41,6 @@ function Use-Icinga()
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) {
$global:IcingaThreads = [hashtable]::Synchronized(@{});
$global:IcingaThreadContent = [hashtable]::Synchronized(@{});
Expand Down Expand Up @@ -107,150 +96,27 @@ function Get-IcingaFrameworkCodeCacheFile()
return (Join-Path -Path (Get-IcingaCacheDir) -ChildPath 'framework_cache.psm1');
}

function Write-IcingaFrameworkCodeCache()
{
Import-IcingaLib '\' -Init -CompileCache;
}

function Import-IcingaLib()
{
param(
[String]$Lib,
# 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;
}
}
# Do nothing, just leave it here as compatibility layer until we
# cleaned every other repository
}

function Publish-IcingaModuleManifest()
function Write-IcingaFrameworkCodeCache()
{
param(
[string]$Module
);

[string]$ManifestDir = Join-Path -Path $PSScriptRoot -ChildPath 'manifests';
[string]$ModuleFile = [string]::Format('{0}.psd1', $Module);
[string]$PSDFile = Join-Path -Path $ManifestDir -ChildPath $ModuleFile;
[string]$CacheFile = Get-IcingaFrameworkCodeCacheFile;
[string]$directory = Join-Path -Path $PSScriptRoot -ChildPath 'lib\';
[string]$CacheContent = '';

if (Test-Path $PSDFile) {
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 '#') {
continue;
}

if ($line.Contains('#')) {
$line = $line.Substring(0, $line.IndexOf('#'));
}

$tmpLine = $line;
while ($tmpLine.Contains(' ')) {
$tmpLine = $tmpLine.Replace(' ', '');
}
if ([string]::IsNullOrEmpty($tmpLine)) {
continue;
# Load modules from directory
Get-ChildItem -Path $directory -Recurse -Filter '*.psm1' |
ForEach-Object {
$CacheContent += (Get-Content -Path $_.FullName -Raw);
$CacheContent += "`r`n";
}

$NewContent += $line;
}

Set-Content -Path $PSDFile -Value $NewContent;
$CacheContent += "Export-ModuleMember -Function @( '*' )";
Set-Content -Path $CacheFile -Value $CacheContent;
}

function Publish-IcingaEventlogDocumentation()
Expand Down
2 changes: 0 additions & 2 deletions lib/core/tools/ConvertFrom-TimeSpan.psm1
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Import-IcingaLib core\tools;

function ConvertFrom-TimeSpan()
{
param (
Expand Down
2 changes: 0 additions & 2 deletions lib/core/tools/ConvertTo-Seconds.psm1
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Import-IcingaLib core\tools;

<#
.SYNOPSIS
Converts unit to seconds.
Expand Down
9 changes: 1 addition & 8 deletions lib/core/tools/New-IcingaCheckCommand.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ function New-IcingaCheckCommand()
'Critical',
'[switch]NoPerfData',
'[int]Verbose'
),
[array]$ImportLib = @()
)
);

if ([string]::IsNullOrEmpty($Name) -eq $TRUE) {
Expand Down Expand Up @@ -49,12 +48,6 @@ function New-IcingaCheckCommand()

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 "function $CommandName()";
Add-Content -Path $ScriptFile -Value "{";
Expand Down
2 changes: 0 additions & 2 deletions lib/help/help/Get-IcingaHelpThresholds.psm1
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Import-IcingaLib icinga\plugin;

function Get-IcingaHelpThresholds()
{
param (
Expand Down

0 comments on commit 5f86b2d

Please sign in to comment.