Skip to content

Commit

Permalink
Merge pull request #376 from Icinga:fix/imc_install_json_error_handling
Browse files Browse the repository at this point in the history
Fix: IMC error handling on invalid JSON for install command/file

Fixes error handling for IMC on automated installation with installation command or answer file, which will not abort the installation in case invalid JSON is provided or commands are used inside the command, not valid or exist on the system.

The system will write an error message and abort the installation in case of initial errors now.
  • Loading branch information
LordHepipud committed Sep 24, 2021
2 parents f99230e + d88e61d commit 30ea537
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions doc/100-General/10-Changelog.md
Expand Up @@ -14,6 +14,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
### Bugfixes

* [#375](https://github.com/Icinga/icinga-powershell-framework/pull/375) Fixes exception on last message printed during `Uninstall-IcingaForWindows`, because the prior used function is no longer present at this point
* [#376](https://github.com/Icinga/icinga-powershell-framework/pull/376) Fixes IMC error handling on invalid JSON for installation command/file

## 1.6.1 (2021-09-15)

Expand Down
24 changes: 19 additions & 5 deletions lib/core/installer/Install-Icinga.psm1
Expand Up @@ -50,15 +50,25 @@ function Install-Icinga()
# Use our install command to configure everything
if ([string]::IsNullOrEmpty($InstallCommand) -eq $FALSE) {

Disable-IcingaFrameworkConsoleOutput;
try {
$JsonInstallCmd = ConvertFrom-Json -InputObject $InstallCommand -ErrorAction Stop;
} catch {
Write-IcingaConsoleError 'Failed to deserialize the provided JSON from file or command: {0}' -Objects $_.Exception.Message;
return;
}

# Add our "old" swap internally
$OldConfigSwap = Get-IcingaPowerShellConfig -Path 'Framework.Config.Swap';
$OldConfigSwap = Get-IcingaPowerShellConfig -Path 'Framework.Config.Swap';
Disable-IcingaFrameworkConsoleOutput;

[hashtable]$IcingaConfiguration = Convert-IcingaForwindowsManagementConsoleJSONConfig -Config (ConvertFrom-Json -InputObject $InstallCommand);
[hashtable]$IcingaConfiguration = Convert-IcingaForwindowsManagementConsoleJSONConfig -Config $JsonInstallCmd;

# First run our configuration values
Invoke-IcingaForWindowsManagementConsoleCustomConfig -IcingaConfiguration $IcingaConfiguration;
$Success = Invoke-IcingaForWindowsManagementConsoleCustomConfig -IcingaConfiguration $IcingaConfiguration;

if ($Success -eq $FALSE) {
return;
}

# In case we use the director, we require to first fetch all basic values from the Self-Service API then
# require to register the host to fet the remaining content
Expand All @@ -74,7 +84,11 @@ function Install-Icinga()

# Now apply our configuration again to ensure the defaults are overwritten again
# Suite a mess, but we can improve this later
Invoke-IcingaForWindowsManagementConsoleCustomConfig -IcingaConfiguration $IcingaConfiguration;
$Success = Invoke-IcingaForWindowsManagementConsoleCustomConfig -IcingaConfiguration $IcingaConfiguration;

if ($Success -eq $FALSE) {
return;
}

Enable-IcingaFrameworkConsoleOutput;

Expand Down
12 changes: 10 additions & 2 deletions lib/core/installer/tools/CustomConfig.psm1
Expand Up @@ -8,7 +8,7 @@ function Invoke-IcingaForWindowsManagementConsoleCustomConfig()
$cmdConfig = $IcingaConfiguration[$cmd];

if ($cmd.Contains(':')) {
continue; # skip for now, as more complicated
continue;
}

$cmdArguments = @{
Expand All @@ -22,6 +22,14 @@ function Invoke-IcingaForWindowsManagementConsoleCustomConfig()
$cmdArguments.Add('DefaultInput', $cmdConfig.Selection)
}

&$cmd @cmdArguments;
try {
&$cmd @cmdArguments;
} catch {
Enable-IcingaFrameworkConsoleOutput;
Write-IcingaConsoleError 'Failed to apply installation configuration of command "{0}" and argument list{1}because of the following error: "{2}"' -Objects $cmd, ($cmdArguments | Out-String), $_.Exception.Message;
return $FALSE;
}
}

return $TRUE;
}

0 comments on commit 30ea537

Please sign in to comment.