Fixes repository resolving and validation

This commit is contained in:
Lord Hepipud 2021-08-20 13:46:47 +02:00
parent 5198d02241
commit 0feef9e41a

View file

@ -1,7 +1,8 @@
function Read-IcingaRepositoryFile() function Read-IcingaRepositoryFile()
{ {
param ( param (
[string]$Name = $null [string]$Name = $null,
[switch]$TryAlternate = $FALSE
); );
if ([string]::IsNullOrEmpty($Name)) { if ([string]::IsNullOrEmpty($Name)) {
@ -27,32 +28,33 @@ function Read-IcingaRepositoryFile()
$WebContent = Get-Content -Path (Join-Path -Path $RepoPath -ChildPath 'ifw.repo.json') -Raw; $WebContent = Get-Content -Path (Join-Path -Path $RepoPath -ChildPath 'ifw.repo.json') -Raw;
} else { } else {
try { try {
$WebContent = Invoke-WebRequest -UseBasicParsing -Uri $Repository.RemotePath;
$RepoPath = $Repository.RemotePath; $RepoPath = $Repository.RemotePath;
} catch {
# Nothing to do if ($TryAlternate) {
$RepoPath = (Join-WebPath -Path $Repository.RemotePath -ChildPath 'ifw.repo.json');
} }
if ($null -eq $WebContent) { $WebContent = Invoke-WebRequest -UseBasicParsing -Uri $RepoPath;
try {
$WebContent = Invoke-WebRequest -UseBasicParsing -Uri (Join-WebPath -Path $Repository.RemotePath -ChildPath 'ifw.repo.json');
} catch {
Write-IcingaConsoleError 'Failed to read repository file from "{0}" or "{0}/ifw.repo.json". Exception: {1}' -Objects $Repository.RemotePath, $_.Exception.Message;
return $null;
}
$RepoPath = $Repository.RemotePath;
}
if ($null -eq $WebContent) {
Write-IcingaConsoleError 'Unable to fetch data for repository "{0}" from any configured location' -Objects $Name;
return $null;
}
if ($null -ne $WebContent) {
if ($WebContent.RawContent.Contains('application/octet-stream')) { if ($WebContent.RawContent.Contains('application/octet-stream')) {
$Content = [System.Text.Encoding]::UTF8.GetString($WebContent.Content) $Content = [System.Text.Encoding]::UTF8.GetString($WebContent.Content)
} else { } else {
$Content = $WebContent.Content; $Content = $WebContent.Content;
} }
} else {
if ($TryAlternate -eq $FALSE) {
return (Read-IcingaRepositoryFile -Name $Name -TryAlternate);
}
}
} catch {
if ($TryAlternate -eq $FALSE) {
return (Read-IcingaRepositoryFile -Name $Name -TryAlternate);
} else {
Write-IcingaConsoleError 'Unable to resolve repository URL "{0}" for repository "{1}": {2}' -Objects $Repository.RemotePath, $Name, $_.Exception.Message;
return $null;
}
}
} }
if ($null -eq $Content) { if ($null -eq $Content) {
@ -60,7 +62,13 @@ function Read-IcingaRepositoryFile()
return $null; return $null;
} }
$RepositoryObject = ConvertFrom-Json -InputObject $Content; try {
$RepositoryObject = ConvertFrom-Json -InputObject $Content -ErrorAction Stop;
} catch {
if ($TryAlternate -eq $FALSE) {
return (Read-IcingaRepositoryFile -Name $Name -TryAlternate);
}
}
return $RepositoryObject; return $RepositoryObject;
} }