mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-20 23:00:35 -05:00
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:
parent
c2e3ff2131
commit
c5903114b2
7 changed files with 37 additions and 48 deletions
|
|
@ -7,8 +7,8 @@ param (
|
|||
# Create an internal 'namespace' for our environment
|
||||
Set-Variable -Name Icinga2 -Option Constant -Value @{
|
||||
Function = @(
|
||||
'Use-Icinga',
|
||||
'Import-IcingaLib',
|
||||
'Import-IcingaDirectoryModules',
|
||||
'Get-Icinga-Lib',
|
||||
'Get-Icinga-Object',
|
||||
'Get-Icinga-Service',
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ Description = 'Icinga 2 Windows Agent Module, which allows to entirely monitor t
|
|||
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.
|
||||
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.
|
||||
CmdletsToExport = @()
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
{
|
||||
param(
|
||||
|
|
@ -24,56 +30,38 @@ function Import-IcingaLib()
|
|||
[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;
|
||||
$module = $module.Replace('.psm1', ''); # Cut possible .psm1 ending
|
||||
[string]$moduleName = $module.Split('\')[-1]; # Get the last element
|
||||
[string]$moduleName = '';
|
||||
|
||||
if ($ForceReload) {
|
||||
$ListOfLoadedModules = Get-Module | Select-Object Name;
|
||||
if ($ListOfLoadedModules -Like "*$moduleName*") {
|
||||
Remove-Module -Name $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 ($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
|
||||
$moduleName = $module.Split('\')[-1]; # Get the last element
|
||||
|
||||
Import-Module ([string]::Format('{0}.psm1', $module)) -Global;
|
||||
}
|
||||
|
||||
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) {
|
||||
$ListOfLoadedModules = Get-Module | Select-Object Name;
|
||||
if ($ListOfLoadedModules -like "*$moduleName*") {
|
||||
Remove-Module -Name $moduleName
|
||||
if ($ListOfLoadedModules -Like "*$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;
|
||||
}
|
||||
|
||||
# Automaticly load the Help library including Plugins
|
||||
Import-IcingaLib help\help;
|
||||
Import-IcingaLib plugins;
|
||||
|
||||
# Initialise base configuration for our module
|
||||
$Icinga2 = & (Join-Path -Path $PSScriptRoot -ChildPath '\core\init.ps1') `
|
||||
-RootDirectory $PSScriptRoot `
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
Import-IcingaDirectoryModules -LibDirectory (Join-Path -Path $PSScriptRoot -ChildPath $MyInvocation.MyCommand.Name);
|
||||
|
|
@ -1 +0,0 @@
|
|||
Import-IcingaDirectoryModules -LibDirectory (Join-Path -Path $PSScriptRoot -ChildPath $MyInvocation.MyCommand.Name);
|
||||
|
|
@ -1 +0,0 @@
|
|||
Import-IcingaDirectoryModules -LibDirectory (Join-Path -Path $PSScriptRoot -ChildPath $MyInvocation.MyCommand.Name);
|
||||
Loading…
Reference in a new issue