|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Fetches plugins within the namespace `Invoke-IcingaCheck*` for a given |
| 4 | + component name or the direct path and creates Icinga Director as well as |
| 5 | + Icinga 2 configuration files. |
| 6 | +
|
| 7 | + The configuration files are printed within a `config` folder of the |
| 8 | + specific module and splitted into `director` and `icinga` |
| 9 | +.DESCRIPTION |
| 10 | + etches plugins within the namespace `Invoke-IcingaCheck*` for a given |
| 11 | + component name or the direct path and creates Icinga Director as well as |
| 12 | + Icinga 2 configuration files. |
| 13 | +
|
| 14 | + The configuration files are printed within a `config` folder of the |
| 15 | + specific module and splitted into `director` and `icinga` |
| 16 | +.FUNCTIONALITY |
| 17 | + Creates Icinga 2 and Icinga Director configuration files for plugins |
| 18 | +.EXAMPLE |
| 19 | + PS>Publish-IcingaPluginConfiguration -ComponentName 'plugins'; |
| 20 | +.EXAMPLE |
| 21 | + PS>Publish-IcingaPluginConfiguration -ComponentPath 'C:\Program Files\WindowsPowerShell\modules\icinga-powershell-plugins'; |
| 22 | +.PARAMETER ComponentName |
| 23 | + The name of the component to lookup for plugins and write configuration for. |
| 24 | + The leading icinga-powershell- is not required and you should simply use the name, |
| 25 | + like 'plugins' or 'mssql' |
| 26 | +.PARAMETER ComponentPath |
| 27 | + The path to the root directory of a PowerShell Plugin repository, like |
| 28 | + 'C:\Program Files\WindowsPowerShell\modules\icinga-powershell-plugins' |
| 29 | +.INPUTS |
| 30 | + System.String |
| 31 | +.LINK |
| 32 | + https://github.com/Icinga/icinga-powershell-framework |
| 33 | +#> |
| 34 | + |
| 35 | +function Publish-IcingaPluginConfiguration() |
| 36 | +{ |
| 37 | + param ( |
| 38 | + [string]$ComponentName, |
| 39 | + [string]$ComponentPath |
| 40 | + ); |
| 41 | + |
| 42 | + if ([string]::IsNullOrEmpty($ComponentName) -And [string]::IsNullOrEmpty($ComponentPath)) { |
| 43 | + Write-IcingaConsoleError 'Please specify either a component name like "plugins" or set the component path to the root folder if a component, like "C:\Program Files\WindowsPowerShell\modules\icinga-powershell\plugins".'; |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + if ([string]::IsNullOrEmpty($ComponentPath)) { |
| 48 | + $ComponentPath = Join-Path -Path (Get-IcingaFrameworkRootPath) -ChildPath ([string]::Format('icinga-powershell-{0}', $ComponentName)); |
| 49 | + } |
| 50 | + |
| 51 | + if ((Test-Path $ComponentPath) -eq $FALSE) { |
| 52 | + Write-IcingaConsoleError 'The path "{0}" for the Icinga for Windows component is not valid' -Objects $ComponentPath; |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + try { |
| 57 | + Import-Module $ComponentPath -Global -Force -ErrorAction Stop; |
| 58 | + } catch { |
| 59 | + [string]$Message = $_.Exception.Message; |
| 60 | + Write-IcingaConsoleError 'Failed to import the module on path "{0}". Please verify that this is a valid PowerShell module root folder. Exception: {1}{2}' -Objects $ComponentPath, (New-IcingaNewLine), $Message; |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + $CheckCommands = Get-Command -ListImported -Name 'Invoke-IcingaCheck*' -ErrorAction SilentlyContinue; |
| 65 | + |
| 66 | + if ($null -eq $CheckCommands) { |
| 67 | + Write-IcingaConsoleError 'No Icinga CheckCommands were configured for module "{0}". Please verify that this is a valid PowerShell module root folder. Exception: {1}{2}' -Objects $ComponentPath, (New-IcingaNewLine), $Message; |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + [array]$CheckList = @(); |
| 72 | + |
| 73 | + [string]$BasketConfigDir = Join-Path -Path $ComponentPath -ChildPath 'config\director'; |
| 74 | + [string]$IcingaConfigDir = Join-Path -Path $ComponentPath -ChildPath 'config\icinga'; |
| 75 | + |
| 76 | + if ((Test-Path $BasketConfigDir)) { |
| 77 | + Remove-Item -Path $BasketConfigDir -Recurse -Force | Out-Null; |
| 78 | + } |
| 79 | + if ((Test-Path $IcingaConfigDir)) { |
| 80 | + Remove-Item -Path $IcingaConfigDir -Recurse -Force | Out-Null; |
| 81 | + } |
| 82 | + |
| 83 | + if ((Test-Path $BasketConfigDir) -eq $FALSE) { |
| 84 | + New-Item -Path $BasketConfigDir -ItemType Directory | Out-Null; |
| 85 | + } |
| 86 | + if ((Test-Path $IcingaConfigDir) -eq $FALSE) { |
| 87 | + New-Item -Path $IcingaConfigDir -ItemType Directory | Out-Null; |
| 88 | + } |
| 89 | + |
| 90 | + foreach ($check in $CheckCommands) { |
| 91 | + [string]$CheckPath = $check.Module.ModuleBase; |
| 92 | + |
| 93 | + if ($CheckPath.Contains($ComponentPath) -eq $FALSE) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + $CheckList += [string]$check; |
| 98 | + Get-IcingaCheckCommandConfig -CheckName $check -OutDirectory $BasketConfigDir -FileName $check; |
| 99 | + Get-IcingaCheckCommandConfig -CheckName $check -OutDirectory $IcingaConfigDir -FileName $check -IcingaConfig; |
| 100 | + } |
| 101 | + |
| 102 | + Get-IcingaCheckCommandConfig -CheckName $CheckList -OutDirectory $BasketConfigDir -FileName ([string]::Format('{0}_Bundle', (Get-Culture).TextInfo.ToTitleCase($ComponentName))); |
| 103 | + Get-IcingaCheckCommandConfig -CheckName $CheckList -OutDirectory $IcingaConfigDir -FileName ([string]::Format('{0}_Bundle', (Get-Culture).TextInfo.ToTitleCase($ComponentName))) -IcingaConfig; |
| 104 | +} |
0 commit comments