Reworked entire Lib and Module loading

Modules are now automaticly loaded recursively for the spcified folder, allowing us to name modules into a "namespace", preventing our modules to interfear with possible installed modules.
This commit is contained in:
Lord Hepipud 2019-07-22 12:41:07 +02:00
parent c2e3ff2131
commit c5903114b2
7 changed files with 37 additions and 48 deletions

View file

@ -7,8 +7,8 @@ param (
# Create an internal 'namespace' for our environment # Create an internal 'namespace' for our environment
Set-Variable -Name Icinga2 -Option Constant -Value @{ Set-Variable -Name Icinga2 -Option Constant -Value @{
Function = @( Function = @(
'Use-Icinga',
'Import-IcingaLib', 'Import-IcingaLib',
'Import-IcingaDirectoryModules',
'Get-Icinga-Lib', 'Get-Icinga-Lib',
'Get-Icinga-Object', 'Get-Icinga-Object',
'Get-Icinga-Service', 'Get-Icinga-Service',

View file

@ -25,7 +25,7 @@ Description = 'Icinga 2 Windows Agent Module, which allows to entirely monitor t
PowerShellVersion = '3.0' PowerShellVersion = '3.0'
# Aus diesem Modul zu exportierende Funktionen. Um optimale Leistung zu erzielen, verwenden Sie keine Platzhalter und löschen den Eintrag nicht. Verwenden Sie ein leeres Array, wenn keine zu exportierenden Funktionen vorhanden sind. # Aus diesem Modul zu exportierende Funktionen. Um optimale Leistung zu erzielen, verwenden Sie keine Platzhalter und löschen den Eintrag nicht. Verwenden Sie ein leeres Array, wenn keine zu exportierenden Funktionen vorhanden sind.
FunctionsToExport = @( 'Import-IcingaLib', 'Import-IcingaDirectoryModules', 'Start-Icinga-Checker', 'Stop-Icinga-Checker', 'Get-Icinga-Lib', 'Get-Icinga-Object', 'Get-Icinga-Service', 'Start-Icinga-Service', 'Stop-Icinga-Service', 'Restart-Icinga-Service', 'Install-Icinga-Service', 'Uninstall-Icinga-Service', 'Get-Icinga-Setup', 'Install-Icinga', 'Start-Icinga-Daemon', 'Stop-Icinga-Daemon', 'Icinga-Client', 'Get-Icinga-Command', 'New-Icinga-Monitoring', 'Get-Icinga-Counter', 'Get-Icinga-Config', 'Set-Icinga-Config', 'Remove-Icinga-Config', 'New-Icinga-Config' ) FunctionsToExport = @( 'Use-Icinga', 'Import-IcingaLib', 'Start-Icinga-Checker', 'Stop-Icinga-Checker', 'Get-Icinga-Lib', 'Get-Icinga-Object', 'Get-Icinga-Service', 'Start-Icinga-Service', 'Stop-Icinga-Service', 'Restart-Icinga-Service', 'Install-Icinga-Service', 'Uninstall-Icinga-Service', 'Get-Icinga-Setup', 'Install-Icinga', 'Start-Icinga-Daemon', 'Stop-Icinga-Daemon', 'Icinga-Client', 'Get-Icinga-Command', 'New-Icinga-Monitoring', 'Get-Icinga-Counter', 'Get-Icinga-Config', 'Set-Icinga-Config', 'Remove-Icinga-Config', 'New-Icinga-Config' )
# Aus diesem Modul zu exportierende Cmdlets. Um optimale Leistung zu erzielen, verwenden Sie keine Plat'zhalter und löschen den Eintrag nicht. Verwenden Sie ein leeres Array, wenn keine zu exportierenden Cmdlets vorhanden sind. # Aus diesem Modul zu exportierende Cmdlets. Um optimale Leistung zu erzielen, verwenden Sie keine Plat'zhalter und löschen den Eintrag nicht. Verwenden Sie ein leeres Array, wenn keine zu exportierenden Cmdlets vorhanden sind.
CmdletsToExport = @() CmdletsToExport = @()

View file

@ -9,6 +9,12 @@
#> #>
function Use-Icinga()
{
# This function will allow us to load this entire module including possible
# actions, making it available within our shell environment
}
function Import-IcingaLib() function Import-IcingaLib()
{ {
param( param(
@ -24,56 +30,38 @@ function Import-IcingaLib()
[Switch]$ForceReload [Switch]$ForceReload
); );
[string]$directory = Join-Path -Path $PSScriptRoot -ChildPath 'lib'; [string]$directory = Join-Path -Path $PSScriptRoot -ChildPath 'lib\';
[string]$module = Join-Path -Path $directory -ChildPath $Lib; [string]$module = Join-Path -Path $directory -ChildPath $Lib;
$module = $module.Replace('.psm1', ''); # Cut possible .psm1 ending [string]$moduleName = '';
[string]$moduleName = $module.Split('\')[-1]; # Get the last element
if ($ForceReload) { $ListOfLoadedModules = Get-Module | Select-Object Name;
$ListOfLoadedModules = Get-Module | Select-Object Name;
if ($ListOfLoadedModules -Like "*$moduleName*") { # Load modules from directory
Remove-Module -Name $moduleName; if ((Test-Path $module -PathType Container)) {
Get-ChildItem -Path $module -Recurse -Filter *.psm1 |
ForEach-Object {
[string]$modulePath = $_.FullName;
$moduleName = $_.Name.Replace('.psm1', '');
if ($ForceReload) {
if ($ListOfLoadedModules -like "*$moduleName*") {
Remove-Module -Name $moduleName
}
}
Import-Module ([string]::Format('{0}', $modulePath)) -Global;
} }
} } else {
$module = $module.Replace('.psm1', ''); # Cut possible .psm1 ending
Import-Module ([string]::Format('{0}.psm1', $module)) -Global; $moduleName = $module.Split('\')[-1]; # Get the last element
}
function Import-IcingaDirectoryModules()
{
param(
[Parameter(
Position=0,
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)
]
[String]$LibDirectory,
# 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
);
$LibDirectory = $LibDirectory.Replace('.psm1', '');
if (-Not (Test-Path $LibDirectory)) {
Write-Output ([string]::Format('Include directory not found: {0}', $LibDirectory));
return;
}
Get-ChildItem -Path $LibDirectory -Recurse -Filter *.psm1 |
ForEach-Object {
[string]$modulePath = $_.FullName;
[string]$moduleName = $_.Name.Replace('.psm1', '');
if ($ForceReload) { if ($ForceReload) {
$ListOfLoadedModules = Get-Module | Select-Object Name; if ($ListOfLoadedModules -Like "*$moduleName*") {
if ($ListOfLoadedModules -like "*$moduleName*") { Remove-Module -Name $moduleName;
Remove-Module -Name $moduleName
} }
} }
Import-Module ([string]::Format('{0}', $modulePath)) -Global; Import-Module ([string]::Format('{0}.psm1', $module)) -Global;
} }
} }
@ -346,6 +334,10 @@ function Get-Icinga-Object()
return $Icinga2; return $Icinga2;
} }
# Automaticly load the Help library including Plugins
Import-IcingaLib help\help;
Import-IcingaLib plugins;
# Initialise base configuration for our module # Initialise base configuration for our module
$Icinga2 = & (Join-Path -Path $PSScriptRoot -ChildPath '\core\init.ps1') ` $Icinga2 = & (Join-Path -Path $PSScriptRoot -ChildPath '\core\init.ps1') `
-RootDirectory $PSScriptRoot ` -RootDirectory $PSScriptRoot `

View file

@ -1 +0,0 @@
Import-IcingaDirectoryModules -LibDirectory (Join-Path -Path $PSScriptRoot -ChildPath $MyInvocation.MyCommand.Name);

View file

@ -1 +0,0 @@
Import-IcingaDirectoryModules -LibDirectory (Join-Path -Path $PSScriptRoot -ChildPath $MyInvocation.MyCommand.Name);

View file

@ -1 +0,0 @@
Import-IcingaDirectoryModules -LibDirectory (Join-Path -Path $PSScriptRoot -ChildPath $MyInvocation.MyCommand.Name);