Skip to content

Commit eb6c493

Browse files
author
Lachlan Donald
authored
Merge pull request #614 from jeremiahsnapp/windows-fixes
Ensure unhealthy Windows instances get marked correctly
2 parents a7ef7ad + e7ac783 commit eb6c493

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

packer/windows/conf/bin/bk-install-elastic-stack.ps1

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ function on_error {
99
$errorLine=$_.InvocationInfo.ScriptLineNumber
1010
$errorMessage=$_.Exception
1111

12+
$instance_id=(Invoke-WebRequest -UseBasicParsing http://169.254.169.254/latest/meta-data/instance-id).content
13+
1214
aws autoscaling set-instance-health `
13-
--instance-id "(Invoke-WebRequest -UseBasicParsing http://169.254.169.254/latest/meta-data/instance-id).content" `
15+
--instance-id "$instance_id" `
1416
--health-status Unhealthy
1517

1618
cfn-signal `
@@ -117,11 +119,32 @@ Set-PSDebug -Trace 0
117119

118120
Write-Output "Creating buildkite-agent user account in Administrators group"
119121

120-
$Count = Get-Random -min 24 -max 32
121-
$Password = -join ((65..90) + (97..122) + (48..57) | Get-Random -Count $Count | ForEach-Object {[char]$_})
122122
$UserName = "buildkite-agent"
123123

124-
New-LocalUser -Name $UserName -PasswordNeverExpires -Password ($Password | ConvertTo-SecureString -AsPlainText -Force) | out-null
124+
$StopLoop = $false
125+
[int]$RetryCount = "0"
126+
127+
# a Try/Catch block is used in a loop to make a few extra attempts at creating the user account before finally giving up and failing
128+
# because sometimes the generated random password does not satisfy the system's password policy
129+
Do {
130+
Try {
131+
$Count = Get-Random -min 24 -max 32
132+
$Password = -join ((65..90) + (97..122) + (48..57) | Get-Random -Count $Count | ForEach-Object {[char]$_})
133+
134+
New-LocalUser -Name $UserName -PasswordNeverExpires -Password ($Password | ConvertTo-SecureString -AsPlainText -Force) | out-null
135+
$StopLoop = $true
136+
}
137+
Catch {
138+
If ($RetryCount -gt 10){
139+
Write-Output "Could not create $UserName user after 10 retries."
140+
exit 1
141+
}
142+
Else {
143+
Write-Output "Could not create $UserName user, retrying..."
144+
$RetryCount = $RetryCount + 1
145+
}
146+
}
147+
} While ($StopLoop -eq $false)
125148

126149
If ($Env:BUILDKITE_WINDOWS_ADMINISTRATOR -eq "true") {
127150
Add-LocalGroupMember -Group "Administrators" -Member $UserName | out-null

templates/aws-stack.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ Resources:
785785
powershell -file C:\buildkite-agent\bin\bk-configure-docker.ps1 >> C:\buildkite-agent\elastic-stack.log
786786
787787
$Env:BUILDKITE_STACK_NAME="${AWS::StackName}"
788-
$Env:BUILDKITE_STACK_VERSION=%v
788+
$Env:BUILDKITE_STACK_VERSION="%v"
789789
$Env:BUILDKITE_SCALE_IN_IDLE_PERIOD="${ScaleInIdlePeriod}"
790790
$Env:BUILDKITE_SECRETS_BUCKET="${LocalSecretsBucket}"
791791
$Env:BUILDKITE_AGENT_TOKEN="${BuildkiteAgentToken}"

0 commit comments

Comments
 (0)