Improved stability for Director Wizard

Fixes #27
This commit is contained in:
Christian Stein 2019-12-06 20:28:09 +01:00
parent c7c2ef8c22
commit 58c6d0fd7d
2 changed files with 42 additions and 10 deletions

View file

@ -40,5 +40,7 @@ function Register-IcingaDirectorSelfServiceHost()
Set-IcingaPowerShellConfig -Path 'IcingaDirector.SelfService.ApiKey' -Value $JsonContent; Set-IcingaPowerShellConfig -Path 'IcingaDirector.SelfService.ApiKey' -Value $JsonContent;
Write-Host 'Host was successfully registered within Icinga Director';
return $JsonContent; return $JsonContent;
} }

View file

@ -4,7 +4,8 @@ function Start-IcingaAgentDirectorWizard()
[string]$DirectorUrl, [string]$DirectorUrl,
[string]$SelfServiceAPIKey = $null, [string]$SelfServiceAPIKey = $null,
$OverrideDirectorVars = $null, $OverrideDirectorVars = $null,
[bool]$RunInstaller = $FALSE [bool]$RunInstaller = $FALSE,
[switch]$ForceTemplateKey
); );
[hashtable]$DirectorOverrideArgs = @{} [hashtable]$DirectorOverrideArgs = @{}
@ -23,7 +24,16 @@ function Start-IcingaAgentDirectorWizard()
} }
} }
$SelfServiceAPIKey = Get-IcingaPowerShellConfig -Path 'IcingaDirector.SelfService.ApiKey'; $LocalAPIKey = Get-IcingaPowerShellConfig -Path 'IcingaDirector.SelfService.ApiKey';
if ($ForceTemplateKey) {
if ($SelfServiceAPIKey -eq $LocalAPIKey) {
$ForceTemplateKey = $FALSE;
}
}
if ($ForceTemplateKey -eq $FALSE) {
$SelfServiceAPIKey = $LocalAPIKey;
if ([string]::IsNullOrEmpty($SelfServiceAPIKey)) { if ([string]::IsNullOrEmpty($SelfServiceAPIKey)) {
$LegacyTokenPath = Join-Path -Path Get-IcingaAgentConfigDirectory -ChildPath 'icingadirector.token'; $LegacyTokenPath = Join-Path -Path Get-IcingaAgentConfigDirectory -ChildPath 'icingadirector.token';
if (Test-Path $LegacyTokenPath) { if (Test-Path $LegacyTokenPath) {
@ -31,14 +41,27 @@ function Start-IcingaAgentDirectorWizard()
Set-IcingaPowerShellConfig -Path 'IcingaDirector.SelfService.ApiKey' -Value $SelfServiceAPIKey; Set-IcingaPowerShellConfig -Path 'IcingaDirector.SelfService.ApiKey' -Value $SelfServiceAPIKey;
} }
} }
}
if ([string]::IsNullOrEmpty($SelfServiceAPIKey)) { if ([string]::IsNullOrEmpty($SelfServiceAPIKey)) {
$SelfServiceAPIKey = (Get-IcingaAgentInstallerAnswerInput -Prompt 'Please enter your Self-Service API key' -Default 'v').answer; $SelfServiceAPIKey = (Get-IcingaAgentInstallerAnswerInput -Prompt 'Please enter your Self-Service API key' -Default 'v').answer;
} else { } else {
if ($ForceTemplateKey -eq $FALSE) {
$HostKnown = $TRUE; $HostKnown = $TRUE;
} }
}
try {
$Arguments = Get-IcingaDirectorSelfServiceConfig -DirectorUrl $DirectorUrl -ApiKey $SelfServiceAPIKey; $Arguments = Get-IcingaDirectorSelfServiceConfig -DirectorUrl $DirectorUrl -ApiKey $SelfServiceAPIKey;
} catch {
Write-Host ([string]::Format('Failed to connect to your Icinga Director at "{0}". Please try again', $DirectorUrl));
return Start-IcingaAgentDirectorWizard `
-SelfServiceAPIKey ((Get-IcingaAgentInstallerAnswerInput -Prompt 'Please re-enter your SelfService API Key for the Host-Template in case the key is no longer assigned to your host' -Default 'v' -DefaultInput $SelfServiceAPIKey).answer) `
-OverrideDirectorVars $OverrideDirectorVars `
-ForceTemplateKey;
}
$Arguments = Convert-IcingaDirectorSelfServiceArguments -JsonInput $Arguments; $Arguments = Convert-IcingaDirectorSelfServiceArguments -JsonInput $Arguments;
if ($OverrideDirectorVars -eq $TRUE -And -Not $RunInstaller) { if ($OverrideDirectorVars -eq $TRUE -And -Not $RunInstaller) {
@ -51,7 +74,14 @@ function Start-IcingaAgentDirectorWizard()
} }
if ($HostKnown -eq $FALSE) { if ($HostKnown -eq $FALSE) {
while ($TRUE) {
try {
$SelfServiceAPIKey = Register-IcingaDirectorSelfServiceHost -DirectorUrl $DirectorUrl -ApiKey $SelfServiceAPIKey -Hostname (Get-IcingaHostname @Arguments); $SelfServiceAPIKey = Register-IcingaDirectorSelfServiceHost -DirectorUrl $DirectorUrl -ApiKey $SelfServiceAPIKey -Hostname (Get-IcingaHostname @Arguments);
break;
} catch {
$SelfServiceAPIKey = (Get-IcingaAgentInstallerAnswerInput -Prompt ([string]::Format('Failed to register host within Icinga Director. Please re-enter your SelfService API Key. If this prompt continues, drop your host key at "Hosts -> {0} -> Agent"', (Get-IcingaHostname @Arguments))) -Default 'v' -DefaultInput $SelfServiceAPIKey).answer;
}
}
# Host is already registered # Host is already registered
if ($null -eq $SelfServiceAPIKey) { if ($null -eq $SelfServiceAPIKey) {