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

Deprecated: Remove legacy import feature from files #322

Merged
merged 1 commit into from
Aug 7, 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 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