Added support to manage framework config

This commit is contained in:
Lord Hepipud 2019-10-05 21:56:23 +02:00
parent 59be230567
commit 7150fd1236
8 changed files with 177 additions and 0 deletions

View file

@ -0,0 +1,25 @@
function Get-IcingaConfigTreeCount()
{
param(
$Path = ''
);
$Config = Read-IcingaPowerShellConfig;
$PathArray = $Path.Split('.');
$ConfigObject = $Config;
[int]$Count = 0;
foreach ($entry in $PathArray) {
if (-Not (Test-IcingaPowerShellConfigItem -ConfigObject $ConfigObject -ConfigKey $entry)) {
continue;
}
$ConfigObject = $ConfigObject.$entry;
}
foreach ($config in $ConfigObject.PSObject.Properties) {
$Count += 1;
}
return $Count;
}

View file

@ -0,0 +1,20 @@
function Get-IcingaPowerShellConfig()
{
param(
$Path = ''
);
$Config = Read-IcingaPowerShellConfig;
$PathArray = $Path.Split('.');
$ConfigObject = $Config;
foreach ($entry in $PathArray) {
if (-Not (Test-IcingaPowerShellConfigItem -ConfigObject $ConfigObject -ConfigKey $entry)) {
return $null;
}
$ConfigObject = $ConfigObject.$entry;
}
return $ConfigObject;
}

View file

@ -0,0 +1,14 @@
function New-IcingaPowerShellConfigItem()
{
param(
$ConfigObject,
[string]$ConfigKey,
$ConfigValue = $null
);
if ($null -eq $ConfigValue) {
$ConfigValue = (New-Object -TypeName PSOBject);
}
$ConfigObject | Add-Member -MemberType NoteProperty -Name $ConfigKey -Value $ConfigValue;
}

View file

@ -0,0 +1,23 @@
function Read-IcingaPowerShellConfig()
{
$ConfigDir = Get-IcingaPowerShellConfigDir;
$ConfigFile = Join-Path -Path $ConfigDir -ChildPath 'config.json';
if ($global:IcingaDaemonData.FrameworkRunningAsDaemon) {
if ($global:IcingaDaemonData.ContainsKey('Config')) {
return $global:IcingaDaemonData.Config;
}
}
if (-Not (Test-Path $ConfigFile)) {
return (New-Object -TypeName PSOBject);
}
[string]$Content = Get-Content -Path $ConfigFile;
if ([string]::IsNullOrEmpty($Content)) {
return (New-Object -TypeName PSOBject);
}
return (ConvertFrom-Json -InputObject $Content);
}

View file

@ -0,0 +1,32 @@
function Remove-IcingaPowerShellConfig()
{
param(
$Path = ''
);
if ([string]::IsNullOrEmpty($Path)) {
throw 'Please specify a valid path to an object';
}
$Config = Read-IcingaPowerShellConfig;
$PathArray = $Path.Split('.');
$ConfigObject = $Config;
[int]$Index = $PathArray.Count;
foreach ($entry in $PathArray) {
if (-Not (Test-IcingaPowerShellConfigItem -ConfigObject $ConfigObject -ConfigKey $entry)) {
return $null;
}
if ($index -eq 1) {
$ConfigObject.PSObject.Properties.Remove($entry);
break;
}
$ConfigObject = $ConfigObject.$entry;
$Index -= 1;
}
Write-IcingaPowerShellConfig $Config;
}

View file

@ -0,0 +1,31 @@
function Set-IcingaPowerShellConfig()
{
param(
$Path = '',
$Value = $null
);
$Config = Read-IcingaPowerShellConfig;
$PathArray = $Path.Split('.');
$ConfigObject = $Config;
[int]$Index = $PathArray.Count;
$InputValue = $null;
foreach ($entry in $PathArray) {
if ($index -eq 1) {
$InputValue = $Value;
}
if (-Not (Test-IcingaPowerShellConfigItem -ConfigObject $ConfigObject -ConfigKey $entry)) {
New-IcingaPowerShellConfigItem -ConfigObject $ConfigObject -ConfigKey $entry -ConfigValue $InputValue;
}
if ($index -eq 1) {
$ConfigObject.$entry = $Value;
break;
}
$ConfigObject = $ConfigObject.$entry;
$index -= 1;
}
Write-IcingaPowerShellConfig $Config;
}

View file

@ -0,0 +1,9 @@
function Test-IcingaPowerShellConfigItem()
{
param(
$ConfigObject,
$ConfigKey
);
return ([bool]($ConfigObject.PSobject.Properties.Name -eq $ConfigKey) -eq $TRUE);
}

View file

@ -0,0 +1,23 @@
function Write-IcingaPowerShellConfig()
{
param(
$Config
);
$ConfigDir = Get-IcingaPowerShellConfigDir;
$ConfigFile = Join-Path -Path $ConfigDir -ChildPath 'config.json';
if (-Not (Test-Path $ConfigDir)) {
New-Item -Path $ConfigDir -ItemType Directory | Out-Null;
}
$Content = ConvertTo-Json -InputObject $Config -Depth 100;
Set-Content -Path $ConfigFile -Value $Content;
if ($global:IcingaDaemonData.FrameworkRunningAsDaemon) {
if ($global:IcingaDaemonData.ContainsKey('Config')) {
$global:IcingaDaemonData.Config = $Config;
}
}
}