Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix windows agent not restarting #1318

Merged
merged 3 commits into from
May 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
Start-Transcript -path C:\buildkite-agent\terminate-instance.log -append

$Token = (Invoke-WebRequest -UseBasicParsing -Method Put -Headers @{'X-aws-ec2-metadata-token-ttl-seconds' = '60'} http://169.254.169.254/latest/api/token).content

$InstanceId = (Invoke-WebRequest -UseBasicParsing -Headers @{'X-aws-ec2-metadata-token' = $Token} http://169.254.169.254/latest/meta-data/instance-id).content
$Region = (Invoke-WebRequest -UseBasicParsing -Headers @{'X-aws-ec2-metadata-token' = $Token} http://169.254.169.254/latest/meta-data/placement/region).content

Write-Output "terminate-instance: disconnecting agent..."
nssm stop buildkite-agent

Write-Output "terminate-instance: requesting instance termination..."
Write-Output "$(Get-Date) terminate-instance: requesting instance termination..."
aws autoscaling terminate-instance-in-auto-scaling-group --region "$Region" --instance-id "$InstanceId" "--should-decrement-desired-capacity" 2> $null

# If autoscaling request was successful, we will terminate the instance, otherwise, if
# BuildkiteTerminateInstanceAfterJob is set to true, we will mark the instance as unhealthy
# so that the ASG will terminate it despite scale-in protection. Otherwise, we should not
# terminate the instance, so we need to retart the agent.
if ($lastexitcode -eq 0) {
Write-Output "terminate-instance: terminating instance..."
Write-Output "$(Get-Date) terminate-instance: terminating instance..."
} else {
Write-Output "terminate-instance: ASG could not decrement (we're already at minSize)"
Write-Output "$(Get-Date) terminate-instance: ASG could not decrement (we're already at minSize)"
if ($Env:BUILDKITE_TERMINATE_INSTANCE_AFTER_JOB -eq "true") {
Write-Output "terminate-instance: marking instance as unhealthy"
Write-Output "$(Get-Date) terminate-instance: marking instance as unhealthy"
aws autoscaling set-instance-health `
--instance-id "$InstanceId" `
--region "$Region" `
--health-status Unhealthy
} else {
Write-Output "terminate-instance: restarting agent..."
Write-Output "$(Get-Date) terminate-instance: restarting agent..."
nssm start buildkite-agent
}
}

Stop-Transcript