Skip to content

Commit

Permalink
Set an upper limit for token download retries
Browse files Browse the repository at this point in the history
Addresses #1845
  • Loading branch information
meteorcloudy authored Jan 10, 2024
1 parent 4d607e4 commit 8bff27c
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion buildkite/startup-windows-pdssd.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,35 @@ if ($myhostname -like "*trusted*") {
$key = "buildkite-untrusted-agent-token"
}
$buildkite_agent_token_file = "c:\buildkite\buildkite_agent_token.enc"

Write-Host "Getting Buildkite Agent token from GCS..."
while ($true) {

# Try at most 30 times to download file, if not succeeding, shutdown the VM.
$maxAttempts = 30
$attemptCount = 0

while ($attemptCount -lt $maxAttempts) {
try {
(New-Object Net.WebClient).DownloadFile($buildkite_agent_token_url, $buildkite_agent_token_file)
Write-Host "Token downloaded successfully."
break
} catch {
$msg = $_.Exception.Message
Write-Host "Failed to download token: $msg"

$attemptCount++
Write-Host "Attempt $attemptCount of $maxAttempts"

Start-Sleep -Seconds 10
}
}

# Check if maximum attempts were reached and shut down if necessary
if ($attemptCount -eq $maxAttempts) {
Write-Host "Maximum attempts reached. Shutting down the machine..."
Stop-Computer
}

## Decrypt the Buildkite agent token.
Write-Host "Decrypting Buildkite Agent token using KMS..."
$buildkite_agent_token = & gcloud kms decrypt --project $project --location global --keyring buildkite --key $key --ciphertext-file $buildkite_agent_token_file --plaintext-file -
Expand Down

0 comments on commit 8bff27c

Please sign in to comment.