Skip to content

Commit 30ea537

Browse files
authored
Merge pull request #376 from Icinga:fix/imc_install_json_error_handling
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.
2 parents f99230e + d88e61d commit 30ea537

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

doc/100-General/10-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
1414
### Bugfixes
1515

1616
* [#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
17+
* [#376](https://github.com/Icinga/icinga-powershell-framework/pull/376) Fixes IMC error handling on invalid JSON for installation command/file
1718

1819
## 1.6.1 (2021-09-15)
1920

lib/core/installer/Install-Icinga.psm1

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,25 @@ function Install-Icinga()
5050
# Use our install command to configure everything
5151
if ([string]::IsNullOrEmpty($InstallCommand) -eq $FALSE) {
5252

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

5560
# Add our "old" swap internally
56-
$OldConfigSwap = Get-IcingaPowerShellConfig -Path 'Framework.Config.Swap';
61+
$OldConfigSwap = Get-IcingaPowerShellConfig -Path 'Framework.Config.Swap';
62+
Disable-IcingaFrameworkConsoleOutput;
5763

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

6066
# First run our configuration values
61-
Invoke-IcingaForWindowsManagementConsoleCustomConfig -IcingaConfiguration $IcingaConfiguration;
67+
$Success = Invoke-IcingaForWindowsManagementConsoleCustomConfig -IcingaConfiguration $IcingaConfiguration;
68+
69+
if ($Success -eq $FALSE) {
70+
return;
71+
}
6272

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

7585
# Now apply our configuration again to ensure the defaults are overwritten again
7686
# Suite a mess, but we can improve this later
77-
Invoke-IcingaForWindowsManagementConsoleCustomConfig -IcingaConfiguration $IcingaConfiguration;
87+
$Success = Invoke-IcingaForWindowsManagementConsoleCustomConfig -IcingaConfiguration $IcingaConfiguration;
88+
89+
if ($Success -eq $FALSE) {
90+
return;
91+
}
7892

7993
Enable-IcingaFrameworkConsoleOutput;
8094

lib/core/installer/tools/CustomConfig.psm1

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function Invoke-IcingaForWindowsManagementConsoleCustomConfig()
88
$cmdConfig = $IcingaConfiguration[$cmd];
99

1010
if ($cmd.Contains(':')) {
11-
continue; # skip for now, as more complicated
11+
continue;
1212
}
1313

1414
$cmdArguments = @{
@@ -22,6 +22,14 @@ function Invoke-IcingaForWindowsManagementConsoleCustomConfig()
2222
$cmdArguments.Add('DefaultInput', $cmdConfig.Selection)
2323
}
2424

25-
&$cmd @cmdArguments;
25+
try {
26+
&$cmd @cmdArguments;
27+
} catch {
28+
Enable-IcingaFrameworkConsoleOutput;
29+
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;
30+
return $FALSE;
31+
}
2632
}
33+
34+
return $TRUE;
2735
}

0 commit comments

Comments
 (0)