mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-20 23:00:35 -05:00
Add support for dynamic background daemon loading
This commit is contained in:
parent
0d59fc9a15
commit
7b8380bee8
4 changed files with 71 additions and 2 deletions
22
lib/daemon/Get-IcingaBackgroundDaemons.psm1
Normal file
22
lib/daemon/Get-IcingaBackgroundDaemons.psm1
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
function Get-IcingaBackgroundDaemons()
|
||||||
|
{
|
||||||
|
$Daemons = Get-IcingaPowerShellConfig -Path 'BackgroundDaemon.EnabledDaemons';
|
||||||
|
|
||||||
|
if ($null -eq $Daemons) {
|
||||||
|
return $null;
|
||||||
|
}
|
||||||
|
|
||||||
|
[hashtable]$Output = @{};
|
||||||
|
|
||||||
|
foreach ($daemon in $Daemons.PSObject.Properties) {
|
||||||
|
$Arguments = @{};
|
||||||
|
|
||||||
|
foreach ($argument in $daemon.Value.Arguments.PSObject.Properties) {
|
||||||
|
$Arguments.Add($argument.Name, $argument.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
$Output.Add($daemon.Name, $Arguments);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $Output;
|
||||||
|
}
|
||||||
22
lib/daemon/Register-IcingaBackgroundDaemon.psm1
Normal file
22
lib/daemon/Register-IcingaBackgroundDaemon.psm1
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
function Register-IcingaBackgroundDaemon()
|
||||||
|
{
|
||||||
|
param(
|
||||||
|
[string]$Command,
|
||||||
|
[hashtable]$Arguments
|
||||||
|
);
|
||||||
|
|
||||||
|
if ([string]::IsNullOrEmpty($Command)) {
|
||||||
|
throw 'Please specify a Cmdlet to run as Background Daemon';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (-Not (Test-IcingaFunction $Command)) {
|
||||||
|
throw ([string]::Format('The Cmdlet "{0}" is not available in your session. Please restart the session and try again or verify your input', $Command));
|
||||||
|
}
|
||||||
|
|
||||||
|
$Path = [string]::Format('BackgroundDaemon.EnabledDaemons.{0}', $Command);
|
||||||
|
|
||||||
|
Set-IcingaPowerShellConfig -Path ([string]::Format('{0}.Command', $Path)) -Value $Command;
|
||||||
|
Set-IcingaPowerShellConfig -Path ([string]::Format('{0}.Arguments', $Path)) -Value $Arguments;
|
||||||
|
|
||||||
|
Write-Host ([string]::Format('Background daemon Cmdlet "{0}" has been configured', $Command));
|
||||||
|
}
|
||||||
|
|
@ -6,8 +6,16 @@ function Start-IcingaPowerShellDaemon()
|
||||||
Use-Icinga -LibOnly -Daemon;
|
Use-Icinga -LibOnly -Daemon;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
# Todo: Add dynamic loading of enabled background tasks
|
$EnabledDaemons = Get-IcingaBackgroundDaemons;
|
||||||
Start-IcingaServiceCheckDaemon;
|
|
||||||
|
foreach ($daemon in $EnabledDaemons.Keys) {
|
||||||
|
if (-Not (Test-IcingaFunction $daemon)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$daemonArgs = $EnabledDaemons[$daemon];
|
||||||
|
&$daemon @daemonArgs;
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
# Todo: Add exception handling
|
# Todo: Add exception handling
|
||||||
}
|
}
|
||||||
|
|
|
||||||
17
lib/daemon/Unregister-IcingaBackgroundDaemon.psm1
Normal file
17
lib/daemon/Unregister-IcingaBackgroundDaemon.psm1
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
function Unregister-IcingaBackgroundDaemon()
|
||||||
|
{
|
||||||
|
param(
|
||||||
|
[string]$BackgroundDaemon,
|
||||||
|
[hashtable]$Arguments
|
||||||
|
);
|
||||||
|
|
||||||
|
if ([string]::IsNullOrEmpty($BackgroundDaemon)) {
|
||||||
|
throw 'Please specify a Cmdlet to remove from running as Background Daemon';
|
||||||
|
}
|
||||||
|
|
||||||
|
$Path = [string]::Format('BackgroundDaemon.EnabledDaemons.{0}', $BackgroundDaemon);
|
||||||
|
|
||||||
|
Remove-IcingaPowerShellConfig -Path $Path;
|
||||||
|
|
||||||
|
Write-Host 'Background daemon has been removed';
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue