Retry interrupted client package downloads
This commit is contained in:
@@ -38,6 +38,39 @@ function Get-OCSentinelManifest {
|
||||
}
|
||||
}
|
||||
|
||||
function Get-OCSentinelArtifact {
|
||||
param(
|
||||
[Parameter(Mandatory)][string]$Uri,
|
||||
[Parameter(Mandatory)][string]$DestinationPath
|
||||
)
|
||||
|
||||
$parameters = @{ Uri = $Uri; OutFile = $DestinationPath; TimeoutSec = 300 }
|
||||
if ((Get-Command Invoke-WebRequest).Parameters.ContainsKey("UseBasicParsing")) {
|
||||
$parameters.UseBasicParsing = $true
|
||||
}
|
||||
|
||||
for ($attempt = 1; $attempt -le 3; $attempt++) {
|
||||
try {
|
||||
Remove-Item -LiteralPath $DestinationPath -Force -ErrorAction SilentlyContinue
|
||||
Invoke-WebRequest @parameters
|
||||
|
||||
if (-not (Test-Path -LiteralPath $DestinationPath) -or (Get-Item -LiteralPath $DestinationPath).Length -eq 0) {
|
||||
throw "The downloaded artifact is empty."
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
catch {
|
||||
if ($attempt -eq 3) {
|
||||
throw "Could not download the OCSentinel package after 3 attempts. Last error: $($_.Exception.Message)"
|
||||
}
|
||||
|
||||
Write-Warning "Package download attempt $attempt failed. Retrying."
|
||||
Start-Sleep -Seconds (5 * $attempt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Initialize-OCSentinelTls
|
||||
|
||||
$installRoot = Join-Path ${env:ProgramFiles} "OCSentinel"
|
||||
@@ -122,7 +155,7 @@ $extractRoot = Join-Path $downloadRoot "payload"
|
||||
New-Item -ItemType Directory -Force -Path $downloadRoot, $extractRoot | Out-Null
|
||||
|
||||
Write-Host "Downloading artifact: $($manifest.artifactUrl)"
|
||||
Invoke-WebRequest -Uri ([string]$manifest.artifactUrl) -OutFile $zipPath -TimeoutSec 300
|
||||
Get-OCSentinelArtifact -Uri ([string]$manifest.artifactUrl) -DestinationPath $zipPath
|
||||
|
||||
$actualHash = Get-Sha256Hex -Path $zipPath
|
||||
$expectedHash = ([string]$manifest.sha256).ToLowerInvariant()
|
||||
|
||||
Reference in New Issue
Block a user